基本上我想实现的是一个紧挨着的矩形集合,我可以从一个数组中控制。我似乎无法找到关于这个愚蠢简单的事情的任何可靠信息,因为大多数教程都需要我太大的帮助才能找到帮助。到目前为止,我甚至无法使它渲染网格,它使它非常大。 (出于某种原因?)
#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();
}
答案 0 :(得分:6)
让我们首先定义我认为你的价值观意味着什么,因为你好像没有按照你的意图使用它们:
SCREEN
:窗口大小(以像素为单位)。GRID
:每个四边形的大小,以像素为单位。因此,每个方向适合的四边形数量为SCREEN / GRID
。在绘制循环之前,可以计算一次该值:
int quadCount = SCREEN / GRID;
下一个重要的部分是理解OpenGL坐标系。如果您不应用任何转换(并且实际上不需要用于您的用例),则映射到窗口的x
和y
的范围来自{ {1}}到-1.0f
。因此,要填充窗口,所有四边形的坐标都需要在两个方向上填充1.0f
范围。
由于您需要生成的坐标范围为[-1.0f, 1.0f]
(从2.0f
到-1.0f
),因此此坐标系中每个四边形的宽度/高度为1.0f
。我会在嵌套循环之外计算一次这个值:
2.0f / quadCount
现在,要将四边形放置在给定位置,它们的大小需要乘以它们的索引。再次记住,坐标从float quadSize = 2.0f / static_cast<float>(quadCount);
开始。因此渲染代码可能如下所示:
(-1.0f, -1.0f)