Commit f089b23a authored by Sadman Kazi's avatar Sadman Kazi 🎸

Add normals and basic shadows

parent 89b02001
Pipeline #121 passed with stage
in 2 minutes and 22 seconds
#version 330
in vec2 texCoord0;
in vec3 normal0;
out vec4 fragColor;
uniform sampler2D u_diffuse;
uniform vec3 u_ambientLight;
void main () {
fragColor = texture2D(u_diffuse, texCoord0.xy);
vec4 totalLight = vec4(u_ambientLight, 1);
fragColor = texture2D(u_diffuse, texCoord0.xy) * clamp(dot(-vec3(0,0,1), normal0), 0.0, 1.0);
}
......@@ -2,12 +2,15 @@
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 u_transform;
void main () {
gl_Position = u_transform * vec4(1.5*position, 1.0);
texCoord0 = texCoord;
normal0 = (u_transform * vec4(normal, 0.0)).xyz;
}
......@@ -8,22 +8,24 @@ Mesh::Mesh(const std::string& fileName) {
Mesh::Mesh(Vertex* vertices, unsigned int numVertices, unsigned int* indices, unsigned int numIndices)
{
m_drawCount = numVertices;
//binding the vertex array object
m_drawCount = numVertices; //binding the vertex array object
glGenVertexArrays(1, &m_vertexArrayObject);
glBindVertexArray(m_vertexArrayObject);
// retrieve the data from the vertices
std::vector<glm::vec3*> posCoords;
std::vector<glm::vec2*> texCoords;
std::vector<glm::vec3*> normals;
posCoords.reserve(numVertices);
texCoords.reserve(numVertices);
normals.reserve(numVertices);
for (unsigned int i = 0; i < numVertices; i++) {
posCoords.push_back(vertices[i].getPos());
texCoords.push_back(vertices[i].getTexCoord());
normals.push_back(vertices[i].getNormal());
}
//writing it to the GPU using buffers
......@@ -101,7 +103,6 @@ void Mesh::InitMesh(const IndexedModel& model) {
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
/*
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[NORMAL_VB]);
//take data and put it in the buffer, from program to GPU memory
glBufferData(GL_ARRAY_BUFFER, model.normals.size() * sizeof(model.normals[0]), &model.normals[0], GL_STATIC_DRAW); //static draw means that the data is not gonna change
......@@ -109,7 +110,6 @@ void Mesh::InitMesh(const IndexedModel& model) {
//need a vertex attribute for every vertex member, tells where in the sequence to look at in the GPU memory
glEnableVertexAttribArray(2); //look at it as an array
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
*/
//get some buffers to work with
glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
......
......@@ -32,6 +32,7 @@ private:
POSITION_VB,
INDEX_VB,
TEXCOORD_VB,
NORMAL_VB,
NUM_BUFFERS
};
......
......@@ -6,15 +6,18 @@
class Vertex
{
public:
Vertex(const glm::vec3& pos, const glm::vec2& texCoord) :
Vertex(const glm::vec3& pos, const glm::vec2& texCoord, const glm::vec3& normal = glm::vec3(0, 0, 0)) :
m_pos(pos),
m_texCoord(texCoord) {}
m_texCoord(texCoord),
m_normal(normal) {}
inline glm::vec3* getPos() { return &m_pos; }
inline glm::vec2* getTexCoord() { return &m_texCoord; }
inline glm::vec3* getNormal() { return &m_normal; }
private:
glm::vec3 m_pos;
glm::vec2 m_texCoord;
glm::vec3 m_normal;
};
#endif // VERTEX_H_
......@@ -71,9 +71,8 @@ void Window::initGraphics(float r, float g, float b, float a) {
glClearColor(r, g, b, a);
//enable back face culling
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
//gpu will know which vertex is at the top (z value)
glEnable(GL_DEPTH_TEST);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment