Created
August 1, 2012 05:34
-
-
Save slembcke/3224017 to your computer and use it in GitHub Desktop.
Infinite shadow projection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#include <GLUT/glut.h> | |
int window_width = 500; | |
int window_height = 500; | |
int mousex = 0; | |
int mousey = 0; | |
void reshape(int width, int height) { | |
glViewport (0, 0, (GLsizei) width, (GLsizei) height); | |
glMatrixMode (GL_PROJECTION); | |
glLoadIdentity(); | |
gluOrtho2D(0, width, 0, height); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
window_width = width; | |
window_height = height; | |
} | |
void shadowVert(GLfloat x, GLfloat y){ | |
glVertex3f(x, y, 1.0f); | |
glVertex3f(x, y, 0.0f); | |
} | |
void drawLight(GLfloat x, GLfloat y, GLfloat d, GLfloat r, GLfloat g, GLfloat b){ | |
glDisable(GL_BLEND); | |
glColor4f(0.0f, 0.0f, 0.0f, 0.0f); | |
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE); | |
// Render shadow mask | |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
{ // set up the shadow matrix | |
GLfloat mat[16] = { | |
1.0f, 0.0f, 0.0f, 0.0f, | |
0.0f, 1.0f, 0.0f, 0.0f, | |
x, y, 0.0f, 1.0f, | |
-x, -y, 0.0f, 0.0f, | |
}; | |
glLoadMatrixf(mat); | |
} | |
// draw some shadow segments | |
glBegin(GL_QUAD_STRIP); { | |
shadowVert(100, 200); | |
shadowVert(200, 200); | |
shadowVert(200, 100); | |
} glEnd(); | |
glBegin(GL_QUAD_STRIP); { | |
shadowVert( 50, 150); | |
shadowVert(150, 50); | |
} glEnd(); | |
glBegin(GL_QUAD_STRIP); { | |
shadowVert(300, 400); | |
shadowVert(400, 300); | |
} glEnd(); | |
// render light map | |
glEnable(GL_BLEND); | |
glBlendFunc(GL_DST_ALPHA, GL_ONE); | |
glColor4f(1.0f, 1.0f, 1.0f, 1.0f); | |
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); | |
glLoadIdentity(); | |
glTranslatef(x, y, 0.0f); | |
glBegin(GL_TRIANGLE_FAN); { | |
glColor4f(1.0f, 1.0f, 1.0f, 1.0f); | |
glVertex2f(0.0f, 0.0f); | |
glColor4f(0.0f, 0.0f, 0.0f, 1.0f); | |
for(int i=0; i<=30; i++){ | |
GLfloat angle = 2.0f*M_PI*(GLfloat)i/30.0f; | |
glVertex2f(cosf(angle)*d, sinf(angle)*d); | |
} | |
} glEnd(); | |
} | |
void draw() { | |
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glMatrixMode(GL_MODELVIEW); | |
drawLight(mousex, mousey, 300.0f, 1.0f, 1.0f, 1.0f); | |
drawLight(250.0f, 250.0f, 200.0f, 0.8f, 0.8f, 0.8f); | |
glutSwapBuffers(); | |
int err; | |
while(err = glGetError()) | |
printf("%s\n", gluErrorString(err)); | |
} | |
void mouse(int x, int y){ | |
mousex = x; | |
mousey = window_height - y; | |
glutPostRedisplay(); | |
} | |
void initGL(int width, int height) { | |
reshape(width, height); | |
} | |
int main(int argc, char** argv) { | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); | |
glutInitWindowSize(window_width, window_height); | |
glutCreateWindow("GLUT"); | |
glutReshapeFunc(reshape); | |
glutDisplayFunc(draw); | |
glutPassiveMotionFunc(mouse); | |
initGL(window_width, window_height); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment