user2803017
user2803017

Reputation: 19

How to set pixel coordinates?

Is it possible to draw something in OpenGL on the drawing scene with giving window pixel coordinates?

For example, I'd like to draw a single point in a 400x400 window (e.g. in the middle of that window). Is there any quick way to set everything up so I could just type:

glVertex3f(200.0 , 200.0 , 1.0);?

Upvotes: 1

Views: 469

Answers (1)

FakeTruth
FakeTruth

Reputation: 135

You need to set up an orthogonal projection matrix for that first.

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0.0f, WindowWidth, WindowHeight, 0.0f, 0.0f, 10.0f);
glMatrixMode(GL_MODELVIEW);

You can then render in window coordinates.

glPointSize(5.0f);
glBegin(GL_POINTS);
    glVertex3f(100.0f, 100.0f, 1.0f);
glEnd();

Should render a point with a diameter of 5 pixels on window coordinates [100, 100]

Do note that this old way of rendering is deprecated and you should use VBOs and the like, but it is still good for testing.

Upvotes: 1

Related Questions