Help end child hunger

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
 

  14 Responses to “Setup for GLSL – Example”

  1. Hi,
    What does the toonify function do ? I commented the toon2 codes and ran the code but could not find any difference.

    Srikanth

  2. Great Tutorial! It’s the best!! 🙂

    In the example shaders, I don’t see the headers: “#version 110” or “#version 120”. Why? That are not absolutely needed? (sorry my english) 🙂

    • Not in those examples, the headers are only required if you want to assure that the GLSL compiler will use at least that version, or give an error if unable to do it. By default, if a version is not specified, the version will by 1.1. Hence, in modern shaders the version is required as you point out.

  3. Hey! I tried the GLUT example in OpenGL 2.0 syntax. What should i see if i run it? A complete white box?

  4. i want to learn GLSL, where is the enter point that i should to know, i am a newer for GLSL, but i know some on OpenGL!

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

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

  7. 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

  8. 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.

    • 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

  9. 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:\Users\charisma\Documents\OpenGL\GLSL Tutorial\glutglsl_2.0\ogl.obj glutglsl

    • 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

  10. 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: