Featured Posts

8. Bump Mapping in GLSL8. Bump Mapping in GLSL Introduction Bump mapping is essential in todays computer games, and computer graphics in general. Would you like to know the best thing about it? It is extremely simple to implement. Bump mapping works...

Read more

Swiftless GLSL Shader DeveloperSwiftless GLSL Shader Developer Swiftless GLSL Shader Developer   Version 0.1a Currently Swiftless GLSL Shader Developer is in it's first public release, and is currently in alpha status, meaning it is not complete and may contain...

Read more

Wordpress Optimization Wordpress Website Optimizations Introduction Wordpress itself is a fairly wonderful tool. Since switching to it, I find it is a lot quicker to make changes to my website and it is also quicker to get...

Read more

36. OpenGL Framebuffers36. OpenGL Framebuffers Introduction Frame buffers are one of those mythical things that we have all heard of, but many beginner OpenGL developers avoid because there is not much information about them, and they can be confusing...

Read more

1. Terrain Class1. Terrain Class Terrain is one of those things that so far, hasn't been perfectly recreated in computer graphics. But it is almost there! Looking over a beautiful landscape can be one of the most amazing feelings in the...

Read more

  • Prev
  • Next

1. GLSL Setup

Posted on : 25-03-2010 | By : Swiftless | In : GLSL

Tags: , , , ,

28

GLSL is the OpenGL Shader Language, and is the OpenGL version of HLSL for DirectX. It allows us to manipulate the scene on a per vertex and per fragment basis, giving us total control.

Jump To:

shader.h Source
shader.cpp Source
main.cpp Source
shader.vert Source
shader.frag Source
Download

To start off the OpenGL Shader Language tutorials, I am going to take our basic
Smooth Rotation OpenGL tutorial and add to it. Now for those of you that didn’t
read about GLSL on the tutorials page, up the top, it will just give you a brief outline on what shaders can do as well as how it compares to DirectX, and I recommend taking a look.

If you don’t know anything about GLSL already, a shader program is made up of either a Vertex Shader and a Fragment Shader, or a Vertex Shader, Geometry Shader and Fragment Shader, which is supported on graphics cards from the Nvidia 8xxx cards onwards.

Here is a quick rundown on the different types of shader programs:

A Vertex Shader allows us to manipulate vertices, and gives us access to vertex normals, our lighting pipeline, object materials, and other functions.

A Fragment Shader allows us to manipulate the outputting pixels that we see on our screen, here we can create our effects such as bloom, lighting, texturing, fog, etc.

A Geometry Shader (which I will introduce in later tutorials), allows us to actually create and remove vertices as we see fit. This is great for dynamic level of detail, allows us to create ‘fuzzy’ polygons, explode shapes, and do a bunch of nifty effects.

 

I’d like to point out now, that this tutorial is aimed for OpenGL 2.0 and up, where GLSL is made a part of OpenGL. All prior versions will need to use ARB extensions.

Shader.H File:

Being our first GLSL tutorial, this might be a little long as there is a bit to setting up your first application. It’s not really complicated, and the final code is quite short, but because there is such a range of things to do, this page is going to be a bit long.

Having used Java quite a bit recently thanks to my University, I am growing a fondness for Object Oriented programming. This tutorial is going to take an Object Oriented style approach at GLSL shaders, and we will end up with a class that we can reuse each time we want to create a shader program.

To start off with our class declaration, open up a new C++ header file called "shader.h". In this we want to start off with a new class, so type something along the lines of:

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.
  #ifndef __SHADER_H
#define __SHADER_H

#if ( (defined(__MACH__)) && (defined(__APPLE__)) )   
#include <stdlib.h>
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#include <OpenGL/glext.h>
#else
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glext.h>
#endif

#include <string>

class Shader {
public:
    Shader();
    ~Shader();

private:

};
#endif

This will be a nice clean skeleton on which we are going to add everything. You may notice the extra long code for our header files which includes an if statement. This code is explained on the new OpenGL Tips page, in short, it means you can compile on Mac as well.

Now, we want to add a few methods to the public: section of our class. We will give whoever uses this class the ability to create a shader using the constructor, or by using an init method. Both of which will take the file names for the vertex and fragment shader.

