Crash when compiling/linking/using GLSL shaders in win32?

Started by
5 comments, last by thetooth 12 years ago
Hello I was just trying to use GLSL shaders in win32 but having issues with my function:


//LOAD SHADER FUNCTION
GLuint LoadProgram(const char * vertex_file_path, const char * Fragment_file_path)
{
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
cout << "Vertex Shader Creation" << endl;
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
cout << "Fragment Shader Creation" << endl;
//READ THE VERTEX SHADER CODE
string VertexShaderCode;
ifstream VertexShaderStream(vertex_file_path, std::ios::in);
cout << "Reading Vertex Shader" << endl;
if(VertexShaderStream.is_open())
{
string Line = "";
while(getline(VertexShaderStream, Line))
VertexShaderCode += "\n" + Line;
VertexShaderStream.close();
}
//READ THE FRAGMENT SHADER CODE
string FragmentShaderCode;
ifstream FragmentShaderStream(Fragment_file_path, std::ios::in);
if(FragmentShaderStream.is_open())
{
string Line = "";
while(getline(FragmentShaderStream, Line))
FragmentShaderCode += "\n" + Line;
FragmentShaderStream.close();
}
GLint Result = GL_FALSE;
int InfoLogLength;
//Compile Vertex Shader
printf("Compiling Shader : %s\n", vertex_file_path);
char const * VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL);
glCompileShader(VertexShaderID);
//Check Vertex Shader
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
vector<char> VertexShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);
//Compile Fragment Shader
printf("Compiling Shader : %s\n", Fragment_file_path);
char const * FragmentSourcePointer = FragmentShaderCode.c_str();
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL);
glCompileShader(FragmentShaderID);
//Check Fragment Shader
glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
vector<char> FragmentShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);
fprintf(stdout, "Linking Program\n");
GLuint ProgramID = glCreateProgram();
//Bind Attribute
glBindAttribLocation(ProgramID, 0, "position");
glBindAttribLocation(ProgramID, 1, "Texcoord0");
//Link The Program
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);
texture1 = glGetUniformLocation(ProgramID, "myTextureSampler");
matrixuniform = glGetUniformLocation(ProgramID, "myMatrix");

//Check The Program
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
vector<char> ProgramErrorMessage( max(InfoLogLength, int(1)) );
fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);
//Delete Shader
glDeleteShader(VertexShaderID);
glDeleteShader(FragmentShaderID);
glUseProgram(ProgramID);
//Return ProgramID
return ProgramID;
}


this function ^^
EDIT: It crashes after that function ^^

not being executed after I call hRC = wglCreateContext(hDC);

or any other time

I have no idea why? could someone tell me what the common errors are for this function not working in win32? it works in SDL/C++? so why shouldn't it work with this ???.



Thanks in advance
Advertisement
I don’t understand. Your topic mentions a crash but your post does not. Your post says that function is not executed (called?) after wglCreateContext() or any other time.
Exactly what is the error?


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

it crashes thats why it won't execute should have made that clearer damit lol
I tested your code and found nothing _wrong_ with what it does, you could lay off the STL a bit but i was able to load a shader and run it, so your crash is being caused by something else.

screen-04-15-2012_01-45-15_8986.jpg

Also it would help if you posted some more information, im sure your debugger will give you a stack trace, so just follow that and see what the environment looks like at that point in time.

One more thing to note is its not a good idea to delete your shaders after linking, just in case you decide(or more importantly, the driver decides) the shader needs to be recompiled you will have linker problems.
arghhh. Sorry, edit fail... deleted :-).
I fixed my problem I forgot to use wglMakeCurrent(hDC, hRC); after wglCreateContext(hDC) Stupid error. but now my problem is trying to do texture mapping I need to get a HBITMAP to a glTexImage2D function in OpenGL has any one had any luck with this?.
http://msdn.microsoft.com/en-us/library/dd183371(v=vs.85).aspx

all of the data needed to handle the image provided for you in that type, just initialise the texture with the contents of bmBits, afaik this is just raw pixel data which is what opengl takes using glTexImage2D, where this gets tricky is if you're trying to use some built in resources or perhaps a strange format, in which case you'll have to pay attention to the encoding of the pixel data other wise you'll end up with garbage or no output.

This topic is closed to new replies.

Advertisement