开发者

OpenGL: How to convert world coordinate to screen coordinate?

    GL.LoadIdentity();
    GL.Translate(0f, 0f,-1f);

    GL.Begin(BeginMode.Quads);
    GL.Color4(1f, 0f, 0f, 1f);

    GL.Vertex3(-1.0f, 1.0f,0);              
    G开发者_高级运维L.Vertex3(1.0f, 1.0f,0);               
    GL.Vertex3(1.0f, -1.0f,0);              
    GL.Vertex3(-1.0f, -1.0f,0);             
    GL.End();

This is using world coordinate system, and now I want to do it using screen location in pixel. How to do that? Many thanks for help


OpenGL has no "world" coordinate system. It has modelview space, which is transformed by the modelview matrix and it has clip space which is reached from modelview space transforming it by the projection matrix. Clip space is [-1, 1]^3 and it mapped into the viewport and the Z buffer depth range.

So what you need to do is, defining a chain of transformations that maps modelview coordinates into viewport coordinates by an identity transform (well it won't work for Z). The transform from clip space to viewport is fixed. So you want to do this using the projection, leaving the modelview an identity transform:

This does the trick:

glViewport(x, y, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Put it in front of every set of drawing commands to happen in viewport pixel space.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