Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Color Cube

  1. #1
    Newbie Newbie
    Join Date
    Nov 2013
    Posts
    1

    Color Cube

    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();
    }
    Last edited by Naxical13; 11-16-2013 at 11:02 PM.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,106
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •