# Drawing an orthogonal cube.

Hi, I’ve just started learning OpenGL as part of my course. I’m struggling to get my head around one question, in which I need to draw a cube
with the centre at (0,0,0) and with two corner points (-1, -1, -1) and (1, 1, 1). It also needs to face orthogonal to x,y,z axis.

I’ve currently made 6 squares, so a cube, but as it is front on I can only see 1 face and so it looks just like a square.

``````
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer

gluLookAt(0.0f, 1.0f, 10.0f,
0.0f, 1.0f, 9.0f,
0.0f, 1.0f, 0.0f); //Set the point looking at

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

// Bottom face (y = -1.0f)
glColor3f(1.0f, 0.5f, 0.0f);     // Orange
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
// Front face  (z = 1.0f)
lColor3f(1.0f, 0.0f, 0.0f);     // Red
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
// Back face (z = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f);     // Blue
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f);     // Magenta
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd();

glutSwapBuffers(); // Swap The Buffers To Not Be Left With A Clear Screen

``````

How would I go about making my cube look orthogonal?

Thanks for any help!

[QUOTE=JaAnTr;1262399]as it is front on I can only see 1 face and so it looks just like a square.

``````
gluLookAt(0.0f, 1.0f, 10.0f,
0.0f, 1.0f, 9.0f,
0.0f, 1.0f, 0.0f); //Set the point looking at

``````

How would I go about making my cube look orthogonal?[/QUOTE]
The above gluLookAt() call has the viewpoint centred horizontally ([var]eyeX[/var]=0) and exactly aligned with top of the cube vertically ([var]eyeY[/var]=1), with the view direction perpendicular to the front face of the cube ([var]center[/var]-[var]eye[/var]=[0,0,-1]). Clearly, you will only be able to see the front face of the cube (with either perspective or orthographic projection).

Try changing the viewpoint to, e.g.

``````
gluLookAt(1.5f, 2.0f, 5.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f); //Set the point looking at

``````

That should show the front, top, and right faces of the cube (in decreasing order of projected area), with the cube centred in the viewport.

Thanks that’s perfect!

I’m now trying to rotate the cube using the arrow keys using glRotatef() and i’ve got it working but it’s not working as I’d like. It’s quite hard to explain but I think the cube is rotating around the global Y axis and the normal X axis instead of rotating always rotating around the normal. Here’s my whole code if you don’t mind taking a look.

``````
// CW1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>   // Standard Header For Most Programs
#include <gl/gl.h>     // The GL Header File
#include <glut.h>   // The GL Utility Toolkit (Glut) Header
#include <math.h>      // For mathematic operation

double rotate_y = 0;
double rotate_x = 0;

char Rendermode;
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

void InitGL(GLvoid)     // Create Some Everyday Functions for Initialization
{

glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
glClearDepth(1.0f);								// Depth Buffer Setup
glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
glDepthFunc(GL_LEQUAL);						    // The Type Of Depth Testing To Do
glEnable(GL_COLOR_MATERIAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer

gluLookAt(1.5f, 2.0f, 5.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f); //Set the point looking at

glRotatef(rotate_x, 1.0f, 0.0f, 0.0f);   //X
glRotatef(rotate_y, 0.0f, 1.0f, 0.0f);   //Y

switch (Rendermode) { //different render mode

case 'f': // for display faces

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

// Bottom face (y = -1.0f)
glColor3f(1.0f, 0.5f, 0.0f);     // Orange
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);

// Front face  (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f);     // Red
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);

// Back face (z = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);

// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f);     // Blue
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);

// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f);     // Magenta
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd();
break;

case 'v': // for display points
glBegin(GL_POINTS);									// Draw points
glColor3f(0.0f, 1.0f, 0.0f);			// Set The Color To Green
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);

glVertex3f(1.0f, -1.0f, -1.0f);		//right side back bottom
glVertex3f(1.0f, 1.0f, -1.0f);		//right side back top

glVertex3f(-1.0f, 1.0f, -1.0f);		//left side back top
glVertex3f(-1.0f, -1.0f, -1.0f);	//left side back bottom

glEnd();						// Done Drawing Points

glPointSize(5);		//Set Points Size
break;

case 'e': // for display edges

glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 1.0f);			// Set The Color To Blue

glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);

glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);

glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);

glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);

glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);

glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);

glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);

glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);

glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);

glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);

glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);

glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);

glEnd();						// Done Drawing Edges

break;
}

// draw frame *************************************************************
//TO DO: Draw Cartesian scene coordinate system
// draw frame *************************************************************

glutSwapBuffers(); // Swap The Buffers To Not Be Left With A Clear Screen
}

void reshape(int width, int height)   // Create The Reshape Function (the viewport)
{
if (height == 0)										// Prevent A Divide By Zero By
{
height = 1;										// Making Height Equal One
}

glViewport(0, 0, width, height);						// Reset The Current Viewport
glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
glLoadIdentity();									// Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);

glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
}

void keyboard(unsigned char key, int x, int y)  // Create Keyboard Function
{
switch (key) {
case 27:        // When Escape Is Pressed...
exit(0);   // Exit The Program
break;        // Ready For Next Case

case 'v':  //Vetices Only
Rendermode = 'v';
break;

case 'e':
Rendermode = 'e';
break;

case 'f':
Rendermode = 'f';
break;

// Rotate the Cube*****************************************
// TO DO : Use some keys to rotate the cube.
// Rotate the Cube*****************************************
default:        // Now Wrap It Up
break;
}
glutPostRedisplay(); //request display() call ASAP
}

//TO DO: using mouse click and move (or arrow keys) to rotate, zoom in/out and pan the camera*********************
void arrow_keys(int a_keys, int x, int y)  // Create Special Function (required for arrow keys)
{
switch (a_keys) {
case GLUT_KEY_UP:     // When Up Arrow Is Pressed...
rotate_x += -5;
break;
case GLUT_KEY_DOWN:               // When Down Arrow Is Pressed...
rotate_x += 5;
break;
case GLUT_KEY_RIGHT:               // When Right Arrow Is Pressed...
rotate_y += 5; // Rotate cube by +5 on the Y-Axis
break;
case GLUT_KEY_LEFT:               // When Left Arrow Is Pressed...
rotate_y += -5; // Rotate cube by -5 on the Y-Axis
break;
default:
break;
}
}

void mouseButton(int button, int state, int x, int y) {
}

void mouseMove(int x, int y) {
}
//TO DO: using mouse click and move (or arrow keys) to rotate, zoom in/out and pan the camera*********************

void main(int argc, char** argv)   // Create Main Function For Bringing It All Together
{
glutInit(&argc, argv); // Erm Just Write It =)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); // Display Mode
glutInitWindowSize(500, 500); // If glutFullScreen wasn't called this is the window size
glutCreateWindow("CW1 OpenGL Framework"); // Window Title (argv[0] for current directory as title)
glutFullScreen();          // Put Into Full Screen
InitGL();
Rendermode = 'f';
glutDisplayFunc(display);  // Matching Earlier Functions To Their Counterparts
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard); //Keybord call back function.
glutSpecialFunc(arrow_keys);// Special key call back function.
glutMouseFunc(mouseButton); // mouse click call back function.
glutMotionFunc(mouseMove); //mouse move call back function.

glutIdleFunc(display);
glutMainLoop();          // Initialize The Main Loop
}

``````

You might want to replace the gluLookAt() call with e.g. glTranslatef(0.0f,0.0f,-5.0f). Otherwise the slightly odd angle will make it harder to figure out what’s going on. Change the initial values of rotate_x and rotate_y if you don’t want it to start face-on.

If that isn’t the behaviour you’re looking for, you’ll need to be more specific.

Thanks, I’ve replaced the gluLookAt(). The issue I’m having is that i’ve essentially made it so the left and right arrow keys rotate the cube left and right and the up and down rotate up and down. Now say for example I run the program, the front facing side of the cube is red and the top face is green. If I use the down arrow to make it now so I can only see the green top face then use the left and right arrows all that happens is the green face spins around and that isn’t how I need it to rotate. I need it to rotate so that in that situation I would see all the other faces when using left and right arrows.

I’m not sure if I’ve explained it well, do you understand what I mean?