I'm trying to render a cube using an exported.obj file by Blender but the lighting looks like wrong.
I think it's because my vertices array contains 8 values but the normals array has only 6.
I can not understand how OpenGL uses this index array. It's a little bit magic for me.
Can anyone help me?
This is the file:
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 0.999999
v -0.999999 1.000000 -1.000001
v 1.000000 1.000000 -1.000000
v 1.000000 1.000000 1.000000
vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 -0.0000
vn -1.0000 -0.0000 0.0000
vn 0.0000 -0.0000 -1.0000
vn 1.0000 -0.0000 0.0000
vn -0.0000 0.0000 1.0000
f 2//1 4//1 1//1
f 8//2 6//2 5//2
f 5//3 2//3 1//3
f 6//4 3//4 2//4
f 3//5 8//5 4//5
f 1//6 8//6 5//6
f 2//1 3//1 4//1
f 8//2 7//2 6//2
f 5//3 6//3 2//3
f 6//4 7//4 3//4
f 3//5 7//5 8//5
f 1//6 4//6 8//6
This is my code:
GLuint cubeVAO, cubeVerticesVBO, cubeColorsVBO, cubeNormalsVBO, cubeIndicesVBO;
glGenVertexArrays(1, &cubeVAO);
glBindVertexArray(cubeVAO);
glGenBuffers(1, &cubeVerticesVBO);
glBindBuffer(GL_ARRAY_BUFFER, cubeVerticesVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), BUFFER_OFFSET(0));
glEnableVertexAttribArray(0);
glGenBuffers(1, &cubeColorsVBO);
glBindBuffer(GL_ARRAY_BUFFER, cubeColorsVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_colors), cube_colors, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), BUFFER_OFFSET(0));
glEnableVertexAttribArray(1);
glGenBuffers(1, &cubeNormalsVBO);
glBindBuffer(GL_ARRAY_BUFFER, cubeNormalsVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_normals), cube_normals, GL_STATIC_DRAW);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), BUFFER_OFFSET(0));
glEnableVertexAttribArray(2);
glGenBuffers(1, &cubeIndicesVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIndicesVBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cube_indices), cube_indices, GL_STATIC_DRAW);
glBindVertexArray(cubeVAO);
cubeShader.use();
glm::mat4 model;
cubeShader.setMat4("model", model);
cubeShader.setMat4("view", view);
cubeShader.setMat4("projection", projection);
cubeShader.setVec3("lampColor", lampColor);
cubeShader.setVec3("lampPos", lampPos);
cubeShader.setVec3("viewPos", viewPos);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
Vertex Shader
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec3 normal;
out vec3 Color;
out vec3 Normal;
out vec3 FragPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
vec4 posv4 = vec4(position, 1.0f);
Color = color;
FragPos = vec3(model * posv4);
Normal = normal;
gl_Position = projection*view*model*posv4;
}
Fragment Shader:
#version 330 core
in vec3 Color;
in vec3 Normal;
in vec3 FragPos;
out vec4 FragColor;
uniform vec3 lampColor;
uniform vec3 lampPos;
uniform vec3 viewPos;
void main()
{
// ambient
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lampColor;
// diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lampPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lampColor;
// specular
float specularStrength = 0.5;
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * lampColor;
vec3 result = (ambient + diffuse + specular) * Color;
FragColor = vec4(result, 1.0);
}
What's wrong? ,-,
Demonstration video:
https://vid.me/vy17h
You'll have to expand the data in the OBJ file. OpenGL expects a normal to be specified for each vertex, individually. In the OBJ file vertices and normals are treated separately. What you have to do is find the unique vertex+normal combinations, store that and index into that new vertex+normal array instead of using separate indices.
With modern OpenGL it is somewhat possible to use different indices for each vertex attribute by making use of vertex shader texture fetch, storing vertex data in textures. But this comes with a performance hit.
The normals array only has 6 values because the cube has 6 faces and you have created a model where each face has 4 normals at the corners pointing in the same direction (ie are identical) - which explains the lighting not looking how you expect.
The obj file uses indexing so it can assign one of these 6 unique values to each of the 8 vertices
Related
My cubes looks like:
But I want this
I can achive this result if I specify normals in VAO and send it, but i draw the cubes with EBO
auto context = QOpenGLContext::currentContext();
auto functions = context->functions();
auto additionalFunctions = context->extraFunctions();
//float side = diagonal/qSqrt(3);
unsigned int VBO;
QVector3D vertices[] = {
QVector3D(-0.5f,0.5f,-0.5f),
QVector3D(-0.5f,0.5f,0.5f),
QVector3D(0.5f,0.5f,-0.5f),
QVector3D(0.5f,0.5f,0.5f),
QVector3D(-0.5f,-0.5f,-0.5f),
QVector3D(-0.5f,-0.5f,0.5f),
QVector3D(0.5f,-0.5f,-0.5f),
QVector3D(0.5f,-0.5f,0.5f),
};
unsigned int indices[] = {
0,1,2,
1,2,3,
4,5,6,
5,6,7,
0,1,5,
0,4,5,
2,3,7,
2,6,7,
0,2,6,
0,4,6,
1,5,7,
1,3,7
};
functions->glGenBuffers(1, &EBO);
functions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
functions->glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices),
indices, GL_STATIC_DRAW);
functions->glGenBuffers(1, &VBO);
additionalFunctions->glGenVertexArrays(1, &VAO);
additionalFunctions->glBindVertexArray(VAO);
functions->glBindBuffer(GL_ARRAY_BUFFER, VBO);
functions->glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
vertices, GL_STATIC_DRAW);
functions->glEnableVertexAttribArray(0);
functions->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(QVector3D),
nullptr);
vertex shader
#version 130
in vec3 aPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0);
}
fragment shader
#version 130
out vec4 FragColor;
uniform vec3 objectColor;
uniform vec3 lightColor;
void main()
{
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;
vec3 result = ambient * objectColor;
FragColor = vec4(lightColor * objectColor, 1.0);
}
So, my question is, is it possible to draw cube with EBO with this 8 verteces and specify this material only with shaders and how i can do it without calculating normals manually in CPU?
Although I call glEnableVertexAttribArray and glVertexAttribPointer, my vertex shader cannot seem to access the value (I say value because i am not sure what it is called, Attribute?).
I am confused as I have other values that the vertex shader can access by doing exactly the same thing.
In the following code the Vertex position in attribute position 0 and the Vertex normal in attribute position 1 can be accessed fine by the shader however when I use the Vertex color value in attribute position 2 as the color for my mesh it is black even if set otherwise.
//Vertex Position
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, vertexPosition));
//Vertex Normal
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, vertexNormal));
//Vertex Colour
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, vertexColor));
Here is the Vertex struct
struct Vertex {
glm::vec3 vertexPosition;
glm::vec3 vertexNormal;
glm::vec3 vertexColor;
glm::vec2 textureCoords;
glm::vec3 vertexTangent;
glm::vec3 vertexBitangent;
Vertex() {}
Vertex(glm::vec3 vertexPosition, glm::vec3 vertexNormal = glm::vec3(0), glm::vec3 vertexColor = glm::vec3(0), glm::vec2 textureCoords = glm::vec2(0)) {
this->vertexPosition = vertexPosition;
this->vertexNormal = vertexNormal;
this->textureCoords = textureCoords;
}
};
Here is the Shader code. If I uncomment the commented line the mesh renders red as expected
#version 330 core
uniform mat4 uModelMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uProjectionMatrix;
uniform vec3 uLightPosition;
layout(location = 0) in vec3 aVertexPosition;
layout(location = 1) in vec3 aVertexNormal;
layout(location = 2) in vec3 aVertexColor;
out vec3 viewDirection;
out vec3 lightPosition;
out vec3 fragmentPosition;
out vec3 materialColor;
flat out vec3 fragmentNormal;
void main(){
materialColor = aVertexColor;
//materialColor = vec3(0.5, 0, 0);
mat4 modelViewMatrix = uViewMatrix * uModelMatrix;
vec3 viewSpacePosition = (modelViewMatrix * vec4(aVertexPosition, 1)).xyz;
viewDirection = normalize(viewSpacePosition);
vec3 viewSpaceNormal = normalize(modelViewMatrix * vec4(aVertexNormal, 0)).xyz;
fragmentNormal = viewSpaceNormal;
fragmentNormal = aVertexNormal;
lightPosition = uLightPosition;
fragmentPosition = vec3(uModelMatrix * vec4(aVertexPosition, 1.0));
gl_Position = uProjectionMatrix * modelViewMatrix * vec4(aVertexPosition, 1);
}
Even if vertexColor defaults to glm::vec3(1) instead of glm::vec3(0) it is still black. I have checked the values for vertexColor before drawing with the shader and the color is not black.
It looks like you are not setting your vertexColor value in the Vertex constructor.
Vertex(glm::vec3 vertexPosition, glm::vec3 vertexNormal = glm::vec3(0), glm::vec3 vertexColor = glm::vec3(0), glm::vec2 textureCoords = glm::vec2(0)) {
this->vertexPosition = vertexPosition;
this->vertexNormal = vertexNormal;
this->vertexColor = vertexColor;
}
I tried to create a textured skybox, following this tutorial:
cubemaps. Some triangles of the box (it is triangulated) are not shown at all, but weirdly enough not every second triangle of a square is missing (1 square's texturing is correct). My problem can be seen in the pictures below. The grayish color is "general" color of the scene, it has nothing to do with the skybox.
I believe the problem is caused by one of the shaders, as I get an
invalid operation
error, while trying to use the shaders of the skybox. What might be the problem?
Here the code of vertex shader:
#version 330 core
layout (location = 0) in vec3 aPos;
out vec3 TexCoords;
uniform mat4 projection;
uniform mat4 view;
void main() {
TexCoords = aPos;
vec4 pos = projection * view * vec4(aPos, 1.0);
gl_Position = pos.xyww;
}
and the fragment shader:
#version 330 core
out vec4 FragColor;
in vec3 TexCoords;
uniform samplerCube skybox;
void main() {
FragColor = texture(skybox, TexCoords);
}
and finally the procedure which loads the cubemap:
GLuint loadCubemap(std::vector<std::string> faces) {
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
int width, height, nrChannels;
for (GLuint i = 0; i < faces.size(); i++) {
unsigned char *data = stbi_load(faces[i].c_str(), &width, &height, &nrChannels, 0);
if (data) {
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
} else {
std::cout << "Cubemap texture failed to load at path: " << faces[i] << std::endl;
stbi_image_free(data);
}
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
return textureID;
}
The requested geometry (it's a .obj file):
o Cube
v 300.000000 -300.000000 -300.000000
v 300.000000 -300.000000 300.000000
v -300.00000 -300.000000 300.000000
v -300.00000 -300.000000 -300.00000
v 300.000000 300.000000 -300.000000
v 300.000000 300.000000 300.00000
v -300.000000 300.000000 300.000000
v -300.000000 300.000000 -300.000000
vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 0.0000
vn 1.0000 -0.0000 0.0000
vn -0.0000 -0.0000 1.0000
vn -1.0000 -0.0000 -0.0000
vn 0.0000 0.0000 -1.0000
s off
f 2//1 4//1 1//1
f 8//2 6//2 5//2
f 5//3 2//3 1//3
f 6//4 3//4 2//4
f 3//5 8//5 4//5
f 1//6 8//6 5//6
f 2//1 3//1 4//1
f 8//2 7//2 6//2
f 5//3 6//3 2//3
f 6//4 7//4 3//4
f 3//5 7//5 8//5
f 1//6 4//6 8//6
I have found the problem! It was not caused by the shaders but by my own stupidity. In the main rendering loop, I forgot to remove an extra binding VAO-buffer + drawing triangles pair after the newly inserted one.
I've, the last month, been following tutorials on a various pages (learnopengl.com) amongst others.
I got to the lighting section (I created a loader to load meshes with Assimp first), my fragmentshader code is pretty much the same as the tutorials lighting ( learnopengl.com/basic_lighting).
Frag code:
#version 130
out vec4 outColor;
in vec3 fragPos;
in vec3 normal;
in vec2 texcoord;
uniform sampler2D tex;
uniform vec3 lightPos;
uniform vec3 lightColor;
void main(){
float ambientStrength = 0.1f;
vec3 ambient = ambientStrength * lightColor;
//Diffuse
vec3 norm = normalize(normal);
vec3 lightDir = normalize(lightPos - fragPos);
float diff = max(dot(norm, lightDir), 0.0f);
vec3 diffuse = diff * lightColor;
float objectColor = 0.5f;
//texture(tex, texcoord)
vec3 result = (ambient + diffuse) * objectColor;
outColor = vec4(result, 1.0f);
}
When, for example, lighting a plane it gives me these results:
When I'm not lighting the plane it's plain grey.
Here's the .obj file or the plane:
Blender v2.69 (sub 0) OBJ File: 'plane.blend'
# www.blender.org
mtllib plane.mtl
o Plane
v 10.000000 0.000000 10.000000
v -10.000000 0.000000 10.000000
v 10.000000 0.000000 -10.000000
v -10.000000 0.000000 -10.000000
vn 0.000000 1.000000 0.000000
usemtl None
s off
f 2//1 1//1 3//1
f 4//1 2//1 3//1
It get's imported correctly, every vertex normal is { 0, 1, 0} and the indices are also correct.
When searching for things like "OpenGL light disortions" or just "OpenGL lighting" I can't find anything like this. So I really have no idea what's wrong.
Oh yeah, I use openGL 3.3, and I have an integrated intel GPU.
CPU is intel core i3-2367M in case there is a problem with the hardware.
Thank you for your time :D
EDIT:
So I just tried to show the color of my normals on the plane, they should be solid green but they are this abomination:
I'll also dump my vertexshadercode in here:
#version 130
in vec3 position;
in vec3 normal;
in vec2 texCoord;
out vec3 fragPos;
out vec3 Normal;
out vec2 texcoord;
uniform mat4 transform;
uniform mat4 view;
uniform mat4 projection;
void main(){
texcoord = texCoord;
Normal = normal;
fragPos = vec3(transform * vec4(position, 1.0f));
gl_Position = projection * view * transform * vec4(position, 1.0);
}
Here's how I set up my mesh:
void mesh::bindVertices(){
glBindVertexArray(vertexArrayObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0); // Position
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, normal)); //Normal
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texCoord)); //Texcoord
glBindVertexArray(0);
}
Vertex.h
#ifndef VERTEX_H
#define VERTEX_H
#include <vector>
#include <glm/glm.hpp>
class Vertex
{
public:
Vertex(){
position = glm::vec3(0.0f, 0.0f, 0.0f);
normal = glm::vec3(0.0f, 0.0f, 0.0f);
texCoord = glm::vec2(0.0f, 0.0f);
};
Vertex(glm::vec3 pos, glm::vec3 nor, glm::vec2 tex){
setUpVertex(pos, nor, tex);
};
~Vertex(){}
void setUpVertex(glm::vec3 pos, glm::vec3 nor, glm::vec2 tex){
this->position = pos;
this->normal = nor;
this->texCoord = tex;
};
void print(){
std::cout << "Position: { " << position.x << ", " << position.y << ", " << position.z << " }\n";
std::cout << "Normals: { " << normal.x << ", " << normal.y << ", " << normal.z << " }\n";
std::cout << "Texture Coordinates: { " << texCoord.x << ", " << texCoord.y << " }" << std::endl;
}
glm::vec3 position;
glm::vec3 normal;
glm::vec2 texCoord;
protected:
private:
};
#endif // VERTEX_H
The print function is there for debugging, and it outputs the correct values, so I really have no idea what is going on.
One thing that is not a problem is loading in the model is what I've managed to gather and the normals are behaving very strangley.
You don't correctly pass your normal data from the vertex shader to the fragment shader. You use out vec3 Normal in the VS, but in vec3 normal (note the lowercase) in the FS, so your input value is just undefined.
I'm creating default VAO and one VBO, and bind them.
I'm loading model data to the array of structs vertex_data_t
glBufferData(GL_ARRAY_BUFFER, nvertices * sizeof(vertex_data_t), vertices, GL_STATIC_DRAW);
Then in draw function i do:
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_data_t), (const GLvoid *)offsetof(vertex_data_t, position));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_TRUE, sizeof(vertex_data_t), (const GLvoid *)offsetof(vertex_data_t, position));
glBindVertexArray(0);
glDrawArrays(GL_TRIANGLES, 0, nvertices);
I'm getting nice, shaded Suzanne:
http://i.stack.imgur.com/uRjpv.png
However, this is wrong! Last argument of glVertexAttribPointer for normal attribute should be 12 aka (const GLvoid *)offsetof(vertex_data_t, normal), but when I do so my Suzanne is broken:
http://i.stack.imgur.com/zBjTS.png
How is it possible? How does shader know an offset to the normal?
Vertex shader:
attribute vec3 vertex;
attribute vec3 normal;
uniform vec4 ambient_color;
uniform vec4 diffuse_color;
uniform vec3 light_position;
uniform mat3 normal_matrix;
uniform mat4 model_view_matrix;
uniform mat4 model_view_projection_matrix;
varying vec4 varying_color;
void main(void) {
vec4 vertex4 = vec4(vertex.x, vertex.y, vertex.z, 1.0);
vec3 eye_normal = normal_matrix * normal;
vec4 position4 = model_view_matrix * vertex4;
vec3 position3 = position4.xyz / position4.w;
vec3 light_direction = normalize(light_position - position3);
float diffuse = max(0.0, dot(eye_normal, light_direction));
varying_color.rgb = diffuse * diffuse_color.rgb;
varying_color.a = diffuse_color.a;
varying_color += ambient_color;
gl_Position = model_view_projection_matrix * vertex4;
}
I think you miss something like:
glBindAttribLocation(progId, 0, "vertex");
glBindAttribLocation(progId, 1, "normal");