GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


Weird results on Linux by serninpc
/* It works perfectly on Windows, but on Linux, I'm getting weird results. */
/* On Linux use: g++ -o cubetest -lglut cubetest.cpp */
#ifdef WINDOWS
#include <windows.h>
#endif
#include <GL/glut.h>
#include <stdio.h>

static GLfloat spin = 0.0;
static float pcolors[] = { 
                0.0, 0.0, 0.0, 1.0, //black
                1.0, 1.0, 1.0, 1.0, //white
                1.0, 1.0, 0.0, 1.0, //yellow
                1.0, 0.0, 0.0, 1.0, //red
                1.0, 0.5, 0.0, 1.0, //orange
                0.0, 0.0, 1.0, 1.0, //blue
                0.0, 1.0, 0.0, 1.0 //green
            };
static int sides[] = { 1,2,3,4,5,6};
void display(void);
void spinDisplay(void);
void reshape(int w, int h);
void setColor(int c);
void drawCubeSide(int c);
void drawCube(float size, int* colors);

int main(int argc, char* argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(500,500);
    int window=glutCreateWindow("Rubik");
    glClearColor(0.0, 0.0, 0.5, 0.0);
    glEnable(GL_DEPTH_TEST);
    //glShadeModel(GL_FLAT);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(spinDisplay);
    glutMainLoop();
    return 0;
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(0.0,0.0,-15.0);
        glRotatef(spin,0.5,0.2,0.1);
        drawCube(2.5, sides);
    glPopMatrix();
    glFlush();
    glutSwapBuffers();
}
void spinDisplay(void)
{
    spin = spin + 0.02;
    if(spin > 360.0)
        spin = spin - 360.0;
    display();
}
void setColor(int c)
{
    glColor3fv(pcolors+4*c);
}
void reshape(int w, int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0,(GLfloat)w/(GLfloat)h,1.0,20.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void drawCubeSide(int c)
{
    setColor(c);
    glBegin(GL_POLYGON);
    {
        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();
}
void drawCube(float size, int* colors)
{
    glScalef(size,size,size);
    glPushMatrix();
    {
        drawCubeSide(colors[0]);
    }
    glPopMatrix();
    glPushMatrix();
    {
                //Back
        glRotatef(180.0, 0.0, 1.0, 0.0);
        drawCubeSide(colors[1]);
    }
    glPopMatrix();
    glPushMatrix();
    {
                //Up
        glRotatef(-90.0, 1.0, 0.0, 0.0);
        drawCubeSide(colors[2]);
    }
    glPopMatrix();
    glPushMatrix();
    {
                //Down
        glRotatef(90.0, 1.0, 0.0, 0.0);
        drawCubeSide(colors[3]);
    }
    glPopMatrix();
    glPushMatrix();
    {
                //Left
        glRotatef(-90.0, 0.0, 1.0, 0.0);
        drawCubeSide(colors[4]);
    }
    glPopMatrix();
    glPushMatrix();
    {
                //Right
        glRotatef(90.0, 0.0, 1.0, 0.0);
        drawCubeSide(colors[5]);
    }
    glPopMatrix();
}

 
Comments by GLProgramming users
 
Author Comment by serninpc 20:52:18 29/Dec/2008 


    On the right image (Linux), the red side of the cube is on top.
The left image (Windows) is the desired result.

Weird thing: it works great using wine.