绘制一个简单的2D矩形网格

See*_*ROM 1 c++ opengl 2d

基本上我想要实现的是一个紧挨着的矩形集合,我可以从一个数组控制.我似乎无法找到关于这个愚蠢简单的事情的任何可靠信息,因为大多数教程都需要我太大的帮助才能找到帮助.到目前为止,我甚至无法使它渲染网格,它使它非常大.(由于某些原因?)

来源

#include <cstdlib>
#include <iostream>
#include <GLUT/GLUT.h>

void display();
void resize(int, int);
void timer(int);

#define SCREEN 512
#define REFRESH 30

const int GRID = 8;

/* Main function: GLUT runs as a console application starting at main()  */
int main(int argc, char** argv) {
    glutInit(&argc, argv);                 // Initialize GLUT
    glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
    glutInitWindowSize(SCREEN, SCREEN);   // Set the window's initial width & height
    glutTimerFunc(0, timer, 0);
    glutInitWindowPosition(0, SCREEN); // Position the window's initial top-left corner
    glutDisplayFunc(display); // Register display callback handler for window re-paint
    glutInitDisplayMode(GLUT_DOUBLE);
    glutReshapeFunc(resize);
    glutMainLoop();           // Enter the event-processing loop
    return 0;
}

/* Handler for window-repaint event. Call back when the window first appears and
 whenever the window needs to be re-painted. */
void display() {
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
    glClear(GL_COLOR_BUFFER_BIT);         // Clear the color buffer (background)
    glMatrixMode(GL_MODELVIEW);     // To operate on Model-View matrix
    glLoadIdentity();

    for(int x=0; x < SCREEN; x+=GRID)
    {
        for(int y=0; y < SCREEN; y+=GRID)
        {
            // Draw a Red 1x1 Square centered at origin
            glPushMatrix();
            glTranslatef((1.0f/x)*GRID, (1.0f/y)*GRID, 0);

            //std::cout << (GLfloat)1/x << " : " << (GLfloat)1/y << std::endl;

            glBegin(GL_QUADS);              // Each set of 4 vertices form a quad
            glColor3f(1.0f, 0.0f, 0.0f); // Red
            glVertex2f(-1.0f/GRID, -1.0f/GRID);    // x, y
            glVertex2f( 1.0f/GRID, -1.0f/GRID);
            glVertex2f( 1.0f/GRID,  1.0f/GRID);
            glVertex2f(-1.0f/GRID,  1.0f/GRID);
            glEnd();

            glPopMatrix();
        }
    }

    /*glBegin(GL_QUADS);              // Each set of 4 vertices form a quad
    glColor3f(1.0f, 0.0f, 0.0f); // Red
    glVertex2f(-0.005, -0.005);    // x, y
    glVertex2f( 0.005, -0.005);
    glVertex2f( 0.005,  0.005);
    glVertex2f(-0.005,  0.005);
    glEnd();*/

    glFlush();  // Render now
}

/* Called back when timer expired */
void timer(int value) {
    glutPostRedisplay();      // Post re-paint request to activate display()
    glutTimerFunc(REFRESH, timer, 0); // next Timer call milliseconds later
}

void resize(int width, int height) {
    // we ignore the params and do:
    glutReshapeWindow( 600, 600);
    gluOrtho2D(0, 0, width, height);
    glTranslatef(-0.5, -0.5, 0);
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
}
Run Code Online (Sandbox Code Playgroud)

它看起来像什么

我看到了什么!

Ret*_*adi 6

让我们首先定义我认为你的价值观意味着什么,因为你好像没有像你想象的那样使用它们:

  • SCREEN:窗口大小,以像素为单位.
  • GRID:每个四边形的大小,以像素为单位.

所以可以适合每个方向的四边形数量是SCREEN / GRID.在绘制循环之前,可以计算一次该值:

int quadCount = SCREEN / GRID;
Run Code Online (Sandbox Code Playgroud)

下一个重要的部分是理解OpenGL坐标系.如果不应用任何转换(和真的没有必要为你的使用情况),范围xy映射到该窗口是从-1.0f1.0f.因此,要填充窗口,所有四边形的坐标都需要[-1.0f, 1.0f]在两个方向上填充范围.

由于您需要生成的坐标范围是2.0f(从-1.0f1.0f),因此该坐标系中每个四边形的宽度/高度为2.0f / quadCount.我会在嵌套循环之外计算一次这个值:

float quadSize = 2.0f / static_cast<float>(quadCount);
Run Code Online (Sandbox Code Playgroud)

现在,要将四边形放置在给定位置,它们的大小需要乘以它们的索引.再次记住,坐标始于(-1.0f, -1.0f).所以渲染代码看起来像这样:

glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);

for (int x = 0; x < quadCount; ++x)
{
    float xPos = -1.0f + x * quadSize;

    for (int y = 0; y < quadCount; ++y)
    {
        float yPos = -1.0f + y * quadSize;

        glVertex2f(xPos,            yPos);
        glVertex2f(xPos + quadSize, yPos);
        glVertex2f(xPos + quadSize, yPos + quadSize);
        glVertex2f(xPos,            yPos + quadSize);
    }
}

glEnd();
Run Code Online (Sandbox Code Playgroud)