Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added screencast 17

  • Loading branch information...
commit 85ffcb375332d3956a6a6349b39701fe1c0ae1ac 1 parent f61ccc8
@davidwparker authored
Showing with 1,226 additions and 0 deletions.
  1. BIN  017/017
  2. +35 −0 017/017.c
  3. +25 −0 017/Makefile
  4. +43 −0 017/animate.c
  5. +3 −0  017/animate.h
  6. BIN  017/animate.o
  7. +52 −0 017/common.h
  8. +85 −0 017/display.c
  9. +6 −0 017/display.h
  10. BIN  017/display.o
  11. +125 −0 017/draw.c
  12. +9 −0 017/draw.h
  13. BIN  017/draw.o
  14. +12 −0 017/error.c
  15. +1 −0  017/error.h
  16. BIN  017/error.o
  17. +15 −0 017/fatal.c
  18. +1 −0  017/fatal.h
  19. BIN  017/fatal.o
  20. +47 −0 017/globals.c
  21. BIN  017/globals.o
  22. +81 −0 017/initialization.c
  23. +3 −0  017/initialization.h
  24. BIN  017/initialization.o
  25. +97 −0 017/interaction.c
  26. +3 −0  017/interaction.h
  27. BIN  017/interaction.o
  28. +53 −0 017/models.c
  29. +6 −0 017/models.h
  30. BIN  017/models.o
  31. +44 −0 017/print.c
  32. +3 −0  017/print.h
  33. BIN  017/print.o
  34. BIN  017/screencasts.a
  35. +86 −0 017/screencasts.h
  36. +267 −0 017/shapes.c
  37. +10 −0 017/shapes.h
  38. BIN  017/shapes.o
  39. +15 −0 017/structs.h
  40. +98 −0 017/textures.c
  41. +1 −0  017/textures.h
  42. BIN  017/textures.o
  43. BIN  017/txBrick14.bmp
  44. BIN  017/txCrate.bmp
  45. BIN  017/txIce7.bmp
  46. BIN  017/txLava1.bmp
  47. BIN  017/txRock5.bmp
  48. BIN  017/txVenus.bmp
  49. BIN  017/txWood3.bmp
