Beginner wants to create multicoloured rotating cube

Hi I am a complete noob so please forgive my clumsiness.

I have been following an opengl 2,1 tutorial for my linux system with good results @ OpenGL 2 Tutorials – Swiftless Tutorials - Various older tutorials and newer programming musings

I saw a tutorial for a multicoloured rotating cube at OpenGL Rotating Cube – Xojo Programming Blog

I converted it to c++ abd tried to hack it together with an existing example for a translucent rotating cube.

I have:

#include <GL/glew.h> // Include the GLEW header file  
#include <GL/glut.h> // Include the GLUT header file  
#include <GL/gl.h>
#include <GL/glu.h>

GLfloat xRot;
GLfloat yRot;
GLfloat zRot;
GLfloat z;
bool running = true;
  
bool* keyStates = new bool[256]; // Create an array of boolean values of length 256 (0-255)   

void keyOperations(void)
{
if(keyStates['a'])
{
}
}

void display(void)
{
keyOperations();

glClearColor(1.0f,1.0f,1.0f,0.0f);//set background color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//set the bitplane area of the window to values previously selected by glClearColor, glClearDepth, and glClearStencil. 
glLoadIdentity();

//produces a translation by x y z. 
//The current matrix (see glMatrixMode) is multiplied by this translation matrix, with the product replacing the current matrix,

//glutSolidCube and glutWireCube render a solid or wireframe cube respectively.
glScalef(0.5, 0.5, 0.5);

// Rotate the cube
glRotatef(xRot, 1, 0, 0);
glRotatef(yRot, 0, 1, 0);
glRotatef(zRot, 0, 0, 1);

// Move it around the z axis
glTranslatef(0, 0, z);


glBegin(GL_QUADS);

// Draw the cube
glColor3f(1.0, 1.0, 0.0); // Yellow
glVertex3f(1.0, 1.0,-1.0);
glVertex3f(-1.0, 1.0,-1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f( 1.0, 1.0, 1.0);

glColor3f(1.0, 0.0, 1.0); // Magenta
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f( 1.0, -1.0, -1.0);

glColor3f(1.0, 1.0, 1.0); // White
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0,-1.0, 1.0);
glVertex3f( 1.0,-1.0, 1.0);

glColor3f(1.0, 0.0, 0.0); // Red
glVertex3f( 1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, -1.0);

glColor3f(0.0, 0.0, 1.0); // Blue
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);

glColor3f(0.0, 1.0, 0.0); // Green
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, -1.0);

glEnd();

glFlush();
//forces buffered OpenGL commands to be sent to the GPU for execution in finite time, but it does not wait for them to complete. 
}

void reshape(int width, int height)
{

glViewport(0,0,(GLsizei)width, (GLsizei)height);
//Parameters
//x, y
//Specify the lower left corner of the viewport rectangle, in pixels. The initial value is (0,0).
//width, height
//Specify the width and height of the viewport. When a GL context is first attached to a window, width and height are set to the
//dimensions of that window.

glMatrixMode(GL_PROJECTION); //glMatrixMode
glLoadIdentity();//replace the current matrix with the identity matrix
//gluPerspective(60, (GLfloat)width/(GLfloat)height,1.0,100.0);//gluPerspective — set up a perspective projection matrix
glMatrixMode(GL_MODELVIEW);
}

void keyPressed(unsigned char key, int x, int y)
{
keyStates[key]=true;
}

void keyUp(unsigned char key, int x, int y)
{
keyStates[key]=false;
}

int main(int argc, char **argv)
{
glutInit(&argc,argv);//initialize the GLUT library.
glutInitDisplayMode(GLUT_SINGLE);//sets the initial display mode.
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Your first OpenGL window");

glutDisplayFunc(display);//glutDisplayFunc

glutReshapeFunc(reshape);//sets the reshape callback for the current window.

glutKeyboardFunc(keyPressed);//sets the keyboard callback for the current window

glutKeyboardUpFunc(keyUp);//glutKeyboardUpFunc

glutMainLoop();//enters the GLUT event processing loop.
}

I’ve managed to get the cube on the screen but I can’t get it to rotate.

Please tell me how to rotate it.

Many thanks.

Hi!

It’s been ages since I used OpenGL in this way but from a quick view of your code I see that none of the rotations - xRot, yRot, zRot - have been defined.

Try setting them to the degree of rotation you want and see what happens.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.