diff --git a/020/020.c b/020/020.c new file mode 100644 index 0000000..1d3bd05 --- /dev/null +++ b/020/020.c @@ -0,0 +1,113 @@ +#include "screencasts.h" + +/* + * initializeGlobals() + * ------ + * Initializes the global variables. + */ +void initializeGlobals(void) +{ + /* WINDOW */ + windowHeight=DEF_WINDOW_HEIGHT; + windowWidth=DEF_WINDOW_WIDTH; + + /* 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; +} + +void windowKey(unsigned char key,int x,int y) +{ + /* Exit on ESC */ + if (key == 27) exit(0); + + /* Change the X angle */ + else if (key=='r') th = (th-5)%360; + else if (key=='R') th = (th+5)%360; + + redisplayAll(); +} + +void drawScene(void) +{ + int i; + const float di = 0.95; + // Draw background quad + glBegin(GL_QUADS); + glColor3f(1,1,1); + glVertex2f(-0.8*di,-0.4*di); + glVertex2f(-0.8*di,+0.4*di); + glVertex2f(+0.8*di,+0.4*di); + glVertex2f(+0.8*di,-0.4*di); + glEnd(); + + // Two X's + // Left aliased + // Right anti-aliased + for (i=-1;i<=1;i+=2) { + // Aliased line setup + if (i<0) { + glDisable (GL_LINE_SMOOTH); + glDisable (GL_BLEND); + glLineWidth (1.5); + } + // Anti-aliased line setup + else { + glEnable(GL_LINE_SMOOTH); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + //glHint(TARGET, MODE) + //TARGET = GL_FRAGMENT_SHADER_DERIVATIVE_HINT, GL_LINE_SMOOTH_HINT, + // GL_TEXTURE_COMPRESSION_HINT, GL_POLYGON_SMOOTH_HINT + //MODE = GL_DONT_CARE, GL_NICEST, GL_FASTEST + glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE); + //glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); + //glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST); + glLineWidth(1.5); + } + // Draw X lines + glBegin(GL_LINES); + glColor3f(1,0,0); + glVertex2f(-di*Cos(th)+0.05*i,-di*Sin(th)); + glVertex2f(+di*Cos(th)+0.05*i,+di*Sin(th)); + glColor3f(0,0,1); + glVertex2f(-di*Cos(th)+0.05*i,+di*Sin(th)); + glVertex2f(+di*Cos(th)+0.05*i,-di*Sin(th)); + glEnd(); + } +} + +/* + * main() + * ---- + * Start up GLUT and tell it what to do + */ +int main(int argc,char* argv[]) +{ + initializeGlobals(); + + /* screencast specific variables */ + windowName = "OpenGL screenscasts 20: Anti-Aliasing"; + screencastID = 20; + + glutInit(&argc,argv); + glutInitDisplayMode(GLUT_RGB | GLUT_ALPHA); + glutInitWindowSize(windowWidth,windowHeight); + glutInitWindowPosition(450,350); + glutCreateWindow(windowName); + + glutDisplayFunc(display); + glutReshapeFunc(displayReshape); + glutKeyboardFunc(windowKey); + + redisplayAll(); + glutMainLoop(); + return 0; +} + diff --git a/020/Makefile b/020/Makefile new file mode 100644 index 0000000..84e4bf4 --- /dev/null +++ b/020/Makefile @@ -0,0 +1,25 @@ +# Target to build +TARGET = 020 +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 display.o + ar -rcs screencasts.a $^ diff --git a/020/common.h b/020/common.h new file mode 100644 index 0000000..4435376 --- /dev/null +++ b/020/common.h @@ -0,0 +1,27 @@ +/* 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)) +/* Determine number of elements in an array */ +#define Length(x) (sizeof (x) / sizeof *(x)) + +/* Common #defines */ +/* Defaults for window sizing */ +#define DEF_WINDOW_HEIGHT 600 +#define DEF_WINDOW_WIDTH 700 + +/* Projection */ +#define DEF_ASP 1 +#define DEF_DIM 5 +#define DEF_TH 340 +#define DEF_PH 30 +#define DEF_FOV 25 +#define DEF_ECX 0 +#define DEF_ECY 0 +#define DEF_ECZ 0 + +/* Draw defaults */ +#define DEF_AXES 1 +#define DEF_PARAMS 1 +#define DEF_OVERLAY 0 diff --git a/020/display.c b/020/display.c new file mode 100644 index 0000000..32407ea --- /dev/null +++ b/020/display.c @@ -0,0 +1,57 @@ +#include "screencasts.h" + +/* + * 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(); + // 2D only for this example + gluOrtho2D(-asp,+asp,-1,+1); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + +/* + * display() + * ------ + * Display the scene + */ +void display(void) +{ + /* setup functions */ + glClear(GL_COLOR_BUFFER_BIT); + + /* Draw Scene */ + drawScene(); + + /* Flush and sanity check */ + glFlush(); + errCheck("display sanity check"); +} + +/* + * redisplayAll() + * ------ + * This is called whenever we need to draw the display + */ +void redisplayAll(void) +{ + displayProject(fov,asp,dim); + glutPostRedisplay(); +} diff --git a/020/display.h b/020/display.h new file mode 100644 index 0000000..ac0779e --- /dev/null +++ b/020/display.h @@ -0,0 +1,4 @@ +void displayReshape(int width,int height); +void displayProject(double fov,double asp,double dim); +void display(void); +void redisplayAll(void); diff --git a/020/error.c b/020/error.c new file mode 100644 index 0000000..fc12a95 --- /dev/null +++ b/020/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); +} diff --git a/020/error.h b/020/error.h new file mode 100644 index 0000000..8ce2d08 --- /dev/null +++ b/020/error.h @@ -0,0 +1 @@ +void errCheck(char* where); diff --git a/020/fatal.c b/020/fatal.c new file mode 100644 index 0000000..3f8e974 --- /dev/null +++ b/020/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); +} diff --git a/020/fatal.h b/020/fatal.h new file mode 100644 index 0000000..f071718 --- /dev/null +++ b/020/fatal.h @@ -0,0 +1 @@ +void fatal(const char* format , ...); diff --git a/020/globals.c b/020/globals.c new file mode 100644 index 0000000..7fe2601 --- /dev/null +++ b/020/globals.c @@ -0,0 +1,18 @@ +#include "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; + +/* 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; diff --git a/020/print.c b/020/print.c new file mode 100644 index 0000000..67390a6 --- /dev/null +++ b/020/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); +} diff --git a/020/print.h b/020/print.h new file mode 100644 index 0000000..c79ea62 --- /dev/null +++ b/020/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, ...); diff --git a/020/screencasts.h b/020/screencasts.h new file mode 100644 index 0000000..e1a4af0 --- /dev/null +++ b/020/screencasts.h @@ -0,0 +1,51 @@ +#ifndef SCREENCASTS +#define SCREENCASTS + +/* Standard headers */ +#include +#include +#include +#include +#include +#include + +/* OpenGL and friends */ +#ifdef USEGLEW +#include +#endif +#define GL_GLEXT_PROTOTYPES +#ifdef __APPLE__ +#include +#else +#include +#endif + +/* Includes */ +#include "common.h" /* common is just defines */ +#include "display.h" /* display -> setup scene to draw */ + +/* COMMON */ +#include "error.h" /* error convenience */ +#include "fatal.h" /* fatal convenience */ +#include "print.h" /* printing functions */ + +/* GLOBALS (externs required here) */ +/* Don't forget to initialize globals! */ +extern int screencastID; + +/* Window info */ +extern char *windowName; +extern int windowWidth; +extern int windowHeight; + +/* 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 */ + +#endif diff --git a/common.h b/common.h index bdcfb98..9681c23 100644 --- a/common.h +++ b/common.h @@ -3,6 +3,8 @@ /* Macro for sin & cos in degrees */ #define Cos(th) cos(PI/180*(th)) #define Sin(th) sin(PI/180*(th)) +/* Determine number of elements in an array */ +#define Length(x) (sizeof (x) / sizeof *(x)) /* Common #defines */ /* Defaults for window sizing */ @@ -11,24 +13,25 @@ /* Projection */ #define DEF_ASP 1 -#define DEF_DIM 10 +#define DEF_DIM 5 #define DEF_TH 340 #define DEF_PH 30 -#define DEF_FOV 55 -#define DEF_ECX 2 +#define DEF_FOV 25 +#define DEF_ECX 0 #define DEF_ECY 0 -#define DEF_ECZ 4 +#define DEF_ECZ 0 /* Draw defaults */ #define DEF_AXES 1 -#define DEF_PARMS 1 +#define DEF_PARAMS 1 +#define DEF_OVERLAY 0 /* Shape degrees */ #define DEF_D 5 /* Lighting */ #define DEF_LIGHT 1 -#define DEF_DISTANCE 10 +#define DEF_DISTANCE 5 #define DEF_AMBIENT 35 #define DEF_DIFFUSE 100 #define DEF_EMISSION 0 @@ -36,6 +39,9 @@ #define DEF_SHININESS 0 #define DEF_L_Y 0 #define DEF_L_PH 90 +#define DEF_AONE 1 +#define DEF_OPAQUE 1 +#define DEF_ALPHA 0 /* Textures */ #define TEX_DEFAULT 0 @@ -46,3 +52,8 @@ #define TEX_EARTH 5 #define TEX_WOOD 6 #define TEX_VENUS 7 +#define TEX_STAINGLASS 8 + +/* Animation */ +#define DEF_ANIMATE 1 +#define DEF_ANIMATE_PAUSED 0