Coloring

Back to GLSL Tutorial Index

Discuss this Tutorial in the Forum

Jump To:

C++ Source
Vertex Shader Source
Fragment Shader Source
Download

CPP File:

Guess what?!?! No changes here.. Yay :)

#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <windows.h>
#include <stdio.h>

#pragma comment(lib,"glew32.lib")

char *VertexShaderSource,*FragmentShaderSource;

int VertexShader,FragmentShader;

int ShaderProgram;

GLfloat angle = 0.0;

char *readShaderFile(char *FileName) {
FILE *fp;
char *DATA = NULL;

int flength = 0;

fp = fopen(FileName,"rt");

fseek(fp, 0, SEEK_END);
flength = ftell(fp);
rewind(fp);

DATA = (char *)malloc(sizeof(char) * (flength+1));
flength = fread(DATA, sizeof(char), flength, fp);
DATA[flength] = '\0';

fclose(fp);

return DATA;
}

void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,0,-5);
glRotatef(angle,1,1,1);
//glColor3f(1,0,0);
glutSolidCube(2);
glutSwapBuffers();
angle++;
}

void InitShader (void) {
GLEW_ARB_vertex_shader;
GLEW_ARB_fragment_shader;

VertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
FragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);

VertexShaderSource = readShaderFile("color.vert");
FragmentShaderSource = readShaderFile("color.frag");

const char * VS = VertexShaderSource;
const char * FS = FragmentShaderSource;

glShaderSourceARB(VertexShader, 1, &VS,NULL);
glShaderSourceARB(FragmentShader, 1, &FS,NULL);

free(VertexShaderSource);free(FragmentShaderSource);

glCompileShaderARB(VertexShader);
glCompileShaderARB(FragmentShader);

ShaderProgram = glCreateProgramObjectARB();

glAttachObjectARB(ShaderProgram,VertexShader);
glAttachObjectARB(ShaderProgram,FragmentShader);

glLinkProgramARB(ShaderProgram);
glUseProgramObjectARB(ShaderProgram);
}

void DeInitShader (void) {
glDetachObjectARB(ShaderProgram,VertexShader);
glDetachObjectARB(ShaderProgram,FragmentShader);

glDeleteObjectARB(ShaderProgram);
}

void Init (void) {
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}

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

int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("A basic OpenGL Window");
glewInit();
InitShader();
Init();
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutMainLoop ();
DeInitShader();
return 0;
}

Vertex Shader:

Guess what?!?!?! No changes here either :)

void main(void)
{
// Set the position of the current vertex
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Fragment Shader:

Guess what?!?!?! We have changes here.. Finally :)

Here we are going to change the color of our output
render to a color of our choice.. My choice is going
to be blue. Just because I can :)

In GLSL we have different types of variables we can
use. The most used types (IMO) are the vec types.
They consist of vec1, vec2, vec3, vec4, the vec
standing for vector, even though it is generally
used to hold multiple part floats and integers.

Now the gl_FragColor that we have to set, takes
a vec4 type variable. The correct way to use this is
like so:
DO: vec4(1,2,3,4)
DO NOT: vec4 1,2,3,4 or (vec4)1,2,3,4

Now for colours, you would have entered in 3 numbers
one for Red, one for Green and one for Blue for the
glColor3f calls. This is the same, only it also takes
an alpha value as well just like glColor4f. GLSL also
uses the same order for colours, so the first is Red
the second is Green, the third is Blue and the fourth
is Alpha.

To get the colour blue, we therefore need:
vec4(r,g,b,a) which would come to vec4(0,0,1,1).

This can either be written straight out, or we can
store this in a variable,
eg: vec4 Blue = vec4(0,0,1,1);
and then call gl_FragColor = Blue.

But in this tutorial I am going to call the line:
gl_FragColor = vec4(0,0,1,1);

And that is all there is to it. We now have a blue
cube.

Also keep in mind, that any color set in GLSL will
overwrite the color set by glColor, unless you call
gl_Color in the shader as in the previous tutorial.

void main(void)
{
// Set the output color of our current pixel
gl_FragColor = vec4(0,0,1,1);
}


Download:

Download C++ .CPP Source Code for this Tutorial

Download Vertex Shader Source Code for this Tutorial

Download Fragment Shader Source Code for this Tutorial

     

 

Copyright 2008, Donald Urquhart
Proudly supported by http://www.cdadc.com