Geometry shader and MVP matrices issues - c++

I'm working with openGL's Geometry Shader, and doing some tests to get the hang of it before I move on to more complex tasks. I wrote some code to transform inputted GL_POINTS into GL_TRIANGLE_STRIPs, and I apply the MVP matrix transformation in the Geometry Shader. While rendering without the MVP matrix produces the correct output, rendering with it produces nothing. Here is the code (hopefully minimal enough):
int main(){
// Create context and window
sf::Context context;
sf::Window window(sf::VideoMode(800, 600), "Voxel Engine");
window.setVerticalSyncEnabled(true);
// Initialize glew
glewExperimental = true;
GLenum err = glewInit();
if (err != GLEW_OK){
std::cout << "glewIniti failed, aborting...\n";
}
// Create VAO
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
// Vector for verts
std::vector<glm::vec3> vertices;
// Insert a few vertices
for (int i = -5; i < 5; i++){
vertices.push_back(glm::vec3(i*.3, 0.0, 0.0));
}
// View matrix vectors
glm::vec3 eye = glm::vec3(0.0f, 5.0f, 5.0f);
glm::vec3 at = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
// Create VBO for vertices
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
// Create MVP matrices
glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
glm::mat4 View = glm::lookAt(eye, at, up);
glm::mat4 Model = glm::mat4(1.0f);
// Load the shaders
GLuint programID = LoadShaders("VertexShader.glsl", "GeometryShader.glsl", "FragmentShader.glsl");
// Get uniform locations
GLuint M = glGetUniformLocation(programID, "M");
GLuint V = glGetUniformLocation(programID, "V");
GLuint P = glGetUniformLocation(programID, "P");
// Main loop
bool running = true;
sf::Event event;
while (running){
while (window.pollEvent(event)){
switch (event.type){
case sf::Event::Closed:
window.close();
return 0;
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
glUniformMatrix4fv(M, 1, GL_FALSE, &Model[0][0]);
glUniformMatrix4fv(V, 1, GL_FALSE, &View[0][0]);
glUniformMatrix4fv(P, 1, GL_FALSE, &Projection[0][0]);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_POINTS, 0, 10);
glDisableVertexAttribArray(0);
window.display();
}
return 0;
}
Vertex Shader:
#version 430 core
layout(location = 0) in vec3 position;
void main(){
gl_Position = vec4(position, 1);
}
Geometry Shader:
#version 430 core
layout (points) in;
uniform mat4 M, V, P;
layout (triangle_strip) out;
layout (max_vertices = 14) out;
void main(void){
// create a vertex for each point on the cube
vec4 v1 = M*V*P*(gl_in[0].gl_Position + vec4(.1, -.1, -.1, 0.0));
vec4 v2 = M*V*P*(gl_in[0].gl_Position + vec4(-.1, -.1, -.1, 0.0));
vec4 v3 = M*V*P*(gl_in[0].gl_Position + vec4(.1, .1, -.1, 0.0));
vec4 v4 = M*V*P*(gl_in[0].gl_Position + vec4(-.1, .1, -.1, 0.0));
vec4 v5 = M*V*P*(gl_in[0].gl_Position + vec4(.1, -.1, .1, 0.0));
vec4 v6 = M*V*P*(gl_in[0].gl_Position + vec4(-.1, -.1, .1, 0.0));
vec4 v7 = M*V*P*(gl_in[0].gl_Position + vec4(-.1, .1, .1, 0.0));
vec4 v8 = M*V*P*(gl_in[0].gl_Position + vec4(.1, .1, .1, 0.0));
gl_Position = v4;
EmitVertex();
gl_Position = v3;
EmitVertex();
gl_Position = v7;
EmitVertex();
gl_Position = v8;
EmitVertex();
gl_Position = v5;
EmitVertex();
gl_Position = v3;
EmitVertex();
gl_Position = v1;
EmitVertex();
gl_Position = v4;
EmitVertex();
gl_Position = v2;
EmitVertex();
gl_Position = v7;
EmitVertex();
gl_Position = v6;
EmitVertex();
gl_Position = v5;
EmitVertex();
gl_Position = v2;
EmitVertex();
gl_Position = v1;
EmitVertex();
EndPrimitive();
}
Fragment Shader:
#version 430 core
out vec4 color;
void main(){
color = vec4(0, 1, 0, 1);
}
I can provide my LoadShaders function if necessary, but I don't think the issue lies there.
So, performance aside, is there anything I'm doing which is blatantly incorrect?

Related

Vertex shader recieving input but no output is generated

I have this vertex shader, which simply passes the position given and passes the UV and color to the fragment shader:
#version 330 core
layout (location = 0) in vec2 in_pos;
layout (location = 1) in vec2 in_texUV;
layout (location = 2) in vec4 in_color;
out vec2 ex_texUV;
out vec4 ex_color;
uniform mat4 projection;
void main()
{
gl_Position = vec4(in_pos, 0.0, 1.0) * projection;
ex_texUV = in_texUV;
ex_color = in_color;
}
Edit: The fragment shader is shown here, and all uniforms are properly set:
#version 330 core
in vec2 in_texUV;
in vec4 in_color;
out vec4 out_color;
uniform vec2 screenSize;
uniform vec3 transparentColour;
uniform sampler2D sprite;
uniform sampler2D palette;
uniform int paletteLines[0x100];
void main()
{
if (in_color.a == 0.0) {
vec4 coord = gl_FragCoord - 0.5;
vec2 screenPos;
screenPos.x = coord.x * screenSize.x;
screenPos.y = coord.y * screenSize.y;
int id = paletteLines[int(screenPos.y)];
int index = int(texture2D(sprite, in_texUV).r * 255);
if (index == 0)
discard;
vec2 palvec;
palvec.x = index;
palvec.y = id;
out_color = texture(palette, palvec);
}
}
(The projection variable is properly set, shown using NVIDIA Nsight.)
Both the vertex and fragment shader have been edited to be simple passthroughs (even setting the fragment shader to a constant vec4(1.0, 1.0, 1.0, 1.0),) but it's always shown nothing.
To setup for the shader, I first set up the VAO and VBO to pass from a list of DrawVertex:
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(2, &GFXVBO);
glBindBuffer(GL_ARRAY_BUFFER, GFXVBO);
glVertexAttribPointer(0, 2, GL_SHORT, GL_FALSE, sizeof(DrawVertex), 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_SHORT, GL_FALSE, sizeof(DrawVertex), (void *)(sizeof(short) * 2));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(DrawVertex), (void *)(sizeof(short) * 4));
glEnableVertexAttribArray(2);
and then draw using the code below (the VAO and VBO are ensured to be bound, and gfxShader is just a helper to use a program):
gfxShader.use();
// [setup the program, change uniforms as necessary]
// lastRenderCount is how many to render
// gfxPolyList is the list of DrawVertex
glBufferData(GL_ARRAY_BUFFER, lastRenderCount * sizeof(DrawVertex), gfxPolyList, GL_DYNAMIC_DRAW);
glDrawArrays(GL_TRIANGLES, 0, lastRenderCount);
gfxShader.stop();
However, despite this, although RenderDoc shows that the input is being passed through, the output shows nothing at all. On top of this, NVIDIA Nsight says that no fragments are being drawn. Where could I be going wrong?
For context, here is struct DrawVertex:
struct DrawVertex {
short x;
short y;
short u;
short v;
Color color = 0; //0xRRGGBBAA
};
gl_Position = vec4(in_pos, 0.0, 1.0) * projection;
This is wrong. Matrix multiplication is not commutative. Should be:
gl_Position = projection * vec4(in_pos, 0.0, 1.0);
If you don't believe me, try this:
glm::mat4 proj = {
1.0f, 0.0f, 3.0f, 5.0f,
0.0f, 1.0f, 3.0f, 6.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 3.0f, 1.0f
};
glm::vec4 vec = { 0.0f, 1.0f, 0.0f, 1.0f };
glm::vec4 result1 = proj * vec;
glm::vec4 result2 = vec * proj;
std::cout << "result1: " << result1.x << result1.y << result1.z << result1.w << '\n';
std::cout << "result2: " << result2.x << result2.y << result2.z << result2.w << '\n';
// Output:
result1: 0167
result2: 5701
https://learnopengl.com/Getting-started/Transformations
Edit: Your fragment shader runs only if in_color.a == 0.0? Are you sure this is correct? Maybe you meant to use != instead.

