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
 

9 comments on “Setup for GLSL – Example

  1. nautilus on said:

    No meh, There are no memory leaks in it because he free all the resources before getting out the routine.

  2. meh on said:

    This code contains memory leaks. *vs and *fs both point to data on the heap never deleted.

  3. Dileep S on said:

    I am a beginner in GLSL. I could not find the code corresponding toon.vert and toon.frag where I can see contents of these files ? Regards, Dileep S

  4. Matt on said:

    Where have you got textFileRead() function from? My Code::Blocks says that “error: ´textFileRead´ was not declared in this scope”. I tried to include fstream, but without success.

    • ARF on said:

      I wrote it :-)

      Don’t know how to help you in this as I0ve never worked with Code::Blocks. Could it be a project settings issue?

      Antonio

  5. Charisma on said:

    Hi I got an error of compiling your program. It says that it cannot link _glAttachShader , _glCreateShader and so on.

    Error 3 error LNK2001: unresolved external symbol __imp____glewAttachShader C:UserscharismaDocumentsOpenGLGLSL Tutorialglutglsl_2.0ogl.obj glutglsl

    • György Straub on said:

      From the amount of information you’ve provided it seems that you’ve got trouble with GLEW. Check that it’s compiled, linked, the linker libraries are where your project expects them to be, and also make sure that libglew is linked against before opengl32, i.e.

      -lglew
      -lopengl32

      Cheers,
      György

  6. Sam on said:

    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.

    • ARF on said:

      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

Your email address will not be published. Required fields are marked *

*

83,875 Spam Comments Blocked so far by Spam Free Wordpress

HTML tags are not allowed.

© 2013 Lighthouse3d.com Suffusion theme by Sayontan Sinha