finished basic lighting chapter (look into converting everything into viewspace instead worldspace)
This commit is contained in:
parent
d474246586
commit
31db98c4c1
|
@ -15,7 +15,7 @@
|
|||
|
||||
#include "vertices.h"
|
||||
|
||||
// Continue: https://learnopengl.com/Lighting/Basic-Lighting
|
||||
// Continue: https://learnopengl.com/Lighting/Materials
|
||||
// Chapter: Not yet started
|
||||
//
|
||||
// TODO: look at project->properties->VC++ Directories-> change everything to the Environment var
|
||||
|
@ -171,8 +171,8 @@ int main()
|
|||
//Texture2D containerImage("images/container.jpg");
|
||||
//Texture2D faceImage("images/awesomeface.png", 0, GL_RGBA);
|
||||
|
||||
std::vector<float> verts(std::begin(vertices_cube), std::end(vertices_cube));
|
||||
std::vector<Nebulix::VertexAttribute> vertexAttribs = { Nebulix::VertexAttribute() };
|
||||
std::vector<float> verts(std::begin(vertices_cube_normals), std::end(vertices_cube_normals));
|
||||
std::vector<Nebulix::VertexAttribute> vertexAttribs = { Nebulix::VertexAttribute(), Nebulix::VertexAttribute() };
|
||||
Nebulix::GameObject cube(verts, vertexAttribs);
|
||||
vec3 lightPosition = glm::vec3(1.2f, 1.0f, 2.0f);
|
||||
Nebulix::GameObject lightBulb(verts, vertexAttribs, lightPosition, glm::vec3(0.2f));
|
||||
|
@ -188,7 +188,7 @@ int main()
|
|||
deltaTime = currentFrameTime - lastFrameTime;
|
||||
lastFrameTime = currentFrameTime;
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
ProcessInput(window);
|
||||
|
@ -198,15 +198,26 @@ int main()
|
|||
for (size_t i = 0; i < allShaders.size(); i++)
|
||||
{
|
||||
auto shader = allShaders[i];
|
||||
auto model = allObjects[i].GetModelMatrix();
|
||||
|
||||
if(i == 0)
|
||||
{
|
||||
float angle = 20.0f * currentFrameTime;
|
||||
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
||||
}
|
||||
|
||||
shader->Use();
|
||||
shader->SetMatrix("viewMatrix", cam.GetViewMatrix()); // more or less the camera
|
||||
shader->SetMatrix("projectionMatrix", projectionMatrix);
|
||||
shader->SetMatrix("modelMatrix", allObjects[i].GetModelMatrix());
|
||||
//shader->SetMatrix("modelMatrix", allObjects[i].GetModelMatrix());
|
||||
shader->SetMatrix("modelMatrix", model);
|
||||
}
|
||||
|
||||
lighting->Use();
|
||||
lighting->SetFloat("lightColour", 1.0f, 1.0f, 1.0f);
|
||||
lighting->SetFloat("objectColour", 1.0f, 0.5f, 0.31f);
|
||||
lighting->SetFloat("lightPosition", lightPosition);
|
||||
lighting->SetFloat("viewPosition", cam.Position);
|
||||
|
||||
for (size_t i = 0; i < allObjects.size(); i++)
|
||||
{
|
||||
|
|
|
@ -16,7 +16,7 @@ namespace Nebulix
|
|||
int NrOfElements = 3;
|
||||
int Stride;
|
||||
|
||||
VertexAttribute(GLenum attributeType = GL_FLOAT, GLboolean normalised = GL_FALSE, int nrOfElements = 3)
|
||||
VertexAttribute(int nrOfElements = 3, GLenum attributeType = GL_FLOAT, GLboolean normalised = GL_FALSE)
|
||||
: AttributeType{ attributeType }, Normalised{ normalised }, NrOfElements{ nrOfElements }
|
||||
{
|
||||
if (attributeType == GL_INT)
|
||||
|
|
|
@ -49,6 +49,10 @@ namespace Nebulix
|
|||
{
|
||||
glUniform4f(glGetUniformLocation(shaderId, name.c_str()), x, y, z, w);
|
||||
}
|
||||
void SetFloat(const std::string& name, glm::vec3 vec) const
|
||||
{
|
||||
glUniform3f(glGetUniformLocation(shaderId, name.c_str()), vec.x, vec.y, vec.z);
|
||||
}
|
||||
|
||||
void SetMatrix(const std::string& name, glm::mat4 mat) const
|
||||
{
|
||||
|
|
|
@ -2,8 +2,27 @@
|
|||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 lightColour, objectColour;
|
||||
uniform vec3 lightPosition, viewPosition;
|
||||
|
||||
in vec3 FragmentPos;
|
||||
in vec3 Normal;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(lightColour * objectColour, 1.0);
|
||||
float ambientStrength = 0.1;
|
||||
vec3 ambientLight = lightColour * ambientStrength;
|
||||
|
||||
vec3 normal = normalize(Normal);
|
||||
vec3 lightDir = normalize(lightPosition - FragmentPos);
|
||||
float diff = max(dot(normal, lightDir), 0.0);
|
||||
vec3 diffuseLight = diff * lightColour;
|
||||
|
||||
float specularStrength = 0.5;
|
||||
vec3 viewDir = normalize(viewPosition - FragmentPos);
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32); // The exponent (here 32) is the shininess of the specular highlight
|
||||
vec3 specularLight = specularStrength * spec * lightColour;
|
||||
|
||||
vec3 result = (ambientLight + diffuseLight + specularLight) * objectColour;
|
||||
FragColor = vec4(result, 1.0);
|
||||
}
|
|
@ -1,12 +1,19 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 normalVector;
|
||||
|
||||
uniform mat4 modelMatrix;
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
|
||||
out vec3 FragmentPos;
|
||||
out vec3 Normal;
|
||||
|
||||
void main()
|
||||
{
|
||||
// NOTE: inverse() is very costly to calculate, so the normal matrix should be calculated on the CPU and sent over (just like we do with the model matrix)
|
||||
Normal = mat3(transpose(inverse(modelMatrix))) * normalVector;
|
||||
FragmentPos = vec3((modelMatrix) * vec4(aPos, 1.0));
|
||||
// note that we read the multiplication from right to left
|
||||
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(aPos, 1.0);
|
||||
}
|
||||
|
|
|
@ -45,6 +45,50 @@ float vertices_cube[] = {
|
|||
-0.5f, 0.5f, -0.5f
|
||||
};
|
||||
|
||||
float vertices_cube_normals[] = { // object coordinates; normal vector
|
||||
-0.5f, -0.5f, -0.5f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1.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
|
||||
};
|
||||
|
||||
float vertices_container[] = { // object coordinates, uv-coordinates
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
|
||||
|
|
Loading…
Reference in New Issue
Block a user