簡體   English   中英

OpenGL - 創建一個立方體網格

[英]OpenGL - Create a grid of cubes

在此處輸入圖像描述 在此處輸入圖像描述 我想在 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;

正確的?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM