Skip to content

Commit

Permalink
added Screencast 20 on anti aliasing
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwparker committed Dec 21, 2012
1 parent e719cac commit eb043a2
Show file tree
Hide file tree
Showing 14 changed files with 388 additions and 6 deletions.
113 changes: 113 additions & 0 deletions 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;
}

25 changes: 25 additions & 0 deletions 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 $^
27 changes: 27 additions & 0 deletions 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
57 changes: 57 additions & 0 deletions 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();
}
4 changes: 4 additions & 0 deletions 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);
12 changes: 12 additions & 0 deletions 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);
}
1 change: 1 addition & 0 deletions 020/error.h
@@ -0,0 +1 @@
void errCheck(char* where);
15 changes: 15 additions & 0 deletions 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);
}
1 change: 1 addition & 0 deletions 020/fatal.h
@@ -0,0 +1 @@
void fatal(const char* format , ...);
18 changes: 18 additions & 0 deletions 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;
44 changes: 44 additions & 0 deletions 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);
}
3 changes: 3 additions & 0 deletions 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, ...);
51 changes: 51 additions & 0 deletions 020/screencasts.h
@@ -0,0 +1,51 @@
#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 "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

0 comments on commit eb043a2

Please sign in to comment.