Swiftless Game Programming Site
Home Tutorials Contact
Home
News
OpenGL
GLSL
OpenCL
Maths
Misc
Work in Progress
Resume
 
 

How to use vertex alphas in OpenGL

If you would like to see this site updated, please help out and

This OpenGL tutorial will show you how to add different alpha values to specific vertices in your shape. It uses the same glColor4f commands but the code is now written just before each vertex.

Vertex alphas are basically the same as vertex colors. They just set the
alpha of selected vertices instead of just setting the whole object to a
selected alpha.

So anyway, I did this with a normal quad
but it does work with any shape you choose
Here it is:
I have started my quad.
I then set the color red for the first vertex, with an alpha of only 0.2.
I then draw that vertex.
The next is green, with an alpha value of 1.
Then blue with an alpha value of 0.2.
And finally white with an alpha value of 1.

glBegin(GL_QUADS);
glColor4f(1,0,0,0.2);
glVertex3f(-0.5, -0.5, 0.0);
glColor4f(0,1,0,1);
glVertex3f(-0.5, 0.5, 0.0);
glColor4f(0,0,1,0.2);
glVertex3f(0.5, 0.5, 0.0);
glColor4f(1,1,1,1);
glVertex3f(0.5, -0.5, 0.0);
glEnd();

Just remember to call each vertex after you call the color
for this to work.

Just email swiftless@gmail.com if you have any questions :-)

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
  //Vertex alphas are basically the same
//as vertex colors. They just set the
//alpha of selected vertices instead of
//just setting the whole object to a
//selected alpha.

//So anyway, I did this with a normal quad
//but it does work with any shape you choose
//Here it is:
//I have started my quad.
//I then set the color red for the first vertex, with an alpha of only 0.2
//I then draw that vertex.
//The next is green, with an alpha value of 1.
//Then blue with an alpha value of 0.2.
//And finally white with an alpha value of 1.

//glBegin(GL_QUADS);
//glColor4f(1,0,0,0.2);
//glVertex3f(-0.5, -0.5, 0.0);
//glColor4f(0,1,0,1);
//glVertex3f(-0.5, 0.5, 0.0);
//glColor4f(0,0,1,0.2);
//glVertex3f(0.5, 0.5, 0.0);
//glColor4f(1,1,1,1);
//glVertex3f(0.5, -0.5, 0.0);
//glEnd();

//Just remember to call each vertex after you call the color
//for this to work.

#include <GL/gl.h>
#include <GL/glut.h>

void square (void) {
    glBegin(GL_QUADS);
    glColor4f(1,0,0,0.2); //red, alpha 0.2
    glVertex3f(-0.5, -0.5, 0.0);
    glColor4f(0,1,0,1); //green, alpha 1
    glVertex3f(-0.5, 0.5, 0.0);
    glColor4f(0,0,1,0.2); //blue, alpha 0.2
    glVertex3f(0.5, 0.5, 0.0);
    glColor4f(1,1,1,1); //white, alpha 1
    glVertex3f(0.5, -0.5, 0.0);
    glEnd();
}

void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glTranslatef(0,0,-1);
    square();
    glFlush();
}

void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
    glMatrixMode (GL_MODELVIEW);
}

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window");
    glutDisplayFunc (display);
    glutReshapeFunc (reshape);
    glutMainLoop ();
    return 0;
}


Download C++ Source Code for this Tutorial

Download Visual Basic Source Code for this Tutorial


Comments:

Name: Baby Stuey
Date: 2010-03-07 13:56:36
Comment:
A fun extension....WHEEEEE!

#include <GL/gl.h>
#include <GL/glut.h>

float alphaShift = 0.0; // alpha value [0..1]
float shiftAmount = +0.0001; // alter this to get a nice speed for your hardware

void square (void)
{ glBegin(GL_QUADS);
glColor4f(1.0, 0.0, 0.0, 1.0 - alphaShift); // red
glVertex3f(-0.5, -0.5, 0.0);
glColor4f(1.0, 1.0, 0.0, 1.0 - alphaShift); // yellow
glVertex3f(-0.5, 0.5, 0.0);
glColor4f(0.2, 1.0, 0.0, alphaShift); // "green"
glVertex3f(0.5, 0.5, 0.0);
glColor4f(0.2, 0.0, 1.0, alphaShift); // "blue"
glVertex3f(0.5, -0.5, 0.0);
glEnd();
// Y---G
// | |
// R---B

alphaShift += shiftAmount;
// make sure alpha stays between 0 and 1
if(alphaShift > 1.0)
{ alphaShift = 1.0;
shiftAmount = -shiftAmount;
} else if(alphaShift < 0.0)
{ alphaShift = 0.0;
shiftAmount = -shiftAmount;
}
}

void display (void)
{ glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glTranslatef(0,0,-1);
square();
glFlush();
}

void reshape(int w, int h)
{ glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char **argv)
{ glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("A basic OpenGL Window");
glutDisplayFunc(display);
glutIdleFunc(display); // keep updating the alpha values
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Name   Email


Reload Image

 
     

 

Copyright 2009, Donald Urquhart AKA Swiftless
Check out: http://www.cdadc.com