removed resourcebase, added a shader for untextured phong materials
This commit is contained in:
parent
b9dc55d0cf
commit
3b1e1be464
@ -6,7 +6,7 @@
|
||||
#include <glm/vec4.hpp>
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include "material.h"
|
||||
#include "resourcebase.h"
|
||||
#include "asciimodule.h"
|
||||
|
||||
class Texture;
|
||||
|
||||
@ -26,7 +26,7 @@ public:
|
||||
backColor(myBackColor),
|
||||
fontColor(myFontColor)
|
||||
{
|
||||
shader = ResourceBase::getShader("ascii");
|
||||
shader = ASCIIModule::getShader();
|
||||
}
|
||||
|
||||
virtual void bindAttributes();
|
||||
@ -34,16 +34,17 @@ public:
|
||||
|
||||
class ASCIIEntity : public Entity
|
||||
{
|
||||
GLuint rows;
|
||||
GLuint columns;
|
||||
char* textBuffer;
|
||||
|
||||
glm::vec2 position;
|
||||
glm::vec2 size;
|
||||
glm::mat4 modelView;
|
||||
|
||||
ASCIIMaterial* mat;
|
||||
|
||||
GLuint rows;
|
||||
GLuint columns;
|
||||
|
||||
char* textBuffer;
|
||||
|
||||
void updateModelView();
|
||||
|
||||
public:
|
||||
|
@ -1,5 +1,7 @@
|
||||
#include "asciimodule.h"
|
||||
|
||||
Shader* ASCIIModule::shader;
|
||||
|
||||
ASCIIModule::ASCIIModule()
|
||||
{
|
||||
|
||||
@ -9,3 +11,13 @@ void ASCIIModule::renderGL(Camera* myCamera)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Shader* ASCIIModule::getShader()
|
||||
{
|
||||
return shader;
|
||||
}
|
||||
|
||||
void ASCIIModule::setShader(Shader* myShader)
|
||||
{
|
||||
shader = myShader;
|
||||
}
|
||||
|
@ -9,14 +9,16 @@ class Shader;
|
||||
|
||||
class ASCIIModule : public Module
|
||||
{
|
||||
Shader* shader;
|
||||
static Shader* shader;
|
||||
std::vector<ASCIIEntity*> entities;
|
||||
|
||||
|
||||
public:
|
||||
ASCIIModule();
|
||||
void addEntity(ASCIIEntity* myEntity);
|
||||
virtual void renderGL(Camera* myCamera);
|
||||
|
||||
static Shader* getShader();
|
||||
static void setShader(Shader* myShader);
|
||||
};
|
||||
|
||||
#endif // ASCIIMODULE_H
|
||||
|
2
camera.h
2
camera.h
@ -10,7 +10,7 @@ public:
|
||||
virtual glm::mat4 getProjectionMatrix() = 0;
|
||||
virtual glm::mat4 getViewMatrix() = 0;
|
||||
|
||||
virtual void resize(int width, int height) = 0;
|
||||
virtual void resize(int width, int height) = 0;
|
||||
};
|
||||
|
||||
#endif // CAMERA_H
|
||||
|
45
phongcolor.frag
Normal file
45
phongcolor.frag
Normal file
@ -0,0 +1,45 @@
|
||||
#version 330 core
|
||||
|
||||
// material
|
||||
uniform vec3 materialKd;
|
||||
uniform vec3 materialKs;
|
||||
uniform float materialNs;
|
||||
|
||||
uniform vec3 dirLight[2];
|
||||
uniform int nbPointLights;
|
||||
uniform vec3 pointLights[8];
|
||||
|
||||
// fragment
|
||||
in vec3 varNormal;
|
||||
in vec2 varTexCoord;
|
||||
|
||||
in vec3 lightDirInView[5];
|
||||
in vec3 halfVecInView[5];
|
||||
|
||||
// resultat
|
||||
layout(location = 0)out vec4 outColor;
|
||||
|
||||
// --------------------
|
||||
|
||||
vec3 computeLight(in vec3 kd, in vec3 ks, in float ns, in vec3 color, in vec3 normal, in vec3 lightDir, in vec3 halfVec){
|
||||
float diffuse = 0;
|
||||
float specular = 0;
|
||||
|
||||
diffuse = dot(normal, lightDir);
|
||||
diffuse = diffuse < 0 ? 0 : diffuse;
|
||||
specular = dot(halfVec, normal);
|
||||
specular = specular < 0 ? 0 : specular;
|
||||
|
||||
return color*diffuse*(kd+ks*pow(specular, ns));
|
||||
}
|
||||
|
||||
void main(void) {
|
||||
int i;
|
||||
vec3 kd = materialKd;
|
||||
vec3 light = 0.1*kd + computeLight(kd, materialKs, materialNs, dirLight[1], varNormal, lightDirInView[0], halfVecInView[0]);
|
||||
for(i=1; i<nbPointLights+1; ++i)
|
||||
light += computeLight(kd, materialKs, materialNs, pointLights[i*2 -1], varNormal, lightDirInView[i], halfVecInView[i]);
|
||||
|
||||
outColor = vec4(light, 1);
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ PhongEntity::~PhongEntity()
|
||||
delete[] vbo;
|
||||
}
|
||||
|
||||
void PhongEntity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix)
|
||||
void PhongEntity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix, Lights::Light* dirLight, Lights* pointLights)
|
||||
{
|
||||
glm::mat4 modelViewMatrix = viewMatrix * modelMatrix;
|
||||
glm::mat4 mvp = projectionMatrix * modelViewMatrix;
|
||||
@ -29,6 +29,8 @@ void PhongEntity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMat
|
||||
shader->bindMatrix(shader->getLocation("modelViewMatrix"), modelViewMatrix);
|
||||
shader->bindMatrix(shader->getLocation("normalMatrix"), normalMatrix);
|
||||
shader->bindMatrix(shader->getLocation("MVP"), mvp);
|
||||
shader->bindVec3Array(shader->getLocation("dirLight"), (glm::vec3*)dirLight, 2);
|
||||
pointLights->bind(shader->getLocation("pointLights"), shader->getLocation("nbPointLights"), shader);
|
||||
drawGroup(i);
|
||||
}
|
||||
}
|
||||
|
@ -4,14 +4,13 @@
|
||||
#include <glew/glew.h>
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include <vector>
|
||||
#include "lights.h"
|
||||
|
||||
class Mesh;
|
||||
class Material;
|
||||
class Shader;
|
||||
|
||||
#include "entity.h"
|
||||
|
||||
class PhongEntity : public Entity
|
||||
class PhongEntity
|
||||
{
|
||||
protected:
|
||||
enum {
|
||||
@ -36,7 +35,7 @@ public:
|
||||
PhongEntity(Mesh* myMesh);
|
||||
~PhongEntity();
|
||||
|
||||
virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix);
|
||||
void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix, Lights::Light* dirLight, Lights* pointLights);
|
||||
|
||||
void initGL(bool isDynamic = false);
|
||||
void destroyGL();
|
||||
|
@ -1,22 +1,29 @@
|
||||
#include "phongmaterial.h"
|
||||
#include "texture.h"
|
||||
#include "phongmodule.h"
|
||||
|
||||
#define TEX_ID 0
|
||||
|
||||
void PhongMaterial::updateShader()
|
||||
{
|
||||
shader = PhongModule::getShader(diffuse_texture == NULL ? PhongModule::PHONG_COLOR : PhongModule::PHONG_TEXTURE);
|
||||
}
|
||||
|
||||
void PhongMaterial::bindAttributes()
|
||||
{
|
||||
shader->bind();
|
||||
shader->bindVec3(shader->getLocation("materialKd"), kd);
|
||||
shader->bindVec3(shader->getLocation("materialKs"), ks);
|
||||
shader->bindFloat(shader->getLocation("materialNs"), ns);
|
||||
if(tex != NULL)
|
||||
if(diffuse_texture != NULL)
|
||||
{
|
||||
tex->bind(TEX_ID);
|
||||
diffuse_texture->bind(TEX_ID);
|
||||
shader->bindInteger(shader->getLocation("baseTexture"), TEX_ID);
|
||||
}
|
||||
}
|
||||
|
||||
void PhongMaterial::setTexture(Texture* myTexture)
|
||||
{
|
||||
tex = myTexture;
|
||||
diffuse_texture = myTexture;
|
||||
updateShader();
|
||||
}
|
||||
|
@ -3,7 +3,6 @@
|
||||
|
||||
#include "material.h"
|
||||
#include "glm/vec3.hpp"
|
||||
#include "resourcebase.h"
|
||||
|
||||
class Texture;
|
||||
|
||||
@ -13,19 +12,22 @@ public:
|
||||
glm::vec3 kd;
|
||||
glm::vec3 ks;
|
||||
float ns;
|
||||
Texture* tex;
|
||||
Texture* diffuse_texture;
|
||||
Texture* normal_map;
|
||||
|
||||
PhongMaterial() : kd(0.5f), ks(0.5f), ns(10), tex(NULL)
|
||||
PhongMaterial() : kd(0.5f), ks(0.5f), ns(10), diffuse_texture(NULL)
|
||||
{
|
||||
shader = ResourceBase::getShader("phong");
|
||||
updateShader();
|
||||
}
|
||||
PhongMaterial(glm::vec3 myKd, glm::vec3 myKs, float myNs) : kd(myKd), ks(myKs), ns(myNs), tex(NULL)
|
||||
PhongMaterial(glm::vec3 myKd, glm::vec3 myKs, float myNs) : kd(myKd), ks(myKs), ns(myNs), diffuse_texture(NULL)
|
||||
{
|
||||
shader = ResourceBase::getShader("phong");
|
||||
updateShader();
|
||||
}
|
||||
virtual void bindAttributes();
|
||||
|
||||
void setTexture(Texture* myTexture);
|
||||
|
||||
void updateShader();
|
||||
};
|
||||
|
||||
#endif // PHONGMATERIAL_H
|
||||
|
@ -1,18 +1,17 @@
|
||||
#include "phongmodule.h"
|
||||
#include "lights.h"
|
||||
#include "shader.h"
|
||||
#include "resourcebase.h"
|
||||
#include "phongentity.h"
|
||||
#include "camera.h"
|
||||
#include "mesh.h"
|
||||
|
||||
Shader* PhongModule::shaders[NB_SHADERS] = {0, 0};
|
||||
|
||||
PhongModule::PhongModule(Lights::Light* myDirLight, Lights* myPointLights) :
|
||||
dirLight(myDirLight),
|
||||
pointLights(myPointLights),
|
||||
shader(ResourceBase::getShader("phong"))
|
||||
pointLights(myPointLights)
|
||||
{
|
||||
dirLightLocation = shader->getLocation("dirLight");
|
||||
nbPointLightsLocation = shader->getLocation("nbPointLights");
|
||||
pointLightsLocation = shader->getLocation("pointLights");
|
||||
|
||||
}
|
||||
|
||||
void PhongModule::addEntity(PhongEntity* myEntity)
|
||||
@ -27,9 +26,16 @@ void PhongModule::clearEntities()
|
||||
|
||||
void PhongModule::renderGL(Camera* myCamera)
|
||||
{
|
||||
shader->bind();
|
||||
shader->bindVec3Array(dirLightLocation, (glm::vec3*)dirLight, 2);
|
||||
pointLights->bind(pointLightsLocation, nbPointLightsLocation, shader);
|
||||
for(PhongEntity* e : entities)
|
||||
e->draw(myCamera->getViewMatrix(), myCamera->getProjectionMatrix());
|
||||
e->draw(myCamera->getViewMatrix(), myCamera->getProjectionMatrix(), dirLight, pointLights);
|
||||
}
|
||||
|
||||
void PhongModule::setShader(ShaderType slot, Shader* myShader)
|
||||
{
|
||||
shaders[slot] = myShader;
|
||||
}
|
||||
|
||||
Shader* PhongModule::getShader(ShaderType slot)
|
||||
{
|
||||
return shaders[slot];
|
||||
}
|
||||
|
@ -12,23 +12,27 @@ class PhongEntity;
|
||||
|
||||
class PhongModule : public Module
|
||||
{
|
||||
private:
|
||||
Lights::Light* dirLight;
|
||||
Lights* pointLights;
|
||||
GLuint dirLightLocation;
|
||||
GLuint nbPointLightsLocation;
|
||||
GLuint pointLightsLocation;
|
||||
|
||||
Shader* shader;
|
||||
std::vector<PhongEntity*> entities;
|
||||
|
||||
public:
|
||||
enum ShaderType {PHONG_COLOR, PHONG_TEXTURE, NB_SHADERS};
|
||||
|
||||
PhongModule(Lights::Light* myDirLight, Lights* myPointLights);
|
||||
|
||||
void addEntity(PhongEntity* myEntity);
|
||||
void clearEntities();
|
||||
|
||||
void virtual renderGL(Camera* myCamera);
|
||||
|
||||
static void setShader(ShaderType slot, Shader* myShader);
|
||||
static Shader* getShader(ShaderType slot);
|
||||
private:
|
||||
Lights::Light* dirLight;
|
||||
Lights* pointLights;
|
||||
GLuint dirLightLocation;
|
||||
GLuint nbPointLightsLocation;
|
||||
GLuint pointLightsLocation;
|
||||
std::vector<PhongEntity*> entities;
|
||||
|
||||
static Shader* shaders[NB_SHADERS];
|
||||
};
|
||||
|
||||
#endif // PHONGMODULE_H
|
||||
|
@ -1,80 +0,0 @@
|
||||
#include "resourcebase.h"
|
||||
|
||||
ResourceBase::DataBase<Texture> ResourceBase::textures;
|
||||
ResourceBase::DataBase<Mesh> ResourceBase::meshes;
|
||||
ResourceBase::DataBase<Material> ResourceBase::materials;
|
||||
ResourceBase::DataBase<Shader> ResourceBase::shaders;
|
||||
ResourceBase::DataBase<PhongEntity> ResourceBase::entities;
|
||||
ResourceBase::DataBase<Lights> ResourceBase::lights;
|
||||
|
||||
void ResourceBase::setTexture(const std::string &textureName, Texture* myTexture)
|
||||
{
|
||||
textures.add(textureName, myTexture);
|
||||
}
|
||||
|
||||
void ResourceBase::setMesh(const std::string &meshName, Mesh* myMesh )
|
||||
{
|
||||
meshes.add(meshName, myMesh);
|
||||
}
|
||||
|
||||
void ResourceBase::setMaterial(const std::string &materialName, Material* myMaterial)
|
||||
{
|
||||
materials.add(materialName, myMaterial);
|
||||
}
|
||||
|
||||
void ResourceBase::setShader(const std::string &shaderName, Shader* myShader)
|
||||
{
|
||||
shaders.add(shaderName, myShader);
|
||||
}
|
||||
|
||||
void ResourceBase::setEntity(const std::string &entityName, PhongEntity* myEntity)
|
||||
{
|
||||
entities.add(entityName, myEntity);
|
||||
}
|
||||
|
||||
void ResourceBase::setLights(const std::string &lightsName, Lights* myLights)
|
||||
{
|
||||
lights.add(lightsName, myLights);
|
||||
}
|
||||
|
||||
|
||||
Texture* ResourceBase::getTexture(const std::string &textureName)
|
||||
{
|
||||
return textures.get(textureName);
|
||||
}
|
||||
|
||||
Mesh* ResourceBase::getMesh(const std::string &meshName)
|
||||
{
|
||||
return meshes.get(meshName);
|
||||
}
|
||||
|
||||
Material* ResourceBase::getMaterial(const std::string &materialName)
|
||||
{
|
||||
return materials.get(materialName);
|
||||
}
|
||||
|
||||
Shader* ResourceBase::getShader(const std::string &shaderName)
|
||||
{
|
||||
return shaders.get(shaderName);
|
||||
}
|
||||
|
||||
PhongEntity* ResourceBase::getEntity(const std::string &entityName)
|
||||
{
|
||||
return entities.get(entityName);
|
||||
}
|
||||
|
||||
Lights* ResourceBase::getLights(const std::string &lightsName)
|
||||
{
|
||||
return lights.get(lightsName);
|
||||
}
|
||||
|
||||
|
||||
std::string ResourceBase::getMaterialName(Material* myMat)
|
||||
{
|
||||
return materials.getName(myMat);
|
||||
}
|
||||
|
||||
std::string ResourceBase::getTextureName(Texture* myTex)
|
||||
{
|
||||
return textures.getName(myTex);
|
||||
}
|
@ -1,82 +0,0 @@
|
||||
#ifndef RESOURCEBASE_H
|
||||
#define RESOURCEBASE_H
|
||||
|
||||
class Texture;
|
||||
class Mesh;
|
||||
class Material;
|
||||
class Shader;
|
||||
class PhongEntity;
|
||||
class Lights;
|
||||
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stdio.h>
|
||||
|
||||
class ResourceBase
|
||||
{
|
||||
public:
|
||||
enum {TEXTURES, MESHES, MATERIALS, SHADERS, ENTITIES};
|
||||
|
||||
static void setTexture(const std::string &textureName, Texture* myTexture);
|
||||
static void setMesh(const std::string &meshName, Mesh* myMesh);
|
||||
static void setMaterial(const std::string &materialName, Material* myMaterial);
|
||||
static void setShader(const std::string &shaderName, Shader* myShader);
|
||||
static void setEntity(const std::string &entityName, PhongEntity* myEntity);
|
||||
static void setLights(const std::string &lightsName, Lights* myLights);
|
||||
|
||||
static Texture* getTexture(const std::string &textureName);
|
||||
static Mesh* getMesh(const std::string &meshName);
|
||||
static Material* getMaterial(const std::string &materialName);
|
||||
static Shader* getShader(const std::string &shaderName);
|
||||
static PhongEntity* getEntity(const std::string &entityName);
|
||||
static Lights* getLights(const std::string &lightsName);
|
||||
|
||||
static std::string getMaterialName(Material* myMat);
|
||||
static std::string getTextureName(Texture* myTex);
|
||||
|
||||
protected:
|
||||
template <typename T>
|
||||
class DataBase
|
||||
{
|
||||
public:
|
||||
std::vector<std::string> names;
|
||||
std::unordered_map<std::string, T*> data;
|
||||
|
||||
void add(const std::string &name, T* t)
|
||||
{
|
||||
data[name] = t;
|
||||
names.push_back(name);
|
||||
}
|
||||
|
||||
T* get(const std::string &name)
|
||||
{
|
||||
if(data.count(name))
|
||||
return data[name];
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Requesting unloaded resource : %s\n", name.c_str());
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
std::string getName(T* ptr)
|
||||
{
|
||||
for(const std::string &n : names)
|
||||
{
|
||||
if(ptr == get(n))
|
||||
return n;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
};
|
||||
|
||||
static DataBase<Texture> textures;
|
||||
static DataBase<Mesh> meshes;
|
||||
static DataBase<Material> materials;
|
||||
static DataBase<Shader> shaders;
|
||||
static DataBase<PhongEntity> entities;
|
||||
static DataBase<Lights> lights;
|
||||
};
|
||||
|
||||
#endif // RESOURCEBASE_H
|
@ -29,8 +29,7 @@ SOURCES += shader.cpp \
|
||||
phongmaterial.cpp \
|
||||
sphere.cpp \
|
||||
lights.cpp \
|
||||
sparrowrenderer.cpp \
|
||||
resourcebase.cpp \
|
||||
sparrowrenderer.cpp \
|
||||
phongmodule.cpp \
|
||||
skyboxmodule.cpp \
|
||||
framebuffer.cpp \
|
||||
@ -49,8 +48,7 @@ HEADERS += shader.h \
|
||||
phongmaterial.h \
|
||||
sphere.h \
|
||||
lights.h \
|
||||
sparrowrenderer.h \
|
||||
resourcebase.h \
|
||||
sparrowrenderer.h \
|
||||
phongmodule.h \
|
||||
skyboxmodule.h \
|
||||
module.h \
|
||||
|
@ -79,3 +79,4 @@ Camera* SparrowRenderer::getCamera()
|
||||
return camera;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user