light bugs on 3D model [duplicate]

This question already has an answer here:
light source is not set correctly
(1 answer)
Closed 1 year ago.
I have this picture below that shows the 3D model and the light is not as I expected, I've already tried many ways but I can't figure out how to fix it. my normal vectors are fine
the goemetry:
glGenVertexArrays(1, &VAOArray);
glBindVertexArray(VAOArray);
/* GENERATE THE BUFFERS */
glGenBuffers(1, &bufferArray);
/* SELECT THAT BUFFER TO WORK WITH */
glBindBuffer(GL_ARRAY_BUFFER, bufferArray);
glBufferData(GL_ARRAY_BUFFER, myMeshes.at(j).realPositions.size() * sizeof(float), (GLfloat*)RealPos, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glGenBuffers(1, &normalArray);
glBindBuffer(GL_ARRAY_BUFFER, normalArray);
glBufferData(GL_ARRAY_BUFFER, myMeshes.at(j).realNormals.size()*sizeof(float), (GLfloat*)RealNor, GL_STATIC_DRAW);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(3);
this is my code:
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
projection = glm::perspective(glm::radians(45.0f), (float)width / (float)heigh, 0.1f, 100.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, transZ)); //this is for scroll mouse
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -2.0f));
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -5.0f));
glUseProgram(programT);
int lightColorLoc = glGetUniformLocation(programT, "lightColor");
glUniformMatrix4fv(lightColorLoc, 1, GL_FALSE, glm::value_ptr(glm::vec3(1.0f, 0.0f, 0.0f)));
int objectColorLoc = glGetUniformLocation(programT, "objectColor");
glUniformMatrix4fv(objectColorLoc, 1, GL_FALSE, glm::value_ptr(glm::vec3(1.0f, 0.5f, 0.31f)));
glm::vec3 lightPos(2.0f, 4.0f, 5.0f);
int lightPosLoc = glGetUniformLocation(programT, "lightPos");
glUniformMatrix4fv(lightPosLoc, 1, GL_FALSE, glm::value_ptr(lightPos));
int projectionLocLight = glGetUniformLocation(programT, "projection");
glUniformMatrix4fv(projectionLocLight, 1, GL_FALSE, glm::value_ptr(projection));
glm::mat4 modelLight = glm::mat4(1.0f);
int modelLocLight = glGetUniformLocation(programT, "model");
glUniformMatrix4fv(modelLocLight, 1, GL_FALSE, glm::value_ptr(modelLight));
glm::mat4 viewLight = glm::mat4(1.0f);
int viewLocLight = glGetUniformLocation(programT, "view");
glUniformMatrix4fv(viewLocLight, 1, GL_FALSE, glm::value_ptr(viewLight));
int viewLoc = glGetUniformLocation(programT, "view");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
int projectionLoc = glGetUniformLocation(programT, "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
model = glm::rotate(model, glm::radians(rotX), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, glm::radians(rotY), glm::vec3(.0f, 1.0f, .0f));
model = glm::rotate(model, glm::radians(rotZ), glm::vec3(.0f, 0.0f, 1.0f));
int modelLoc = glGetUniformLocation(programT, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glGenerateMipmap(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureArray[0]);
glUniform1i(glGetUniformLocation(programT, "ourTexture"), 0);
glBindVertexArray(VAOArray[0]);
glDrawArrays(GL_TRIANGLES, 0, myMeshes.at(0).Indices.size());
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
glfwSetKeyCallback(window, key_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
}
vertex shader used for this project:
#version 330 core
layout (location = 0) in vec3 RealPos;
layout (location = 1) in vec3 vertex_color;
layout (location = 2) in vec2 vertex_textcoord;
layout (location = 3) in vec3 RealNor;
out vec3 vs_pos;
out vec3 vs_color;
out vec2 vs_text;
out vec3 normal;
out vec3 FragPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
vs_pos = RealPos;
vs_color = vertex_color;
vs_text=vertex_textcoord;
FragPos = vec3(model * vec4(vs_pos, 1.0));
mat3 normalMat = mat3(inverse(transpose(model)));
normal = RealNor * normalMat;
gl_Position = projection * view * model * vec4(vs_pos, 1.0);
}
fragment shader used:
#version 330 core
in vec2 vs_text;
in vec3 normal;
in vec3 FragPos;
uniform vec3 lightPos;
out vec4 gl_FragColor;
uniform vec3 objectColor;
uniform vec3 lightColor;
uniform sampler2D ourTexture;
void main()
{
vec3 norm = normalize(normal);
vec3 lightDir = normalize(lightPos - FragPos);
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;
float diff = max(abs(dot(norm, lightDir)), 0.0);
vec3 diffuse = diff * lightColor;
vec3 result = (ambient + diffuse) * objectColor;
gl_FragColor = texture(ourTexture, vs_text) * diff;
}
can someone help me to fix this or maybe guide me to solve it?
thanks
It applies that matrix * vector == vector * (matrix)^T
You are calculating normals via normal = RealNor * normalMat;
This is not equal to normalMat * RealNor which results in wrong normals. So kommutate the expression to normalMat * RealNor and see if it fixes the issue.

Additional sprite UVs when rendering sprites in OpenGL

I have sprites in an atlas rendering in OpenGL properly with the code below. My problem comes from trying to add a secondary "texture" to sample so I can do some multitexturing magic. The problem I think it's that the second sprite is also in an atlas and it's being affected by the VAO offsets so I can't really pick the right UVs to get the exact point I need. I've tried adding some calculations to reverse engineer the correct UVs for this sprite inside the other texture (you can see my attempt at the bottom) but this doesn't seem to work. What would be the best approach to do this?
Preparation:
glm::vec4 fRect;
fRect.x = static_cast<float>(iRect.x) / textureWidth;
fRect.y = static_cast<float>(iRect.y) / textureHeight;
fRect.z = (static_cast<float>(iRect.z) / textureWidth) + fRect.x;
fRect.w = (static_cast<float>(iRect.w) / textureHeight) + fRect.y;
// Configure VAO/VBO
GLuint VBO;
GLfloat vertices[] = {
// Pos // Tex
0.0f, 1.0f, fRect.x, fRect.w,
1.0f, 0.0f, fRect.z, fRect.y,
0.0f, 0.0f, fRect.x, fRect.y,
0.0f, 1.0f, fRect.x, fRect.w,
1.0f, 1.0f, fRect.z, fRect.w,
1.0f, 0.0f, fRect.z, fRect.y
};
GLuint VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindVertexArray(VAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
Rendering:
// Prepare transformations
glm::mat4 modelMatrix;
modelMatrix = glm::translate(modelMatrix, position);
// modelMatrix = glm::translate(modelMatrix, -glm::vec3(spriteOffset.x, spriteOffset.y, 0.0f));
modelMatrix = glm::rotate(modelMatrix, rotate.x, glm::vec3(1.0f, 0.0f, 0.0f));
// modelMatrix = glm::rotate(modelMatrix, rotate.y, glm::vec3(0.0f, 1.0f, 0.0f));
modelMatrix = glm::rotate(modelMatrix, rotate.z, glm::vec3(0.0f, 0.0f, 1.0f));
modelMatrix = glm::translate(modelMatrix, glm::vec3(-spriteOffset.x, -spriteOffset.y, 0.0f));
modelMatrix = glm::scale(modelMatrix, glm::vec3(size, 1.0f));
//(...)
glUniformMatrix4fv(modelMatrixLocation, 1, false, glm::value_ptr( modelMatrix ) );
glUniformMatrix4fv(viewMatrixLocation, 1, false, glm::value_ptr(viewMatrix));
glUniformMatrix4fv(projectionMatrixLocation, 1, false, glm::value_ptr(projectionMatrix));
ASSERT( !HasOpenGLErrors(), "OpenGL error!" );
glUniform3f(multColorLocation, multColour.x, multColour.y, multColour.z );
glUniform3f(addColorLocation, addColour.x, addColour.y, addColour.z );
ASSERT( !HasOpenGLErrors(), "OpenGL error!" );
// Bind Texture, etc
glDrawArrays(GL_TRIANGLES, 0, 6);
Vertex shader:
#version 330 core
layout (location = 0) in vec4 vertex; // <vec2 position, vec2 texCoords>
out vec2 TexCoords;
uniform mat4 model_matrix, view_matrix, projection_matrix;
void main()
{
TexCoords = vec2(vertex.z, 1.0 - vertex.w);
gl_Position = projection_matrix*view_matrix*model_matrix*vec4(vertex.xyz,1);
}
Fragment shader:
#version 330 core
in vec2 TexCoords;
out vec4 color;
uniform sampler2D texture;
uniform vec3 multColour;
uniform vec3 addColour;
void main()
{
vec4 minColour = vec4(0.0, 0.0, 0.0, 0.0);
vec4 maxColour = vec4(1.0, 1.0, 1.0, 1.0);
vec4 texColour = texture(texture, TexCoords);
if(texColour.a < 0.01)
discard;
color = clamp(vec4(multColour, 1.0) * texColour + vec4(addColour,0.0), minColour, maxColour);
}
Failed attempt at reading the right UVs in fragment shader:
float normU = (TexCoords.x - currUVs.x) / (currUVs.z - currUVs.x);
float icU = mix(icUVs.x, icUVs.z, normU);
float normV = (TexCoords.y - currUVs.y) / (currUVs.w - currUVs.y);
float icV = mix(icUVs.y, icUVs.w, normV);
vec2 UV = vec2(icU, icV );
vec4 initial = texture(initialColor, UV);
Where currUVs are the values of fRect passed in the VAO above and the icUVs are the UV bounds (min and max values) for the second sprite within the atlas texture.
So far it seems like all sprites that have no offset applied will render properly but if I passed in any kind of spriteOffset into the rendering, then it will render wrong.
How can I solve this? Is there a way of applying the VAO rects in the shaders and then be able to get the second sprite correctly?

Visual issue using Frambuffer Object as texture

My OpenGL engine draws a given scene into a Framebuffer Object, then uses its color attachment as a texture. It is then put on a square in the viewport.
The problem is that I see a strange visual artifact:
The square is built with
glm::vec2 square[4];
square[0] = glm::vec2(0.f, 0.f);
square[1] = glm::vec2(engWidth, 0.f);
square[2] = glm::vec2(0.f, engHeight);
square[3] = glm::vec2(engWidth, engHeight);
glm::vec2 texcoords[4];
texcoords[0] = glm::vec2(0.f, 0.f);
texcoords[1] = glm::vec2(1.f, 0.f);
texcoords[2] = glm::vec2(0.f, 1.f);
texcoords[3] = glm::vec2(1.f, 1.f);
glGenBuffers(2, Buffer2D);
glBindBuffer(GL_ARRAY_BUFFER, Buffer2D[0]);
glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(glm::vec2), square, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, Buffer2D[1]);
glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(glm::vec2), texcoords, GL_STATIC_DRAW);
orthographicProj = glm::ortho(0.f, (float)engWidth, 0.f, (float)engHeight, -1.f, 1.f);
Where engWidth and engHeight are the actual window size.
Then the frame is rendered with
shaderProgram->setMatrix("Clip", orthographicProj);
glBindBuffer(GL_ARRAY_BUFFER, Buffer2D[0]);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, Buffer2D[1]);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Vertex and fragment shaders for the final phase are simple
const char *frontVertexShader = R"(
#version 440 core
uniform mat4 Clip;
layout(location = 0) in vec2 Position;
layout(location = 1) in vec2 TexCoord;
out vec2 Tex_Coord;
void main() {
gl_Position = Clip * vec4(Position, 0.0, 1.0);
Tex_Coord = TexCoord;
}
)";
const char *frontFragmentShader = R"(
#version 440 core
in vec2 Tex_Coord;
uniform sampler2D sampler;
out vec4 Fragment;
void main() {
Fragment = texture(sampler, Tex_Coord);
}
)";
I tried using triangles instead a triangle strip but with the same result.
Any idea? I can post more code if needed.
I don't know if this will help, but for what it's worth this is what I do for a full screen quad:
(Note the vertex has no texture component - it's just -1 to 1).
v[0].x = -1.f; v[0].y = -1.f; v[0].z = 0.f;
v[1].x = -1.f; v[1].y = 1.f; v[1].z = 0.f;
v[2].x = 1.f; v[2].y = -1.f; v[2].z = 0.f;
v[3].x = 1.f; v[3].y = 1.f; v[3].z = 0.f;
The vertex shader is simply:
void main()
{
attrib_Fragment_Texture = attrib_Position.xy * 0.5 + 0.5;
gl_Position = vec4(attrib_Position.xy, 0.0, 1.0);
}
With the following pixel shader:
void main(void)
{
Out_Colour = texture2D(Map_Diffuse, attrib_Fragment_Texture);
}
You don't need an orthographic projection matrix.
Found it! I forgot to call glClear on the backbuffer and for a strange reason only some pixel presented the issue.

My shaders stopped working

I seem to have broken the shaders in my program, here is their code:
vertex shader
#version 330 core
uniform mat4 camera;
uniform mat4 model;
layout(location = 0) in vec3 vert;
layout(location = 1) in vec3 vertNormal;
out vec3 fragVert;
out vec3 fragNormal;
void main() {
// Pass some variables to the fragment shader
fragNormal = vertNormal;
fragVert = vert;
// Apply all matrix transformations to vert
gl_Position = camera * model * vec4(vert, 1);
}
fragment shader
#version 150 core
uniform mat4 model;
uniform vec3 cameraPosition;
// material settings
uniform float materialShininess;
uniform vec3 materialSpecularColor;
uniform vec3 materialColor;
uniform struct Light {
vec3 position;
vec3 intensities; //a.k.a the color of the light
float attenuation;
float ambientCoefficient;
} light;
in vec3 fragNormal;
in vec3 fragVert;
out vec4 finalColor;
void main() {
vec3 normal = normalize(transpose(inverse(mat3(model))) * fragNormal);
vec3 surfacePos = vec3(model * vec4(fragVert, 1));
vec4 surfaceColor = vec4(materialColor, 1);
vec3 surfaceToLight = normalize(light.position - surfacePos);
vec3 surfaceToCamera = normalize(cameraPosition - surfacePos);
//ambient
vec3 ambient = light.ambientCoefficient * surfaceColor.rgb * light.intensities;
//diffuse
float diffuseCoefficient = max(0.0, dot(normal, surfaceToLight));
vec3 diffuse = diffuseCoefficient * surfaceColor.rgb * light.intensities;
//specular
float specularCoefficient = 0.0;
if(diffuseCoefficient > 0.0)
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normal))), materialShininess);
vec3 specular = specularCoefficient * materialSpecularColor * light.intensities;
//attenuation
float distanceToLight = length(light.position - surfacePos);
float attenuation = 1.0 / (1.0 + light.attenuation * pow(distanceToLight, 2));
//linear color (color before gamma correction)
vec3 linearColor = ambient + attenuation*(diffuse + specular);
//final color (after gamma correction)
vec3 gamma = vec3(1.0/2.2);
finalColor = vec4(pow(linearColor, gamma), surfaceColor.a);
}
I have an asset that I am loading from an obj file, then drawing it like such:
void OpenGLView::run()
{
initializeAndSetupWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "PhongBunny");
glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
loadBunnyAsset();
AssetInstance bunny1;
bunny1.asset = bunny;
bunny1.position = glm::vec3(2.0f, 2.0f, 2.0f);
bunny1.scale = glm::vec3(1.0f, 1.0f, 1.0f);
do{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
loadUniforms(bunny1);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, bunny.vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, bunny.normalBuffer);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bunny.elementBuffer);
glDrawElements(GL_TRIANGLES, bunny.elementsSize, GL_UNSIGNED_INT, (void*)0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glfwSwapBuffers(window);
glfwPollEvents();
} while (!glfwWindowShouldClose(window));
glfwDestroyWindow(window);
glfwTerminate();
}
with this being the function to load uniforms:
void OpenGLView::loadUniforms(AssetInstance assetInstance)
{
Asset* asset = &assetInstance.asset;
glUseProgram(asset->shaderProgramID);
glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 1000.0f);
glm::mat4 camera = Projection * getViewMatrix();
glm::mat4 model = translate(assetInstance.position) * scale(assetInstance.position);
GLuint cameraID = glGetUniformLocation(asset->shaderProgramID, "camera");
GLuint modelID = glGetUniformLocation(asset->shaderProgramID, "model");
GLuint cameraPositionID = glGetUniformLocation(asset->shaderProgramID, "cameraPosition");
GLuint lightPositionID = glGetUniformLocation(asset->shaderProgramID, "light.position");
GLuint lightIntensitiesID = glGetUniformLocation(asset->shaderProgramID, "light.intensities");
GLuint lightAttenuationID = glGetUniformLocation(asset->shaderProgramID, "light.attenuation");
GLuint lightAmbientCoefficientID = glGetUniformLocation(asset->shaderProgramID, "light.ambientCoefficient");
GLuint materialColorID = glGetUniformLocation(asset->shaderProgramID, "materialColor");
GLuint materialShininessID = glGetUniformLocation(asset->shaderProgramID, "materialShininess");
GLuint materialSpecularColorID = glGetUniformLocation(asset->shaderProgramID, "materialSpecularColor");
glUniformMatrix4fv(cameraID, 1, GL_FALSE, &camera[0][0]);
glUniformMatrix4fv(modelID, 1, GL_FALSE, &model[0][0]);
glUniform3fv(cameraPositionID, 1, &cameraPosition[0]);
glUniform3fv(lightPositionID, 1, &light.position[0]);
glUniform3fv(lightIntensitiesID, 1, &light.intensities[0]);
glUniform1f(lightAttenuationID, light.attenuation);
glUniform1f(lightAmbientCoefficientID, light.ambientCoefficient);
glUniform3fv(materialColorID, 1, &assetInstance.materialColor[0]);
glUniform1f(materialShininessID, assetInstance.materialShininess);
glUniform3fv(materialSpecularColorID, 1, &assetInstance.materialSpecularColor[0]);
}
and some setup being done here:
OpenGLView::OpenGLView()
{
light.position = glm::vec3(0.0f, 7.0f, 3.0f);
light.intensities = glm::vec3(0.3f, 0.3, 0.3f);
light.attenuation = 0.3f;
light.ambientCoefficient = 0.005f;
cameraPosition = glm::vec3(5.0f, 3.0f, 8.0f);
}
For a while I had the bunny1's position set to 0, 0, 0 which caused it to not be drawn at all, I can't figure out why that is? Then when I changed it to 1, 1, 1 it started to draw, but now my key_callback function (which rotates and scales the bunny) stopped working. Also, here are my translate and scale functions:
glm::mat4 OpenGLView::translate(glm::vec3 position)
{
return glm::translate(glm::mat4(), position);
}
glm::mat4 OpenGLView::scale(glm::vec3 size)
{
return glm::scale(glm::mat4(), size);
}
and I can't figure out why changing bunny1.position seems to scale the bunny instead of translating its position?
The reason why your bunny's scale changes when changing bunny1.position is because you scale your bunny by bunny1.position:
glm::mat4 model = translate(assetInstance.position) * scale(assetInstance.position);
That might also be the reason why the bunny disapears when setting it's position to (0,0,0) since you then scale it to 0.