removed all deprecated classes and methods, added crappyModule for opengl 2.1 rendering
This commit is contained in:
parent
a00d1ee777
commit
b4aa0acfda
@ -14,11 +14,10 @@ endif(WIN32)
|
||||
set(LIB_SRC_LIST
|
||||
framebuffer.cpp
|
||||
gbuffer.cpp
|
||||
lights.cpp
|
||||
meshbuilder.cpp
|
||||
phongentity.cpp
|
||||
phongmaterial.cpp
|
||||
phongmodule.cpp
|
||||
crappymodule.cpp
|
||||
shader.cpp
|
||||
skyboxmodule.cpp
|
||||
sparrowrenderer.cpp
|
||||
|
@ -1,27 +0,0 @@
|
||||
#include "asciientity.h"
|
||||
#include <glm/ext.hpp>
|
||||
#include "texture.h"
|
||||
|
||||
#define TEX_ID 0
|
||||
|
||||
void ASCIIMaterial::bindAttributes()
|
||||
{
|
||||
shader->bindVec4(shader->getLocation("backColor"), backColor);
|
||||
shader->bindVec4(shader->getLocation("fontColor"), fontColor);
|
||||
if(glyphMap != NULL)
|
||||
{
|
||||
glyphMap->bind(TEX_ID);
|
||||
shader->bindInteger(shader->getLocation("glyphMap"), TEX_ID);
|
||||
}
|
||||
}
|
||||
|
||||
void ASCIIEntity::updateModelView()
|
||||
{
|
||||
modelView = glm::translate(glm::mat4(), glm::vec3(position, 0));
|
||||
modelView = glm::scale(modelView, glm::vec3(size, 1));
|
||||
}
|
||||
|
||||
void ASCIIEntity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix)
|
||||
{
|
||||
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
#ifndef ASCIIENTITY_H
|
||||
#define ASCIIENTITY_H
|
||||
|
||||
#include <glew/glew.h>
|
||||
#include <glm/vec2.hpp>
|
||||
#include <glm/vec4.hpp>
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include "material.h"
|
||||
#include "asciimodule.h"
|
||||
|
||||
class Texture;
|
||||
|
||||
#include "entity.h"
|
||||
|
||||
class ASCIIMaterial : public Material
|
||||
{
|
||||
Texture* glyphMap;
|
||||
glm::vec4 backColor;
|
||||
glm::vec4 fontColor;
|
||||
|
||||
public:
|
||||
ASCIIMaterial(Texture* myGlyphMap = NULL,
|
||||
glm::vec4 myBackColor = glm::vec4(1, 1, 1, 0),
|
||||
glm::vec4 myFontColor = glm::vec4(0)) :
|
||||
glyphMap(myGlyphMap),
|
||||
backColor(myBackColor),
|
||||
fontColor(myFontColor)
|
||||
{
|
||||
shader = ASCIIModule::getShader();
|
||||
}
|
||||
|
||||
virtual void bindAttributes();
|
||||
};
|
||||
|
||||
class ASCIIEntity : public Entity
|
||||
{
|
||||
glm::vec2 position;
|
||||
glm::vec2 size;
|
||||
glm::mat4 modelView;
|
||||
|
||||
ASCIIMaterial* mat;
|
||||
|
||||
GLuint rows;
|
||||
GLuint columns;
|
||||
|
||||
char* textBuffer;
|
||||
|
||||
void updateModelView();
|
||||
|
||||
public:
|
||||
ASCIIEntity(int nbRows = 10, int nbColumns = 40, ASCIIMaterial* myMat = NULL) :
|
||||
position(glm::vec2(0)),
|
||||
size(glm::vec2(1)),
|
||||
mat(myMat),
|
||||
rows(nbRows),
|
||||
columns(nbColumns)
|
||||
{
|
||||
textBuffer = new char[rows*columns];
|
||||
// TODO set all chars to ' '
|
||||
updateModelView();
|
||||
}
|
||||
|
||||
virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix);
|
||||
};
|
||||
|
||||
#endif // ASCIIENTITY_H
|
@ -1,23 +0,0 @@
|
||||
#include "asciimodule.h"
|
||||
|
||||
Shader* ASCIIModule::shader;
|
||||
|
||||
ASCIIModule::ASCIIModule()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ASCIIModule::renderGL(Camera* myCamera)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Shader* ASCIIModule::getShader()
|
||||
{
|
||||
return shader;
|
||||
}
|
||||
|
||||
void ASCIIModule::setShader(Shader* myShader)
|
||||
{
|
||||
shader = myShader;
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
#ifndef ASCIIMODULE_H
|
||||
#define ASCIIMODULE_H
|
||||
|
||||
#include "module.h"
|
||||
#include <vector>
|
||||
|
||||
class ASCIIEntity;
|
||||
class Shader;
|
||||
|
||||
class ASCIIModule : public Module
|
||||
{
|
||||
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
|
55
crappymodule.cpp
Normal file
55
crappymodule.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
#include "crappymodule.h"
|
||||
#include "shader.h"
|
||||
#include "phongentity.h"
|
||||
#include "camera.h"
|
||||
#include "mesh.h"
|
||||
#include "sparrowrenderer.h"
|
||||
#include "scene.h"
|
||||
#include "glassert.h"
|
||||
#include <glm/ext.hpp>
|
||||
|
||||
void CrappyModule::renderGL(Camera* myCamera, Scene* scene)
|
||||
{
|
||||
glAssert(glEnable(GL_LIGHTING));
|
||||
glAssert(glClearColor(0, 0.1f, 0.05f, 1.0));
|
||||
glAssert(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
|
||||
glLoadIdentity();
|
||||
glAssert(glEnable(GL_LIGHT0));
|
||||
glAssert(glLightfv(GL_LIGHT0, GL_AMBIENT, glm::value_ptr(glm::vec4(glm::vec3(0.1f), 1))));
|
||||
glAssert(glLightfv(GL_LIGHT0, GL_POSITION, glm::value_ptr(glm::vec4(0))));
|
||||
glAssert(glLightfv(GL_LIGHT0, GL_DIFFUSE, glm::value_ptr(glm::vec4(0, 0, 0, 1))));
|
||||
glAssert(glLightfv(GL_LIGHT0, GL_SPECULAR, glm::value_ptr(glm::vec4(0, 0, 0, 1))));
|
||||
|
||||
int i=1;
|
||||
for(SceneIterator<Light*>* lightIt = scene->getLights();
|
||||
lightIt->isValid(); lightIt->next())
|
||||
{
|
||||
Light* l = lightIt->getItem();
|
||||
glAssert(glEnable(GL_LIGHT0 + i));
|
||||
glAssert(glLightfv(GL_LIGHT0 + i, GL_AMBIENT, glm::value_ptr(glm::vec4(glm::vec3(0), 1))));
|
||||
if(l->isDirectionnal()){
|
||||
glAssert(glLightfv(GL_LIGHT0 + i, GL_POSITION, glm::value_ptr(glm::vec4(l->getDir(), 0))));
|
||||
}else{
|
||||
glAssert(glLightfv(GL_LIGHT0 + i, GL_POSITION, glm::value_ptr(glm::vec4(l->getPos(), 1))));
|
||||
}
|
||||
glAssert(glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, glm::value_ptr(glm::vec4(l->getColor(), 1))));
|
||||
glAssert(glLightfv(GL_LIGHT0 + i, GL_SPECULAR, glm::value_ptr(glm::vec4(l->getColor(), 1))));
|
||||
++i;
|
||||
}
|
||||
|
||||
for(SceneIterator<PhongEntity*>* entityIt = scene->getGeometry();
|
||||
entityIt->isValid(); entityIt->next())
|
||||
{
|
||||
PhongEntity* entity = entityIt->getItem();
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glm::mat4 modelViewMatrix = myCamera->getViewMatrix() * entity->modelMatrix;
|
||||
glLoadMatrixf(glm::value_ptr(modelViewMatrix));
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadMatrixf(glm::value_ptr(myCamera->getProjectionMatrix()));
|
||||
entity->drawDisplayList();
|
||||
}
|
||||
|
||||
glAssert(glDisable(GL_LIGHTING));
|
||||
for(int j=0; j<i; ++j)
|
||||
glAssert(glDisable(GL_LIGHTING));
|
||||
}
|
15
crappymodule.h
Normal file
15
crappymodule.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef CRAPPYMODULE_H
|
||||
#define CRAPPYMODULE_H
|
||||
|
||||
#include "module.h"
|
||||
#include <cstddef>
|
||||
#include <glew/glew.h>
|
||||
|
||||
class CrappyModule : public Module
|
||||
{
|
||||
public:
|
||||
virtual void renderGL(Camera* myCamera, Scene* scene = NULL);
|
||||
virtual bool requiresModernOpenGL() {return false;}
|
||||
};
|
||||
|
||||
#endif // CRAPPYMODULE_H
|
@ -5,7 +5,6 @@
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
#include <glew/glew.h>
|
||||
#include "lights.h"
|
||||
|
||||
class Shader;
|
||||
class PhongEntity;
|
||||
@ -17,14 +16,7 @@ public:
|
||||
|
||||
virtual void renderGL(Camera* myCamera, Scene* scene);
|
||||
private:
|
||||
/*Lights::Light* dirLight;
|
||||
Lights* pointLights;
|
||||
GLuint dirLightLocation;
|
||||
GLuint nbPointLightsLocation;
|
||||
GLuint pointLightsLocation;
|
||||
std::vector<PhongEntity*> entities;
|
||||
|
||||
static Shader* shaders[NB_SHADERS];*/
|
||||
};
|
||||
|
||||
#endif // DEFERREDMODULE_H
|
||||
|
@ -121,14 +121,16 @@ void ForwardModule::compileShaders(Scene* scene)
|
||||
for(int i=0; i<size; ++i)
|
||||
geometryFlags[i] = false;
|
||||
|
||||
for(SceneIterator<PhongEntity*>* EntityIt = scene->getGeometry();
|
||||
EntityIt->isValid(); EntityIt->next())
|
||||
// get material flags
|
||||
for(SceneIterator<PhongEntity*>* entityIt = scene->getGeometry();
|
||||
entityIt->isValid(); entityIt->next())
|
||||
{
|
||||
Mesh* m = EntityIt->getItem()->getMesh();
|
||||
Mesh* m = entityIt->getItem()->getMesh();
|
||||
for(Mesh::Group &g : m->indiceGroups)
|
||||
geometryFlags[g.material->getFlags()] = true;
|
||||
}
|
||||
|
||||
// shader compilation
|
||||
for(int i=0; i<size; ++i)
|
||||
{
|
||||
if(geometryFlags[i])
|
||||
@ -136,6 +138,7 @@ void ForwardModule::compileShaders(Scene* scene)
|
||||
std::vector<const char*> defines;
|
||||
defines.push_back(lightStr[AMBIENT_LIGHT]);
|
||||
|
||||
// set defines
|
||||
if(i & NORMAL_MAP_FLAG)
|
||||
defines.push_back(flagStr[NORMAL_MAP]);
|
||||
if(i & AMBIENT_TEXTURE_FLAG)
|
||||
|
20
lights.cpp
20
lights.cpp
@ -1,20 +0,0 @@
|
||||
#include "lights.h"
|
||||
#include "shader.h"
|
||||
|
||||
void Lights::addLight(const glm::vec3 &myPosition, const glm::vec3 myColor)
|
||||
{
|
||||
if(lights.size() < MAX_LIGHTS)
|
||||
{
|
||||
Light l;
|
||||
l.position = myPosition;
|
||||
l.color = myColor;
|
||||
lights.push_back(l);
|
||||
}
|
||||
}
|
||||
|
||||
void Lights::bind(GLuint lightsLocation, GLuint nbLocation, Shader* shader)
|
||||
{
|
||||
if(lights.size() > 0)
|
||||
shader->bindVec3Array(lightsLocation, (glm::vec3*)lights.data(), lights.size()*2);
|
||||
shader->bindInteger(nbLocation, (GLuint)lights.size());
|
||||
}
|
30
lights.h
30
lights.h
@ -1,30 +0,0 @@
|
||||
#ifndef LIGHTS_H
|
||||
#define LIGHTS_H
|
||||
|
||||
#include <glew/glew.h>
|
||||
#include <glm/vec3.hpp>
|
||||
#include <vector>
|
||||
|
||||
#define MAX_LIGHTS 4
|
||||
|
||||
class Shader;
|
||||
|
||||
class Lights
|
||||
{
|
||||
public:
|
||||
void addLight(const glm::vec3 &myPosition = glm::vec3(0), const glm::vec3 myColor = glm::vec3(1));
|
||||
void bind(GLuint lightsLocation, GLuint nbLocation, Shader* shader);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
glm::vec3 position;
|
||||
glm::vec3 color;
|
||||
bool shadowCaster;
|
||||
// Shadowmap fbo
|
||||
} Light;
|
||||
|
||||
private:
|
||||
std::vector<Light> lights;
|
||||
};
|
||||
|
||||
#endif // LIGHTS_H
|
@ -25,15 +25,8 @@ class Shader;
|
||||
class Material
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* deprecated, you should use bindAttributes(Shader*) instead
|
||||
*/
|
||||
virtual void bindAttributes() = 0;
|
||||
|
||||
virtual void bindAttributes(Shader*) = 0;
|
||||
virtual unsigned int getFlags() = 0;
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // MATERIAL_H
|
||||
|
52
phong.vert
52
phong.vert
@ -1,52 +0,0 @@
|
||||
#version 330 core
|
||||
|
||||
// Matrices
|
||||
uniform mat4 modelViewMatrix;
|
||||
uniform mat4 MVP;
|
||||
uniform mat4 normalMatrix;
|
||||
uniform mat4 viewMatrix;
|
||||
|
||||
uniform vec3 dirLight[2];
|
||||
uniform int nbPointLights;
|
||||
uniform vec3 pointLights[8];
|
||||
|
||||
layout(location = 0)in vec3 inPosition;
|
||||
layout(location = 1)in vec3 inNormal;
|
||||
layout(location = 2)in vec4 inTexCoord;
|
||||
|
||||
out vec3 lightDirInView[5];
|
||||
out vec3 halfVecInView[5];
|
||||
|
||||
out vec3 varNormal;
|
||||
out vec2 varTexCoord;
|
||||
|
||||
void computeDirectionnalLightingVectorsInView(in vec3 posInView, in vec3 lightDirInWorld, out vec3 lightDir, out vec3 halfVec){
|
||||
lightDir = mat3(viewMatrix)*lightDirInWorld;
|
||||
halfVec = normalize(normalize(lightDir) - normalize(posInView));
|
||||
lightDir = normalize(lightDir);
|
||||
}
|
||||
|
||||
void computePointLightingVectorsInView(in vec3 posInView, in vec3 lightPosition, out vec3 lightDir, out vec3 halfVec){
|
||||
lightDir = vec3(viewMatrix*vec4(lightPosition, 1.0)) - posInView;
|
||||
halfVec = normalize(lightDir - posInView);
|
||||
lightDir = normalize(lightDir);
|
||||
}
|
||||
|
||||
void main(void) {
|
||||
int i;
|
||||
computeDirectionnalLightingVectorsInView(vec3(modelViewMatrix*vec4(inPosition, 1.0)), dirLight[0], lightDirInView[0], halfVecInView[0]);
|
||||
for(i=1; i<nbPointLights+1; ++i)
|
||||
computePointLightingVectorsInView(vec3(modelViewMatrix*vec4(inPosition, 1.0)),
|
||||
pointLights[i*2 - 2],
|
||||
lightDirInView[i],
|
||||
halfVecInView[i]);
|
||||
|
||||
// normales corrigees (en fonction de la vue)
|
||||
varNormal = normalize(vec3(normalMatrix*vec4(inNormal,0)));
|
||||
|
||||
// coordonnees de texture
|
||||
varTexCoord = inTexCoord.xy;
|
||||
|
||||
// position du vertex
|
||||
gl_Position = MVP * vec4(inPosition, 1.0);
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
#version 330 core
|
||||
|
||||
// material
|
||||
uniform vec3 materialAmbient;
|
||||
uniform vec3 materialKd;
|
||||
uniform vec3 materialKs;
|
||||
uniform float materialNs;
|
||||
|
||||
uniform vec3 dirLight[2];
|
||||
uniform int nbPointLights;
|
||||
uniform vec3 pointLights[8];
|
||||
|
||||
// texture
|
||||
uniform sampler2D baseTexture;
|
||||
uniform sampler2D bumpMap;
|
||||
|
||||
// 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 = vec3(texture2D(baseTexture, varTexCoord))*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(materialAmbient + light, 1);
|
||||
}
|
||||
|
@ -1,57 +0,0 @@
|
||||
#version 330 core
|
||||
|
||||
// Matrices
|
||||
uniform mat4 modelViewMatrix;
|
||||
uniform mat4 MVP;
|
||||
uniform mat4 normalMatrix;
|
||||
uniform mat4 viewMatrix;
|
||||
|
||||
uniform vec3 dirLight[2];
|
||||
uniform int nbPointLights;
|
||||
uniform vec3 pointLights[8];
|
||||
|
||||
layout(location = 0)in vec3 inPosition;
|
||||
layout(location = 1)in vec3 inNormal;
|
||||
layout(location = 2)in vec2 inTexCoord;
|
||||
layout(location = 2)in vec3 inTangent[2];
|
||||
|
||||
out vec3 lightDirInView[5];
|
||||
out vec3 halfVecInView[5];
|
||||
|
||||
out vec3 varNormal;
|
||||
out vec2 varTexCoord;
|
||||
out vec3 varTangent;
|
||||
out vec3 varBinormal;
|
||||
|
||||
void computeDirectionnalLightingVectorsInView(in vec3 posInView, in vec3 lightDirInWorld, out vec3 lightDir, out vec3 halfVec){
|
||||
lightDir = mat3(viewMatrix)*lightDirInWorld;
|
||||
halfVec = normalize(normalize(lightDir) - normalize(posInView));
|
||||
lightDir = normalize(lightDir);
|
||||
}
|
||||
|
||||
void computePointLightingVectorsInView(in vec3 posInView, in vec3 lightPosition, out vec3 lightDir, out vec3 halfVec){
|
||||
lightDir = vec3(viewMatrix*vec4(lightPosition, 1.0)) - posInView;
|
||||
halfVec = normalize(lightDir - posInView);
|
||||
lightDir = normalize(lightDir);
|
||||
}
|
||||
|
||||
void main(void) {
|
||||
int i;
|
||||
computeDirectionnalLightingVectorsInView(vec3(modelViewMatrix*vec4(inPosition, 1.0)), dirLight[0], lightDirInView[0], halfVecInView[0]);
|
||||
for(i=1; i<nbPointLights+1; ++i)
|
||||
computePointLightingVectorsInView(vec3(modelViewMatrix*vec4(inPosition, 1.0)),
|
||||
pointLights[i*2 - 2],
|
||||
lightDirInView[i],
|
||||
halfVecInView[i]);
|
||||
|
||||
// normales corrigees (en fonction de la vue)
|
||||
varNormal = normalize(vec3(normalMatrix*vec4(inNormal,0)));
|
||||
|
||||
// coordonnees de texture
|
||||
varTexCoord = inTexCoord.xy;
|
||||
varTangent = inTangent[0];
|
||||
varBinormal = inTangent[1];
|
||||
|
||||
// position du vertex
|
||||
gl_Position = MVP * vec4(inPosition, 1.0);
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
#version 330 core
|
||||
|
||||
// material
|
||||
uniform vec3 materialAmbient;
|
||||
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(materialAmbient + light, 1);
|
||||
}
|
||||
|
@ -18,44 +18,6 @@ PhongEntity::PhongEntity(Mesh* myMesh) :
|
||||
modelMatrix()
|
||||
{}
|
||||
|
||||
void PhongEntity::draw(const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix, Lights::Light* dirLight, Lights* pointLights)
|
||||
{
|
||||
if(SparrowRenderer::isModernOpenGLAvailable())
|
||||
modernDraw(viewMatrix, projectionMatrix, dirLight, pointLights);
|
||||
else
|
||||
crappyDraw(viewMatrix, projectionMatrix, dirLight, pointLights);
|
||||
}
|
||||
|
||||
void PhongEntity::modernDraw(const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix, Lights::Light* dirLight, Lights* pointLights)
|
||||
{
|
||||
glm::mat4 modelViewMatrix = viewMatrix * modelMatrix;
|
||||
glm::mat4 mvp = projectionMatrix * modelViewMatrix;
|
||||
glm::mat4 normalMatrix = glm::transpose(glm::inverse(modelViewMatrix));
|
||||
for(int i=0; i<mesh->indiceGroups.size(); ++i)
|
||||
{
|
||||
PhongMaterial* mat = (PhongMaterial*)mesh->indiceGroups[i].material;
|
||||
mat->bindAttributes();
|
||||
Shader* shader = mat->getShader();
|
||||
shader->bindMat4(shader->getLocation("viewMatrix"), viewMatrix);
|
||||
shader->bindMat4(shader->getLocation("modelViewMatrix"), modelViewMatrix);
|
||||
shader->bindMat4(shader->getLocation("normalMatrix"), normalMatrix);
|
||||
shader->bindMat4(shader->getLocation("MVP"), mvp);
|
||||
shader->bindVec3Array(shader->getLocation("dirLight"), (glm::vec3*)dirLight, 2);
|
||||
pointLights->bind(shader->getLocation("pointLights"), shader->getLocation("nbPointLights"), shader);
|
||||
drawGroup(i);
|
||||
}
|
||||
}
|
||||
|
||||
void PhongEntity::crappyDraw(const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix, Lights::Light* dirLight, Lights* pointLights)
|
||||
{
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glm::mat4 modelViewMatrix = viewMatrix * modelMatrix;
|
||||
glLoadMatrixf(glm::value_ptr(modelViewMatrix));
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadMatrixf(glm::value_ptr(projectionMatrix));
|
||||
glCallList(displayList);
|
||||
}
|
||||
|
||||
void PhongEntity::modernInit(bool isDynamic)
|
||||
{
|
||||
GLenum buffer_type = isDynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW;
|
||||
@ -121,7 +83,7 @@ void PhongEntity::crappyInit()
|
||||
for(int i=0; i<mesh->indiceGroups.size(); ++i)
|
||||
{
|
||||
PhongMaterial* mat = (PhongMaterial*)(mesh->indiceGroups[i].material);
|
||||
mat->bindAttributes();
|
||||
mat->bindAttributes(NULL);
|
||||
glAssert(glBegin(GL_TRIANGLES));
|
||||
for(int j=0; j<mesh->indiceGroups[i].indices.size(); ++j)
|
||||
{
|
||||
@ -185,6 +147,11 @@ void PhongEntity::drawGroup(int groupId)
|
||||
glAssert(glDrawElements(GL_TRIANGLES, mesh->indiceGroups[groupId].indices.size(), GL_UNSIGNED_INT, mesh->indiceGroups[groupId].indices.data()));
|
||||
}
|
||||
|
||||
void PhongEntity::drawDisplayList()
|
||||
{
|
||||
glAssert(glCallList(displayList));
|
||||
}
|
||||
|
||||
PhongEntity* PhongEntity::clone()
|
||||
{
|
||||
PhongEntity* myClone = new PhongEntity(mesh);
|
||||
|
@ -4,7 +4,6 @@
|
||||
#include <glew/glew.h>
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include <vector>
|
||||
#include "lights.h"
|
||||
|
||||
class Mesh;
|
||||
class Material;
|
||||
@ -31,14 +30,12 @@ protected:
|
||||
GLuint* vbo;
|
||||
|
||||
void modernInit(bool isDynamic);
|
||||
void modernDraw(const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix, Lights::Light* dirLight, Lights* pointLights);
|
||||
|
||||
// old opengl :
|
||||
|
||||
GLuint displayList;
|
||||
|
||||
void crappyInit();
|
||||
void crappyDraw(const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix, Lights::Light* dirLight, Lights* pointLights);
|
||||
|
||||
public:
|
||||
/**
|
||||
@ -48,7 +45,7 @@ public:
|
||||
|
||||
PhongEntity(Mesh* myMesh);
|
||||
|
||||
void draw(const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix, Lights::Light* dirLight, Lights* pointLights);
|
||||
void drawDisplayList();
|
||||
void drawGroup(int groupId); // temporarily public
|
||||
|
||||
void initGL(bool isDynamic = false);
|
||||
@ -58,7 +55,7 @@ public:
|
||||
|
||||
/**
|
||||
* @brief this method returns a clone of the current entity.
|
||||
* The same VAO will be used to render both, so there is no need to call initGL on the clone
|
||||
* The same VAO/displayList will be used to render both, so there is no need to call initGL on the clone
|
||||
*/
|
||||
PhongEntity* clone();
|
||||
};
|
||||
|
@ -1,51 +1,13 @@
|
||||
#include "phongmaterial.h"
|
||||
#include "texture.h"
|
||||
#include "phongmodule.h"
|
||||
#include "sparrowrenderer.h"
|
||||
#include "glassert.h"
|
||||
#include "shader.h"
|
||||
#include <glm/ext.hpp>
|
||||
|
||||
void PhongMaterial::updateShader()
|
||||
{
|
||||
shader = PhongModule::getShader(diffuse_texture == NULL ? PhongModule::PHONG_COLOR : PhongModule::PHONG_TEXTURE);
|
||||
}
|
||||
|
||||
void PhongMaterial::bindAttributes()
|
||||
{
|
||||
if(SparrowRenderer::isModernOpenGLAvailable())
|
||||
{
|
||||
shader->bind();
|
||||
shader->bindVec3(shader->getLocation("materialAmbient"), ambient);
|
||||
shader->bindVec3(shader->getLocation("materialKd"), diffuse);
|
||||
shader->bindVec3(shader->getLocation("materialKs"), specular);
|
||||
shader->bindFloat(shader->getLocation("materialNs"), shininess);
|
||||
if(diffuse_texture != NULL)
|
||||
{
|
||||
diffuse_texture->bind(DIFFUSE_TEXTURE);
|
||||
shader->bindInteger(shader->getLocation("baseTexture"), DIFFUSE_TEXTURE);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
glAssert(glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, glm::value_ptr(glm::vec4(ambient, 1))));
|
||||
glAssert(glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, glm::value_ptr(glm::vec4(diffuse, 1))));
|
||||
glAssert(glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, glm::value_ptr(glm::vec4(specular, 1))));
|
||||
glAssert(glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess));
|
||||
if(diffuse_texture != NULL)
|
||||
diffuse_texture->bind(DIFFUSE_TEXTURE);
|
||||
else
|
||||
{
|
||||
GLenum texSlot = GL_TEXTURE0+DIFFUSE_TEXTURE;
|
||||
glAssert(glActiveTexture(texSlot));
|
||||
glAssert(glBindTexture(GL_TEXTURE_2D, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PhongMaterial::bindAttributes(Shader* myShader)
|
||||
{
|
||||
if(SparrowRenderer::isModernOpenGLAvailable())
|
||||
if(SparrowRenderer::isModernOpenGLAvailable() && myShader != NULL)
|
||||
{
|
||||
// TODO store the attributes location (in the shader class maybe)
|
||||
myShader->bindFloat(myShader->getLocation("materialNs"), shininess);
|
||||
@ -112,12 +74,6 @@ unsigned int PhongMaterial::getFlags()
|
||||
return flags;
|
||||
}
|
||||
|
||||
void PhongMaterial::setTexture(Texture* myTexture)
|
||||
{
|
||||
setDiffuseTexture(myTexture);
|
||||
updateShader();
|
||||
}
|
||||
|
||||
void PhongMaterial::setAmbientTexture(Texture* myTexture)
|
||||
{
|
||||
ambient_texture = myTexture;
|
||||
|
@ -27,9 +27,7 @@ public:
|
||||
diffuse_texture(NULL),
|
||||
specular_texture(NULL),
|
||||
normal_map(NULL)
|
||||
{
|
||||
updateShader();
|
||||
}
|
||||
{}
|
||||
|
||||
PhongMaterial(glm::vec3 myKd, glm::vec3 myKs, float myNs) :
|
||||
ambient(0),
|
||||
@ -40,13 +38,9 @@ public:
|
||||
diffuse_texture(NULL),
|
||||
specular_texture(NULL),
|
||||
normal_map(NULL)
|
||||
{
|
||||
updateShader();
|
||||
}
|
||||
{}
|
||||
|
||||
virtual void bindAttributes();
|
||||
|
||||
virtual void bindAttributes(Shader* myShader);
|
||||
virtual void bindAttributes(Shader* myShader = NULL);
|
||||
|
||||
virtual unsigned int getFlags();
|
||||
Shader* getShader() {return shader;}
|
||||
@ -61,8 +55,6 @@ public:
|
||||
void setSpecularTexture(Texture* myTexture);
|
||||
void setNormalMap(Texture* myNormalMap);
|
||||
|
||||
void updateShader();
|
||||
|
||||
private:
|
||||
Shader* shader;
|
||||
};
|
||||
|
@ -1,59 +0,0 @@
|
||||
#include "phongmodule.h"
|
||||
#include "lights.h"
|
||||
#include "shader.h"
|
||||
#include "phongentity.h"
|
||||
#include "camera.h"
|
||||
#include "mesh.h"
|
||||
#include "sparrowrenderer.h"
|
||||
#include "glassert.h"
|
||||
#include <glm/ext.hpp>
|
||||
|
||||
Shader* PhongModule::shaders[NB_SHADERS] = {0, 0};
|
||||
|
||||
PhongModule::PhongModule(Lights::Light* myDirLight, Lights* myPointLights) :
|
||||
dirLight(myDirLight),
|
||||
pointLights(myPointLights)
|
||||
{
|
||||
if(!SparrowRenderer::isModernOpenGLAvailable())
|
||||
{
|
||||
glAssert(glEnable(GL_LIGHTING));
|
||||
glAssert(glEnable(GL_LIGHT0));
|
||||
glAssert(glLightfv(GL_LIGHT0, GL_AMBIENT, glm::value_ptr(glm::vec4(glm::vec3(0.1f), 1))));
|
||||
glAssert(glLightfv(GL_LIGHT0, GL_POSITION, glm::value_ptr(glm::vec4(dirLight->position, 0))));
|
||||
// TODO add point lights
|
||||
}
|
||||
}
|
||||
|
||||
void PhongModule::addEntity(PhongEntity* myEntity)
|
||||
{
|
||||
entities.push_back(myEntity);
|
||||
}
|
||||
|
||||
void PhongModule::clearEntities()
|
||||
{
|
||||
entities.clear();
|
||||
}
|
||||
|
||||
void PhongModule::renderGL(Camera* myCamera, Scene* scene)
|
||||
{
|
||||
glAssert(glClearColor(0, 0, 0, 1.0));
|
||||
glAssert(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
|
||||
if(!SparrowRenderer::isModernOpenGLAvailable())
|
||||
{
|
||||
glLoadIdentity();
|
||||
glAssert(glLightfv(GL_LIGHT0, GL_DIFFUSE, glm::value_ptr(glm::vec4(dirLight->color, 1))));
|
||||
glAssert(glLightfv(GL_LIGHT0, GL_SPECULAR, glm::value_ptr(glm::vec4(dirLight->color, 1))));
|
||||
}
|
||||
for(PhongEntity* e : entities)
|
||||
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];
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
#ifndef PHONGMODULE_H
|
||||
#define PHONGMODULE_H
|
||||
|
||||
#include "module.h"
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
#include <glew/glew.h>
|
||||
#include "lights.h"
|
||||
|
||||
class Shader;
|
||||
class PhongEntity;
|
||||
|
||||
class PhongModule : public Module
|
||||
{
|
||||
public:
|
||||
enum ShaderType {
|
||||
PHONG_COLOR, // rendering untextured mesh
|
||||
PHONG_TEXTURE, // rendering textured mesh
|
||||
//TODO : rendering bump mapped mesh
|
||||
NB_SHADERS
|
||||
};
|
||||
|
||||
PhongModule(Lights::Light* myDirLight, Lights* myPointLights);
|
||||
|
||||
void addEntity(PhongEntity* myEntity);
|
||||
void clearEntities();
|
||||
|
||||
virtual void renderGL(Camera* myCamera, Scene* scene = NULL);
|
||||
virtual bool requiresModernOpenGL() {return false;}
|
||||
|
||||
// modern opengl methods
|
||||
|
||||
/**
|
||||
* @brief all used shaders shall be provided through this method before rendering
|
||||
*/
|
||||
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,49 +0,0 @@
|
||||
#version 330 core
|
||||
|
||||
// material
|
||||
uniform vec3 materialAmbient;
|
||||
uniform vec3 materialKd;
|
||||
uniform vec3 materialKs;
|
||||
uniform float materialNs;
|
||||
|
||||
uniform vec3 dirLight[2];
|
||||
uniform int nbPointLights;
|
||||
uniform vec3 pointLights[8];
|
||||
|
||||
// texture
|
||||
uniform sampler2D baseTexture;
|
||||
|
||||
// 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 = vec3(texture2D(baseTexture, varTexCoord))*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(materialAmbient + light, 1);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user