c++ - OpenGL - 创建立方体网格

标签 c++ opengl glut

enter image description here enter image description here我想使用我已经制作的多维数据集在 C++ 中使用带有过剩实现的 OpenGL 创建一个网格。网格应该是 15x15 。尝试使用 for 循环,但似乎找不到一种有效的方法,使用循环编写尽可能少的代码。这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/gl.h>     // openGL header
#include <GL/glu.h>   // glut header
#include <GL/glut.h>   // glut header




void init()
{       //for 3d lighting
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_COLOR_MATERIAL);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    GLint viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport);
    double aspect = (double)viewport[2] / (double)viewport[3];
    gluPerspective(60, aspect, 1, 100);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // move back a bit
    glTranslatef( 0, 0, -35 );


    float e=0,f=0;


    for(int i=0;i<8;i++) {
        for(int j=0;j<8;j++) {      
            glPushMatrix();
                glTranslatef(0.0f+e,0.0f+f,0.0f); //right and up
                glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
                    glColor3ub(245, 245, 220); //Beige
                    glutSolidCube(2.25);
                glPopMatrix();

            /*glPushMatrix();
                glTranslatef(0.0f-e,0.0f-f,0.0f); //
                glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
                    glColor3ub(245, 245, 220); //Beige
                    glutSolidCube(2.25);
                glPopMatrix();*/


                f=+-2.63;
            }
            f=0;
            e+=2.63;
        }

    /*e=0;
    for(int i=0;i<8;i++) {
            glPushMatrix();
            glTranslatef(0.0f,0.0f+e,0.0f);
            glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
                glColor3ub(245, 245, 0); //Beige
                glutSolidCube(2.2);
            glPopMatrix();

            e+=2.63;
        }*/

    glutSwapBuffers();

}

void reshape(GLsizei width, GLsizei height) { 

    // GLsizei for non-negative integer // Compute aspect ratio of the new window

    if (height == 0) height = 1; // To prevent divide by 0 
    GLfloat aspect = (GLfloat)width / (GLfloat)height; // Set the viewport to cover the new window 
    glViewport(0, 0, width, height); // Set the aspect ratio of the clipping volume                 glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix 
    glLoadIdentity(); // Reset // Enable perspective projection with fovy, aspect, zNear and zFar 
    gluPerspective(45.0f, aspect, 0.1f, 100.0f); 

} 

void timer(int extra)
{
    glutPostRedisplay();
    glutTimerFunc(16, timer, 0);
}



int main(int argc, char **argv)
{
    glutInit(&argc, argv);
        glutInitWindowSize(600,600);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE| GLUT_MULTISAMPLE);
        glEnable(GL_MULTISAMPLE); //anti-alliasing  
        glutCreateWindow("CUBES");

     glutDisplayFunc(display);
     glutReshapeFunc(reshape);
     glutTimerFunc(0, timer, 0);

     init();

     glutMainLoop();
     return 0;
}

想法是在中心创建立方体并复制以制作理想的 15x15 网格。

最佳答案

我想这可能是你的问题,

f=+-2.63;  

should be

f += -2.63;

对吗?

关于c++ - OpenGL - 创建立方体网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58979207/

相关文章:

opengl - 如何在 GTX 560 及更高版本上使用 OpenGL 进行立体 3D?

c - glutMainLoop() 中的内存损坏?

c++ - MXE Qt5 应用程序在 Docker 容器中构建失败

c++ - 查找两点之间的整数坐标

c++ - 如何复制文件并继承Windows EFS?

c++ - OpenGL 中的第三个三角形顶点在窗口中渲染时显示为 0.0

c++ - 如何让C++编译器知道一个函数是 `Idempotent`

c++ - 使用 calloc 分配内存时出错(释放堆 block XXX 在释放后在 YYY 修改)

c - 屏幕上看不到实心球体

c++ - 如何在 Linux 上将 glut 与 Windows 交叉编译器一起使用?