2017-04-12 75 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