Awfully jagged cube edges in GLUT

Member
Posts: 281
Joined: 2009.04
Post: #1
I'm coding a 3D scene with lighting (W,A,S,D control with mouse look) but when I move the mouse to look the cubes edges become very very rough.

I also cannot seem to code a heads up display.
Screenshot:
[Image: image002.jpg]

main.c

Code:
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <GLUT/glut.h>



#define DEG_TO_RAD        0.0174532925199f
#define ROT_VELOCITY    90.0f
#define MOVE_VELOCITY    25.0f


float bob;
float walk;
GLfloat        heading = 0.0f, pitch = 0.0f, xp = 0.0f, yp = 20.0f, zp = 0.0f;

typedef enum
    {
        ENTER    = 3,
        TAB        = 9,
        RETURN    = 13,
        ESC        = 27,
        SPACE    = 32,
        DEL        = 127,
        UP_ARROW, DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW,
        NUM_KEY_CODES
    } KeyCode;

bool    key[NUM_KEY_CODES], keyDown[NUM_KEY_CODES], keyUp[NUM_KEY_CODES];
int        lastFrameTime = 0;
float    dt;

float moX ,  moY;
// mouse

drawGLString(GLfloat x, GLfloat y, char *textString)
{
    int le;
    int qs;
    
    
    glRasterPos2f(x, y);
    le = (int) strlen(textString);
    for (qs = 0; qs < le; qs++)
    {
        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, textString[qs]);
        
    }
}




void getMouse(int x, int y)
{

    
    
    
    pitch =  ((1050 - y - 360));
    
    if (pitch > 90.0f)
        pitch = 89.0f;

    if (pitch < -90.0f)
        pitch = -90.0f;
    
    
    
    heading = ((x*1));
    
    pitch = pitch * -1;
}


void drawHUD(void)
{
    
    
    glBegin(GL_QUADS);
    glVertex2f(900.0f, 585.0f); // UL
    glVertex2f(900.0f, 465.0f); // BL
    glVertex2f(780.0f, 465.0f); // BR
    glVertex2f(780.0f,585.0f);
    glEnd();
    
}

void drawScene(void)
{
    glBegin(GL_QUADS);
    glColor3f(0.0f,1.0f,0.0f);
    glVertex3f( -100.0f, -1.0f, -100.0f);    
    
    glColor3f(1.0f,0.0f,0.0f);
    glVertex3f(-100.0f,-1.0f, 100.0f);        
    
    glColor3f(0.0f,1.0f,0.0f);
    glVertex3f( 100.0f,-1.0f, 100.0f);
    
    glColor3f(0.5f,0.0f,1.0f);
    glVertex3f( 100.0f,-1.0f,-100.0f);
    glEnd();
    glTranslatef(0.0f,4.0f,0.0f);
    
    //yay!
    glutSolidCube(10);
    
    glTranslatef(10.0f,0.0f,15.0f);
    
    
    glutSolidCube(10);
    
    
}







void update(void)
{    
    
    
    
    /*
    
     if (key[LEFT_ARROW])
     {
     heading -= ROT_VELOCITY * dt;
     while (heading < 0.0f)
     heading += 360.0f;
    
    
    
    
    
    
    
     }
     if (key[RIGHT_ARROW])
     {
     heading += ROT_VELOCITY * dt;
     while (heading > 360.0f)
     heading -= 360.0f;
    
    
     }
     if (key[UP_ARROW])
     {
     pitch += ROT_VELOCITY * dt;
     if (pitch > 45.0f)
     pitch = 45.0f;
    
     }
     if (key[DOWN_ARROW])
     {
    
    
     pitch -= ROT_VELOCITY * dt;
     if (pitch < -45.0f)
     pitch = -45.0f;
    
    
     }
    
     */
    
    
    ////
    
    
    if (key['w'])
    {
        xp += cos(DEG_TO_RAD * (heading - 90.0f)) * MOVE_VELOCITY * dt;
        zp += sin(DEG_TO_RAD * (heading - 90.0f)) * MOVE_VELOCITY * dt;
        
    }
    if (key['s'])
    {
        xp -= cos(DEG_TO_RAD * (heading - 90.0f)) * MOVE_VELOCITY * dt;
        zp -= sin(DEG_TO_RAD * (heading - 90.0f)) * MOVE_VELOCITY * dt;
    }
    if (key['a'])
    {
        xp -= cos(DEG_TO_RAD * heading) * MOVE_VELOCITY * dt;
        zp -= sin(DEG_TO_RAD * heading) * MOVE_VELOCITY * dt;
    }
    if (key['d'])
    {
        xp += cos(DEG_TO_RAD * heading) * MOVE_VELOCITY * dt;
        zp += sin(DEG_TO_RAD * heading) * MOVE_VELOCITY * dt;
    }
    if (key['q'])
    {
        yp += MOVE_VELOCITY * dt;
    }
    if (key['z'])
    {
        yp -= MOVE_VELOCITY * dt;
    }
}



//################################################################################​################################################################################​###############################################################
//################################################################################​###                        ###########################################################################​############################################
//################################################################################​###            DISPLAY     ###########################################################################​############################################
//################################################################################​###                        ###########################################################################​############################################




