1

I am pretty new to OpenGL I having been flowing the tutorials at http://open.gl/. I've tried to expand upon them for a school project. I am having an issue with multiple textures not rendering correctly. For some reason the previous texture is "lingering on" underneath the next texture. I'm not sure what is happening here that would really appreciate some insight. I have a picture of the issue below that I think clearly shows the issue and the code below that.

Here is the link on imgur because I can't post pictures here yet: Picture of Issue

Here is the code:

 //Needed for SDL GLEW Support
 #define NO_SDL_GLEXT

//Headers
#include <GL/glew.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <iostream>
#include <ctime>
#include <SOIL.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

// Shader sources
const char* vertexSource =
"#version 150\n"
"in vec2 position;"
"in vec3 color;"
"in vec2 texcoord;"
"out vec3 Color;"
"out vec2 Texcoord;"
"uniform mat4 trans;"
"void main() {"
"   Color = color;"
"   Texcoord = texcoord;"
"   gl_Position = vec4( position, 0.0, 1.0 );"
"}";

const char* fragmentSource =
"#version 150\n"
"in vec3 Color;"
"in vec2 Texcoord;"
"out vec4 outColor;"
"uniform sampler2D texture;"
"uniform int texNum;"
"void main() {"
"   outColor = texture( texture, Texcoord );"
"}";

int main( int argc, char *argv[] )
{
//Create the SDL Window
SDL_Init( SDL_INIT_VIDEO );
SDL_Surface* surface = SDL_SetVideoMode( 1280, 720, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_OPENGL );
SDL_WM_SetCaption( "OpenGL", 0 );

// Initialize GLEW
glewExperimental = GL_TRUE;
glewInit();

//Enable transparency
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// Create Vertex Array Object
GLuint vao;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );

// Create a Vertex Buffer Object and copy the vertex data to it
GLuint vbo;
glGenBuffers( 1, &vbo );

float backgroundVertices[] = {
//  Position      Color             Texcoords
    //Background
    -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, //Bottom-left
     1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, //Bottom-right
     1.0f,  1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, //Top-right
     1.0f,  1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, //Top-right
    -1.0f,  1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, //Top-left
    -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, //Bottom-left

    //X
    -0.8f, -0.8f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, //Bottom-left
    -0.2f, -0.8f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, //Bottom-right
    -0.2f, -0.6f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, //Top-right
    -0.2f, -0.6f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, //Top-right
    -0.8f, -0.6f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, //Top-left
    -0.8f, -0.8f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, //Bottom-left

    //Y
     0.2f, -0.8f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, //Bottom-left
     0.8f, -0.8f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, //Bottom-right
     0.8f, -0.6f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, //Top-right
     0.8f, -0.6f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, //Top-right
     0.2f, -0.6f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, //Top-left
     0.2f, -0.8f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, //Bottom-left

    //Logo
    -0.25f, -0.1f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, //Bottom-left
     0.25f, -0.1f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, //Bottom-right
     0.25f,  0.7f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, //Top-right
     0.25f,  0.7f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, //Top-right
    -0.25f,  0.7f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, //Top-left
    -0.25f, -0.1f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f  //Bottom-left
};

glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, sizeof( backgroundVertices ), backgroundVertices, GL_STATIC_DRAW );

glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vbo );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( backgroundVertices ), backgroundVertices, GL_STATIC_DRAW );

// Create and compile the vertex shader
GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER );
glShaderSource( vertexShader, 1, &vertexSource, NULL );
glCompileShader( vertexShader );

// Create and compile the fragment shader
GLuint fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( fragmentShader, 1, &fragmentSource, NULL );
glCompileShader( fragmentShader );

// Link the vertex and fragment shader into a shader program
GLuint shaderProgram = glCreateProgram();
glAttachShader( shaderProgram, vertexShader );
glAttachShader( shaderProgram, fragmentShader );
glBindFragDataLocation( shaderProgram, 0, "outColor" );
glLinkProgram( shaderProgram );
glUseProgram( shaderProgram );

// Specify the layout of the vertex data
GLint posAttrib = glGetAttribLocation( shaderProgram, "position" );
glEnableVertexAttribArray( posAttrib );
glVertexAttribPointer( posAttrib, 2, GL_FLOAT, GL_FALSE, 7 * sizeof( float ), 0 );

