PDA

View Full Version : Color Cube



Naxical13
11-16-2013, 09:01 PM
Hi!
I am trying to draw a color cube in OpenGL. All I am getting is a square. Kindly help me! :)
This is my code:

#include<GL/glut.h>
#include<math.h>
float vertices[8][3]={{0,0,0},{0,0,200},{0,200,0},{200,0,0},{0,200,200 },{200,0,200},{200,200,0},{200,200,200}};
float colors[8][3]={{0.6,0.9,0.1},{0.2,0.1,0.3},{0.7,0.7,0.5},{0.2,0 .7,0.4},{0.6,0.6,0.4},{0.1,0.1,0.5},{0.7,0.2,0.5}, {0.9,0.7,0.4}};
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);

glColor3fv(colors[0]);
glVertex3fv(vertices[0]);
glColor3fv(colors[2]);
glVertex3fv(vertices[2]);
glColor3fv(colors[5]);
glVertex3fv(vertices[5]);
glColor3fv(colors[1]);
glVertex3fv(vertices[1]);
glEnd();
glBegin(GL_POLYGON);
glColor3fv(colors[0]);
glVertex3fv(vertices[0]);
glColor3fv(colors[3]);
glVertex3fv(vertices[3]);
glColor3fv(colors[6]);
glVertex3fv(vertices[6]);
glColor3fv(colors[2]);
glVertex3fv(vertices[2]);
glEnd();
glBegin(GL_POLYGON);
glColor3fv(colors[1]);
glVertex3fv(vertices[1]);
glColor3fv(colors[4]);
glVertex3fv(vertices[4]);
glColor3fv(colors[7]);
glVertex3fv(vertices[7]);
glColor3fv(colors[5]);
glVertex3fv(vertices[5]);
glEnd();
glBegin(GL_POLYGON);
glColor3fv(colors[4]);
glVertex3fv(vertices[4]);
glColor3fv(colors[3]);
glVertex3fv(vertices[3]);
glColor3fv(colors[6]);
glVertex3fv(vertices[6]);
glColor3fv(colors[7]);
glVertex3fv(vertices[7]);
glEnd();
glBegin(GL_POLYGON);
glColor3fv(colors[2]);
glVertex3fv(vertices[2]);
glColor3fv(colors[6]);
glVertex3fv(vertices[6]);
glColor3fv(colors[7]);
glVertex3fv(vertices[7]);
glColor3fv(colors[5]);
glVertex3fv(vertices[5]);
glEnd();
glBegin(GL_POLYGON);
glColor3fv(colors[0]);
glVertex3fv(vertices[0]);
glColor3fv(colors[3]);
glVertex3fv(vertices[3]);
glColor3fv(colors[4]);
glVertex3fv(vertices[4]);
glColor3fv(colors[1]);
glVertex3fv(vertices[1]);
glEnd();
glFlush();
}

void init()
{
glClearColor(1.0,1.0,1.0,0);
glEnable(GL_DEPTH_TEST);
glOrtho(-960,960,-720,720,-600,600);
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPT H);
glutInitWindowSize(960,720);
glutInitWindowPosition(0,0);
glutCreateWindow("Color Cube");
glutDisplayFunc(display);
init();
glutMainLoop();
}

tonyo_au
11-17-2013, 05:02 AM
You haven't setup a camera so you are getting the default translation matrices. Have a look at http://www.songho.ca/opengl/gl_projectionmatrix.html