Jump to content
  • Advertisement
Sign in to follow this  
AlternativeUniverse

OpenGL Trouble displaying texture with SOIL (newbie)

This topic is 580 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

Hey guys, back with another newbie question.  :( 

 

I'm trying to create a simple 2D platform game in an attempt to learn and discover more things about OpenGL. Basically, I have created a quad that fits the entire window and now I would like to texture it so that it looks like a background. However, I can't seem to be able to get it to work as I keep getting an access violation error. 

 

Here's some relevant parts of my code I hope you find useful:

 
Global variables
GLuint g_texture = 0;
int iWidth, iHeight;
 
 
Startup function:
{
// ... glewinit, shader creation, vertices and color creation etc...
 
GLfloat Texture[] =
{
0.0f, 0.0f,  // Top Left
1.0f, 0.0f,  // Top Right
1.0f, 1.0f,  // Bottom Right
0.0f, 1.0f   // Bottom Left
};
 
//... buffer generations etc...
 
glGenTextures(1, &g_texture);
glBindTexture(GL_TEXTURE_2D, g_texture);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
 
unsigned char* image = SOIL_load_image("adventure.png", &iWidth, &iHeight, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, iWidth, iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
glGenerateMipmap(GL_TEXTURE_2D);
 
SOIL_free_image_data(image);
 
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
}
 
Render function
 
void Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
glUseProgram(program);
glBindTexture(GL_TEXTURE_2D, g_texture);
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0); 
glBindVertexArray(0);
 
glutSwapBuffers();
}
 
And lastly, here are my shaders:
 
Vertex Shader
 
#version 430 core
 
in layout (location = 0) vec3 VertexPos;
in layout (location = 1) vec4 VertexColors;
in layout (location = 2) vec2 TextureCoord;
 
out vec4 outputColors;
out vec2 TexCoord;
 
void main()
{
gl_Position = vec4(VertexPos, 1.0f);
outputColors = VertexColors;
TexCoord = vec2(TextureCoord.x, TextureCoord.y);
}
 
Fragment Shader
 
#version 430 core
 
// Inputs
in vec4 outputColors;
in vec2 TexCoord;
 
// Output Color
out vec4 VertexColors;
 
uniform sampler2D Texture;
 
void main()
{
VertexColors = texture(Texture, TexCoord) * outputColors;
//VertexColors = outputColors;
}
 
I hope someone could point out where I'm going wrong with this.
 
Thank you very much for reading my thread. All help is greatly appreciated.  :)

Share this post


Link to post
Share on other sites
Advertisement

Hello,

When you load your image with SOIL, you tell it to load it in RGB, with this option SOIL_LOAD_RGB

But when you transfer it to VRAM, you use GL_RGBA... Ouch...

Try using GL_RGB for internal format, and GL_RGB (or GL_BGR) for format; in you glTextureImage2D call.

Share this post


Link to post
Share on other sites

Hello,

When you load your image with SOIL, you tell it to load it in RGB, with this option SOIL_LOAD_RGB

But when you transfer it to VRAM, you use GL_RGBA... Ouch...

Try using GL_RGB for internal format, and GL_RGB (or GL_BGR) for format; in you glTextureImage2D call.

Oh, what a silly mistake.

Thank you for pointing that out I can see my texture now!

I'll try to pay more attention to my arguments from this point on to make sure they are all consistent. 

Thank you once again for your help. :)  

Share this post


Link to post
Share on other sites

Try using GL_RGB for internal format, and GL_RGB (or GL_BGR) for format; in you glTextureImage2D call.

 

There is still no need to use RGB as internal format. Best is always something like a 1, 2 or 4 component sized format like GL_RGBA8.

The drivers internaly use RGBA for RGB formats anway. And some of the "newer" gl functionalities (e.g. image load store) do not even support unsized and/or RGB formats.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!