So we need to add the lines:

Shader(const char *vsFile, const char *fsFile);
void init(const char *vsFile, const char *fsFile);

Ok, so we now know how to create our shaders. But what about using them? Well all we need are three methods, bind and unbind to enable and disable our shader, and id to get the number associated with our shader, so that we can pass variable through to the shader:

void bind();
void unbind();
unsigned int id();

Nice, now all that is left is to do is to create three private variables. One for our shader program, one for our fragment shader and a final one for our vertex shader. I will be calling them:

unsigned int shader_id;
unsigned int shader_vp;
unsigned int shader_fp;

When we put all of this into shader.h, we get the following file, which is quite simple and does all that we need.

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.

  

#ifndef __SHADER_H
#define __SHADER_H

#if ( (defined(__MACH__)) && (defined(__APPLE__)) )   
#include <stdlib.h>
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#include <OpenGL/glext.h>
#else
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glext.h>
#endif
 
#include <string>

class Shader {
public:
    Shader();
    Shader(const char *vsFile, const char *fsFile);
    ~Shader();
    
    void init(const char *vsFile, const char *fsFile);
    
    void bind();
    void unbind();
    
    unsigned int id();
    
private:
    unsigned int shader_id;
    unsigned int shader_vp;
    unsigned int shader_fp;
};

#endif

Shader.CPP File:

Now lets move onto our shader.cpp file. This is where we are going to fill all of our methods outlined above, and introduce a new method.

So lets start from the beginning, with our constructors:

Shader::Shader() {  
}

Shader::Shader(const char *vsFile, const char *fsFile) {
    init(vsFile, fsFile);
}

Phew, now that that’s out of the way.. Lets move onto our init method.

The first two lines we are going to add, are going to tell OpenGL to create a vertex shader and a fragment shader, and assign the id’s of these to our shader_vp and shader_fp variables.

    shader_vp = glCreateShader(GL_VERTEX_SHADER);
    shader_fp = glCreateShader(GL_FRAGMENT_SHADER);
    
Now the following two lines, read in our vertex shader and fragment shader files. These are just standard text documents, with whatever extension you wish to add. The method textFileRead() can be any method that returns the contents of the shader files as a char*. This tutorial includes code for this, but I will not be explaining it, as I assume you have some knowledge of C++.

    const char* vsText = textFileRead(vsFile);
    const char* fsText = textFileRead(fsFile);    
    
Then I just do a little check to see if we were able to load both the files, if we have a problem reading them, we will just skip the method, and OpenGL should go on with everything like normal, just our shader won’t function.

    if (vsText == NULL || fsText == NULL) {
        cerr << “Either vertex shader or fragment shader file not found.“ << endl;
        return;
    }

The next two lines pass our shader file contents to OpenGL to attach it to our shaders.
    
    glShaderSource(shader_vp, 1, &vsText, 0);
    glShaderSource(shader_fp, 1, &fsText, 0);

This next two lines, tells OpenGL to compile our shaders, which we have already bound the source code to.
    
    glCompileShader(shader_vp);
    glCompileShader(shader_fp);
    
And finally, we create our shader_id as a shader program. And then attach the vertex shader and the fragment shader to the shader program, and finally, link the program.

    shader_id = glCreateProgram();
    glAttachShader(shader_id, shader_fp);
    glAttachShader(shader_id, shader_vp);
    glLinkProgram(shader_id);

So now we can read in a shader file and compile it as a shader program. So lets take a look at the methods which allow us to use this:

This first method, id(), will return the shader_id program. This is used for when we bind our shader, or for when we want to pass variables through to our shader.
unsigned int Shader::id() {
    return shader_id;
}

This second method, bind(), will simply attach our shader, and anything drawn afterwards will use this shader, either until this shader is unbound or another shader is enabled.
void Shader::bind() {
    glUseProgram(shader_id);
}

The third of our usage methods, is unbind(), this simply binds the shader 0, which is reserved for OpenGL, and will disable our current shader.
void Shader::unbind() {
    glUseProgram(0);
}

So whats left now? We can create our shader, and we can use our shader. So why don’t we clean up our shader when we are done?

