All pastes #631269 Raw Edit

avt

public c v1 · immutable
#631269 ·published 2007-07-23 20:52 UTC
rendered paste body
/* * This program draws a cube with GL_QUAD_STRIP and GL_QUADS * and colors it. It also lets the user spin the cube with the * mouse while the left mouse button (button 1) is pressed and * to zoom with the scroll wheel. * * Copyleft (C) avt 2007 * GNU GPL */#include <stdio.h>#include <stdlib.h>#include <math.h>#include <GL/glfw.h>static int *x;static int *y;static double *zoom;void render(void){    int w;     // window width    int h;     //    "   height    double t;  // time    t = glfwGetTime();    glfwGetWindowSize(&w,&h);    // Dont allow divide-by-zero    h = h < 1 ? 1 : h;    // Set viewport    glViewport(0,0,w,h);    glClearColor(0.0f,0.0f,0.0f,0.0f);    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);    glEnable(GL_DEPTH_TEST);    // Set up proj matrix    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    gluPerspective(65.0,                  (double)w/(double)h,                  1.0,                  100.0);    // Set up modelview matrix    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();    gluLookAt(0.0,0.0,*zoom,              0.0,0.0,0.0,              0.0,1.0,0.0);    // HERE BE DRAGONS    // Save current modelview matrix    glPushMatrix();    glRotatef(1.0f*(float)*x,1.0f,0.0f,0.0f);    glRotatef(1.0f*(float)*y,0.0f,1.0f,0.0f);    glBegin(GL_QUAD_STRIP);    glColor3f(1.0f,0.0f,0.0f);    glVertex3f(-1.0f,-1.0f, 1.0f);    glVertex3f(-1.0f,-1.0f,-1.0f);    glVertex3f(-1.0f, 1.0f, 1.0f);    glVertex3f(-1.0f, 1.0f,-1.0f);    glColor3f(0.0f,1.0f,0.0f);    glVertex3f( 1.0f, 1.0f, 1.0f);    glVertex3f( 1.0f, 1.0f,-1.0f);    glColor3f(0.0f,0.0f,1.0f);    glVertex3f( 1.0f,-1.0f, 1.0f);    glVertex3f( 1.0f,-1.0f,-1.0f);    glColor3f(1.0f,0.0f,0.0f);    glVertex3f(-1.0f,-1.0f, 1.0f);    glVertex3f(-1.0f,-1.0f,-1.0f);    glEnd();    glBegin(GL_QUADS);    glColor3f(1.0f,0.0f,0.0f);    glVertex3f(-1.0f,-1.0f,-1.0f);    glColor3f(1.0f,0.0f,0.0f);    glVertex3f(-1.0f, 1.0f,-1.0f);    glColor3f(0.0f,1.0f,0.0f);    glVertex3f( 1.0f, 1.0f,-1.0f);    glColor3f(0.0f,0.0f,1.0f);    glVertex3f( 1.0f,-1.0f,-1.0f);    glEnd();    glBegin(GL_QUADS);    glColor3f(1.0f,0.0f,0.0f);    glVertex3f(-1.0f,-1.0f, 1.0f);    glColor3f(1.0f,0.0f,0.0f);    glVertex3f(-1.0f, 1.0f, 1.0f);    glColor3f(0.0f,1.0f,0.0f);    glVertex3f( 1.0f, 1.0f, 1.0f);    glColor3f(0.0f,0.0f,1.0f);    glVertex3f( 1.0f,-1.0f, 1.0f);    glEnd();    glPopMatrix();}int main(int argc,char** argv){    int running;    x = malloc(sizeof(int));    y = malloc(sizeof(int));    zoom = malloc(sizeof(double));    glfwInit();    // Open window    if(!glfwOpenWindow(800,600,8,8,8,8,24,0,GLFW_WINDOW)){        fprintf(stderr,"Error starting GLFW...");        glfwTerminate();        return 1;    }    glfwSetWindowTitle("This is not, i repeat not, lesson06");    do {        render();        glfwSwapBuffers();        if(glfwGetMouseButton(GLFW_MOUSE_BUTTON_1)==GLFW_PRESS)            glfwGetMousePos(x,y);        *zoom = ((double)glfwGetMouseWheel())+10.0f;        // Quit if pressing ESC        running = !glfwGetKey(GLFW_KEY_ESC) &&                  glfwGetWindowParam(GLFW_OPENED);    } while(running);    glfwTerminate();    free(zoom);    free(y);    free(x);    return 0;}