gpt4 book ai didi

c++ - 绘制一个简单的二维矩形网格

转载 作者:行者123 更新时间:2023-11-30 00:50:22 25 4
gpt4 key购买 nike

基本上我想要实现的是一组彼此相邻的矩形,我可以通过数组进行控制。我似乎找不到关于这种愚蠢简单的东西的任何可靠信息,因为大多数教程都跳得太大了,我找不到有用的东西。到目前为止,我什至无法让它渲染网格,它把它画得非常大。 (出于某种原因?)

来源

#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();
}

它看起来像什么

What I'm seeing!

最佳答案

让我们首先定义我认为您的值(value)观的含义,因为您似乎没有按预期使用它们:

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

所以每个方向可以容纳的四边形数量是SCREEN/GRID。这个值可以在绘制循环之前计算一次:

int quadCount = SCREEN / GRID;

下一个重要部分是了解 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);

现在,要将四边形放置在给定位置,它们的大小需要乘以它们的索引。再次记住,坐标从 (-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();

关于c++ - 绘制一个简单的二维矩形网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25318258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com