I will be doing this from within the destructor method for our shader, and this should only be called once at the end of our application, otherwise we will have to recreate our shader, which is an expensive process.

Our desctructor is made up of only 5 method calls to OpenGL. The first two, just detach our vertex shader and fragment shader from our shader project, and the final 3 just delete all of the shaders and then deletes the shader program.

    glDetachShader(shader_id, shader_fp);
    glDetachShader(shader_id, shader_vp);
    
    glDeleteShader(shader_fp);
    glDeleteShader(shader_vp);
    glDeleteProgram(shader_id);

And that’s all there is to creating a shader program. It’s not that tricky, and it all makes sense when sit back and think about what it is doing.

Now lets take a look at the main.cpp file below for this project.

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.
79.
80.
81.
82.
83.
  #include “shader.h”
#include <string.h>
#include <iostream>
#include <stdlib.h>

using namespace std;

static char* textFileRead(const char *fileName) {
    char* text;
    
    if (fileName != NULL) {
        FILE *file = fopen(fileName, “rt”);
        
        if (file != NULL) {
            fseek(file, 0, SEEK_END);
            int count = ftell(file);
            rewind(file);
            
            if (count > 0) {
                text = (char*)malloc(sizeof(char) * (count + 1));
                count = fread(text, sizeof(char), count, file);
                text[count] = ‘′;
            }
            fclose(file);
        }
    }
    return text;
}

Shader::Shader() {
    
}

Shader::Shader(const char *vsFile, const char *fsFile) {
    init(vsFile, fsFile);
}

void Shader::init(const char *vsFile, const char *fsFile) {
    shader_vp = glCreateShader(GL_VERTEX_SHADER);
    shader_fp = glCreateShader(GL_FRAGMENT_SHADER);
    
    const char* vsText = textFileRead(vsFile);
    const char* fsText = textFileRead(fsFile);    
    
    if (vsText == NULL || fsText == NULL) {
        cerr << “Either vertex shader or fragment shader file not found.“ << endl;
        return;
    }
    
    glShaderSource(shader_vp, 1, &vsText, 0);
    glShaderSource(shader_fp, 1, &fsText, 0);
    
    glCompileShader(shader_vp);
    glCompileShader(shader_fp);
    
    shader_id = glCreateProgram();
    glAttachShader(shader_id, shader_fp);
    glAttachShader(shader_id, shader_vp);
    glLinkProgram(shader_id);
}

Shader::~Shader() {
    glDetachShader(shader_id, shader_fp);
    glDetachShader(shader_id, shader_vp);
    
    glDeleteShader(shader_fp);
    glDeleteShader(shader_vp);
    glDeleteProgram(shader_id);
}

unsigned int Shader::id() {
    return shader_id;
}

void Shader::bind() {
    glUseProgram(shader_id);
}

void Shader::unbind() {
    glUseProgram(0);
}

Main.CPP File:

As most of the main.cpp file is based on the double buffered window tutorial, I am only going to explain the lines that are related to GLSL and implementing our new class. The first thing we are going to do here, is include our header file:

#include “shader.h”

Now that we have access to our shader class, we need to declare a Shader object. I am going to call it shader for lack of a better word in this tutorial:

Shader shader;

Now before we can do anything with our shaders, we need to call glewInit(), this sets up GLEW so that we can use the extensions required to use shaders. This code will go below our glutCreateWindow call in our main method:

glewInit();

Simple enough so far. Now we want to initialize our shader. Inside our void init(void) method, add the following line:

shader.init(“shader.vert”, “shader.frag”);

For this tutorial, our shader programs are going to be called shader.vert and shader.frag and will be located in the same directory as the the compiled application.

All that is left now is to use the shader. In our display method, we want the shader to be applied to our cube, so before we draw the cube we want to bind our shader, and afterwards we want to unbind it:

shader.bind();
cube();
shader.unbind();

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.

  #if ( (defined(__MACH__)) && (defined(__APPLE__)) )   
#include <stdlib.h>
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#include <OpenGL/glext.h>
#else
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glext.h>
#endif

#include “shader.h”

Shader shader;

GLfloat angle = 0.0; //set the angle of rotation

