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.
Related
I am learning OpenGL from http://learnopengl.com and I have a problem with transformation based on this chapter Coordinate Systems...
I want to render something like this Movie but I have something like this Movie2 in 5 seconds its back on the screen. Sorry for many links but I think it's easier to show this by video.
It's my render loop:
const auto projection = glm::perspectiveFov(glm::radians(45.0f), 800.0f, 600.0f, 0.1f, 100.0f);
const auto view = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -3.0f));
while (!glfwWindowShouldClose(window))
{
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
flatColorShader->bind();
flatColorShader->setMat4("u_Projection", projection);
flatColorShader->setMat4("u_View", view);
auto model = glm::mat4(1.0f);
model = glm::rotate(model, static_cast<float>(glfwGetTime()) * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f));
flatColorShader->setMat4("u_Model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(vao);
glfwSwapBuffers(window);
glfwPollEvents();
}
Vaertex shader:
#version 460 core
layout (location = 0) in vec3 a_Pos;
layout (location = 1) in vec2 a_TexCoord;
out vec2 v_TexCoord;
uniform mat4 u_Projection;
uniform mat4 u_Model;
uniform mat4 u_View;
void main()
{
v_TexCoord = vec2(a_TexCoord.x, 1.0f - a_TexCoord.y);
gl_Position = u_Projection * u_Model * u_View * vec4(a_Pos, 1.0);
}
And Fragment shader:
#version 460 core
in vec2 v_TexCoord;
out vec4 color;
uniform sampler2D u_Texture;
void main()
{
color = texture(u_Texture, v_TexCoord);
}
I suppose it is a problem with the model matrix, but I don't known what. Can somebody help me with that's problem?
The order of the vertex transformations in the vertex shader is incorrect:
gl_Position = u_Projection * u_Model * u_View * vec4(a_Pos, 1.0);
gl_Position = u_Projection * u_View * u_Model * vec4(a_Pos, 1.0);
The order matters, because matrix multiplications are not commutative.
I am trying to build lighting using this tutorial. However, lighting appears on wrong side of human object and I do not know why.
Normals were created per triangle. Vertices of a triangle basically have the same normal:
glm::vec3 calculateNormal(glm::vec3 vertice_1, glm::vec3 vertice_2, glm::vec3 vertice_3)
{
glm::vec3 vector_1 = vertice_2 - vertice_1;
glm::vec3 vector_2 = vertice_3 - vertice_1;
return glm::normalize(glm::cross(vector_1, vector_2));
}
Here is code for vertex shader:
#version 330 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 normal;
out vec4 vert_color;
out vec3 Normal;
out vec3 FragPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform mat4 transform;
uniform vec4 color;
void main()
{
vert_color = color;
gl_Position = projection * view * model * transform * vec4(pos.x, pos.y, pos.z, 1.0);
FragPos = vec3(model * transform * vec4(pos, 1.0));
Normal = normal;
}
Fragment shader:
#version 330 core
uniform vec3 cameraPos;
uniform vec3 lightPos;
uniform vec3 lightColor;
in vec4 vert_color;
in vec3 FragPos;
in vec3 Normal;
out vec4 frag_color;
void main()
{
float ambientStrength = 0.1;
float specularStrength = 0.5;
vec3 ambient = ambientStrength * lightColor;
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(Normal, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
vec3 viewDir = normalize(cameraPos - FragPos);
vec3 reflectDir = reflect(-lightDir, Normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * lightColor;
vec3 result = (ambient + diffuse + specular) * vec3(vert_color.x, vert_color.y, vert_color.z);
frag_color = vec4(result, vert_color.w);
}
Main loop:
wxGLCanvas::SetCurrent(*glContext);
glClearDepth(1.0f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glm::mat4 model, view, projection;
model = glm::translate(model, modelPos); // modelPos is
view = fpsCamera->getViewMatrix();
projection = fpsCamera->getProjectionMatrix(windowWidth, windowHeight);
color = glm::vec4(0.310f, 0.747f, 0.185f, 1.0f);
glm::vec3 lightPos = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
glm::mat4 phantomtTransformation;
phantomtTransformation = glm::rotate(phantomtTransformation, - glm::pi<float>() / 2.0f, glm::vec3(1.0f, 0.0f, 0.0f));
phantomtTransformation = glm::rotate(phantomtTransformation, - glm::pi<float>() , glm::vec3(0.0f, 0.0f, 1.0f));
ShaderProgram shaderProgram;
shaderProgram.loadShaders("Shaders/phantom.vert", "Shaders/phantom.frag");
glClearStencil(0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
shaderProgram.use();
shaderProgram.setUniform("transform", phantomtTransformation);
shaderProgram.setUniform("model", model);
shaderProgram.setUniform("view", view);
shaderProgram.setUniform("projection", projection);
shaderProgram.setUniform("color", color);
shaderProgram.setUniform("lightColor", lightColor);
shaderProgram.setUniform("lightPos", lightPos);
shaderProgram.setUniform("cameraPos", fpsCamera->getPosition());
glStencilMask(0xFF); // Write to stencil buffer
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glStencilFunc(GL_ALWAYS, 0, 0xFF); // Set any stencil to 0
glStencilFunc(GL_ALWAYS, 1, 0xFF); // Set any stencil to object ID
m_pantomMesh->draw();
glStencilFunc(GL_ALWAYS, 0, 0xFF); // Set any stencil to 0 // no need for testing
glFlush();
wxGLCanvas::SwapBuffers();
View from front of the object:
View from back of the object:
EDIT:
In order to debug I removed object rotation matrix from main loop:
glm::mat4 phantomtTransformation;
phantomtTransformation = glm::rotate(phantomtTransformation, - glm::pi<float>() / 2.0f, glm::vec3(1.0f, 0.0f, 0.0f));
phantomtTransformation = glm::rotate(phantomtTransformation, - glm::pi<float>() , glm::vec3(0.0f, 0.0f, 1.0f));
shaderProgram.setUniform("transform", phantomtTransformation);
and changed line in fragment shader from
frag_color = vec4(result, vert_color.w);
to
frag_color = vec4(Normal, vert_color.w);
in order to visualize Normal values. As a result I noticed that when camera changes position phantom also changes color which means that normal values are also changing.
I think the cause of your problem is that you are not applying your model transformation to your normal vectors. Since you definitely do not want to skew them, you will have to create a special matrix for your normals.
As is explained further down the tutorial that you mentioned, the matrix can be constructed like this
Normal = mat3(transpose(inverse(model))) * aNormal;
in your vertex shader.
However, I highly recommend that you calculate the matrix in your application code instead, since you would calculate it per vertex in the above example.
Since you are using the glm library, it would look like this instead:
glm::mat3 model_normal = glm::mat3(glm::transpose(glm::inverse(model)));
You can then load your new model_normal matrix into the shader as a uniform mat3.
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 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);
}
I have a problem with displaying textures with GLSL in OpenGL 3.3 (Core profile). I have triple-checked everything and still can't find mistake. I'm using SDL for window handling and for texture loading.
This is my texture loading function
glEnable (GL_TEXTURE_2D);
glGenTextures(1, &generated_texture);
glBindTexture(GL_TEXTURE_2D, generated_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, image->w, image->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, image->pixels);
Passing to shaders
glActiveTexture(GL_TEXTURE0);
glUniform1i( glGetUniformLocation(shader, "texture_diffuse"), 0 );
glBindTexture(GL_TEXTURE_2D, texture_diffuse);
Vertex Shader
#version 330 core
struct Light
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec4 position;
};
struct Material
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec3 emission;
float shininess;
float reflectivity;
float ior;
float opacity;
};
layout(location = 0) in vec4 VertexPosition;
layout(location = 1) in vec3 VertexNormal;
layout(location = 2) in vec2 VertexTexture;
uniform mat4 PMatrix; //Camera projection matrix
uniform mat4 VMatrix; //Camera view matrix
uniform mat3 NMatrix; //MVMatrix ... -> converted into normal matrix (inverse transpose operation)
uniform mat4 MVPMatrix;
uniform Light light;
uniform Material material;
uniform sampler2D texture_diffuse;
//The prefix ec means Eye Coordinates in the Eye Coordinate System
out vec4 ecPosition;
out vec3 ecLightDir;
out vec3 ecNormal;
out vec3 ecViewDir;
out vec2 texture_coordinate;
void main()
{
ecPosition = VMatrix * VertexPosition;
ecLightDir = vec3(VMatrix * light.position - ecPosition);
ecNormal = NMatrix * VertexNormal;
ecViewDir = -vec3(ecPosition);
texture_coordinate = VertexTexture;
gl_Position = PMatrix * ecPosition;
}
Fragment Shader
#version 330 core
in vec3 ecNormal;
in vec4 ecPosition;
in vec3 ecLightDir;
in vec3 ecViewDir;
in vec2 texture_coordinate;
out vec4 FragColor;
struct Light
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec4 position;
};
struct Material
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec3 emission;
float shininess;
float reflectivity;
float ior;
float opacity;
};
uniform Light light;
uniform Material material;
uniform sampler2D texture_diffuse;
void main()
{
vec3 N = normalize(ecNormal);
vec3 L = normalize(ecLightDir);
vec3 V = normalize(ecViewDir);
float lambert = dot(N,L);
vec4 _tex = texture2D( texture_diffuse, texture_coordinate );
FragColor = _tex;
}
Also everything except textures works ( texture_coordinate etc. )
Does anyone see any possible error?
There are a few things I can see:
glEnable (GL_TEXTURE_2D);
This will just generate an error, as this enable is not valid in GL core profiles.
vec4 _tex = texture2D( texture_diffuse, texture_coordinate );
This will not compile, as the function texture2D is not available in GLSL3.30. It is called just texture and will derive the texture type from the sampler variable you call it with.
You should check for GL errors and especially the compile and link states of you shaders (and the info logs).