Start work on lighting
This commit is contained in:
21
core/Light.cpp
Normal file
21
core/Light.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "Light.h"
|
||||
|
||||
Name Light::GetType() const {return "Light";}
|
||||
|
||||
Name DirectionalLight::GetType() const {return "DirectionalLight";}
|
||||
|
||||
DirectionalLight::DirectionalLight() {
|
||||
this->direction = Vector3(0.0f);
|
||||
this->color = Vector3(0.0f);
|
||||
this->ambient = 0.0f;
|
||||
this->diffuse = 0.0f;
|
||||
this->specular = 0.0f;
|
||||
}
|
||||
|
||||
DirectionalLight::DirectionalLight(Vector3 direction, Vector3 color, cfloat ambient, cfloat diffuse, cfloat specular) {
|
||||
this->direction = direction;
|
||||
this->color = color;
|
||||
this->ambient = ambient;
|
||||
this->diffuse = diffuse;
|
||||
this->specular = specular;
|
||||
}
|
||||
22
core/Light.h
Normal file
22
core/Light.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef LIGHT_H
|
||||
#define LIGHT_H
|
||||
|
||||
#include "types.h"
|
||||
#include "Spatial.h"
|
||||
|
||||
class Light : public Spatial {
|
||||
public:
|
||||
Vector3 color;
|
||||
cfloat ambient, diffuse, specular;
|
||||
virtual Name GetType() const;
|
||||
};
|
||||
|
||||
class DirectionalLight : public Light {
|
||||
public:
|
||||
Vector3 direction;
|
||||
DirectionalLight();
|
||||
DirectionalLight(Vector3 direction, Vector3 color, cfloat ambient, cfloat diffuse, cfloat specular);
|
||||
virtual Name GetType() const;
|
||||
};
|
||||
|
||||
#endif /* LIGHT_H */
|
||||
@@ -42,4 +42,5 @@ Material::Material() {
|
||||
usesColor = false;
|
||||
usesTex = false;
|
||||
alphaScissor = 0.0f;
|
||||
unshaded = false;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ struct Material {
|
||||
Texture tex;
|
||||
bool usesTex;
|
||||
cfloat alphaScissor;
|
||||
bool unshaded;
|
||||
Material();
|
||||
};
|
||||
|
||||
|
||||
@@ -24,10 +24,12 @@ void SubMesh::SetupSubMesh() {
|
||||
// Vertex positions
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) 0);
|
||||
// Vertex UV
|
||||
// Vertex normal
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(3 * sizeof(float)));
|
||||
// TODO normals
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) (3 * sizeof(float)));
|
||||
// Vertex UV
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) (6 * sizeof(float)));
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
@@ -36,11 +38,14 @@ void SubMesh::Draw(Shader *shader) {
|
||||
shader->UpdateColor(material.usesColor, material.color);
|
||||
shader->UpdateTex(material.usesTex, material.tex);
|
||||
shader->UpdateAlphaScissor(material.alphaScissor);
|
||||
shader->UpdateUnshaded(material.unshaded);
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, indices.size() * 3, GL_UNSIGNED_INT, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
Name Mesh::GetType() const {return "Mesh";}
|
||||
|
||||
Mesh::Mesh() {}
|
||||
|
||||
Mesh::~Mesh() {
|
||||
@@ -94,8 +99,11 @@ SubMesh *Mesh::aiMesh2SubMesh(aiMesh *aimesh){
|
||||
SubMesh *sub = new SubMesh();
|
||||
for (int i = 0; i < aimesh->mNumVertices; i++) {
|
||||
aiVector3D aiPosition = aimesh->mVertices[i];
|
||||
aiVector3D aiNormal = aimesh->mNormals[i];
|
||||
aiVector3D aiUV = aimesh->mTextureCoords[0][i]; // TODO get ALL texture coords
|
||||
Vertex vertex(aiPosition.x, aiPosition.y, aiPosition.z, aiUV.x, aiUV.y);
|
||||
Vertex vertex(aiPosition.x, aiPosition.y, aiPosition.z,
|
||||
aiNormal.x, aiNormal.y, aiNormal.z,
|
||||
aiUV.x, aiUV.y);
|
||||
sub->vertices.push_back(vertex);
|
||||
}
|
||||
for (int i = 0; i < aimesh->mNumFaces; i++) {
|
||||
|
||||
@@ -43,6 +43,7 @@ public:
|
||||
static Mesh *FromFile(const char *filename);
|
||||
virtual bool IsDrawable() const {return true;}
|
||||
virtual void Draw(Shader *shader);
|
||||
virtual Name GetType() const;
|
||||
protected:
|
||||
SubMeshList submeshes;
|
||||
virtual void SetupMesh();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "Node.h"
|
||||
|
||||
Name Node::GetType() const {return "Node";}
|
||||
|
||||
Node *Node::root = {new Node()};
|
||||
Node *Node::GetRoot() {
|
||||
return root;
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
class Node; // Forwards declare
|
||||
class NodeList : public std::vector<Node*> {
|
||||
public:
|
||||
@@ -16,6 +18,7 @@ public:
|
||||
virtual bool IsDrawable() const;
|
||||
virtual void Draw(){};
|
||||
virtual bool IsTransformable() const;
|
||||
virtual Name GetType() const;
|
||||
private:
|
||||
static Node *root;
|
||||
};
|
||||
|
||||
@@ -23,9 +23,12 @@ Screen::Screen() {
|
||||
// Vertex positions
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) 0);
|
||||
//Vertex UV
|
||||
// Vertex Normals
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) (3 * sizeof(float)));
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) (3 * sizeof(float)));
|
||||
//Vertex UV
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) (6 * sizeof(float)));
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
|
||||
@@ -72,6 +72,18 @@ void Shader::UpdateTex(bool usesTex, Texture tex) {
|
||||
void Shader::UpdateAlphaScissor(cfloat alphaScissor) {
|
||||
glUniform1f(glGetUniformLocation(id, "material.alphaScissor"), alphaScissor);
|
||||
}
|
||||
void Shader::UpdateUnshaded(bool unshaded) {
|
||||
glUniform1i(glGetUniformLocation(id, "material.unshaded"), (int) unshaded);
|
||||
}
|
||||
|
||||
void Shader::UpdateDirectionalLight(DirectionalLight directionalLight) {
|
||||
glUniform3fv(glGetUniformLocation(id, "directionalLight.direction"), 1, glm::value_ptr(directionalLight.direction));
|
||||
glUniform3fv(glGetUniformLocation(id, "directionalLight.color"), 1, glm::value_ptr(directionalLight.color));
|
||||
|
||||
glUniform1f(glGetUniformLocation(id, "directionalLight.ambient"), directionalLight.ambient);
|
||||
glUniform1f(glGetUniformLocation(id, "directionalLight.diffuse"), directionalLight.diffuse);
|
||||
glUniform1f(glGetUniformLocation(id, "directionalLight.specular"), directionalLight.specular);
|
||||
}
|
||||
|
||||
Name Shader::GetName() const {
|
||||
return "Unnamed Shader";
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "types.h"
|
||||
#include "Material.h"
|
||||
#include "Light.h"
|
||||
|
||||
class Shader {
|
||||
public:
|
||||
@@ -20,6 +21,9 @@ public:
|
||||
void UpdateTex(bool usesTex);
|
||||
void UpdateTex(bool usesTex, Texture tex);
|
||||
void UpdateAlphaScissor(cfloat alphaScissor);
|
||||
void UpdateUnshaded(bool unshaded);
|
||||
|
||||
void UpdateDirectionalLight(DirectionalLight directionalLight);
|
||||
|
||||
virtual Name GetName() const;
|
||||
};
|
||||
|
||||
3
core/Spatial.cpp
Normal file
3
core/Spatial.cpp
Normal file
@@ -0,0 +1,3 @@
|
||||
#include "Spatial.h"
|
||||
|
||||
Name Spatial::GetType() const {return "Spatial";}
|
||||
@@ -8,6 +8,7 @@ class Spatial : public Node {
|
||||
public:
|
||||
Transform transform;
|
||||
virtual bool IsTransformable() const { return true;}
|
||||
virtual Name GetType() const;
|
||||
};
|
||||
|
||||
#endif /* SPATIAL_H */
|
||||
|
||||
18
core/Util.h
18
core/Util.h
@@ -5,10 +5,28 @@
|
||||
#include <string>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "Node.h"
|
||||
|
||||
namespace Util {
|
||||
void Die(const char *msg);
|
||||
void Die(const char * msg, const char * more);
|
||||
void Die(std::string msg);
|
||||
|
||||
template<class T>
|
||||
T* FindNodeByType(Node *&root, const Name &type) {
|
||||
if (root->GetType() == type) {
|
||||
return dynamic_cast<T*>(root);
|
||||
}
|
||||
|
||||
for (Node *child : root->children) {
|
||||
T* res = FindNodeByType<T>(child, type);
|
||||
if (res) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* UTIL_H */
|
||||
|
||||
@@ -4,6 +4,9 @@ Vertex::Vertex() {
|
||||
x = 0.0f;
|
||||
y = 0.0f;
|
||||
z = 0.0f;
|
||||
nx = 0.0f;
|
||||
ny = 0.0f;
|
||||
nz = 0.0f;
|
||||
u = 0.0f;
|
||||
v = 0.0f;
|
||||
}
|
||||
@@ -12,6 +15,9 @@ Vertex::Vertex(cfloat x, cfloat y, cfloat z) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
this->nz = 0.0f;
|
||||
this->ny = 0.0f;
|
||||
this->nz = 0.0f;
|
||||
this->u = 0.0f;
|
||||
this->v = 0.0f;
|
||||
}
|
||||
@@ -20,6 +26,22 @@ Vertex::Vertex(cfloat x, cfloat y, cfloat z, cfloat u, cfloat v) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
this->nx = 0.0f;
|
||||
this->ny = 0.0f;
|
||||
this->nz = 0.0f;
|
||||
this->u = u;
|
||||
this->v = v;
|
||||
}
|
||||
|
||||
Vertex::Vertex(cfloat x, cfloat y, cfloat z,
|
||||
cfloat nx, float ny, cfloat nz,
|
||||
cfloat u, cfloat v) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
this->nx = nx;
|
||||
this->ny = ny;
|
||||
this->nz = nz;
|
||||
this->u = u;
|
||||
this->v = v;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,11 @@ struct Vertex {
|
||||
Vertex();
|
||||
Vertex(cfloat x, cfloat y, cfloat z);
|
||||
Vertex(cfloat x, cfloat y, cfloat z, cfloat u, cfloat v);
|
||||
Vertex(cfloat x, cfloat y, cfloat z,
|
||||
cfloat nx, float ny, cfloat nz,
|
||||
cfloat u, cfloat v);
|
||||
cfloat x, y, z;
|
||||
cfloat nx, ny, nz;
|
||||
cfloat u, v;
|
||||
};
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#include "Util.h"
|
||||
|
||||
#include "Shaders/FlatShader.h"
|
||||
#include "Shaders/ScreenShader.h"
|
||||
|
||||
@@ -17,6 +19,7 @@
|
||||
#include "Input.h"
|
||||
#include "Node.h"
|
||||
#include "Mesh.h"
|
||||
#include "Light.h"
|
||||
#include "Scripting/Lua.h"
|
||||
|
||||
// Thirdparty Includes
|
||||
@@ -49,7 +52,6 @@ void render(Node *curr, Shader *shader, Matrix model) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
int err;
|
||||
@@ -113,6 +115,15 @@ int main() {
|
||||
view = glm::translate(view, -camera->transform.position);
|
||||
shader->UpdateView(view);
|
||||
|
||||
// Find the lights
|
||||
DirectionalLight *dirLight = Util::FindNodeByType<DirectionalLight>(root, "DirectionalLight");
|
||||
if (dirLight) {
|
||||
shader->UpdateDirectionalLight(*dirLight);
|
||||
} else {
|
||||
// No light in scene
|
||||
shader->UpdateDirectionalLight(DirectionalLight());
|
||||
}
|
||||
|
||||
// Render the scene tree
|
||||
render(root, shader, Matrix(1.0f));
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef TYPES_H
|
||||
#define TYPES_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
Reference in New Issue
Block a user