void init(void) {
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    
    shader.init(“shader.vert”, “shader.frag”);
}

void cube (void) {
    glRotatef(angle, 1.0, 0.0, 0.0); //rotate on the x axis
    glRotatef(angle, 0.0, 1.0, 0.0); //rotate on the y axis
    glRotatef(angle, 0.0, 0.0, 1.0); //rotate on the z axis
    glColor4f(1.0, 0.0, 0.0, 1.0);
    glutWireCube(2);
}

void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();  
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    
    shader.bind();
    cube();
    shader.unbind();
    
    glutSwapBuffers();
    angle += 0.1f;
}

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);
    //set up
the double buffering

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH;

    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(“A basic OpenGL Window);
    
    glewInit()

    glutDisplayFunc(display);
    glutIdleFunc(display);
    
    glutReshapeFunc(reshape);
    
    init();
    
    glutMainLoop();
    
    return 0;
}

Vertex Shader Source:

This is pretty much the most basic of vertex shaders. Vertex shaders
allow for us to manipulate the vertices of an object. The major
advantage of this would be if you were moving vertices or a mass
group of vertices, in a particular way. For example, water could
be created with a plane tesselated enough, and the vertex shader
would be faster at computing the positions of the vertices than
if you were doing it in immediate mode.

Anyway, this shader, just like all other GLSL shaders, has a main
function, it is possible to have other functions inside a shader,
but OpenGL will look for the main function and call it.

Inside our main function, I am setting the position of the vertices
to exactly where they would be if I hadn’t used this shader. We are
multiplying the projection matrix, by the current vertex to place
the vertex where it should belong.

The variable gl_Position, is used to set the position of the current
vertex. Another method of setting the vertex to where it should be
without shaders, to set gl_Position to ftransform; ftransform is a
GLSL variable that does the exact same as
glModelViewProjectionMatrix * glVertex;

I don’t use ftransform() personally, and I believe it is even deprecated
when using geometry shaders. (Just don’t quote me on that)

So all this shader will do is place the vertices we are drawing to where
they should have been in the first place.

*Note* All vertex shaders are called for every vertex individually.

Every vertex shader, needs to have the line
gl_Position = *something*;
because the point of the vertex shader is to set the positions of
the vertices.

1.
2.
3.
4.
5.
  void main() {            
    // Set the position of the current vertex 
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

 

Fragment Shader Source:

The fragment shader needs a main function, just as the

vertex shader does. But unlike a vertex shader, this is
called for every pixel on the screen.

All fragment shaders must end with the line
gl_FragColor = vec4(*something*);
because the fragment shader is setting the current color
drawn on the screen.

In the fragment shader, gl_FragColor will end up being
the color that we want to display on the screen. In
this case we are setting that to the color white, so every pixel
belonging to our shape will be white.

GLSL has a few different types, I recommend taking a look at the
GLSL quick reference guide to get an idea of all of them.
gl_FragColor takes the value of vec4() which is a 4 float
vector. This can be written as vec4(red, green, blue, alpha)
or you can even pass through other vec4′s in the constructor,
and even shorthand to vec4(1.0) to make all values 1.0. This will
give us our white color.

And that is all there is to shaders (for now).
If you have followed through this far, well done. Give
yourself a pat on the back :)

If you have any questions, please email me at swiftless@gmail.com

Revision: 1.1
Added in use of glewInit() for Windows machines.

1.
2.
3.
4.
  void main() {
    // Set the output color of our current pixel
    gl_FragColor = vec4(1.0);
}

 

Download:

Download shader.h Source Code for this Tutorial

Download shader.cpp Source Code for this Tutorial

Download main.cpp Source Code for this Tutorial

Download shader.vert Source Code for this Tutorial

Download shader.frag Source Code for this Tutorial

