PDA

View Full Version : My Drawing Has Weird Lines



LarrySellers
10-14-2014, 05:06 PM
Hey guys,

I'm drawing a bunny with Phong lighting (so far I have only implemented the diffuse and the ambient) and my bunny has some weird graphics stuff going on. I'm wondering if it might have something to do with my glEnables?

Anyways, here is what my bunny looks like, maybe someone know what I did wrong?

1480
http://imgur.com/kOu9dt4,dAbMH6w,74Mvp8z#0
1481
http://imgur.com/kOu9dt4,dAbMH6w,74Mvp8z#1
1482
http://imgur.com/kOu9dt4,dAbMH6w,74Mvp8z#2

Here is my source code:

main method


#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include "glm\glm.hpp"
#include <iostream>
#include "housekeeping.h"
#include "shader.h"
#include "glm\gtc\matrix_transform.hpp"
#include "controls.h"
#include "objloader.h"

#define WINDOW_WIDTH 600
#define WINDOW_HEIGHT 480
#define FILE_PATH "bunny.obj"


int main()
{
std::vector<glm::vec3> vertices, normals;
std::vector<GLuint> elements;

loadObjSimple(FILE_PATH, vertices, normals, elements);

GLFWwindow* window = initializeAndSetupWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Triangle");

GLuint programID = LoadShaders("vertexShader.vert", "fragmentShader.frag");

GLuint vertexArray;
glGenVertexArrays(1, &vertexArray);
glBindVertexArray(vertexArray);

GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);

GLuint normalBuffer;
glGenBuffers(1, &normalBuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);

GLuint elementbuffer;
glGenBuffers(1, &elementbuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(unsigned int), &elements[0], GL_STATIC_DRAW);

GLuint ModelID = glGetUniformLocation(programID, "Model");
GLuint LightID = glGetUniformLocation(programID, "lightPosition_worldSpace");

GLuint MatrixID = glGetUniformLocation(programID, "MVP");

do{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
glm::mat4 Model = getModelMatrix();
glm::mat4 View = getViewMatrix();
glm::mat4 MVP = Projection * View * Model;

glm::vec3 lightPosition(5.0, 5.0, 3.0);

glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
glUniformMatrix4fv(ModelID, 1, GL_FALSE, &Model[0][0]);
glUniform3f(LightID, lightPosition.x, lightPosition.y, lightPosition.z);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);

glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);

glDrawElements(GL_TRIANGLES, elements.size(), GL_UNSIGNED_INT, (void*)0);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

glfwSwapBuffers(window);
glfwPollEvents();
} while (!glfwWindowShouldClose(window));

glfwDestroyWindow(window);
glfwTerminate();
return 0;
}


vertex shader


#version 330 core

//Inputs
layout(location = 0) in vec3 vertexPosition_modelSpace;
layout(location = 1) in vec3 vertexNormal_modelSpace;

//Uniforms
uniform mat4 MVP;
uniform mat4 Model;
uniform vec3 lightPosition_worldSpace;

//Outputs
out vec3 fragNormal;
out vec3 fragVertex;
out mat4 model;
out vec3 fragLightPosition;

void main()
{
gl_Position = MVP * vec4(vertexPosition_modelSpace, 1.0);
fragNormal = vertexNormal_modelSpace;
model = Model;
fragVertex = vertexPosition_modelSpace;
fragLightPosition = lightPosition_worldSpace;

}


Fragment Shader


#version 330 core

in vec3 fragNormal;
in vec3 fragVertex;
in vec3 fragLightPosition;
in mat4 model;

out vec3 finalColor;

void main()
{
//Define ambient, diffuse, and specular colors
vec3 lightColor = vec3(0.5, 0.0, 0.8);
float ambientCoefficient = 0.2;

//Calculate ambient light
vec3 ambient = lightColor * ambientCoefficient;

//End calculating ambient light


//Calculae diffuse

//Get normal in world coordinates
mat3 normalMatrix = transpose(inverse(mat3(model)));
vec3 normal = normalize(normalMatrix * fragNormal);

//Calculate position of fragment in world coordinates
vec3 position = vec3(model * vec4(fragVertex, 1.0));

//Calculate surface to light vector
vec3 surfaceToLight = fragLightPosition - position;

//Calculate cos(AoI)
float diffuseCoefficient = max(0.0, dot(normal, surfaceToLight) / length(surfaceToLight) * length(normal));

vec3 diffuse = diffuseCoefficient * lightColor;

//End calculating diffuse

finalColor = diffuse + ambient;
}


Thanks!

LarrySellers
10-14-2014, 05:47 PM
Nevermind, I forgot to enable depth test and set gl depth function to depth_less.

Whoops :-/