How to draw multiple cubes in OpenGL

I want to draw some cubes using glutSolidCube at some points in space. In the examples I found, just call glutSolidCube and it works, but the only way the cube draws for me is if the string is enclosed in glBegin(GL_POLYGON) , which is not required in the examples I saw, and I only get one cube instead of several. I have:

 glColor3f(1, 0, 0); glLoadIdentity(); glTranslatef(5,2,1); glutSolidCube(1); glLoadIdentity(); glTranslatef(10,8,0); glutSolidCube(1); glLoadIdentity(); glTranslatef(3,7,9); glutSolidCube(1); glLoadIdentity(); glTranslatef(1,4,6); glutSolidCube(1); 

When I start, nothing happens. I know that there are no problems with points outside of my view, because if I draw vertices at the same points, I can see them. As far as I can tell from the examples and documentation that I read, I am not doing anything wrong. Can someone tell me what I'm doing wrong, or give me a piece of code that draws several cubes?

+6
source share
2 answers

Try the following:

 glColor3f(1, 0, 0); glPushMatrix(); glTranslatef(5,2,1); glutSolidCube(1); glPopMatrix(); glPushMatrix(); glTranslatef(10,8,0); glutSolidCube(1); glPopMatrix(); glPushMatrix(); glTranslatef(3,7,9); glutSolidCube(1); glPopMatrix(); glPushMatrix(); glTranslatef(1,4,6); glutSolidCube(1); glPopMatrix(); 

Without reinstalling the model view matrix using glLoadIdentity() . Note that first you need to call glOrtho() or glPerspective() to install the camera once.

+5
source
 #include <GL/glut.h> void init() { 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 ); static float angle = 0; angle += 1.0f; glPushMatrix(); glTranslatef(0,0,0); glRotatef(angle, 0.1, 0.2, 0.5); glColor3ub(255,0,255); glutSolidCube(5); glPopMatrix(); glPushMatrix(); glTranslatef(10,-10,0); glRotatef(angle, 0.1, 0.2, 0.5); glColor3ub(255,0,0); glutSolidCube(5); glPopMatrix(); glPushMatrix(); glTranslatef(10,10,0); glRotatef(angle, 0.1, 0.2, 0.5); glColor3ub(0,255,0); glutSolidCube(5); glPopMatrix(); glPushMatrix(); glTranslatef(-10,10,0); glRotatef(angle, 0.1, 0.2, 0.5); glColor3ub(0,0,255); glutSolidCube(5); glPopMatrix(); glPushMatrix(); glTranslatef(-10,-10,0); glRotatef(angle, 0.1, 0.2, 0.5); glColor3ub(255,255,0); glutSolidCube(5); glPopMatrix(); glutSwapBuffers(); } void reshape(int w, int h) { glViewport(0, 0, w, h); } void timer(int extra) { glutPostRedisplay(); glutTimerFunc(16, timer, 0); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitWindowSize(640,480); glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); glutCreateWindow("CUBES"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutTimerFunc(0, timer, 0); init(); glutMainLoop(); return 0; } 
+5
source

Source: https://habr.com/ru/post/914492/


All Articles