2017-04-12 95 views
0

我跟隨this tutorial一步一步,我甚至複製粘貼整個代碼,但它仍然無法加載紋理。這裏是我的代碼,所關心問題的部分:OpenGL土壤 - 無法加載紋理

GLuint texture; 
glGenTextures(1, &texture); 
glBindTexture(GL_TEXTURE_2D, texture); // All upcoming GL_TEXTURE_2D operations now have effect on this texture object 
             // Set the texture wrapping parameters 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture wrapping to GL_REPEAT (usually basic wrapping method) 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
// Set texture filtering parameters 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
// Load image, create texture and generate mipmaps 
int width, height; 
unsigned char* image = SOIL_load_image("container.jpg", &width, &height, 0, SOIL_LOAD_RGB); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image); 
glGenerateMipmap(GL_TEXTURE_2D); 
SOIL_free_image_data(image); 
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture when done, so we won't accidentily mess up our texture. 

這裏是我的着色器:

#version 330 core 
in vec3 ourColor; 
in vec2 TexCoord; 

out vec4 color; 

uniform sampler2D ourTexture; 

void main() 
{ 
    color = texture(ourTexture, TexCoord); 
} 

而且

#version 330 core 
layout (location = 0) in vec3 position; 
layout (location = 1) in vec3 color; 
layout (location = 2) in vec2 texCoord; 

out vec3 ourColor; 
out vec2 TexCoord; 

void main() 
{ 
    gl_Position = vec4(position, 1.0f); 
    ourColor = color; 
    TexCoord = texCoord; 
} 

我用土來加載圖像數據。它是否過時?我該怎麼辦?

+0

什麼是您的項目結構是什麼樣子? 'container.jpg'可能不在您的項目目錄的根目錄中。 – Vallentin

+2

檢查**無符號字符*圖像的值** – Mozfox

+0

正如Mozfox所說:'if(!image)std :: cerr <<「圖像加載錯誤\ n」;' – jparimaa

回答

0

您所關注的教程code似乎是錯誤的,因爲它不會調用glActiveTexture也不會glUniform。請參閱教程末尾的其他文件的game loop code

也許你缺少這樣的事情:

glActiveTexture(GL_TEXTURE0); 
glBindTexture(GL_TEXTURE_2D, texture); 
glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture"), 0); 
+0

是的,它的工作原理!謝謝。 –

+0

[僅供參考LearnOpenGL教程**包括此內容。](https://learnopengl.com/#!Getting-started/Textures) – Vallentin