GLSL noob problem...
Posted: Mon May 17, 2010 5:01 pm
Hi all!
Well, my journey into OpenGL is progressing. I've made a few projects using the FFP functionality from standard OpenGL 1.1 specification implementation. Now i'm moving onto using OpenGL 2.0 and GLSL.
I'm using GLEW and GLUT to test a basic shader manager class. However, I'm getting an access violation error whenever I try to run the program. I think the problem lies with the shader and program objects. Anyways, here is the code:
SHMAN.h
and the interface implemntation:
It would be really awsome if any one of you could perhaps look at my code and explain to me what I'm doing wrong. Thank you in advance!
Well, my journey into OpenGL is progressing. I've made a few projects using the FFP functionality from standard OpenGL 1.1 specification implementation. Now i'm moving onto using OpenGL 2.0 and GLSL.
I'm using GLEW and GLUT to test a basic shader manager class. However, I'm getting an access violation error whenever I try to run the program. I think the problem lies with the shader and program objects. Anyways, here is the code:
SHMAN.h
Code: Select all
//GLSL shader manager interface
#ifndef _SHMAN_H_
#define _SHMAN_H_
//system includes
#include <gl\glew.h>
#include <gl\gl.h>
#include <iostream>
#define MAX_INFO_LOG_SIZE 2048
//Shader manager class
class SHMAN
{
public:
//default constructor, get the shaders
SHMAN(const GLchar * vertShader, const GLchar * fragShader)
{
vertShaderSrc = vertShader;
fragShaderSrc = fragShader;
}
//SHMAN destructor
~SHMAN()
{
CleanUp();
}
//Shader manager core functions
void CreateSHMANObject();
void LoadShaders();
void CompileShaders();
void AttachToProgObj();
void LinkProgObj();
void ValidateProgObj();
void UseProjObj();
void CleanUp(); //detach shaders from program objects and delete the shader objects
private:
GLint success;
GLint currentGPUProg;
const GLchar * vertShaderSrc;
const GLchar * fragShaderSrc;
GLchar * log;
GLuint shmanProg;
GLuint shmanVertsh;
GLuint shmanFragsh;
};
#endif
Code: Select all
#include "shman.h"
using namespace std;
void SHMAN::CreateSHMANObject()
{
shmanVertsh = glCreateShader(GL_VERTEX_SHADER); //create new vertex shader object
shmanFragsh = glCreateShader(GL_FRAGMENT_SHADER); //create new fragment shader object
}
void SHMAN::LoadShaders()
{
//load vertex shader
const GLchar *strPtr[1];
strPtr[0] = vertShaderSrc;
glShaderSource(shmanVertsh, 1, strPtr, NULL);
//load fragment shader
strPtr[0] = vertShaderSrc;
glShaderSource(shmanFragsh, 1, strPtr, NULL);
}
void SHMAN::CompileShaders()
{
//compile the vertex shader and and get compilation status
glCompileShader(shmanVertsh);
glGetShaderiv(shmanVertsh, GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(shmanVertsh, MAX_INFO_LOG_SIZE, NULL, log);
cout << "\nVertex shader compilation error!" << endl;
cout << "\nPlease see compilation log details below: " << endl;
cout << "\n" << log << endl;
}
//compile the fragment shader and get compilation status
glCompileShader(shmanFragsh);
glGetShaderiv(shmanFragsh, GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(shmanFragsh, MAX_INFO_LOG_SIZE, NULL, log);
cout << "\nFragment shader compilation error!" << endl;
cout << "\nPlease see compilation log details below: " << endl;
cout << "\n" << log << endl;
}
}
void SHMAN::AttachToProgObj()
{
shmanProg = glCreateProgram();
glAttachShader(shmanProg, shmanVertsh);
glAttachShader(shmanProg, shmanFragsh);
}
void SHMAN::LinkProgObj()
{
//link the program object
glLinkProgram(shmanProg);
glGetProgramiv(shmanProg, GL_LINK_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(shmanProg, MAX_INFO_LOG_SIZE, NULL, log);
cout << "\nGPU program object link error!" << endl;
cout << "\nPlease see compilation log details below: " << endl;
cout << "\n" << log << endl;
}
}
void SHMAN::ValidateProgObj()
{
//validate the program before running on GPU
glValidateProgram(shmanProg);
glGetProgramiv(shmanProg, GL_VALIDATE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(shmanProg, MAX_INFO_LOG_SIZE, NULL, log);
cout << "\nGPU program validation error!" << endl;
cout << "\nPlease see compilation log details below: " << endl;
cout << "\n" << log << endl;
}
}
void SHMAN::UseProjObj()
{
glUseProgram(shmanProg);
//disable if un-needed
//currentGPUProg = glGetIntegerv(GL_CURRENT_PROGRAM, currentGPUProg);
//cout << "\nCurrent GPU program object handle: " << currentGPUProg << " \n" << endl;
}
void SHMAN::CleanUp()
{
glDetachShader(shmanProg, shmanVertsh);
glDetachShader(shmanProg, shmanFragsh);
glDeleteProgram(shmanProg);
}
