OpenGL - What to do so that 1 point is equal to 1pixel

203 views Asked by At

I am trying to make a gdi+ to opengl translator(coz gdi+ is v.slow), but I have no idea of what to do so that primitives drawn match the actual screen coordinates(like in gdi+). Without this feature, it will be useless, as porting will be very difficult. I am currently using openTK in vb.net. eg-

GL.Begin(PrimitiveType.Lines)
    GL.Vertex2(0, 0)
    GL.Vertex2(100, 0)
GL.End()

renders a line far longer that 100px.The perspective settings are:

    Dim perspective As Matrix4 = Matrix4.CreatePerspectiveFieldOfView(1, Width / Height, 1, 10000)
    Dim lookat As Matrix4 = Matrix4.LookAt(0, 0, 225, 0, 0, 0, 0, 1, 0)

Z of eye is set to 255 as it gives pixel accurate measurements but only in the centre of the screen. As we move further, it's not accurate at all. I think that I need to make the surface spherical, so that every point on it is at the same distance, but don't know how to do that.

Below is a sample picture. The rectangle below the line is of 100px(gdi+) and the line is made using the above code(gl). Please open this link to view the image

1

There are 1 answers

4
Majd Akar On

BDL is right, first, we need to get the projection matrix. If you are using old openGL (which you shouldn't!), you can call:

glOrtho(0, window_width, 0, window_height, -1, 1);

where window_width and window_height are expressed in pixels. To store this in a matrix you can do:

glGetFloatv(GL_PROJECTION_MATRIX, projection_matrix);

Then when you draw your vertices, coordinates can be expressed as:

glVertex2i(pX,  pY);
glVertex2i(pX + 100,  pY);

This will draw a 100 pixel line starting at (pX,pY). (please note this does not use vbo's etc.., I used these calls to illustrate easily)

If you are using modern openGL, just calculate that projection matrix and pass it to your shader. To calculate the projection matrix manually, you can follow this guide:

http://www.songho.ca/opengl/gl_projectionmatrix.html