问题
I want to write a small project with OpenGL C++ and I want to use pixel instead of float value, for example like that:
glVertext2f(420, 300);
Instead of:
glVertex2f(0.4, -0.34);
Is it possible?
回答1:
If you want to use pixel coordinates your rendering, it's pretty easy to do so using an orthographic matrix, which you can create using glOrtho. Assuming your window is 800x600 you could use the following code:
// Set your projection matrix
glMatrixMode(GL_PROJECTION);
glOrtho(0, 800, 0, 600, -1, 1);
// Restore the default matrix mode
glMatrixMode(GL_MODELVIEW);
glOrtho
expects the parameters to be 'left, right, bottom, top' so this will actually put the origin at the lower left (most OpenGL coordinate systems have Y increase as you move up). However, you want to have the origin in the upper left, as is common with most pixel based drawing systems, you'd want to reverse the bottom and top parameters.
This will let you call glVertex2f with pixel coordinates instead of the default clip coordinates. Note that you don't have to call a special function to convert from an int to a float. C and C++ should both do an implicit conversion. i.e. glVertext2f(420, 300);
来源:https://stackoverflow.com/questions/20832087/how-can-i-specify-vertices-in-pixels-in-opengl