Description
By default, the center point is in the center of the screen, the range is from [ -1,1], so the actual coordinates of the object map to the screen coordinates need a conversion process, described above to use the GLUORTHO2D function through the positive projection, the physical coordinates mapped to the screen coordinates, but the coordinates of the origin in the lower left corner, in general, The origin of the screen starts at the top left, so in all of the examples above, you need to recalculate the coordinates of the y-axis and the high minus y-axis component of the screen. Now, by re-setting the lower-left coordinate of the orthographic projection and the coordinates of the upper-right corner, the one by one corresponding relationship is established, and no conversion is required, similar to the rotation of the camera, to adjust the coordinate system.
Code
#include <gl/glut.h>
#include <Windows.h>
#define WINDOWHEIGHT 480
#define WINDOWWIDTH 800
void Myinit ()
{
Glclearcolor (1.0,1.0,1.0,0.0);
Glmatrixmode (gl_projection);
Glloadidentity ();
gluortho2d (0.0, windowwidth, 0.0, windowheight);
gluortho2d (0.0, windowwidth, windowheight, 0.0);
}
void Mydisplay (void)
{
Glclearcolor (1.0f, 1.0f, 1.0f, 0.0f);
Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit);
Glflush ();
}
void Mymouse (int button,int state,int x,int y)
{
if (State==glut_down)
{
Glpointsize (10);
Glbegin (gl_points);
glcolor3f (1.0f, 0.0f, 0.0f);
GLVERTEX2F (x, windowheight-y);
glvertex2f (x, y);
Glend ();
Glflush ();
}
}
int main (int argc, char* argv[])
{
Glutinit (&ARGC, argv);
Glutinitdisplaymode (Glut_rgba | Glut_single);
Glutinitwindowsize (WindowWidth, windowheight);
Glutinitwindowposition (100,150);
Glutcreatewindow ("OpenGL window 1");
Glutdisplayfunc (&mydisplay);
Glutmousefunc (&mymouse);
Myinit ();
Glutmainloop ();
return 0;
}
Two-dimensional image to two-dimensional screen display, corresponding to screen coordinates