C GLUT drawing problems.

Member
Posts: 281
Joined: 2009.04
Post: #1
I've set up a rather fancy box with a direction pointer, and I've coded a 'laser'. The idea is the direction pointer extends 1000px in the direction specified by dir.
I've used NSBeep(); to check where my code stops, and I've concluded the code reaches the point at where It should draw the laser, but it does nothing onscreen.

Here's main.c:
Code:
// Standard GLUT/C Setup.

#include <stdlib.h>


#include <GLUT/glut.h>

enum
{
    false,
    true
};

typedef unsigned char   Bool;

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; // delta time (how much time in between frames, used for all movement calculations)
float    shipPosX;
float    shipPosY;
int dir;



void fire()

{

    
    if (dir==0)
    {
        //works here
        glBegin(GL_LINES);
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX,shipPosY+500,0.0f);
        glEnd();
    }
    else if (dir==90)
    {
        glBegin(GL_LINES);
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX+500,shipPosY,0.0f);
        glEnd();
    }
    else if (dir==180)
    {
        glBegin(GL_LINES);
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX,shipPosY-500,0.0f);
        glEnd();
    }
    else if (dir==-180)
    {
        glBegin(GL_LINES);
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX-500,shipPosY,0.0f);
        glEnd();
    }
    
    
}


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;

    }
}


void display(void)
{
    if (lastFrameTime == 0)
    {
        lastFrameTime = glutGet(GLUT_ELAPSED_TIME);
    }
    int now = glutGet(GLUT_ELAPSED_TIME);
    int elapsedMilliseconds = now - lastFrameTime;
    float dt = elapsedMilliseconds / 1000.0f;
    lastFrameTime = now;
    
    // "key" is cached locally so that it stays down until the next key up event
    if (key[LEFT_ARROW])
    {
        shipPosX -= 200.0f * dt;
        dir = -90;
    

    }
    if (key[RIGHT_ARROW])
    {
        shipPosX += 200.0f * dt;
        dir = 90;
    }
    if (key[UP_ARROW])
    {
        shipPosY += 200.0f * dt;
        dir = 0;
    }
    if (key[DOWN_ARROW])
    {
        shipPosY -= 200.0f * dt;
        dir =180;
    }
    if (key[SPACE])
    {


        fire();
        
    }
    
    // also do wasd to demonstrate discrete "keyDown" events

    
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    // This code  is where you put all drawing code. v
    
    // draw the ship @ the position
    
    
    
    
    glBegin(GL_LINE_LOOP);
    glVertex3f(shipPosX-40,shipPosY-40, 0.0f);
    glVertex3f(shipPosX+40,shipPosY-40, 0.0f);
    glVertex3f(shipPosX+40, shipPosY+40, 0.0f);
    glVertex3f(shipPosX-40,shipPosY+40,0.0f);
    
    
    
    glEnd();
    glBegin(GL_LINE_LOOP);
    glVertex3f(shipPosX-30,shipPosY-30, 0.0f);
    glVertex3f(shipPosX+30,shipPosY-30, 0.0f);
    glVertex3f(shipPosX+30, shipPosY+30, 0.0f);
    glVertex3f(shipPosX-30,shipPosY+30,0.0f);
    
    
    
    glEnd();
    
    if (dir==0)
    {
        glBegin(GL_LINES);
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX,shipPosY+40,0.0f);
        glEnd();
    }
    else if (dir==90)
    {
        glBegin(GL_LINES);
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX+40,shipPosY,0.0f);
        glEnd();
    }
    else if (dir==180)
    {
        glBegin(GL_LINES);
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX,shipPosY-40,0.0f);
        glEnd();
    }
    else if (dir==-90)
    {
        glBegin(GL_LINES);
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX-40,shipPosY,0.0f);
        glEnd();
    }
    
    
    
    
    // That's the ship drawn
    
    
    
    // drawframe
    glutSwapBuffers();
    
    // do this to clear the key ups and key downs for each frame
    resetKeyboardInput();
}

void reshape(int width, int height)
{
    glViewport(0, 0, width, height);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, width, 0, height);
    glMatrixMode(GL_MODELVIEW);
}


void idle(void)
{
    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    //glutInitWindowSize(1680,1050);
    glutInitWindowSize(640, 480);
    
    glutCreateWindow("GLUT Program");
    
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);
    glutFullScreen();
    
    glutKeyboardFunc(keyboard);
    glutKeyboardUpFunc(keyboardUp);
    glutSpecialFunc(keyboardSpecial);
    glutSpecialUpFunc(keyboardSpecialUp);
    
    
    
    
    //glutFullScreen();
    glutMainLoop();
    return EXIT_SUCCESS;
}

~ Bring a Pen ~
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #2
You're calling fire to draw before the color buffer gets cleared, so you're drawing your laser beam and then erasing everything! Move your clear to the top of display(), like:

Code:
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #3
Thanks AnotherJake! WackoSmileGrinCool

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