openGL - custom pixel coordinate system

Overview

The default in openGL is the normalized coordinate system . This coordinate system is scaled and presented under different display conditions. has certain benefits.
But sometimes in order to facilitate operation and display, we need to draw according to the pixel coordinate system. Configuration examples are given below.
Take a look at the example to verify it yourself.

example

#include <iostream>
#include <GLUT/GLUT.h>
using namespace std;
float window_size = 600;
void InitEnvironment()                     //对环境进行初始化操作
{   glClearColor(0.0,0.0,0.0,0);
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluOrtho2D(0,window_size,0,window_size);
}
void myDisplay(void)
{   
//绘制一个带颜色的小矩形
//注意这里的坐标都已经是像素坐标下的了
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_QUADS);
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex2f(100, 200);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex2f(200, 200);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex2f(200, 300);
    glColor3f(0.5f, 0.5f, 0.5f);
    glVertex2f(100, 300);
    glEnd();
    glFlush();
}
int main(int argc, char *argv[])
{   glutInit(&argc, argv);   //初始化GLUT
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(300, 100);
    glutInitWindowSize(window_size, window_size);
    glutCreateWindow("演示1");
    InitEnvironment();   //初始化
    glutDisplayFunc(&myDisplay);   //回调函数
    glutMainLoop();    //持续显示,当窗口改变会重新绘制图形
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325754801&siteId=291194637