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


glLookAt seems to have no effect by Linzz
#include "glut.h"

typedef GLfloat point3[3];


//point3 data[32][4][4];

/* 306 vertices */
void display(void);
void drawPanel(point3 a, point3 b, point3 c, point3 d);
void reshape(int w, int h);
/* 32 patches each defined by 16 vertices, arranged in a 4 x 4 array */
/* NOTE: numbering scheme for teapot has vertices labeled from 1 to 306 */
/* remnent of the days of FORTRAN */

main(int argc, char *argv[])
{
/* Standard GLUT initialization */
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
    glutInitWindowSize(500, 500); /* 500 x 500 pixel window */
    glutInitWindowPosition(0,0); /* place window top left on display */
    glutCreateWindow("3d House with grass and sky background"); /* window title */
    glEnable(GL_DEPTH_TEST);
    glClearColor(1.0, 1.0, 1.0, 1.0); /* draw on white background */
    glColor3f(0.0, 1.0, 0.0); /* draw in black */
    glutDisplayFunc(display); /* display callback invoked when window is opened */
    glutReshapeFunc(reshape);
    glutMainLoop(); /* enter event loop */
}

void display(void)
{
/* HOUSE - as a cube */
    point3 hCube[8] = {
        {0.4,-0.4,0.0}, {0.4,0.4,0.0},
        {-0.4,0.4,0.0}, {-0.4,-0.4,0.0},
        {-0.4,-0.4,-0.8}, {-0.4,0.4,-0.8},
        {0.4,0.4,-0.8}, {0.4,-0.4,-0.8}};
    /* House roof points */
    point3 hRoof[2] = {
        {0.0,0.6,0.0}, {0.0,0.6,-0.8}};
    /* Grass ground */
    point3 grass[8] = {
        {2.0,0.0,0.0}, {2.0,0.0,-2.0},
        {-2.0,0.0,-2.0}, {-2.0,0.0,0.0}};

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    /* house */
    glTranslatef(0.0,0.0,-2.0);
    glColor3f(1.0, 0.0, 0.0);
    drawPanel(hCube[0],hCube[1],hCube[2],hCube[3]);//f wall
    glColor3f(1.0, 0.1, 0.0);
    drawPanel(hCube[3],hCube[2],hCube[5],hCube[4]);//l wall
    glColor3f(1.0, 0.2, 0.0);
    drawPanel(hCube[7],hCube[6],hCube[1],hCube[0]);//b wall
    glColor3f(1.0, 0.3, 0.0);
    drawPanel(hCube[4],hCube[5],hCube[6],hCube[7]);//r wall
    glColor3f(1.0, 0.4, 0.0);
    drawPanel(hCube[2],hRoof[0],hRoof[1],hCube[5]);//l roof
    glColor3f(1.0, 0.5, 0.0);
    drawPanel(hCube[6],hRoof[1],hRoof[0],hCube[1]);//r roof

    /* grass */
    glColor3f(0.0,1.0,0.0);
    drawPanel(grass[0],grass[1],grass[2],grass[3]);
        
    glFlush();
}

/* A function to draw a panel. Requires pointer to a valid point3 array. */
void drawPanel(point3 a, point3 b, point3 c, point3 d)
{
    glBegin(GL_POLYGON);
        glVertex3fv(a);
        glVertex3fv(b);
        glVertex3fv(c);
        glVertex3fv(d);
    glEnd();
}

void reshape(int w, int h)
{
    float ratio;
    // Prevent a divide by zero, when window is too short
    // (you cant make a window of zero width).
    if (h == 0)
        h = 1;
    ratio = 1.0* w / h;

    // Reset the coordinate system before modifying
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    // Set the viewport to be the entire window
    glViewport(0, 0, w, h);

    // Set the correct perspective.
    gluPerspective(60,ratio,0.1,1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    /* First 3: camera position. second3: point we're looking at, third 3: tilt */
    gluLookAt(0.0,0.0,0.0, 
              0.0,0.0,-1.0,
              0.0f,1.0f,0.0f);
}