GLint colAttrib = glGetAttribLocation( shaderProgram, "color" );
glEnableVertexAttribArray( colAttrib );
glVertexAttribPointer( colAttrib, 3, GL_FLOAT, GL_FALSE, 7 * sizeof( float ), (void*)( 2 * sizeof( float ) ) );

GLint texAttrib = glGetAttribLocation( shaderProgram, "texcoord" );
glEnableVertexAttribArray( texAttrib );
glVertexAttribPointer( texAttrib, 2, GL_FLOAT, GL_FALSE, 7 * sizeof( float ), (void*)( 5 * sizeof( float ) ) );

// Load textures
GLuint textures[4];
glGenTextures( 4, textures );

int width, height;
unsigned char* image;

glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D, textures[0] );
    image = SOIL_load_image( "menubackground.png", &width, &height, 0, SOIL_LOAD_RGBA );
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image );
    SOIL_free_image_data( image );

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_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

glActiveTexture( GL_TEXTURE1 );
glBindTexture( GL_TEXTURE_2D, textures[1] );
    image = SOIL_load_image( "winona.png", &width, &height, 0, SOIL_LOAD_RGBA );
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image );
    SOIL_free_image_data( image );

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_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

glActiveTexture( GL_TEXTURE2 );
glBindTexture( GL_TEXTURE_2D, textures[2] );
    image = SOIL_load_image( "kombat.png", &width, &height, 0, SOIL_LOAD_RGBA );
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image );
    SOIL_free_image_data( image );

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_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

glActiveTexture( GL_TEXTURE3 );
glBindTexture( GL_TEXTURE_2D, textures[3] );
    image = SOIL_load_image( "logo.png", &width, &height, 0, SOIL_LOAD_RGBA );
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image );
    SOIL_free_image_data( image );

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_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

glActiveTexture( GL_TEXTURE4 );
glBindTexture( GL_TEXTURE_2D, textures[4] );
    image = SOIL_load_image( "JaredSpriteSheet.png", &width, &height, 0, SOIL_LOAD_RGBA );
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image );
    SOIL_free_image_data( image );

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_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

SDL_Event windowEvent;
while(true)
{
    if (SDL_PollEvent(&windowEvent))
    {
        if (windowEvent.type == SDL_QUIT ) break;
    }

    // Clear the screen to black
    glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
    glClear( GL_COLOR_BUFFER_BIT );

    //Draw rectangles from the 2 triangles using 6 indices and set textures
    //glActiveTexture( GL_TEXTURE0 );
    //glBindTexture( GL_TEXTURE_2D, textures[0] );
    glUniform1i( glGetUniformLocation( shaderProgram, "texture" ), 0 );
    glDrawArrays( GL_TRIANGLES, 0, 6 );

    //glActiveTexture( GL_TEXTURE1 );
    //glBindTexture( GL_TEXTURE_2D, textures[1] );
    glUniform1i( glGetUniformLocation( shaderProgram, "texture" ), 1 ); 
    glDrawArrays( GL_TRIANGLES, 6, 12 );

    //glActiveTexture( GL_TEXTURE2 );
    //glBindTexture( GL_TEXTURE_2D, textures[2] );
    glUniform1i( glGetUniformLocation( shaderProgram, "texture" ), 2 );
    glDrawArrays( GL_TRIANGLES, 12, 18 );

    //glActiveTexture( GL_TEXTURE3 );
    //glBindTexture( GL_TEXTURE_2D, textures[3] );
    glUniform1i( glGetUniformLocation( shaderProgram, "texture" ), 3 );
    glDrawArrays( GL_TRIANGLES, 18, 24 );

    glBindTexture( GL_TEXTURE_2D, 0 );

    SDL_GL_SwapBuffers();
}

SDL_Quit();
return 0;

glDeleteTextures( 4, textures );

glDeleteProgram( shaderProgram );
glDeleteShader( fragmentShader );
glDeleteShader( vertexShader );

glDeleteBuffers( 1, &vbo );

glDeleteVertexArrays( 1, &vao );

}

  • One of the things that I don't understand about this issue is why some of the textures are "lingering" while others are not. For example the background is drawn and textured first then second texture is the word "Winona". For some reason the background texture isn't "lingering" underneath it. But the third texture that is drawn has the second texture underneath and the fourth texture has the third texture underneath. Why is the second texture not being affected? – Chrono Fatalis Nov 29 '12 at 04:10
  • It might be in your shader code. I didn't see any issues with your code (but I might have overlooked something). – seesharper Nov 29 '12 at 13:22

0 Answers0