VN:F [1.9.3_1094]
Please rate so I know where to improve the site. 1 means needs a lot of improvement, 10 means perfect. If you leave a low rating, please state why. I don't want people just coming to bash the site.
Rating: 9.0/10 (6 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
1. GLSL Setup, 9.0 out of 10 based on 6 ratings

Related posts:

  1. 2. GLSL Validation
  2. 2. OpenGL 4 Shaders
  3. 3. GLSL Coloring
  4. 4. GLSL Lighting
  5. 7. Texturing in GLSL

Comments (28)

I’m trying out the code on my MacBook Pro (10.6), and it runs. The only problem is that the shader doesn’t do anything.

I know that it is successfully read – I checked in the debugger.
I tried the shader in the OpenGL Shader Builder, and it works, but not in this program. I should mention that I use SDL to set up the OpenGL context. Can it have something to do with that?

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hey,

Try the newest version of shaded.cop which is available in the bump mapping tutorial. If that does not work it might be due to SDL.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Guess how I solved it?

I changed init(“shader.vert”, “shader.frag”) to init(“shoder.vert”, “shader.frag”), made a new empty line under the function call (Xcode doesn’t realize that a file has been modified if you only change a string), rebuilt, changed it back. It worked after the 5th try.

Xcode bugs are so random.

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

line 22 of shader.cpp: text[count] = ‘′;
what is that equal to? looks like ” but dont think that is intended.

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hey Fonix,

That is two ‘, or an empty char.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

gives me an error when i try use the empty char as ”, ” works though

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

oh dear the html doesnt like that, ‘ \ 0 ‘ is what i tried to write

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hi, I just want to ask what exactly should this program do?
I ask because I get only black screen and when I put off the line shader.bind ,I see the red cube. Is it right or should it do something else?

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

It should draw a WHITE cube.
If you get a red cube, that means the (part of the) shader failed to load. Make sure you are in the correct working directory, and it should work :)
Do you use Xcode?

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

To any viewers curious about Swiftless’ thoughts on the timer function, check the “rotation” tutorial in OpenGL. To summarize, he doesn’t recommend using them, in favor of smooth renderings with no lag.

P.S. triple post? what am I doing, trying to set a record for most consecutive posts? Thank you Swiftless for the tutorials and insight, look foward to your next tutorials (apparently, shadow mapping).

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

I know a double post is probably frowned upon, but I was just reviewing main.cpp and got a great idea:

currently, glutDisplayFunc and glutIdleFunc both accept the display function. My idea (which I have tested) is to get rid of glutIdleFunc in favor of glutTimerFunc. Stay with me, the new function called “timer” will look like:

void timer(int value){
//I still don’t know what the input value is
glutPostRedisplay();
glutTimerFunc(32, timer, 0);
}

This must therefore be complimented by another call to glutTimerFunc(32, timer, 0); in the position where glutIdleFunc(display); currently resides.

This still updates the display, but at an arbitrary value of 32 milliseconds per frame. This should save your processor from excessive work (at least on my 2GHz intel). Note this will slow the rotation, so change the line “angle += 0.01f;” in the display function to “angle += 0.5f;”. While the visual difference is minimal, the CPU work is practically gone.

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

I would like to thank you and apologize for the redundancy you experience with that issue (specifically, my role in it). I wasn’t sure where to place glewInit(); so I moved it around and debugged with every shift. My heart skipped a beat when I placed it right after glutCreateWindow(“A basic OpenGL Window”); in main.cpp and it successfully built the window.

To clarify, for others (from my experience):
1. Download files and set them in a project.
–Note: it appears shader.vert and shader.frag don’t even need to be in source or resource, just the project folder.
2. Add “glew32.lib” to Additional Dependencies.
–Make sure you highlight the project name and go to project>properties>config>linker>input.
3. place glewInit(); as specified above.

Thank you again for the swift reply.

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

I downloaded all the files and started debugging. They compile fine (after I add “glew32.lib” to additional dependencies), i just seem to constantly encounter this error message:

“Unhandled exception at 0×00000000 in GLSLintro.exe: 0xC0000005: Access violation.”

I am, unfortunately, not very experienced with C++, but I was hoping someone might tell me what would be causing this error.

I am using Microsoft’s Visual C++ 2008 Express Edition. I added the “shader.frag” and “shader.vert” files to both the “Source Files” and “Resource Files” folders. In both attempts, I was prompted to create a custom build rule for those file types, which I declined.

