gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:13:40 28 4
gpt4 key购买 nike

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/

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