Sign in to follow this  

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

This topic is 2103 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:

[CODE]
//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;
}
[/CODE]

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

not being executed after I call [CODE]hRC = wglCreateContext(hDC);[/CODE]

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.

[img]http://thetooth.name/images/screen-04-15-2012_01-45-15_8986.jpg[/img]

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
[url="http://msdn.microsoft.com/en-us/library/dd183371(v=vs.85).aspx"]http://msdn.microsoft.com/en-us/library/dd183371(v=vs.85).aspx[/url]

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

This topic is 2103 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this