View
BIN  017/017
Binary file not shown
View
35 017/017.c
@@ -0,0 +1,35 @@
+#include "screencasts.h"
+
+/*
+ * main()
+ * ----
+ * Start up GLUT and tell it what to do
+ */
+int main(int argc,char* argv[])
+{
+ initializeGlobals();
+ /* screencast specific variables */
+ windowName = "OpenGL screenscasts 17: Animation part 2";
+ screencastID = 17;
+ toggleAxes = 0;
+ toggleAnimation = 0;
+
+ glutInit(&argc,argv);
+ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
+ glutInitWindowSize(windowWidth,windowHeight);
+ glutInitWindowPosition(450,350);
+ glutCreateWindow(windowName);
+
+ glutDisplayFunc(display);
+ glutReshapeFunc(displayReshape);
+ glutKeyboardFunc(windowKey);
+ glutSpecialFunc(windowSpecial);
+
+ initializeTextures();
+ initializeObjs();
+ timer(toggleAnimation);
+
+ redisplayAll();
+ glutMainLoop();
+ return 0;
+}
View
25 017/Makefile
@@ -0,0 +1,25 @@
+# Target to build
+TARGET = 017
+EXECS = ./$(TARGET)
+
+# Libraries - LINUX
+#LIBS=-lglut -lGLU
+# Libraries - OSX
+LIBS=-framework OpenGL -framework GLUT
+
+all: $(TARGET)
+
+# Generic compile rules
+.c.o:
+ gcc -c -g -O -Wall $<
+
+# Generic compile and link
+%: %.c screencasts.a
+ gcc -Wall -O3 -o ./$@ $^ $(LIBS)
+
+clean:
+ rm -f $(EXECS) *.o *.a
+
+# without .h => globals.o
+screencasts.a:globals.o print.o error.o fatal.o textures.o shapes.o models.o interaction.o initialization.o draw.o display.o animate.o
+ ar -rcs screencasts.a $^
View
43 017/animate.c
@@ -0,0 +1,43 @@
+#include "screencasts.h"
+
+/*
+ * moveCube()
+ * ------
+ * Rotates a cube if animations are on
+ */
+void moveCube(void)
+{
+ cubes[0].tsr.r.y = fmod(cubes[0].tsr.r.y+4.0,360.0);
+}
+
+/*
+ * moveLight()
+ * ------
+ * Moves the light if it is on
+ */
+void moveLight(void)
+{
+ if (toggleLight) lightPh = (lightPh+2)%360;
+}
+
+/*
+ * timer()
+ * ------
+ * Timer called periodically
+ */
+void timer(int value)
+{
+ if (toggleAnimation != DEF_ANIMATE_PAUSED) {
+ moveLight();
+ moveCube();
+ }
+ /*
+ * glutTimerFunc(millisecs, timerCallback, value)
+ * millisecs = how long until you want the callback to be called
+ * timerCallback = the function to be called when time is up
+ * value = can be used for whatever. I'm using it toggle the animation
+ */
+ glutTimerFunc(20,timer,toggleAnimation);
+
+ redisplayAll();
+}
View
3  017/animate.h
@@ -0,0 +1,3 @@
+void moveCube(void);
+void moveLight(void);
+void timer(int);
View
BIN  017/animate.o
Binary file not shown
View
52 017/common.h
@@ -0,0 +1,52 @@
+/* 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))
+
+/* Common #defines */
+/* Defaults for window sizing */
+#define DEF_WINDOW_HEIGHT 450
+#define DEF_WINDOW_WIDTH 500
+
+/* Projection */
+#define DEF_ASP 1
+#define DEF_DIM 5
+#define DEF_TH 340
+#define DEF_PH 30
+#define DEF_FOV 55
+#define DEF_ECX 2
+#define DEF_ECY 0
+#define DEF_ECZ 4
+
+/* Draw defaults */
+#define DEF_AXES 1
+#define DEF_PARAMS 1
+
+/* Shape degrees */
+#define DEF_D 5
+
+/* Lighting */
+#define DEF_LIGHT 1
+#define DEF_DISTANCE 5
+#define DEF_AMBIENT 35
+#define DEF_DIFFUSE 100
+#define DEF_EMISSION 0
+#define DEF_SPECULAR 0
+#define DEF_SHININESS 0
+#define DEF_L_Y 0
+#define DEF_L_PH 90
+
+/* Textures */
+#define TEX_DEFAULT 0
+#define TEX_BRICK 1
+#define TEX_CRATE 2
+#define TEX_ICE 3
+#define TEX_FIRE 4
+#define TEX_EARTH 5
+#define TEX_WOOD 6
+#define TEX_VENUS 7
+
+/* Animation */
+#define DEF_ANIMATE 1
+#define DEF_ANIMATE_PAUSED 0
View
85 017/display.c
@@ -0,0 +1,85 @@
+#include "screencasts.h"
+
+/*
+ * displayInit()
+ * -------
+ * Initializes display
+ */
+void displayInit(void)
+{
+ 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(void)
+{
+ 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+ecX,Ey,Ez+ecZ , ecX,ecY,ecZ , 0,Cos(ph),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(fov,asp,dim);
+}
+
+/*
+ * displayProject()
+ * ------
+ * Sets the projection
+ */
+void displayProject(double fov, double asp, double dim)
+{
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ gluPerspective(fov,asp,dim/16,16*dim);
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+}
+
+/*
+ * display()
+ * ------
+ * Display the scene
+ */
+void display(void)
+{
+ /* setup functions */
+ displayInit();
+ displayEye();
+
+ /* Draw Scene */
+ drawScene();
+
+ glFlush();
+ glutSwapBuffers();
+
+ errCheck("display sanity check");
+}
+
+/*
+ * redisplayAll()
+ * ------
+ * This is called whenever we need to draw the display
+ */
+void redisplayAll(void)
+{
+ displayReshape(windowWidth, windowHeight);
+ glutPostRedisplay();
+}
View
6 017/display.h
@@ -0,0 +1,6 @@
+void displayInit(void);
+void displayEye(void);
+void displayReshape(int width,int height);
+void displayProject(double fov,double asp,double dim);
+void display(void);
+void redisplayAll(void);
View
BIN  017/display.o
Binary file not shown
View
125 017/draw.c
@@ -0,0 +1,125 @@
+#include "screencasts.h"
+
+/*
+ * drawAxes()
+ * ------
+ * Draw the axes
+ */
+void drawAxes(void)
+{
+ if (toggleAxes) {
+ /* Length of axes */
+ double len = 2.0;
+ glDisable(GL_LIGHTING);
+ glColor3fv(white);
+ 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");
+ if (toggleLight)
+ glEnable(GL_LIGHTING);
+ }
+}
+
+/*
+ * drawCube()
+ * ------
+ * Draws a cube
+ */
+void drawCube(cube_s cb)
+{
+ cube(cb.tsr.t.x,cb.tsr.t.y,cb.tsr.t.z,
+ cb.tsr.s.x,cb.tsr.s.y,cb.tsr.s.z,
+ cb.tsr.r.y);
+}
+
+/*
+ * drawLight
+ * ------
+ * Draws the light
+ */
+void drawLight(void)
+{
+ /* Light switch */
+ if (toggleLight) {
+ /* Translate intensity to color vectors */
+ GLfloat Ambient[] = {0.01*ambient ,0.01*ambient ,0.01*ambient ,1.0};
+ GLfloat Diffuse[] = {0.01*diffuse ,0.01*diffuse ,0.01*diffuse ,1.0};
+ GLfloat Specular[] = {0.01*specular,0.01*specular,0.01*specular,1.0};
+ GLfloat Position[] = {distance*Sin(lightPh),lightY,distance*Cos(lightPh),1.0};
+
+ /* Draw light position as sphere (still no lighting here) */
+ glColor3fv(white);
+ glDisable(GL_LIGHTING);
+ sphere(Position[0],Position[1],Position[2], 0.1,0);
+
+ /* Set ambient, diffuse, specular components and position of light 0 */
+ /*
+ Light uses the Phong model
+ Once light is enabled, colors assigned by glColor* isn't used
+ Ambient is light that's been scattered by environment that its direction is impossible to determine
+ Diffuse is is light that comes from one direction, so it's brighter if it comes squarely on surface rather than glances off
+ Specular is light that comes from a particular direction and bounces off in preferred direction
+ Position is the position of our light. In this case it is the same as the sphere.
+ */
+ glLightfv(GL_LIGHT0,GL_AMBIENT, Ambient);
+ glLightfv(GL_LIGHT0,GL_DIFFUSE, Diffuse);
+ glLightfv(GL_LIGHT0,GL_POSITION,Position);
+ glLightfv(GL_LIGHT0,GL_SPECULAR,Specular);
+
+ glEnable(GL_COLOR_MATERIAL);
+ glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
+ glEnable(GL_NORMALIZE);
+ glEnable(GL_LIGHTING);
+ glEnable(GL_LIGHT0);
+ }
+ else {
+ glDisable(GL_LIGHTING);
+ }
+}
+
+/*
+ * drawParameters()
+ * ------
+ * Draw the parameters in the lower left corner
+ */
+void drawParameters(void)
+{
+ if (toggleParams) {
+ glColor3fv(white);
+ /* Display parameters */
+ printAt(5,5,"Angle=%d,%d Dim=%.1f FOV=%d Light=%s",
+ th,ph,dim,fov,toggleLight?"On":"Off");
+ if (toggleLight) {
+ printAt(5,45,"Distance=%d Elevation=%.1f Ambient=%d",distance,lightY,ambient);
+ printAt(5,25,"Diffuse=%d Specular=%d Emission=%d Shininess=%.0f",
+ diffuse,specular,emission,shinyvec[0]);
+ }
+ }
+}
+
+/*
+ * drawScene()
+ * ------
+ * Draws the scene with everything we need in it
+ */
+void drawScene(void)
+{
+ drawAxes();
+ drawParameters();
+ drawLight();
+ currentTexture = textures[TEX_CRATE];
+ drawCube(cubes[0]);
+ currentTexture = textures[TEX_DEFAULT];
+}
View
9 017/draw.h
@@ -0,0 +1,9 @@
+void drawAxes(void);
+void drawLight(void);
+void drawParameters(void);
+void drawScene(void);
+void drawScreencast11(void);
+void drawScreencast12(void);
+void drawScreencast13(void);
+void drawScreencast14(void);
+void drawScreencast15(void);
View
BIN  017/draw.o
Binary file not shown
View
12 017/error.c
@@ -0,0 +1,12 @@
+#include "screencasts.h"
+
+/*
+ * errCheck()
+ * ------
+ * Checks to see if there is an error and displays it if there was
+ */
+void errCheck(char* where)
+{
+ int err = glGetError();
+ if (err) fprintf(stderr,"ERROR: %s [%s]\n",gluErrorString(err),where);
+}
View
1  017/error.h
@@ -0,0 +1 @@
+void errCheck(char* where);
View
BIN  017/error.o
Binary file not shown
View
15 017/fatal.c
@@ -0,0 +1,15 @@
+#include "screencasts.h"
+
+/*
+ * fatal()
+ * ------
+ * Throws a fatal error and exits the program
+ */
+void fatal(const char* format , ...)
+{
+ va_list args;
+ va_start(args,format);
+ vfprintf(stderr,format,args);
+ va_end(args);
+ exit(1);
+}
View
1  017/fatal.h
@@ -0,0 +1 @@
+void fatal(const char* format , ...);
View
BIN  017/fatal.o
Binary file not shown
View
47 017/globals.c
@@ -0,0 +1,47 @@
+#include "screencasts.h"
+/* Global descriptions on screencasts.h */
+
+/* ID-used to keep screencasts separate */
+int screencastID = 0;
+
+/* WINDOW */
+char *windowName="OpenGL screenscasts XX: Placeholder";
+int windowHeight=DEF_WINDOW_HEIGHT;
+int windowWidth=DEF_WINDOW_WIDTH;
+
+/* TOGGLE DRAW DISPLAYS */
+int toggleAxes=DEF_AXES;
+int toggleParams=DEF_PARAMS;
+
+/* PROJECTION */
+double asp=DEF_ASP;
+double dim=DEF_DIM;
+int th=DEF_TH;
+int ph=DEF_PH;
+int fov=DEF_FOV;
+double ecX=DEF_ECX;
+double ecY=DEF_ECY;
+double ecZ=DEF_ECZ;
+
+/* LIGHTING */
+int toggleLight=DEF_LIGHT;
+int distance=DEF_DISTANCE;
+int ambient=DEF_AMBIENT;
+int diffuse=DEF_DIFFUSE;
+int emission=DEF_EMISSION;
+int specular=DEF_SPECULAR;
+int shininess=DEF_SHININESS;
+float shinyvec[1]={1};
+float lightY=DEF_L_Y;
+float white[]={1,1,1,1};
+int lightPh=DEF_L_PH;
+
+/* TEXTURES */
+unsigned int textures[7];
+int currentTexture=TEX_DEFAULT;
+
+/* ANIMATION */
+int toggleAnimation=DEF_ANIMATE;
+
+/* OBJECTS */
+cube_s cubes[1];
View
BIN  017/globals.o
Binary file not shown
View
81 017/initialization.c
@@ -0,0 +1,81 @@
+#include "screencasts.h"
+
+/*
+ * initializeGlobals()
+ * ------
+ * Initializes the global variables.
+ */
+void initializeGlobals(void)
+{
+ /* WINDOW */
+ windowHeight=DEF_WINDOW_HEIGHT;
+ windowWidth=DEF_WINDOW_WIDTH;
+
+ /* TOGGLE */
+ toggleAxes=DEF_AXES;
+ toggleParams=DEF_PARAMS;
+
+ /* PROJECTION */
+ dim=DEF_DIM;
+ th=DEF_TH;
+ ph=DEF_PH;
+ fov=DEF_FOV;
+ asp=DEF_ASP;
+ ecX=DEF_ECX;
+ ecY=DEF_ECY;
+ ecZ=DEF_ECZ;
+
+ /* LIGHTING */
+ toggleLight=DEF_LIGHT;
+ distance=DEF_DISTANCE;
+ ambient=DEF_AMBIENT;
+ diffuse=DEF_DIFFUSE;
+ emission=DEF_EMISSION;
+ specular=DEF_SPECULAR;
+ shininess=DEF_SHININESS;
+ lightY=DEF_L_Y;
+ lightPh=DEF_L_PH;
+
+ /* TEXTURES */
+ currentTexture=TEX_DEFAULT;
+
+ /* ANIMATION */
+ toggleAnimation=DEF_ANIMATE;
+}
+
+/*
+ * initializeObjs(void)
+ * ------
+ * Initializes all of our objects
+ */
+void initializeObjs(void)
+{
+ cube_s cb = {{{0,0,0},{2,2,2},{0,0,0}}};
+ cubes[0] = cb;
+}
+
+/*
+ * initializeTextures()
+ * ------
+ * Loads all of the textures into textures array for use
+ */
+void initializeTextures(void)
+{
+ /*
+ TEX_DEFAULT 0
+ TEX_BRICK 1
+ TEX_CRATE 2
+ TEX_ICE 3
+ TEX_FIRE 4
+ TEX_EARTH 5
+ TEX_WOOD 6
+ TEX_VENUS 7
+ */
+ textures[TEX_BRICK] = loadTexBMP("txBrick14.bmp");
+ textures[TEX_CRATE] = loadTexBMP("txCrate.bmp");
+ textures[TEX_ICE] = loadTexBMP("txIce7.bmp");
+ textures[TEX_FIRE] = loadTexBMP("txLava1.bmp");
+ textures[TEX_EARTH] = loadTexBMP("txRock5.bmp");
+ textures[TEX_WOOD] = loadTexBMP("txWood3.bmp");
+ textures[TEX_VENUS] = loadTexBMP("txVenus.bmp");
+}
View
3  017/initialization.h
@@ -0,0 +1,3 @@
+void initializeGlobals(void);
+void initializeObjs(void);
+void initializeTextures(void);
View
BIN  017/initialization.o
Binary file not shown
View
97 017/interaction.c
@@ -0,0 +1,97 @@
+#include "screencasts.h"
+
+/*
+ * windowKey()
+ * ------
+ * GLUT calls this routine when a non-special key is pressed
+ */
+void windowKey(unsigned char key,int x,int y)
+{
+ /* Exit on ESC */
+ if (key == 27) exit(0);
+ else if (key == 'x' || key == 'X') toggleAxes = 1-toggleAxes;
+ else if (key == 'v' || key == 'V') toggleParams = 1-toggleParams;
+ /* Spacebar */
+ else if (key == 32) toggleAnimation = 1-toggleAnimation;
+ /* Change field of view angle */
+ else if (key == '-' && key>1) fov--;
+ else if (key == '+' && key<179) fov++;
+ /* Change dimensions */
+ else if (key == 'I') dim += 0.2;
+ else if (key == 'i' && dim>1) dim -= 0.2;
+ /* BEGIN LIGHTING FUNCTIONALITY */
+ /* Toggle lighting */
+ else if (key == 'l' || key == 'L') toggleLight = 1-toggleLight;
+ /* Move light (rotation) */
+ else if (key == '<') lightPh -= 5;
+ else if (key == '>') lightPh += 5;
+ /* Light elevation */
+ else if (key == '[') lightY -= 0.5;
+ else if (key == ']') lightY += 0.5;
+ /* Light distance */
+ else if (key == '{') distance -= 1;
+ else if (key == '}') distance += 1;
+ /* Ambient level */
+ else if (key == 'a' && ambient>0) ambient -= 5;
+ else if (key == 'A' && ambient<100) ambient += 5;
+ /* Diffuse level */
+ else if (key == 'd' && diffuse>0) diffuse -= 5;
+ else if (key == 'D' && diffuse<100) diffuse += 5;
+ /* Specular level */
+ else if (key == 's' && specular>0) specular -= 5;
+ else if (key == 'S' && specular<100) specular += 5;
+ /* Emission level */
+ else if (key == 'e' && emission>0) emission -= 5;
+ else if (key == 'E' && emission<100) emission += 5;
+ /* Shininess level */
+ else if (key == 'n' && shininess>-1) shininess -= 1;
+ else if (key == 'N' && shininess<7) shininess += 1;
+
+ /* Translate shininess power to value (-1 => 0) */
+ shinyvec[0] = shininess<0 ? 0 : pow(2.0,shininess);
+
+ redisplayAll();
+}
+
+/*
+ * windowMenu
+ * ------
+ * Window menu is the same as the keyboard clicks
+ */
+void windowMenu(int value)
+{
+ windowKey((unsigned char)value, 0, 0);
+}
+
+/*
+ * windowSpecial()
+ * ------
+ * GLUT calls this routine when an arrow key is pressed
+ */
+void windowSpecial(int key,int x,int y)
+{
+ int modifiers = glutGetModifiers();
+ /* If holding shift, then rotate/elevate */
+ if (modifiers == GLUT_ACTIVE_SHIFT) {
+ /* Right/Left - rotate */
+ if (key == GLUT_KEY_RIGHT) th += 5;
+ else if (key == GLUT_KEY_LEFT) th -= 5;
+ /* Up/Down - elevation */
+ else if (key == GLUT_KEY_UP) ph += 5;
+ else if (key == GLUT_KEY_DOWN) ph -= 5;
+ }
+ /* Otherwise, just shift the screen */
+ else {
+ /* Shift */
+ if (key == GLUT_KEY_RIGHT) ecX += .5;
+ else if (key == GLUT_KEY_LEFT) ecX -= .5;
+ else if (key == GLUT_KEY_DOWN) ecZ += .5;
+ else if (key == GLUT_KEY_UP) ecZ -= .5;
+ }
+
+ /* Keep angles to +/-360 degrees */
+ th %= 360;
+ ph %= 360;
+
+ redisplayAll();
+}
View
3  017/interaction.h
@@ -0,0 +1,3 @@
+void windowKey(unsigned char key,int x,int y);
+void windowMenu(int value);
+void windowSpecial(int key,int x,int y);
View
BIN  017/interaction.o
Binary file not shown
View
53 017/models.c
@@ -0,0 +1,53 @@
+#include "screencasts.h"
+
+/*
+ * spike
+ * ------
+ * Draw a spike
+ * at (x, y, z)
+ * radius r, height h, with 360/deg sides
+ * rotated ox around the x axis
+ * rotated oy around the y axis
+ * rotated oz around the z axis
+ */
+void spike(double x, double y, double z,
+ double r,double h,int deg,
+ double ox,double oy,double oz)
+{
+ glPushMatrix();
+ glRotated(oz,0,0,1);
+ glRotated(oy,0,1,0);
+ glRotated(ox,1,0,0);
+
+ cone(x,y,z, r,h,deg);
+ glPopMatrix();
+}
+
+/*
+ * towers
+ * ------
+ * Draw a tower
+ * at (x,y,z)
+ * dimensions (dx,dy,dz)
+ * rotated th about the y axis
+ */
+void tower(double x,double y,double z,
+ double dx,double dy,double dz,
+ double th)
+{
+ glPushMatrix();
+ /* Transformation */
+ glTranslated(x,y,z);
+ glRotated(th,0,1,0);
+ glScaled(dx,dy,dz);
+
+ 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);
+
+ glPopMatrix();
+}
+
View
6 017/models.h
@@ -0,0 +1,6 @@
+void spike(double x, double y, double z,
+ double r,double h,int deg,
+ double ox,double oy,double oz);
+void tower(double x,double y,double z,
+ double dx,double dy,double dz,
+ double th);
View
BIN  017/models.o
Binary file not shown
View
44 017/print.c
@@ -0,0 +1,44 @@
+#include "screencasts.h"
+
+#define LEN 8192
+
+/*
+ * printv()
+ * ------
+ * Actually prints the characters
+ */
+void printv(va_list args, const char* format)
+{
+ char buf[LEN];
+ char* ch=buf;
+ vsnprintf(buf,LEN,format,args);
+ while (*ch)
+ glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,*ch++);
+}
+
+/*
+ * print()
+ * ------
+ * Prints the words on screen
+ */
+void print(const char* format, ...)
+{
+ va_list args;
+ va_start(args,format);
+ printv(args,format);
+ va_end(args);
+}
+
+/*
+ * printAt()
+ * ------
+ * Prints the words at a given X-Y location on screen
+ */
+void printAt(int x, int y, const char* format, ...)
+{
+ va_list args;
+ glWindowPos2i(x,y);
+ va_start(args,format);
+ printv(args,format);
+ va_end(args);
+}
View
3  017/print.h
@@ -0,0 +1,3 @@
+void printv(va_list args, const char* format);
+void print(const char* format, ...);
+void printAt(int x, int y, const char* format, ...);
View
BIN  017/print.o
Binary file not shown
View
BIN  017/screencasts.a
Binary file not shown
View
86 017/screencasts.h
@@ -0,0 +1,86 @@
+#ifndef SCREENCASTS
+#define SCREENCASTS
+
+/* Standard headers */
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <math.h>
+#include <time.h>
+
+/* OpenGL and friends */
+#ifdef USEGLEW
+#include <GL/glew.h>
+#endif
+#define GL_GLEXT_PROTOTYPES
+#ifdef __APPLE__
+#include <GLUT/glut.h>
+#else
+#include <GL/glut.h>
+#endif
+
+/* Includes */
+#include "animate.h" /* animation functionality*/
+#include "common.h" /* common is just defines */
+#include "display.h" /* display -> setup scene to draw */
+#include "draw.h" /* draw -> draw whatever objects in the scene */
+#include "error.h" /* error convenience */
+#include "fatal.h" /* fatal convenience */
+#include "interaction.h" /* user interactions (keyboard, mouse, etc) */
+#include "initialization.h" /* initialization */
+#include "models.h" /* complex objects */
+#include "print.h" /* printing functions */
+#include "shapes.h" /* basic shapes (cube, cone, etc) */
+#include "textures.h"/* texture functionality */
+
+/* Structs */
+#include "structs.h" /* common structs */
+
+/* GLOBALS (externs required here) */
+/* Don't forget to initialize globals! */
+extern int screencastID;
+
+/* Window info */
+extern char *windowName;
+extern int windowWidth;
+extern int windowHeight;
+
+/* Toggle views */
+extern int toggleAxes; /* toggle axes on and off */
+extern int toggleParams; /* toggle parameters on and off */
+
+/* View */
+extern double asp;/* aspect ratio */
+extern double dim;/* dimension of orthogonal box */
+extern int th; /* azimuth of view angle */
+extern int ph; /* elevation of view angle */
+extern int fov; /* field of view for perspective */
+extern double ecX; /* eye center position x */
+extern double ecY; /* eye center position y */
+extern double ecZ; /* eye center position z */
+
+/* Lighting */
+extern int toggleLight; /* toggle light */
+extern int distance; /* light distance */
+extern int ambient; /* ambient intensity % */
+extern int diffuse; /* diffuse intensity % */
+extern int emission; /* emission intensity % */
+extern int specular; /* specular intensity % */
+extern int shininess; /* shininess (power of two) */
+extern float shinyvec[1]; /* shininess (value) */
+extern float lightY; /* elevation of light */
+extern float white[]; /* the color white */
+extern int lightPh; /* light movement */
+
+/* Textures */
+extern unsigned int textures[7]; /* holds our textures */
+extern int currentTexture; /* current texture */
+
+/* Animation */
+extern int toggleAnimation; /* toggle animation */
+
+/* Objects */
+extern cube_s cubes[1]; /* cube objects */
+
+#endif
View
267 017/shapes.c
@@ -0,0 +1,267 @@
+#include "screencasts.h"
+
+/*
+ * cone
+ * ------
+ * Draws a cone
+ * at (x,y,z)
+ * with radius r and height h
+ * with 360/deg sides
+ */
+void cone(double x,double y,double z,
+ double r,double h,int deg)
+{
+ int k;
+
+ glEnable(GL_TEXTURE_2D);
+ glBindTexture(GL_TEXTURE_2D,currentTexture);
+ glPushMatrix();
+
+ /* Transform */
+ glTranslated(x,y,z);
+ glScaled(r,h,r);
+ glRotated(-90,1,0,0);
+
+ /* sides */
+ glBegin(GL_TRIANGLES);
+ for (k=0;k<=360;k+=deg){
+ if (screencastID < 15) glColor3f(0.0,0.0,1.0);
+ glNormal3f(0,0,1);
+ /* center of cone is always center of texture */
+ glTexCoord2f(0.5,0.5);
+ glVertex3f(0,0,1);
+
+ if (screencastID < 15) glColor3f(0.0,1.0,1.0);
+ glNormal3f(Cos(k),Sin(k),r);
+ glTexCoord2f((double)1/2*Cos(k)+0.5,(double)1/2*Sin(k)+0.5);
+ glVertex3f(Cos(k),Sin(k),0);
+
+ if (screencastID < 15) glColor3f(1.0,0.0,1.0);
+ glNormal3f(Cos(k+deg),Sin(k+deg),r);
+ glTexCoord2f((double)1/2*Cos(k+deg)+0.5,(double)1/2*Sin(k+deg)+0.5);
+ glVertex3f(Cos(k+deg),Sin(k+deg),0);
+ }
+ glEnd();
+
+ /* bottom circle */
+ /* rotate back */
+ glRotated(90,1,0,0);
+ glBegin(GL_TRIANGLES);
+ if (screencastID < 15) glColor3f(1.0,1.0,0.0);
+ glNormal3f(0,-1,0);
+ for (k=0;k<=360;k+=deg) {
+ glTexCoord2f(0.5,0.5);
+ glVertex3f(0,0,0);
+ glTexCoord2f(0.5*Cos(k)+0.5,0.5*Sin(k)+0.5);
+ glVertex3f(Cos(k),0,Sin(k));
+ glTexCoord2f(0.5*Cos(k+deg)+0.5,0.5*Sin(k+deg)+0.5);
+ glVertex3f(Cos(k+deg),0,Sin(k+deg));
+ }
+ glEnd();
+
+ glPopMatrix();
+ glDisable(GL_TEXTURE_2D);
+}
+
+/*
+ * cube
+ * ------
+ * Draw a cube
+ * at (x,y,z)
+ * dimensions (dx,dy,dz)
+ * rotated th about the y axis
+ * TODO: Refactor this!!!
+ */
+void cube(double x,double y,double z,
+ double dx,double dy,double dz,
+ double th)
+{
+ /* Cube vertices */
+ GLfloat vertA[3] = { 0.5, 0.5, 0.5};
+ GLfloat vertB[3] = {-0.5, 0.5, 0.5};
+ GLfloat vertC[3] = {-0.5,-0.5, 0.5};
+ GLfloat vertD[3] = { 0.5,-0.5, 0.5};
+ GLfloat vertE[3] = { 0.5, 0.5,-0.5};
+ GLfloat vertF[3] = {-0.5, 0.5,-0.5};
+ GLfloat vertG[3] = {-0.5,-0.5,-0.5};
+ GLfloat vertH[3] = { 0.5,-0.5,-0.5};
+
+ glPushMatrix();
+ /* Transform */
+ glTranslated(x,y,z);
+ glRotated(th,0,1,0);
+ glScaled(dx,dy,dz);
+
+ glEnable(GL_TEXTURE_2D);
+ /* using the current texture */
+ glBindTexture(GL_TEXTURE_2D,currentTexture);
+
+ /* Cube */
+ /* front => ABCD */
+ glBegin(GL_QUADS);
+ glNormal3f(0,0,1);
+ glTexCoord2f(0,0); glVertex3fv(vertA);
+ glTexCoord2f(1,0); glVertex3fv(vertB);
+ glTexCoord2f(1,1); glVertex3fv(vertC);
+ glTexCoord2f(0,1); glVertex3fv(vertD);
+ glEnd();
+
+ /* back => FEHG */
+ glBegin(GL_QUADS);
+ glNormal3f(0,0,-1);
+ glTexCoord2f(0,0); glVertex3fv(vertF);
+ glTexCoord2f(1,0); glVertex3fv(vertE);
+ glTexCoord2f(1,1); glVertex3fv(vertH);
+ glTexCoord2f(0,1); glVertex3fv(vertG);
+ glEnd();
+
+ /* right => EADH */
+ glBegin(GL_QUADS);
+ glNormal3f(1,0,0);
+ glTexCoord2f(0,0); glVertex3fv(vertE);
+ glTexCoord2f(1,0); glVertex3fv(vertA);
+ glTexCoord2f(1,1); glVertex3fv(vertD);
+ glTexCoord2f(0,1); glVertex3fv(vertH);
+ glEnd();
+
+ /* left => BFGC */
+ glBegin(GL_QUADS);
+ glNormal3f(-1,0,0);
+ glTexCoord2f(0,0); glVertex3fv(vertB);
+ glTexCoord2f(1,0); glVertex3fv(vertF);
+ glTexCoord2f(1,1); glVertex3fv(vertG);
+ glTexCoord2f(0,1); glVertex3fv(vertC);
+ glEnd();
+
+ /* top => EFBA */
+ glBegin(GL_QUADS);
+ glNormal3f(0,1,0);
+ glTexCoord2f(0,0); glVertex3fv(vertE);
+ glTexCoord2f(1,0); glVertex3fv(vertF);
+ glTexCoord2f(1,1); glVertex3fv(vertB);
+ glTexCoord2f(0,1); glVertex3fv(vertA);
+ glEnd();
+
+ /* bottom => DCGH */
+ glBegin(GL_QUADS);
+ glNormal3f(0,-1,0);
+ glTexCoord2f(0,0); glVertex3fv(vertD);
+ glTexCoord2f(1,0); glVertex3fv(vertC);
+ glTexCoord2f(1,1); glVertex3fv(vertG);
+ glTexCoord2f(0,1); glVertex3fv(vertH);
+ glEnd();
+
+ glPopMatrix();
+ glDisable(GL_TEXTURE_2D);
+}
+
+/*
+ * cylinder
+ * ------
+ * Draw a cylinder
+ * at (x, y, z)
+ * with radius r and height h
+ */
+void cylinder(double x,double y,double z,
+ double r,double h)
+{
+ int i,k;
+
+ glEnable(GL_TEXTURE_2D);
+ glBindTexture(GL_TEXTURE_2D,currentTexture);
+ glPushMatrix();
+
+ /* Transformation */
+ glTranslated(x,y,z);
+ glScaled(r,h,r);
+ /* sides */
+ glBegin(GL_QUAD_STRIP);
+ for (k=0;k<=360;k+=DEF_D) {
+ glNormal3f(Cos(k),0,Sin(k));
+ // TODO: fix these textures
+ glTexCoord2f(-Cos(k),Sin(k));
+ glVertex3f(Cos(k),+1,Sin(k));
+
+ glTexCoord2f(Cos(k),Sin(k));
+ glVertex3f(Cos(k),-1,Sin(k));
+ }
+ glEnd();
+
+ /* top and bottom circles */
+ /* reuse the currentTexture on top and bottom) */
+ for (i=1;i>=-1;i-=2) {
+ glBegin(GL_TRIANGLE_FAN);
+ glNormal3f(0,i,0);
+ glTexCoord2f(0.5,0.5);
+ glVertex3f(0,i,0);
+ for (k=0;k<=360;k+=DEF_D) {
+ glTexCoord2f(0.5*Cos(k)+0.5,0.5*Sin(k)+0.5);
+ glVertex3f(i*Cos(k),i,Sin(k));
+ }
+ glEnd();
+ }
+
+ glPopMatrix();
+ glDisable(GL_TEXTURE_2D);
+}
+
+/*
+ * sphere
+ * ------
+ * Draw a sphere
+ * at (x,y,z)
+ * radius (r)
+ * rotated rot around the y axis
+ */
+void sphere(double x,double y,double z,double r,double rot)
+{
+ int th,ph;
+ float yellow[] = {1.0,1.0,0.0,1.0};
+ float emissions[] = {0.0,0.0,0.01*emission,1.0};
+ /*
+ Material shininess = specular exponent
+ Material specular = specular color of material
+ Material emission = simulates original lighting from an object
+ */
+ glMaterialfv(GL_FRONT,GL_SHININESS,shinyvec);
+ glMaterialfv(GL_FRONT,GL_SPECULAR,yellow);
+ glMaterialfv(GL_FRONT,GL_EMISSION,emissions);
+ glEnable(GL_TEXTURE_2D);
+ glBindTexture(GL_TEXTURE_2D,currentTexture);
+ glPushMatrix();
+
+ /* Transform */
+ glTranslated(x,y,z);
+ glScaled(r,r,r);
+ glRotated(rot,0,1,0);
+
+ /* Bands of latitude */
+ for (ph=-90;ph<90;ph+=DEF_D) {
+ glBegin(GL_QUAD_STRIP);
+ for (th=0;th<=360;th+=2*DEF_D) {
+ vertex(th,ph);
+ vertex(th,ph+DEF_D);
+ }
+ glEnd();
+ }
+
+ glPopMatrix();
+ glDisable(GL_TEXTURE_2D);
+}
+
+/*
+ * vertex
+ * ------
+ * Draw vertex in polar coordinates with normal
+ */
+void vertex(double th,double ph)
+{
+ double x = Sin(th)*Cos(ph);
+ double y = Cos(th)*Cos(ph);
+ double z = Sin(ph);
+ /* For a sphere at the origin, the position
+ and normal vectors are the same */
+ glNormal3d(x,y,z);
+ glTexCoord2d(th/360.0,ph/180.0+0.5);
+ glVertex3d(x,y,z);
+}
View
10 017/shapes.h
@@ -0,0 +1,10 @@
+void cone(double x,double y,double z,
+ double r,double h,int deg);
+void cube(double x,double y,double z,
+ double dx,double dy,double dz,
+ double th);
+void cylinder(double x,double y,double z,
+ double r,double h);
+void sphere(double x,double y,double z,
+ double r,double rot);
+void vertex(double th,double ph);
View
BIN  017/shapes.o
Binary file not shown
View
15 017/structs.h
@@ -0,0 +1,15 @@
+typedef struct point {
+ float x;
+ float y;
+ float z;
+} point;
+
+typedef struct tsr {
+ point t; /* translation */
+ point s; /* scale */
+ point r; /* rotation */
+} tsr;
+
+typedef struct cube_s {
+ tsr tsr;
+} cube_s;
View
98 017/textures.c
@@ -0,0 +1,98 @@
+#include "screencasts.h"
+
+/*
+ * reverseBytes()
+ * ------
+ * Reverse n bytes
+ */
+static void reverseBytes(void* x,const int n)
+{
+ int k;
+ char* ch = (char*)x;
+ for (k=0;k<n/2;k++)
+ {
+ char tmp = ch[k];
+ ch[k] = ch[n-1-k];
+ ch[n-1-k] = tmp;
+ }
+}
+
+/*
+ * loadTexBMP()
+ * ------
+ * Load texture from BMP file
+ */
+unsigned int loadTexBMP(char* file)
+{
+ unsigned int texture; /* Texture name */
+ FILE* f; /* File pointer */
+ unsigned short magic; /* Image magic */
+ unsigned int dx,dy,size; /* Image dimensions */
+ unsigned short nbp,bpp; /* Planes and bits per pixel */
+ unsigned char* image; /* Image data */
+ unsigned int k; /* Counter */
+
+ /* Open file */
+ f = fopen(file,"rb");
+ if (!f) fatal("Cannot open file %s\n",file);
+ /* Check image magic */
+ if (fread(&magic,2,1,f)!=1) fatal("Cannot read magic from %s\n",file);
+ if (magic!=0x4D42 && magic!=0x424D) fatal("Image magic not BMP in %s\n",file);
+ /* Seek to and read header */
+ if (fseek(f,16,SEEK_CUR) || fread(&dx ,4,1,f)!=1 || fread(&dy ,4,1,f)!=1 ||
+ fread(&nbp,2,1,f)!=1 || fread(&bpp,2,1,f)!=1 || fread(&k,4,1,f)!=1)
+ fatal("Cannot read header from %s\n",file);
+ /* Reverse bytes on big endian hardware (detected by backwards magic) */
+ if (magic==0x424D) {
+ reverseBytes(&dx,4);
+ reverseBytes(&dy,4);
+ reverseBytes(&nbp,2);
+ reverseBytes(&bpp,2);
+ reverseBytes(&k,4);
+ }
+ /* Check image parameters */
+ if (dx<1 || dx>65536) fatal("%s image width out of range: %d\n",file,dx);
+ if (dy<1 || dy>65536) fatal("%s image height out of range: %d\n",file,dy);
+ if (nbp!=1) fatal("%s bit planes is not 1: %d\n",file,nbp);
+ if (bpp!=24) fatal("%s bits per pixel is not 24: %d\n",file,bpp);
+ if (k!=0) fatal("%s compressed files not supported\n",file);
+#ifndef GL_VERSION_2_0
+ /* OpenGL 2.0 lifts the restriction that texture size must be a power of two */
+ for (k=1;k<dx;k*=2);
+ if (k!=dx) fatal("%s image width not a power of two: %d\n",file,dx);
+ for (k=1;k<dy;k*=2);
+ if (k!=dy) fatal("%s image height not a power of two: %d\n",file,dy);
+#endif
+
+ /* Allocate image memory */
+ size = 3*dx*dy;
+ image = (unsigned char*) malloc(size);
+ if (!image) fatal("Cannot allocate %d bytes of memory for image %s\n",size,file);
+ /* Seek to and read image */
+ if (fseek(f,20,SEEK_CUR) || fread(image,size,1,f)!=1)
+ fatal("Error reading data from image %s\n",file);
+ fclose(f);
+ /* Reverse colors (BGR -> RGB) */
+ for (k=0;k<size;k+=3) {
+ unsigned char temp = image[k];
+ image[k] = image[k+2];
+ image[k+2] = temp;
+ }
+
+ /* Sanity check */
+ errCheck("loadTexBMP");
+ /* Generate 2D texture */
+ glGenTextures(1,&texture);
+ glBindTexture(GL_TEXTURE_2D,texture);
+ /* Copy image */
+ glTexImage2D(GL_TEXTURE_2D,0,3,dx,dy,0,GL_RGB,GL_UNSIGNED_BYTE,image);
+ if (glGetError()) fatal("Error in glTexImage2D %s %dx%d\n",file,dx,dy);
+ /* Scale linearly when image size doesn't match */
+ glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
+
+ /* Free image memory */
+ free(image);
+ /* Return texture name */
+ return texture;
+}
View
1  017/textures.h
@@ -0,0 +1 @@
+unsigned int loadTexBMP(char* file);
View
BIN  017/textures.o
Binary file not shown
View
BIN  017/txBrick14.bmp
Binary file not shown
View
BIN  017/txCrate.bmp
Binary file not shown
View
BIN  017/txIce7.bmp
Binary file not shown
View
BIN  017/txLava1.bmp
Binary file not shown
View
BIN  017/txRock5.bmp
Binary file not shown
View
BIN  017/txVenus.bmp
Binary file not shown
View
BIN  017/txWood3.bmp
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.