Skip to content

Instantly share code, notes, and snippets.

@davidwparker
Created December 21, 2012 18:39
Show Gist options
  • Save davidwparker/4354835 to your computer and use it in GitHub Desktop.
Save davidwparker/4354835 to your computer and use it in GitHub Desktop.
OpenGL Screencast 20: Anti-Aliasing
#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;
}
#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();
}
#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;
# 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 $^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment