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;
}
Related
I'm making a simple game in OpenGL where there is one player and multiple bubbles, all being spheres. Unfortunately, instancing doesn't work as expected and causes some strange effects. I'm new to instancing and can't see what may be causing a problem.
Draw method in player class:
void Player::draw(glm::mat4 ViewMat, GLfloat aspect, glm::vec3 light, GLfloat zoom)
{
bindProgram();
bindBuffers();
glm::mat4 Projection = glm::perspective(zoom, aspect, 0.1f, 100.0f);
glm::mat4 Model = glm::mat4(1.0f);
glm::mat4 MVP = Projection * ViewMat * Model;
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "MVP"), 1, GL_FALSE, &MVP[0][0]);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "M"), 1, GL_FALSE, &Model[0][0]);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "V"), 1, GL_FALSE, &ViewMat[0][0]);
glUniform3f(glGetUniformLocation(shaderProgram, "origin"), origin.x, origin.y, origin.z);
glUniform3f(glGetUniformLocation(shaderProgram, "lightPosWorld"), light.x, light.y, light.z);
glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size() / 3);
}
Static draw method in bubble class:
void Bubble::drawAll(glm::mat4 ViewMat, GLfloat aspect, glm::vec3 light, GLfloat zoom,
std::vector<Bubble *> allInstances)
{
std::vector<float> origins;
uint count = 0;
for (auto b : allInstances)
{
origins.push_back(b->origin.x);
origins.push_back(b->origin.y);
origins.push_back(b->origin.z);
count++;
}
allInstances[0]->bindProgram();
allInstances[0]->bindBuffers();
glBufferData(GL_ARRAY_BUFFER, origins.size() * sizeof(origins), origins.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, originsBuffer);
glVertexAttribPointer(1, // attribute. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void *)0 // array buffer offset
);
glm::mat4 Projection = glm::perspective(zoom, aspect, 0.1f, 100.0f);
glm::mat4 Model = glm::mat4(1.0f);
glm::mat4 MVP = Projection * ViewMat * Model;
GLuint programID = allInstances[0]->shaderProgram;
glVertexAttribDivisor(0, 0);
glVertexAttribDivisor(1, 1);
glUniformMatrix4fv(glGetUniformLocation(programID, "MVP"), 1, GL_FALSE, &MVP[0][0]);
glUniformMatrix4fv(glGetUniformLocation(programID, "M"), 1, GL_FALSE, &Model[0][0]);
glUniformMatrix4fv(glGetUniformLocation(programID, "V"), 1, GL_FALSE, &ViewMat[0][0]);
glUniform3f(glGetUniformLocation(programID, "lightPosWorld"), light.x, light.y, light.z);
glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, allInstances[0]->vertices.size() / 3, count);
}
Player vertex shader:
#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
uniform mat4 MVP;
uniform mat4 M;
uniform mat4 V;
uniform vec3 origin;
uniform vec3 lightPosWorld;
uniform vec3 cameraPos;
out vec3 vertexPosWorld;
out vec3 vertexNormal;
out vec3 eyeDirectionCamera;
out vec3 lightDirectionCamera;
void main()
{
gl_Position = MVP * vec4(vertexPosition_modelspace + origin,1);
vertexPosWorld = (M * vec4(vertexPosition_modelspace + origin,1)).xyz;
vertexNormal = normalize(vertexPosWorld - origin);
}
Bubble vertex shader:
#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec3 origin;
uniform mat4 MVP;
uniform mat4 M;
uniform mat4 V;
uniform vec3 lightPosWorld;
uniform vec3 cameraPos;
out vec3 vertexPosWorld;
out vec3 vertexNormal;
out vec3 eyeDirectionCamera;
out vec3 lightDirectionCamera;
void main()
{
gl_Position = MVP * vec4(vertexPosition_modelspace + origin,1);
vertexPosWorld = (M * vec4(vertexPosition_modelspace + origin,1)).xyz;
vertexNormal = normalize(vertexPosWorld - origin);
}
This is what I get:
Effect
But I wanted to get two bubbles that look exactly like the player (which draws correctly).
There are 2 issues in the code:
sizeof(origins) is a byte size of std::vector but not its underlying type. It should be sizeof(float).
glBindBuffer should be called before modifying the buffer’s data and properties.
It should help:
glBindBuffer(GL_ARRAY_BUFFER, originsBuffer);
glBufferData(GL_ARRAY_BUFFER, origins.size() * sizeof(float), origins.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, // attribute. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void *)0 // array buffer offset
);
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.
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);
}
And the error is "Access violation reading location in nvoglv64.dll"
The program worked as intended on my old AMD Radeon HD 6970. A few days ago I bought a GTX 970. Great card, but I would like my program to work. I want to render a quad to the screen for deferred rendering. I try to use OpenGL 4.4
Source:
Preparing the quad:
modelMatrix = mat4(1.0);
vertices.push_back(vec3(-1.0f, -1.0f, 0.0f));
vertices.push_back(vec3(1.0f, -1.0f, 0.0f));
vertices.push_back(vec3(1.0f, 1.0f, 0.0f));
vertices.push_back(vec3(-1.0f, -1.0f, 0.0f));
vertices.push_back(vec3(1.0f, 1.0f, 0.0f));
vertices.push_back(vec3(-1.0f, 1.0f, 0.0f));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
indices.push_back(0);
indices.push_back(1);
indices.push_back(2);
indices.push_back(0);
indices.push_back(2);
indices.push_back(3);
uvs.push_back(vec2(0.0f, 0.0f));
uvs.push_back(vec2(1.0f, 0.0f));
uvs.push_back(vec2(1.0f, 1.0f));
uvs.push_back(vec2(0.0f, 0.0f));
uvs.push_back(vec2(1.0f, 1.0f));
uvs.push_back(vec2(0.0f, 1.0f));
indexCount = static_cast<int>(indices.size());
is2D = true;
unsigned int handle[2];
glGenBuffers(2, handle);
glBindBuffer(GL_ARRAY_BUFFER, handle[0]);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, handle[1]);
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec3), &uvs[0], GL_STATIC_DRAW);
glGenVertexArrays(1, &array2D);
glBindVertexArray(array2D);
glBindBuffer(GL_ARRAY_BUFFER, handle[0]);
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, ((GLubyte *)NULL + (0)));
glEnableVertexAttribArray(0); // Vertex position
glBindBuffer(GL_ARRAY_BUFFER, handle[1]);
glVertexAttribPointer((GLuint)2, 2, GL_FLOAT, GL_FALSE, 0, ((GLubyte *)NULL + (0)));
glEnableVertexAttribArray(1); // Texture coordinates
glBindVertexArray(0);
Rendering the quad:
// The following three lines are called in the render loop (as 2nd pass).
// I skip rendering meshes in the first pass to better understand this error.
//sp->useSubRoutine(srp2); // SP is the shader program
//sp->resetMatrices(); // Set matrices to mat4(1.0);
//dq->render(); // DQ is the display quad
glBindFramebuffer(GL_FRAMEBUFFER, fbo); // fbo is 0 for quad
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
if (shader != nullptr)
{
shader->use();
shader->setModelMatrix(modelMatrix);
}
if (is2D)
{
glBindVertexArray(array2D);
glDrawArrays(GL_TRIANGLES, 0, indexCount); // ERROR HERE
return;
}
Vertex Shader:
#version 440
layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in vec2 vertexUV;
layout(location = 2) in vec3 vertexNormal;
centroid out vec2 UV;
out vec4 position;
out vec3 normal;
uniform mat4 uniMatModel;
uniform mat4 uniMatView;
uniform mat4 uniMatProjection;
uniform mat4 uniMatModelView;
uniform mat4 uniMatModelViewProjection;
uniform mat3 uniMatNormal;
void main()
{
UV = vertexUV;
position = uniMatModelView * vec4(vertexPosition, 1.0);
normal = normalize(uniMatNormal * vertexNormal);
gl_Position = uniMatModelViewProjection * vec4(vertexPosition, 1.0);
}
Fragment Shader:
#version 440
struct lightInfo {
vec4 position;
vec3 intensity;
bool isActive;
};
// IN
centroid in vec2 UV;
in vec4 position;
in vec3 normal;
// OUT
layout (location = 0) out vec4 fragColor;
layout (location = 1) out vec3 positionData;
layout (location = 2) out vec3 normalData;
layout (location = 3) out vec3 colorData;
// SUBROUTINES
subroutine void renderPassType();
subroutine uniform renderPassType renderPass;
// UNIFORMS
uniform sampler2D uniSamTexture;
uniform sampler2D uniSamAlpha;
uniform sampler2D uniSamAmbient;
uniform sampler2D uniSamSpecular;
uniform sampler2D uniSamShininess;
uniform lightInfo uniPointLights[32];
uniform vec3 uniVec3AmbientEmissiveness;
layout(binding=0) uniform sampler2D positionTex;
layout(binding=1) uniform sampler2D normalTex;
layout(binding=2) uniform sampler2D colorTex;
subroutine (renderPassType)
void renderWorld()
{
if (texture2D(uniSamAlpha, UV).rgb[0] == 1.0) // Alphamaps have to be inverted
{
discard;
}
else
{
colorData = texture2D(uniSamTexture, UV).rgb;
}
positionData = vec3(position.x, position.y, position.z);
normalData = normal;
fragColor = vec4(colorData, 1.0);
}
subroutine (renderPassType)
void renderLight()
{
fragColor = vec4(1.0,0.0,0.0,1.0); // For testing purposes set to red
}
void main()
{
renderPass();
}
Such problems can occure on NVIDIA cards when you try to read from a vertex buffer that does not have enough elements in it. For example rendering 9 vertices from a buffer that only contains 6. AMD interestingly does not complain about that.
In your case you bind a vertex buffer to attribute location 2, but you activate location 1. This:
glVertexAttribPointer((GLuint)2, 2, GL_FLOAT, GL_FALSE, 0, ((GLubyte *)NULL + (0)));
glEnableVertexAttribArray(1); // Texture coordinates
should actually be
||
\/
glVertexAttribPointer((GLuint)1, 2, GL_FLOAT, GL_FALSE, 0, ((GLubyte *)NULL + (0)));
glEnableVertexAttribArray(1); // Texture coordinates
Edit:
I just saw another thing
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec3), &uvs[0], GL_STATIC_DRAW);
uvs.size() equals 6 in your application, so OpenGL will try to read 6 * 3 = 18 float. Your uvs-array only contains 12 floats (since you are using vec2 here).
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");