1

我对 OpenGL 很陌生,我一直在http://open.gl/上阅读教程。我试图将它们扩展为一个学校项目。我遇到了多个纹理无法正确渲染的问题。由于某种原因,前一个纹理在下一个纹理下方“徘徊”。我不确定这里发生了什么真的很感激一些洞察力。我在下面有一张问题的图片,我认为它清楚地显示了问题和下面的代码。

这是imgur上的链接,因为我还不能在这里发布图片: 问题图片

这是代码:

 //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 );

}

4

0 回答 0