To expand on the error a little more, a small, green arrow points to the line “shader_vp = glCreateShader(GL_VERTEX_SHADER);” in the Shader::init function after I click “break” in the error message. Also, the “Locals” window has the variables vsText and fsText, both which are described as “0xcccccccc “.

It’s probably something I did wrong, but any help is greatly appreciated.

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hey Mitch,

This is not a problem with your code, on Windows and Linux you need to initialize GLEW and this tutorial was built on OSX.

Add the line glewInit() in your init method, before any shader code is used and that should fix it.

When I get around to updating these tutorials, I’ll be adding that line of code.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

I wish I had read this comment earlier, thought i was going mad ;) please update your tut, swiftless! but great collection!

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hi,

I am using glew-1.5.4 with this tutorial {Microsoft Visual Studio 2010, Windows 7 x64}.

The above example compiles without errors but when I try to run it, exception occurs at the following location:

void Shader::init(const char *vsFile, const char *fsFile) {
shader_vp = glCreateShader(GL_VERTEX_SHADER); <——- Exception
…….
…….

Looking forward for a solution,

Thankyou

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hey OpenGLC++,

I mention a fix for this below. You need to add the line glewInit() to your application if you are on Windows. This tutorial was written on OSX and that call isn’t required for me.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.3_1094]
Rating: +1 (from 1 vote)

need glewInit() on ubuntu as well it seems (my friend said it was something to do with nvidia and ati cards though not 100% sure what exactly, im using an ati HD2400xt)

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hey Audrius,

You are right, if you don’t want to get caught out by null pointers for your text, which will occur if for some reason your file fails to load.

I do however expect people who come into these tutorials to have some grasp of C++ itself.

When I finally get around to rewriting these tutorials, I will work in some more error checking.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hey, just wanted to let you know that you should probably initialize the “text” pointer, because without initializing it, most people will either crash or even worse.

I, for example, have got a habit of always initializing every pointer and always checking if it’s initialized properly (through the console like:

“std::cout << *pointer;"

I know that you know stuff like this, but it was getting a lot in my way, when I first started working with pointers and for the time being, I hope at least my comment will help a couple of people fallen for the pointer trap.)

Ohh, man, I probably have set a record on using the word "pointer" ;D

Otherwise, awesome tutorial as always!!!

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

my bad, this site have own forum.

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

I’m wondering, don’t you plan make on your site forum?
Or you think is better to use gd.net?

I ask out of curiosity…

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Glad I could help Osk, I also have the GTX260 and it is a beast of a card, especially compared to the Intel GMA’s :D

Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

@Swiftless: ok now everything is working. You have right with adding glewInit(); before glutCreateWindow.
Thats work on my main machine (gtx260…).

Now i feel power hehe big thx

Cheers,
Osk

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hi Osk,

The GMA X3100 only supports OpenGL 1.5. I had the GMA 950 in one of my older laptops, and was extremely disappointed at the lack of OpenGL support.

This tutorial is for OpenGL 2.0, when GLSL shaders were integrated into the specification. You might be able to get it to work, if you switch all the shader calls to their ARB alternatives.

For example:
glCreateShader becomes glCreateShaderObjectARB.

Most of the shader calls will have ARB alternatives, and a quick search should be able to find them. If not, I can help you out, I have some old code that uses ARB extensions.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

I put glewInit(); before glutCreateWindow(); and still getting the same mem leak. Thats wierd beacouse i checked a shader loader and looks fine.

But! Now i checked a gpu in computer where i’m temporary working, and it’s a GMA X3100. GPU-Z show 4 pixel shader units and 0 vertex shader unit. Do you think problem is here?

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hi Osk,

Sorry about that, I have to update the tutorial. This is because you need to call glewInit() before you make use of any extensions. So after you call glutCreateWindow, call glewInit().

That line doesn’t appear in this tutorial as it was developed on OSX where you don’t need this call.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 5.0/5 (2 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hi, i’ve a problem to compile (in vs 2010) a project with files from this lesson.
I’m getting memory leak when program calls a method init from shader class. Exactly in this line:
file:shader.cpp
37.”shader_vp = glCreateShader(GL_VERTEX_SHADER);”

Have You any idea what is going on?

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Write a comment

Improve the web with Nofollow Reciprocity.