Jump to content
  • Advertisement
Sign in to follow this  

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

This topic is 2431 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I was just trying to use GLSL shaders in win32 but having issues with my 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;
string VertexShaderCode;
ifstream VertexShaderStream(vertex_file_path, std::ios::in);
cout << "Reading Vertex Shader" << endl;
string Line = "";
while(getline(VertexShaderStream, Line))
VertexShaderCode += "\n" + Line;
string FragmentShaderCode;
ifstream FragmentShaderStream(Fragment_file_path, std::ios::in);
string Line = "";
while(getline(FragmentShaderStream, Line))
FragmentShaderCode += "\n" + Line;
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);
//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);
//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);
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
//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

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
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.


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.

Share this post

Link to post
Share on other sites
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?.

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!