Help those suffering in the Horn of Africa

Setup for GLSL – Example

Prev: Creating a Program Next: The InfoLog
 

The following source code contains all the steps described previously. The variables p,f,v are declared globally as (OpenGL 2.0 syntax) GLuint or (ARB extension syntax) GLhandleARB.

OpenGL 2.0 syntax:

void setShaders() {

	char *vs,*fs;

	v = glCreateShader(GL_VERTEX_SHADER);
	f = glCreateShader(GL_FRAGMENT_SHADER);	

	vs = textFileRead("toon.vert");
	fs = textFileRead("toon.frag");

	const char * vv = vs;
	const char * ff = fs;

	glShaderSource(v, 1, &vv,NULL);
	glShaderSource(f, 1, &ff,NULL);

	free(vs);free(fs);

	glCompileShader(v);
	glCompileShader(f);

	p = glCreateProgram();

	glAttachShader(p,v);
	glAttachShader(p,f);

	glLinkProgram(p);
	glUseProgram(p);
}

ARB extension syntax:

void setShaders() {

	char *vs,*fs;

	v = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
	f = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);	

	vs = textFileRead("toon.vert");
	fs = textFileRead("toon.frag");

	const char * vv = vs;
	const char * ff = fs;

	glShaderSourceARB(v, 1, &vv,NULL);
	glShaderSourceARB(f, 1, &ff,NULL);

	free(vs);free(fs);

	glCompileShaderARB(v);
	glCompileShaderARB(f);

	p = glCreateProgramObjectARB();

	glAttachObjectARB(p,v);
	glAttachObjectARB(p,f);

	glLinkProgramARB(p);

	glUseProgramObjectARB(p);
}

A complete GLUT example is available:OpenGL 2.0 syntax and ARB extension syntax, containing two simple shaders, and the text file reading functions. A Unix version (ARB extension syntax only) can be obtained here thanks to Wojciech Milkowski. Please let him know if you use it: wmilkowski ‘at’ gazeta.pl

 

Prev: Creating a Program Next: The InfoLog
 

2 Responses to “Setup for GLSL – Example”

  1. Currently, I am running the example code..
    I intentionally commented out
    glEnable(GL_DEPTH_TEST);
    to check what the difference is.
    I found that the rendering difference is pretty big.
    I mean they look quite different.

    Would you explain what the effect of using / not using glEnable(GL_DEPTH_TEST); ?

    Another question is…
    I accidentally set gluPerspective(45, ratio, 0, 1000);
    but nothing was displayed… I spent quite some time in figuring it out,
    and I found that I shouldn’t use 0 as the ‘near’ value.
    Is this a general rule I have to follow ?

    Thanks a lot.

    • Hi Sam,

      Depth testing allows the objects to be properly displayed according to their distance, or depth, to the camera. Without it objects are rendered by the order they appear in the source code, and possibly incorrectly overlapping each other.

      As for gluPerspective, yes, zero is definitely not a recommended value. The near value must be greater than zero.

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2012 Lighthouse3d.com Suffusion theme by Sayontan Sinha