void display(void)
{
    // calculate delta time
    if (lastFrameTime == 0)
    {
        lastFrameTime = glutGet(GLUT_ELAPSED_TIME);
    }
    int now = glutGet(GLUT_ELAPSED_TIME);
    int elapsedMilliseconds = now - lastFrameTime;
    dt = elapsedMilliseconds / 1000.0f;
    lastFrameTime = now;
    
    update();
    
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearDepth(1.0f);
    
    
    glEnable (GL_BLEND);
     glEnable (GL_LINE_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glShadeModel(GL_SMOOTH);
    
    glEnable(GL_COLOR_MATERIAL);
    
    glLoadIdentity();
    
    // virtual "camera" is first tranform(s) in scene
    glRotatef(pitch, 1.0f, 0.0f, 0.0f);
    glRotatef(heading, 0.0f, 1.0f, 0.0f);
    glTranslatef(-xp, -yp, -zp);
    
    
    glEnable(GL_LIGHT0);
    drawScene();
    drawHUD();
    

    
    glutSwapBuffers();
    resetKeyboardInput();
}

void reshape(int width, int height)
{
    glViewport(0, 0, width, height);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 1000.0f);
    glMatrixMode(GL_MODELVIEW);
}

void idle(void)
{
    glutPostRedisplay();
}



//################################################################################​################################################################################​###############################################################
//################################################################################​###                        ###########################################################################​############################################
//################################################################################​###            MAIN        ###########################################################################​############################################
//################################################################################​###                        ###########################################################################​############################################





void resetKeyboardInput(void)
{
    int        i;
    
    for (i = 0; i < NUM_KEY_CODES; i++)
    {
        keyDown[i] = false;
        keyUp[i] = false;
    }
}

void keyboard(unsigned char rawKeyCode, int x, int y)
{
    if (rawKeyCode < NUM_KEY_CODES)
    {
        key[rawKeyCode] = true;
        keyDown[rawKeyCode] = true;
    }
}

void keyboardUp(unsigned char rawKeyCode, int x, int y)
{
    if (rawKeyCode < NUM_KEY_CODES)
    {
        key[rawKeyCode] = false;
        keyUp[rawKeyCode] = false;
    }
}

void keyboardSpecial(int rawKeyCode, int x, int y)
{
    switch (rawKeyCode)
    {
        case GLUT_KEY_LEFT:
            key[LEFT_ARROW] = true;
            keyDown[LEFT_ARROW] = true;
            break;
        case GLUT_KEY_UP:
            key[UP_ARROW] = true;
            keyDown[UP_ARROW] = true;
            break;
        case GLUT_KEY_RIGHT:
            key[RIGHT_ARROW] = true;
            keyDown[RIGHT_ARROW] = true;
            break;
        case GLUT_KEY_DOWN:
            key[DOWN_ARROW] = true;
            keyDown[DOWN_ARROW] = true;
            break;
            
            
    }
}

void keyboardSpecialUp(int rawKeyCode, int x, int y)
{
    switch (rawKeyCode)
    {
        case GLUT_KEY_LEFT:
            key[LEFT_ARROW] = false;
            keyUp[LEFT_ARROW] = true;
            break;
        case GLUT_KEY_UP:
            key[UP_ARROW] = false;
            keyUp[UP_ARROW] = true;
            break;
        case GLUT_KEY_RIGHT:
            key[RIGHT_ARROW] = false;
            keyUp[RIGHT_ARROW] = true;
            break;
        case GLUT_KEY_DOWN:
            key[DOWN_ARROW] = false;
            keyUp[DOWN_ARROW] = true;
            break;
            
    }
}




int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(640, 480);
    glutCreateWindow("FATAL ERROR");
    
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);
    
    glutKeyboardFunc(keyboard);
    glutKeyboardUpFunc(keyboardUp);
    glutSpecialFunc(keyboardSpecial);
    glutSpecialUpFunc(keyboardSpecialUp);
    
    
    // mousey
    HideCursor();
    glutPassiveMotionFunc(getMouse);
    glutMotionFunc(getMouse);
    
    
    
    
    
    glutFullScreen();
    glutMainLoop();
    return EXIT_SUCCESS;
}

~ Bring a Pen ~
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
You really can't post a couple hundred lines of code, say that your cube edges "are jaggy", and expect people to be able to figure it out without a working screenshot link. Wink

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #3
OK,

1. there's a screenshot.
2. What do you suggest I do then?

~ Bring a Pen ~
Quote this message in a reply
Member
Posts: 353
Joined: 2002.04
Post: #4
Hey mikey, the screenshot you posted isn't showing up. I tried going directly to the link and it times out.
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #5
Very strange. Hmmm. I will try uploading it with imageShack.
Here goes:

[Image: picture1fap.png]

~ Bring a Pen ~
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #6
Code:
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 1000.0f);

Try increasing the near and decreasing the far. Are you possibly running out of z precision? Also, your image link is a 192.168.x.x address. That is a local network address. That's why it didn't work.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #7
Thanks very much.

That's funny, my computer says that's it's personal website. Huh

~ Bring a Pen ~
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #8
That looks like classic z-fighting if I've ever seen it. The zNear/Far of 0.1 to 1000.0 should be fine. What I would recommend is increasing the precision of the z-buffer instead. I think the default GLUT setup is only 16 bits precision. Try replacing this line:

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);

With this line:

glutInitDisplayString("double rgba depth=24");
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #9
Oh, thanks Skorch and AnotherJake. It works very well now. Smile

~ Bring a Pen ~
Quote this message in a reply
Post Reply