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:: No Line Numbers


cannon by alex
1
 
#include <GL/glut.h>
#include <windows.h>

typedef struct treenode
{
    GLfloat m[16];
    GLfloat clr[3];
    int display_list_id; //dspl_lst drawing the node (may use pointer to a fnctn instead)
    struct treenode *child;
    struct treenode *sibling;
} treenode;

void init();
void idle();
void display();
void reshape(GLint width, GLint height);
void keyboard();
void setupScene();
void traverse(treenode *root);

enum { FLOOR = 1, CANNON, WHEEL, BOX };

treenode floor_node, d_wall1_node, d_wall2_node, cannon_node, l_wheel_node, r_wheel_node, box_node;
static int g_Width = 640;                          // Initial window width
static int g_Height = 480;                         // Initial window height
static GLfloat g_nearPlane = 1;
static GLfloat g_farPlane = 500;

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);

    //enable double buffering
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);

    glutInitWindowSize(g_Width, g_Height);
    glutCreateWindow("Homework 2");
    init();
    glutDisplayFunc(display);
    //glutKeyboardFunc(keyboard);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);
    glutMainLoop();
    return 0;
}

void init()
{
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_MODELVIEW);
    setupScene();
}

void idle()
{
}

void display()
{
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt(0, 0, -100, 0, 0, 0, 0, 1, 0);
    traverse(&floor_node);
    glutSwapBuffers();
}

void reshape(GLint width, GLint height)
{
   g_Width = width;
   g_Height = height;

   glViewport(0, 0, g_Width, g_Height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(65.0, (float)g_Width / g_Height, g_nearPlane, g_farPlane);
   glMatrixMode(GL_MODELVIEW);
}

void keyboard()
{
}

void setupScene()
{
    glNewList(FLOOR, GL_COMPILE);
    glBegin(GL_QUADS);
        glVertex3f(-100.0, 100.0, 0.0);
        glVertex3f(100.0, 100.0, 0.0);
        glVertex3f(100.0, -100.0, 0.0);
        glVertex3f(-100.0, -100.0, 0.0);
    glEnd();
    glEndList();

    glNewList(CANNON, GL_COMPILE);

    glEndList();

    glNewList(WHEEL, GL_COMPILE);

    glEndList();

    glNewList(BOX, GL_COMPILE);

    glEndList();

    // floor node
    glLoadIdentity();
    //glTranslatef(50, 50, 0);
    glRotatef(20, 1.0, 0.0, 0.0);
    //glRotatef(20, 0.0, 1.0, 0.0);
    glScalef(.5, .5, .5);
    glGetFloatv(GL_MODELVIEW_MATRIX,floor_node.m);
    (floor_node.clr)[0] = 1.0;
    (floor_node.clr)[1] = 0.0;
    (floor_node.clr)[2] = 0.0;
    floor_node.display_list_id = FLOOR;
    floor_node.child = &cannon_node;
    floor_node.sibling = &d_wall1_node;

    // wall 1 node
    glLoadIdentity();
    glGetFloatv(GL_MODELVIEW_MATRIX,d_wall1_node.m);
    (d_wall1_node.clr)[0] = 0.0;
    (d_wall1_node.clr)[1] = 1.0;
    (d_wall1_node.clr)[2] = 0.0;
    d_wall1_node.display_list_id = FLOOR;
    d_wall1_node.child = NULL;
    d_wall1_node.sibling = &d_wall2_node;

    // wall 2 node
    glLoadIdentity();
    glGetFloatv(GL_MODELVIEW_MATRIX,d_wall2_node.m);
    (d_wall2_node.clr)[0] = 1.0;
    (d_wall2_node.clr)[1] = 0.0;
    (d_wall2_node.clr)[2] = 0.0;
    d_wall2_node.display_list_id = FLOOR;
    d_wall2_node.child = NULL;
    d_wall2_node.sibling = NULL;

    // cannon node
    glLoadIdentity();
    glGetFloatv(GL_MODELVIEW_MATRIX,cannon_node.m);
    (cannon_node.clr)[0] = 0.0;
    (cannon_node.clr)[1] = 0.0;
    (cannon_node.clr)[2] = 1.0;
    cannon_node.display_list_id = CANNON;
    cannon_node.child = NULL;
    cannon_node.sibling = &l_wheel_node;

    // left cannon wheel
    glLoadIdentity();
    glTranslatef(0.2,0.0,0.0);
    glScalef(0.2,0.2,0.0);
    glGetFloatv(GL_MODELVIEW_MATRIX,l_wheel_node.m);
    (l_wheel_node.clr)[0] = 0.0;
    (l_wheel_node.clr)[1] = 0.0;
    (l_wheel_node.clr)[2] = 0.0;
    l_wheel_node.display_list_id = WHEEL;
    l_wheel_node.child = NULL;
    l_wheel_node.sibling = &r_wheel_node;

    // right cannon wheel
    glLoadIdentity();
    glTranslatef(2.8,0.0,0.0);
    glScalef(0.2,0.2,0.0);
    glGetFloatv(GL_MODELVIEW_MATRIX,r_wheel_node.m);
    (r_wheel_node.clr)[0] = 0.0;
    (r_wheel_node.clr)[1] = 0.0;
    (r_wheel_node.clr)[2] = 0.0;
    r_wheel_node.display_list_id = WHEEL;
    r_wheel_node.child = NULL;
    r_wheel_node.sibling = NULL;

    // box node
    glLoadIdentity();
    glTranslatef(2.5,0.2,0.0);
    glGetFloatv(GL_MODELVIEW_MATRIX,box_node.m);
    (box_node.clr)[0] = 1.0;
    (box_node.clr)[1] = 1.0;
    (box_node.clr)[2] = 1.0;
    box_node.display_list_id = BOX;
    box_node.child = NULL;
    box_node.sibling = NULL;
}

void traverse(treenode *root)
{
    if (root==NULL) return;
    glPushMatrix();
    glPushAttrib(GL_COLOR);
    glMultMatrixf(root->m);
    glColor3f((root->clr)[0],(root->clr)[1],(root->clr)[2]);
    glCallList(root->display_list_id);
    if(root->child != NULL) traverse(root->child);
    glPopMatrix();
    glPopAttrib();
    if(root->sibling != NULL) traverse(root->sibling);
}