سؤال

I'm not sure why this code isn't simply drawing a triangle to screen (orthographically). I'm using OpenTK 1.1 which is the same thing as OpenGL 1.1.

List<Vector3> simpleVertices = new List<Vector3>();
simpleVertices.Add(new Vector3(0,0,0));
simpleVertices.Add(new Vector3(100,0,0));
simpleVertices.Add(new Vector3(100,100,0));

GL.MatrixMode(All.Projection);
GL.LoadIdentity();

GL.MatrixMode(All.Projection);
GL.Ortho(0, 480, 320, 0,0,1000);

GL.MatrixMode(All.Modelview);
GL.LoadIdentity();
GL.Translate(0,0,10);
unsafe
{
  Vector3* data = (Vector3*)Marshal.AllocHGlobal(
                        Marshal.SizeOf(typeof(Vector3)) * simpleVertices.Count);

  for(int i = 0; i < simpleVertices.Count; i++)
  {
    ((Vector3*)data)[i] = simpleVertices[i];
  }

  GL.VertexPointer(3, All.Float, sizeof(Vector3), new IntPtr(data));
  GL.DrawArrays(All.Triangles, 0, simpleVertices.Count);
}

The code executes once every update cycle in a draw function. What I think I'm doing (but evidentially am not) is creating a set of position vertices to form a triangle and drawing it 10 units in front of the camera.

Why is this code not drawing a triangle?

هل كانت مفيدة؟

المحلول

In OpenGL, the Z axis points out of the screen, so when you write

GL.Translate(0,0,10);

it actually translates it "in front" of the screen.

Now your two last parameters to GL.Ortho are 0,1000. This means that everything between 0 and 1000 in the MINUS Z direction ( = in front of the camera ) will be displayed.

In other words, GL.Translate(0,0,-10); will put your object in front of the camera, while GL.Translate(0,0,10); will put it behind.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top