为啥我得到黑色纹理?
Posted
技术标签:
【中文标题】为啥我得到黑色纹理?【英文标题】:why am I getting a black texture?为什么我得到黑色纹理? 【发布时间】:2020-06-16 14:56:05 【问题描述】:main.cpp是这样的:
#include <mLibs/mainIncludes.hpp>
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
// camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
// timing
float deltaTime = 0.0f; // time between current frame and last frame
float lastFrame = 0.0f;
int main()
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
glEnable(GL_DEPTH_TEST);
std::string vShaderPath = fUtils::absFileRelCurDir("shaders/main6.vs"); //define path
std::string fShaderPath = fUtils::absFileRelCurDir("shaders/main6.fs"); //define path
Shader ourShader(vShaderPath.c_str(), fShaderPath.c_str(), "MAIN_OBJECT_SHADER");
float vertices[] =
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 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 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f
;
unsigned int VBO, VAO,texture;
int width, height, nrChannels;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(5* sizeof(float)));
glEnableVertexAttribArray(2);
unsigned char* data;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//absFileRelCurDir gives absolute path of file relative to current directory
data = stbi_load(fUtils::absFileRelCurDir("resources/steelWood.png").c_str(),&width,&height,&nrChannels,0);
if(data)
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,data);
glGenerateMipmap(GL_TEXTURE_2D);
else
std::cout<<"STBI.H::Image Loading Failed"<<std::endl;
return -1;
stbi_image_free(data);
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
while (!glfwWindowShouldClose(window))
// per-frame time logic
// --------------------
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
view = camera.GetViewMatrix();
projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,texture);
ourShader.use();
ourShader.setMat4fv("model", model);
ourShader.setMat4fv("view", view);
ourShader.setMat4fv("projection", projection);
ourShader.setInt("mTexture", texture);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);//set parameters or use glDrawElements
glfwSwapBuffers(window);
glfwPollEvents();
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
void processInput(GLFWwindow *window)
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
camera.ProcessKeyboard(UP, deltaTime);
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
camera.ProcessKeyboard(DOWN, deltaTime);
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
// glfw: whenever the mouse moves, this callback is called
// -------------------------------------------------------
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
if (firstMouse)
lastX = xpos;
lastY = ypos;
firstMouse = false;
float xoffset = lastX - xpos;
float yoffset = ypos - lastY; // reversed since y-coordinates go from bottom to top
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset);
// glfw: whenever the mouse scroll wheel scrolls, this callback is called
// ----------------------------------------------------------------------
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
camera.ProcessMouseScroll(yoffset);
顶点着色器为:
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
layout (location = 2) in vec3 aNormal;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
out vec2 mTexCoord;
void main()
gl_Position = projection * view * model * vec4(aPos, 1.0f);
mTexCoord = aTexCoord;
片段着色器如下:
#version 330 core
out vec4 result;
in vec2 mTexCoord;
uniform sampler2D mTexture;
void main()
result = texture(mTexture, mTexCoord);
我向您保证,上面使用的类按预期工作,因为我之前已经测试过它们。编译和链接着色器时也没有错误。我也试过fewother类似的帖子,但似乎他们没有解决这个问题。 如果有人可以请指出我造成的什么错误让我浪费了一整天,这真的很有帮助。提前致谢。
仅供参考,其余代码在这里: mainIncludes.hpp:
#include <iostream>
#include <glad/include/glad/glad.h>
#include <GLFW/glfw3.h>
#include <mLibs/mainPrototypes.hpp>
#include <mLibs/shader.hpp>
#include <mLibs/fileUtils/fileUtils.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <vector>
#define STB_IMAGE_IMPLEMENTATION
#include <libs/stb_image.h>
#include <mLibs/camera.hpp>
#include <mLibs/genLamp.hpp>
上面使用的shader
类:
#ifndef SHADER_H
#define SHADER_H
#include <glad/include/glad/glad.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#define SHADER 0
#define SHADER_PROGRAM 1
class Shader
private: unsigned int ProgramID;
private:unsigned int vShaderID, fShaderID;
private: static const unsigned int infoLogSize = 1024;
private:char infoLog[infoLogSize];
private:int success;//indicates shader compilation/linking success
private:std::string tag;
public: Shader()
std::cerr<<"SHADER_H::Shader class- no_args_constructor::Warning::Constructor invalid"<<std::endl;
public: Shader(const GLchar* vertexPath, const GLchar* fragmentPath, std::string mTag);
public: void use();
public: void setBool(const std::string &name, bool value);
public: void setInt(const std::string &name, int value);
public: void setFloat(const std::string &name, float value);
public: void setFloat3f(const std::string& name, float value1, float value2, float value3);
public: void setFloat3fv(const std::string& name, glm::vec3 valueVector);
public: void setMat4fv(const std::string& name, glm::mat4 mat4Matrix);
public: int compileShader(unsigned int& shaderID,
GLuint shaderType,const GLchar* const * shaderCode);
public: int compileProgram(std::vector<unsigned int> shaderIDs);
public: int compileProgram(unsigned int& programID,
std::vector<unsigned int> shaderIDs);
void setVec3fv(const std::string& name, glm::vec3 valueVector);
void setVec3fv(const std::string& name, float x, float y, float z);
void setVec4fv(const std::string& name, glm::vec4 valueVector);
void setVec4fv(const std::string& name, float x, float y, float z, float w);
public: int checkError(unsigned int ShaderID, int checkType, GLuint shaderType=GL_VERTEX_SHADER)
switch(checkType)
case SHADER:
glGetShaderiv(ShaderID, GL_COMPILE_STATUS, &success);
if(shaderType == GL_VERTEX_SHADER)
if(!success)
glGetShaderInfoLog(ShaderID, infoLogSize, NULL, infoLog);
std::cout<<"SHADER.H::VERTEX SHADER::"<<tag<<":: SHADERS COMPILATION::FAILED!"<<std::endl;
return -1;
else if(shaderType == GL_FRAGMENT_SHADER)
if(!success)
glGetShaderInfoLog(ShaderID, infoLogSize, NULL, infoLog);
std::cout<<"SHADER.H::FRAGMENT SHADER::"<<tag<<"::SHADERS COMPILATION::FAILED!"<<std::endl;
return -1;
std::cout<<"SHADER.H::SHADERS::"<<tag<<"::SUCCESS::SHADERS COMPILED:: SUCCESS!"<<std::endl;
break;
case SHADER_PROGRAM:
glGetProgramiv(ShaderID, GL_LINK_STATUS, &success);
if(!success)
glGetProgramInfoLog(ShaderID, infoLogSize, NULL, infoLog);
std::cout<<"SHADER.H::SHADER PROGRAM::"<<tag<<"::SHADER PROGRAM LINKING::FAILED!";
return -1;
std::cout<<"SHADER.H::SHADER PROGRAM::"<<tag<<"::SHADER PROGRAM LINKING::SUCCESS!"<<std::endl;
break;
return 1;
public: unsigned int getProgramID()
return this->ProgramID;
;
Shader::Shader(const GLchar* vertexPath, const GLchar* fragmentPath, std::string mTag)
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
tag = mTag;
vShaderFile.exceptions(std::ifstream::failbit|std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit|std::ifstream::badbit);
try
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
vShaderFile.close();
fShaderFile.close();
/*convert stream to string*/
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
catch(std::ifstream::failure e)
std::cout<<"SHADER.H::"<<tag<<"SHADER FILES NOT SUCCESSFULLY READ::FAILED!"<<std::endl;
const char* vShaderCode = vertexCode.c_str();
const char* fShaderCode = fragmentCode.c_str();
compileShader(vShaderID,GL_VERTEX_SHADER, &vShaderCode);
compileShader(fShaderID,GL_FRAGMENT_SHADER, &fShaderCode);
compileProgram(std::vector<unsigned int>vShaderID,fShaderID);
int Shader::compileShader(unsigned int& shaderID,
GLuint shaderType,
const GLchar* const * shaderCode
)
shaderID = glCreateShader(shaderType);
glShaderSource(shaderID,1 , shaderCode, NULL);
glCompileShader(shaderID);
return this->checkError(shaderID, SHADER, shaderType);
int Shader::compileProgram(std::vector<unsigned int> shaderIDs)
return this->compileProgram(this->ProgramID, shaderIDs);
int Shader::compileProgram(
unsigned int& programID,
std::vector<unsigned int> shaderIDs)
programID = glCreateProgram();
for(auto eachShaderID:shaderIDs)
glAttachShader(programID, eachShaderID);
glLinkProgram(programID);
/* OK to delete shaders after linking*/
glDeleteShader(vShaderID);
glDeleteShader(fShaderID);
return this->checkError(programID,SHADER_PROGRAM);
void Shader::setBool(const std::string& name, bool value)
glUniform1i(glGetUniformLocation(this->getProgramID(), name.c_str()),(int)value);
void Shader::setInt(const std::string& name, int value)
glUniform1i(glGetUniformLocation(this->getProgramID(), name.c_str()),value);
void Shader::setFloat(const std::string& name, float value)
glUniform1f(glGetUniformLocation(this->getProgramID(), name.c_str()), value);
void Shader::setFloat3f(const std::string& name, float value1, float value2, float value3)
glUniform3f(glGetUniformLocation(this->getProgramID(), name.c_str()), value1, value2, value3);
void Shader::setFloat3fv(const std::string& name, glm::vec3 valueVector)
this->setFloat3f(name, valueVector.x, valueVector.y, valueVector.z);
void Shader::setVec3fv(const std::string& name, glm::vec3 valueVector)
this->setFloat3fv(name, valueVector);
void Shader::setVec4fv(const std::string& name, glm::vec4 valueVector)
glUniform4f(glGetUniformLocation(this->getProgramID(), name.c_str()),
valueVector.x, valueVector.y, valueVector.z, valueVector.w);
void Shader::setVec4fv(const std::string& name, float x, float y, float z, float w)
this->setVec4fv(name, glm::vec4(x, y, z, w));
void Shader::setMat4fv(const std::string& name, glm::mat4 mat4Matrix)
glUniformMatrix4fv(glGetUniformLocation(this->getProgramID(), name.c_str()),
1, GL_FALSE, glm::value_ptr(mat4Matrix));
void Shader::use()
glUseProgram(this->getProgramID());
#endif
上面使用的fUtils
:
#include <stdio.h> /* defines FILENAME_MAX */
#include <string>
#ifndef FUTILS_H
#define FUTILS_H
#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir ::_getcwd
std::string pathSeperator("\\");
#else
#include <unistd.h>
#define GetCurrentDir ::getcwd
std::string pathSeparator("/");
#endif
namespace fUtils
std::string getcwd()
char mCurrentPath[FILENAME_MAX];
GetCurrentDir(mCurrentPath, sizeof(mCurrentPath));
std::string currentPath(mCurrentPath);
return currentPath;
std::string absFileRelCurDir(std::string filePathRelCurDir)
return std::string(getcwd()+pathSeparator+filePathRelCurDir);
#endif
【问题讨论】:
【参考方案1】:着色器中的纹理采样器必须设置为它应该读取的纹理单元的索引,而不是纹理句柄。见OpenGL Wiki: Sampler
由于您的活动纹理单元是单元 0 (glActiveTexture(GL_TEXTURE0);
),您必须替换
ourShader.setInt("mTexture", texture);
到
ourShader.setInt("mTexture", 0);
【讨论】:
我很幸运,论坛存在于这个时代。非常感谢朋友以上是关于为啥我得到黑色纹理?的主要内容,如果未能解决你的问题,请参考以下文章