Andrew
Andrew

Reputation: 1784

Textures only work in compat and glGetError Always INVALID_ENUM as soon as context is open

I am having some issues with textures when using OpenGL 3.3 Core.

If I use the compat profile it renders perfectly. I tried to debug by using the glGetError()

It returns an invalid enum after getting a vao. And returns the same during the main loop. But surely thats not the issue? - It woudld be impossible to have an error by that point.

I was under the impressning that calling glGetError() would clear the error so the next call would just show the errors between the two?

My Platform is Windows7 64 with a nVidia GTX580 with driver version 310.70.

For my loading lib I am using glload at the min however I have used GLEW also. I am using the GLFW to manage the OpenGL context and DevIL to load texures. I am building in VS2010.

Here is a link to my full source. (I have riped everything from my main program out and named variable as logicly as possible)

full source code

Code that mostly likely to cause the problem.

Loading the image with devIL and buffering to the GPU.

void loadImage(const char* imageFile)
{
    ILuint texid; 
    ilGenImages(1, &texid); 
    ilBindImage(texid); 

    ilLoadImage(imageFile);

    int size = ilGetInteger( IL_IMAGE_SIZE_OF_DATA ) ;
    ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE); 


    glActiveTexture(GL_TEXTURE0);
    glGenTextures(1,&textureBuffer);
    glBindTexture(GL_TEXTURE_2D, textureBuffer);

    glTexImage2D(GL_TEXTURE_2D, 
         0, 
         ilGetInteger(IL_IMAGE_BPP), 
         ilGetInteger(IL_IMAGE_WIDTH),
         ilGetInteger(IL_IMAGE_HEIGHT), 
         0, 
         ilGetInteger(IL_IMAGE_FORMAT),
         GL_UNSIGNED_BYTE,
         ilGetData());

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    ilDeleteImages(1, &texid);


}

Render function called from the main loop.

void render()
{
    glUseProgram(program);
    glBindVertexArray(vao);

    glBindBuffer(GL_ARRAY_BUFFER, dataBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementObject);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D,textureBuffer);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)(8*4));
    glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0); 

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glUseProgram(0);
}

Vertex shader

#version 330

layout(location = 0) in vec2 position;
layout(location = 1) in vec2 coords;
smooth out vec2 textureCO;
void main()
{
    gl_Position = vec4(position.x,position.y,0.0,1.0);
    textureCO = coords;

}

Fragment shader

#version 330

out vec4 outputColor;
smooth in vec2 textureCO;
uniform sampler2D texture1;
void main()
{
    vec4 textureColor = texture(texture1, textureCO);
    outputColor = textureColor;
}

Upvotes: 0

Views: 794

Answers (1)

radical7
radical7

Reputation: 9114

Regarding the question of getting a GL_INVALID_ENUM, this is a common problem with the GLEW library. It's documented here on opengl.org. Just clear the error (by calling glGetError, like you say) and then OpenGL error reporting will behave as normal (or more precisely, as you're expecting).

As for the texture problem, it may be related to the call to ilGetInteger(IL_IMAGE_BPP). In core profiles, the internal format may only be one of the specified image formats, while according to the DevIL documentation, ilGetInteger(IL_IMAGE_BPP) returns an integer value of the number of bytes per pixel. glTexImage*D under compatibilty mode will accept values of 1, 2, 3, or 4 for the internal format, but those values will also generate a GL_INVALID_VALUE error under a core profile.

Upvotes: 1

Related Questions