How can I specify vertices in pixels in OpenGL?

ⅰ亾dé卋堺 提交于 2019-12-25 03:08:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!