Skip to content

Instantly share code, notes, and snippets.

@davidwparker
Created September 27, 2011 21:56
Show Gist options
  • Select an option

  • Save davidwparker/1246364 to your computer and use it in GitHub Desktop.

Select an option

Save davidwparker/1246364 to your computer and use it in GitHub Desktop.
OpenGL Screencast 11: Code Organization
#include "screencasts.h"
/*
* main()
* ----
* Start up GLUT and tell it what to do
*/
int main(int argc,char* argv[])
{
initializeGlobals();
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(windowWidth,windowHeight);
glutCreateWindow(windowName);
glutDisplayFunc(display);
glutReshapeFunc(displayReshape);
glutKeyboardFunc(windowKey);
glutSpecialFunc(windowSpecial);
glutMainLoop();
return 0;
}
/* Poor man's approximation of PI */
#define PI 3.1415926535898
/* Macro for sin & cos in degrees */
#define Cos(th) cos(PI/180*(th))
#define Sin(th) sin(PI/180*(th))
/* D degrees of rotation */
#define DEF_D 5
#include "screencasts.h"
/*
* displayInit
* -------
* Initializes display
*/
void displayInit()
{
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
}
/*
* displayEye()
* ------
* Set the eye position
*/
void displayEye()
{
if (toggleMode) {
double Ex = -2*dim*Sin(th)*Cos(ph);
double Ey = +2*dim *Sin(ph);
double Ez = +2*dim*Cos(th)*Cos(ph);
/* camera/eye position, aim of camera lens, up-vector */
gluLookAt(Ex,Ey,Ez , 0,0,0 , 0,Cos(ph),0);
}
/* Orthogonal - set world orientation */
else {
glRotatef(ph,1,0,0);
glRotatef(th,0,1,0);
}
}
/*
* displayReshape()
* ------
* GLUT calls this routine when the window is resized
*/
void displayReshape(int width,int height)
{
asp = (height>0) ? (double)width/height : 1;
glViewport(0,0, width,height);
displayProject();
}
/*
* displayProject()
* ------
* Sets the projection
*/
void displayProject()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (toggleMode) {
/* perspective */
gluPerspective(fov,asp,dim/4,4*dim);
}
else {
/* orthogonal projection*/
glOrtho(-dim*asp,+dim*asp, -dim,+dim, -dim,+dim);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/*
* display()
* ------
* Display the scene
*/
void display()
{
/* setup functions */
displayInit();
displayEye();
/* draw */
drawAxes();
drawValues();
/* magic here */
drawScene();
glFlush();
glutSwapBuffers();
errCheck("display sanity check");
}
void displayInit(void);
void displayEye(void);
void displayReshape(int width,int height);
void displayProject(void);
void display(void);
#include "screencasts.h"
/*
* drawAxes()
* ------
* Draw the axes
*/
void drawAxes()
{
if (toggleAxes) {
/* Length of axes */
double len = 2.0;
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(len,0,0);
glVertex3d(0,0,0);
glVertex3d(0,len,0);
glVertex3d(0,0,0);
glVertex3d(0,0,len);
glEnd();
/* Label axes */
glRasterPos3d(len,0,0);
print("X");
glRasterPos3d(0,len,0);
print("Y");
glRasterPos3d(0,0,len);
print("Z");
}
}
/*
* drawValues()
* ------
* Draw the values in the lower left corner
*/
void drawValues()
{
if (toggleValues) {
glColor3f(0.8,0.8,0.8);
printAt(5,5,"View Angle (th, ph) =(%d, %d)", th,ph);
printAt(5,25,"Projection mode =(%s)", toggleMode?"Perspective":"Orthogonal");
}
}
void drawScene()
{
/*
cube(1,0,1, 1,1,1, 0);
cube(-1,0,1, 1,1,1, 0);
cone(0,1,0, 1,1,DEF_D);
cone(0,-1,0, 1,1,90);
*/
/* a 'tower' */
/*
cube(0,1.5,0, 1,3,1, 0);
cube(0,3.5,0, 2,1,2, 45);
spike(0,1,-3.5, 0.5,1, 90, 90,0,0);
spike(0,1,3.5, 0.5,1, 90, -90,0,0);
spike(-3.5,1,0, 0.5,1, 90, 0,0,-90);
spike(3.5,1,0, 0.5,1, 90, 0,0,90);
*/
tower(0,0,0, 1,1,1, 0);
tower(4,0,0, 1,1,1.5, 30);
}
void drawAxes(void);
void drawValues(void);
void drawScene(void);
#include "screencasts.h"
void errCheck(char* where)
{
int err = glGetError();
if (err) fprintf(stderr,"ERROR: %s [%s]\n",gluErrorString(err),where);
}
void errCheck(char* where);
#include "screencasts.h"
char *windowName = "OpenGL screenscasts 11: Code Organization";
int windowWidth = 500;
int windowHeight = 450;
int toggleAxes = 1;
int toggleValues = 1;
int toggleMode = 0;
double dim = 5.0;
int th = 340;
int ph = 30;
int fov = 55;
int asp = 1;
# Target to build
#TARGET = 011
TARGET = 011
#EXECS = ./executables/011
EXECS = ./executables/011
# Libraries - LINUX
#LIBS=-lglut -lGLU
# Libraries - OSX
LIBS=-framework OpenGL -framework GLUT
all: $(TARGET)
# Generic compile rules
.c.o:
gcc -c -O -Wall $<
# Generic compile and link
%: %.c screencasts.a
gcc -Wall -O3 -o ./executables/$@ $^ $(LIBS)
clean:
rm -f $(EXECS) *.o *.a
# without .h => globals.o
screencasts.a:globals.o print.o error.o shapes.o models.o interaction.o initialization.o draw.o display.o
ar -rcs screencasts.a $^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment