OpenGL is drawing the UV map instead of the mesh - c++

I am following a youtube tutorial series for OpenGL development. He has uploaded the code on github. However the downloaded code seems to draw the UV map instead of the mesh itself.
I tried to isolate the problem and I think the problem lies in mesh.cpp. I could be wrong.
Maybe the problem is in the glDrawElementsBaseVertex line since we are not specifying the pointer to the indices array.
void Mesh::Draw()
{
glBindVertexArray(m_vertexArrayObject);
glDrawElementsBaseVertex(GL_TRIANGLES, m_numIndices, GL_UNSIGNED_INT, 0, 0);
glBindVertexArray(0);
}
I have tried swapping the position and texture coordinates but that doesnt seem to work.
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(model.positions[0]) * model.positions.size(), &model.positions[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[TEXCOORD_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(model.texCoords[0]) * model.texCoords.size(), &model.texCoords[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
Since I am new to OpenGL I am not able to fix this issue. I have already asked the youtuber and I am yet to get a reply.

My graphics card probably had some compatibility issues with OpenGL 120 so I changed it to 330 and a made a few changes and everything started to work fine. Here are the changes I made.
basicShader.vs
#version 330
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 texCoord;
layout(location = 2) in vec3 normal;
out vec2 texCoord0;
out vec3 normal0;
uniform mat4 MVP;
uniform mat4 Normal;
void main()
{
gl_Position = MVP * vec4(position, 1.0);
texCoord0 = texCoord;
normal0 = (Normal * vec4(normal, 0.0)).xyz;
}
basicShader.fs
#version 330
out vec4 color;
in vec2 texCoord0;
in vec3 normal0;
uniform sampler2D sampler;
uniform vec3 lightDirection;
void main()
{
color = texture(sampler, texCoord0) *
clamp(dot(-lightDirection, normal0), 0.0, 1.0);
}

Related

How i can generate normals in GLSL to apply a lighting to cube?

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?

OpenGL shader is not getting a value from glVertexAttribPointer()

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;
}

My OpenGL program's shaders don't affect the geometry or color the polygons

I have a vertex and fragment shader that worked fine not that long ago, but I tried to implement skeletal animation and somewhere along the line broke something. I have even gone away from using my custom functions and just set up a simple "Draw a triangle and color it" code block, but it's not working.
Drawing code:
GLfloat vertices[] = {
0.5f, 0.5f, 0.0f, // Top Right
0.5f, -0.5f, 0.0f, // Bottom Right
-0.5f, -0.5f, 0.0f, // Bottom Left
-0.5f, 0.5f, 0.0f // Top Left
};
GLuint indices[] = { // Note that we start from 0!
0, 1, 3, // First Triangle
1, 2, 3 // Second Triangle
};
GLuint VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind
glBindVertexArray(0);
while (!win.windowShouldClose) {
win.clearScreen(glm::vec4(1.0f,1.0f,1.0f,1.0f));
newShader.Use();
GLint mvpLoc = glGetUniformLocation(newShader.Program, "MVP");
glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, glm::value_ptr(camera.proj * camera.view));
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
win.display();
}
Vertex shader:
#version 430
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoords;
out vec2 TexCoords;
out vec3 ourColor;
uniform mat4 MVP;
void main()
{
gl_Position = MVP * vec4(position,1.0f);
TexCoords = texCoords;
ourColor = vec3(1.0f, 0.0f, 1.0f);
}
Fragment shader:
#version 430
in vec2 TexCoords;
in vec3 ourColor;
out vec4 color;
uniform sampler2D texture_diffuse1;
void main() {
color = vec4(ourColor,1.0f);
}
I have spent days working on this and I'm no closer to figuring out the solution. I believed that it was a problem with the data I was sending to the shaders, but even making the output of the fragment a constant as shown, the triangles are still black and not affected by the vertex shader at all, as if they were going straight from NDC to screen space.
Because of you did not show the camera attribute, I doubt there is any problem with your projection and view matrix (i.e., the MVP matrix may transform the object out of the screen). And I just have tested your program with glfw library at my computer, and the result of my test shows your code is OK. That is to say, there is no problem with "I believed that it was a problem with the data I was sending to the shaders"
Try to remove vertex shader's MVP matrix:
#version 430
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoords;
out vec2 TexCoords;
out vec3 ourColor;
uniform mat4 MVP;
void main()
{
// Remove MVP matrix
gl_Position = vec4(position,1.0f);
TexCoords = texCoords;
ourColor = vec3(1.0f, 0.0f, 1.0f);
}

OpenGL Reflection shader showing only grey

I'm very new to OpenGL and I've been working with setting up sky boxes, and finally fixed it thanks to some help here but now the reflection shader I've tried to set up by editing some I've found (so a sphere will have a basic reflection effect based on the cube map of my sky box) will not show any color but grey as shown in the image.
http://i.imgur.com/Th56Phg.png
I'm having no luck figuring out, here is my shader code:
Vertex Shader
#version 330 core
attribute vec3 position;
attribute vec2 texCoord;
attribute vec3 normal;
out vec3 Normal;
out vec3 Position;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform mat4 transform;
void main()
{
gl_Position = transform * vec4(position, 1.0);
Normal = mat3(transpose(inverse(model))) * normal;
Position = vec3(model * vec4(position, 1.0f));
}
Fragment Shader
#version 330 core
in vec3 Normal;
in vec3 Position;
out vec4 color;
uniform vec3 cameraPos;
uniform samplerCube skybox;
void main()
{
vec3 I = normalize(Position - cameraPos);
vec3 R = reflect(I, normalize(Normal));
color = texture(skybox, R);
}
And finally this is my usage:
glm::mat4 model;
glm::mat4 view = camera.GetViewProjection();
glm::mat4 projection = glm::perspective(70.0f, (float)1600 / (float)1200, 0.1f, 1000.0f);
glUniformMatrix4fv(glGetUniformLocation(refShader.getProg(), "model"), 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(glGetUniformLocation(refShader.getProg(), "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(refShader.getProg(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniform3f(glGetUniformLocation(refShader.getProg(), "cameraPos"), camera.getPos().x, camera.getPos().y, camera.getPos().z);
glActiveTexture(GL_TEXTURE3);
glUniform1i(glGetUniformLocation(refShader.getProg(), "skybox"), 3);
shader.Bind();
texture.Bind(0);
shader.Update(transformCube, camera);
cubeMesh.Draw();
glBindTexture(GL_TEXTURE_CUBE_MAP, skyboxTexture);
refShader.Bind();
refShader.Update(transform, camera);
sphereMesh.Draw();
This sequnece of operations is wrong:
glActiveTexture(GL_TEXTURE3);
glUniform1i(glGetUniformLocation(refShader.getProg(), "skybox"), 3);
shader.Bind();
Uniforms are per program state in the GL, and glUniform*() calls always affect the uniforms of the program currently in use. You seem to try to set this uniform before the program is bound, so it will fail, and the uniform in that program will still stay at the default value of 0.

weird behavior of glVertexAttribPointer

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");