Skip to content

Instantly share code, notes, and snippets.

@davidwparker
Created December 16, 2011 20:05
Show Gist options
  • Select an option

  • Save davidwparker/1487718 to your computer and use it in GitHub Desktop.

Select an option

Save davidwparker/1487718 to your computer and use it in GitHub Desktop.
OpenGL screencast 19: blending
#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 19: Blending";
screencastID = 19;
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;
}
/*
* drawScene()
* ------
* Draws the scene with everything we need in it
*/
void drawScene(void)
{
int i,j,k;
drawAxes();
drawParameters();
currentTexture = textures[TEX_DEFAULT];
drawLight();
opaque = 1;
for (i = -1; i <= 1; i += 2)
for (j = -1; j <= 1; j += 2)
for (k = -1; k <= 1; k += 2)
if (((i+j+k)&2)==0) {
cube(i,j,k,1,1,1,0);
}
currentTexture = textures[TEX_STAINGLASS];
opaque = 0;
for (i = -1; i <= 1; i += 2)
for (j = -1; j <= 1; j += 2)
for (k = -1; k <= 1; k += 2)
if (((i+j+k)&2)!=0) {
cube(i,j,k,1,1,1,0);
}
currentTexture = textures[TEX_DEFAULT];
opaque = 1;
}
/*
* 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);
if (!opaque) {
glEnable(GL_BLEND); // remember to enable/disable blending
glColor4f(1,1,1,alpha);
if (aone)
// glBlendFunc(SOURCE, DESTINATION)
// Controls how color values in the fragment being processed (source) are
// combined with those already in the framebuffer (destination)
// See Table 6-1 in the Red book
// Older book version here, but same table: http://glprogramming.com/red/chapter06.html
// Constants available
// GL_ZERO, GL_ONE, GL_DST_COLOR, GL_SRC_COLOR, GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_COLOR
// GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA_SATURATE
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
else
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(GL_FALSE); // make the depth buffer read-only
glEnable(GL_TEXTURE_2D);
/* using the current texture */
glBindTexture(GL_TEXTURE_2D,currentTexture);
}
/* Cube */
/* front => ABCD */
glBegin(GL_QUADS);
if (opaque) glColor3f(1,0,0);
glNormal3f(0,0,1);
glTexCoord2f(0,0); glVertex3fv(vertC);
glTexCoord2f(1,0); glVertex3fv(vertD);
glTexCoord2f(1,1); glVertex3fv(vertA);
glTexCoord2f(0,1); glVertex3fv(vertB);
glEnd();
/* back => FEHG */
glBegin(GL_QUADS);
if (opaque) glColor3f(1,1,0);
glNormal3f(0,0,-1);
glTexCoord2f(0,0); glVertex3fv(vertH);
glTexCoord2f(1,0); glVertex3fv(vertG);
glTexCoord2f(1,1); glVertex3fv(vertF);
glTexCoord2f(0,1); glVertex3fv(vertE);
glEnd();
/* right => EADH */
glBegin(GL_QUADS);
if (opaque) glColor3f(1,0,1);
glNormal3f(1,0,0);
glTexCoord2f(0,0); glVertex3fv(vertD);
glTexCoord2f(1,0); glVertex3fv(vertH);
glTexCoord2f(1,1); glVertex3fv(vertE);
glTexCoord2f(0,1); glVertex3fv(vertA);
glEnd();
/* left => BFGC */
glBegin(GL_QUADS);
if (opaque) glColor3f(0,1,0);
glNormal3f(-1,0,0);
glTexCoord2f(0,0); glVertex3fv(vertG);
glTexCoord2f(1,0); glVertex3fv(vertC);
glTexCoord2f(1,1); glVertex3fv(vertB);
glTexCoord2f(0,1); glVertex3fv(vertF);
glEnd();
/* top => EFBA */
glBegin(GL_QUADS);
if (opaque) glColor3f(0,0,1);
glNormal3f(0,1,0);
glTexCoord2f(0,0); glVertex3fv(vertB);
glTexCoord2f(1,0); glVertex3fv(vertA);
glTexCoord2f(1,1); glVertex3fv(vertE);
glTexCoord2f(0,1); glVertex3fv(vertF);
glEnd();
/* bottom => DCGH */
glBegin(GL_QUADS);
if (opaque) glColor3f(0,1,1);
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();
if (!opaque) {
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
glDisable(GL_TEXTURE_2D);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment