【发布时间】:2018-08-18 13:41:04
【问题描述】:
我正在尝试在我的对象(这是一个 3D 立方体)上添加纹理,但我不知道该怎么做,因为我没有得到正确的结果。另外,我正在为此使用 SDL。
这是我初始化所有内容的地方:
void OpenGLWindow::initGL()
{
.
.
.
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
shader = loadShaderProgram("simple.vert", "simple.frag");
glUseProgram(shader);
// ambient
glUniform3f(glGetUniformLocation(shader, "objectColor"), 1.0f, 0.5f, 0.31f);
glUniform3f(glGetUniformLocation(shader, "lightColor"), 1.0f, 1.0f, 1.0f);
glUniform3fv(glGetUniformLocation(shader, "lightPos"), 1, &lightPos[0]);
glUniform3f(glGetUniformLocation(shader, "viewPos"), 0.0f, 0.0f, 3.0f);
int width, height, nrChannels;
unsigned char *data = stbi_load("container.png", &width, &height, &nrChannels, 0);
if (data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
// Set our viewing and projection matrices, since these do not change over time
glm::mat4 projectionMat = glm::perspective(glm::radians(90.0f), 4.0f/3.0f, 0.1f, 10.0f);
int projectionMatrixLoc = glGetUniformLocation(shader, "projectionMatrix");
glUniformMatrix4fv(projectionMatrixLoc, 1, false, &projectionMat[0][0]);
glm::vec3 eyeLoc(0.0f, 0.0f, 2.0f);
glm::vec3 targetLoc(0.0f, 0.0f, 0.0f);
glm::vec3 upDir(0.0f, 1.0f, 0.0f);
glm::mat4 viewingMat = glm::lookAt(eyeLoc, targetLoc, upDir);
int viewingMatrixLoc = glGetUniformLocation(shader, "viewingMatrix");
glUniformMatrix4fv(viewingMatrixLoc, 1, false, &viewingMat[0][0]);
// Load the model that we want to use and buffer the vertex attributes
//geometry.loadFromOBJFile("sphere.obj");
geometry.loadFromOBJFile("cube.obj");
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, 3*geometry.vertexCount()*sizeof(float), geometry.vertexData(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
GLuint texturebuffer;
glGenBuffers(1, &texturebuffer);
glBindBuffer(GL_ARRAY_BUFFER, texturebuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(geometry.textureCoordData()) * sizeof(glm::vec3), geometry.textureCoordData(), GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, texturebuffer);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glPrintError("Setup complete", true);
}
我正在读取一个 .obj 文件,其中包含顶点、法线和纹理的坐标,该文件已经使用了一个将每个坐标存储在适当向量中的类;
例如std::vector 顶点; std::vector 纹理坐标; std::vector 法线;
如何将纹理矢量内的值发送到着色器?
这是我的顶点着色器:
in vec3 position;
out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoord;
uniform mat4 projectionMatrix;
uniform mat4 viewingMatrix;
uniform mat4 modelMatrix;
void main()
{
vec4 transformedPosition = projectionMatrix * viewingMatrix * modelMatrix * vec4(position, 1.0f);
gl_Position = transformedPosition;
FragPos = vec3(modelMatrix * vec4(position, 1.0));
Normal = mat3(transpose(inverse(modelMatrix))) * position;
}
片段着色器:
out vec4 outColor;
in vec3 Normal;
in vec3 FragPos;
uniform vec3 lightPos;
uniform vec3 objectColor;
uniform vec3 lightColor;
uniform vec3 viewPos;
void main()
{
float ambientStrength = 0.06;
vec3 ambient = ambientStrength * lightColor;
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
float specularStrength = 0.1;
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * lightColor;
vec3 result = (ambient + diffuse + specular) * objectColor;
outColor = vec4(result, 1.0);
}
如何将纹理放在对象(3D 立方体)上?
这就是我目前所得到的:
纹理不会出现在所有面上,放大或缩小时,表面会出现一些红色、绿色或蓝色线条。
【问题讨论】:
-
“我没有得到正确的结果” - 你必须更具体。究竟是什么问题?您如何阅读 .obj 文件?见How do I sort the texture positions based on the texture indices given in a Wavefront (.obj) file? 和Rendering meshes with multiple indices
-
我无法获取对象的纹理。我正在使用一个处理该问题的类,我相信它与您发布的第一个链接中的方法相同。我有一个包含所有纹理坐标的向量。我想我应该把它发送到着色器,但我不知道怎么做。
标签: opengl graphics sdl sdl-2 opengl-3