moved trackball camera to renderer, added example to use 2D, scenetree new temporarily uses a 2D pipeline

This commit is contained in:
Anselme FRANÇOIS 2016-06-25 14:53:42 +02:00
parent f2f60a8c20
commit 128bbc2cf2
7 changed files with 93 additions and 152 deletions

View File

@ -102,4 +102,6 @@ unsigned int Engine::getDeltaTime()
void Engine::setScene(SceneTree *scene)
{
m_scene = scene;
m_renderer->setScene(m_scene);
m_renderer->resizeGL(m_window->getSize().x, m_window->getSize().y);
}

View File

@ -1,6 +1,9 @@
#include "engine.h"
#include <input.h>
#include "scene.h"
#include <scene.h>
#include <mesh.h>
#include <guipipeline.h>
#include <phongmaterial.h>
#include "resourcemanager.h"
#include "sparrowrenderer.h"
#include "scenetree.h"
@ -12,8 +15,46 @@
int main(){
Engine engine;
// this creates the opengl context
// the opengl context must exist before any opengl class is used (texture, pipeline, etc..)
engine.createWindow("test");
SceneTree scene; // the opengl context must exist before the scene is created
SceneTree scene;
// creating a 2D mesh
Mesh* mesh = new Mesh();
// first triangle
mesh->addVertex(glm::vec2(50, 50), glm::vec2(0, 0)); // 2D position and then texture coordinates
mesh->addVertex(glm::vec2(50, 150), glm::vec2(0, 1));
mesh->addVertex(glm::vec2(150, 50), glm::vec2(1, 0));
// second triangle
mesh->addVertex(glm::vec2(150, 50), glm::vec2(1, 0));
mesh->addVertex(glm::vec2(50, 150), glm::vec2(1, 0));
mesh->addVertex(glm::vec2(150, 150), glm::vec2(1, 1));
// in the case you don't want to worry about if you defined the triangles in the right order,
// you can call this : mesh->setIsDoubleSided(true);
// creating the material
PhongMaterial *mat = new PhongMaterial();
// setting an arbitrary color to the square : (orange)
mat->diffuse = glm::vec3(1, 0.5, 0);
// you can set a color texture by calling :
// mat->setTexture(PhongMaterial::DIFFUSE_SLOT, myTexture, "my texture name");
// or
// mat->texture[PhongMaterial::DIFFUSE_SLOT] = myTexture;
// don't forget to tell the mesh you want to use this material
mesh->setMaterial(mat);
// uploads the mesh to GPU memory
mesh->initGL();
// creating the scene node
MeshNode *node = new MeshNode(mesh);
scene.addObject(scene.getRootObject(), node);
// the pipeline needs to updates his shaders because the scene changed
// this should be handled somewhere else in the future
GuiPipeline* pipeline = (GuiPipeline*)scene.getPipeline();
pipeline->refreshScene(&scene);
engine.setScene(&scene);
engine.start();

View File

@ -1,6 +1,7 @@
#include "scenetree.h"
#include "resourcemanager.h"
#include <pipeline.h>
#include <guipipeline.h>
#include <algorithm>
// Scene
@ -8,8 +9,8 @@ SceneTree::SceneTree() :
Scene(),
m_skybox(NULL)
{
m_simplepipeline = new SimplePipeline();
m_simplepipeline->setClearColor(glm::vec3(0.1f, 0.4f, 0.25f));
m_pipeline = new GuiPipeline();
//m_pipeline->setClearColor(glm::vec3(0.1f, 0.4f, 0.25f));
}
SceneTree::~SceneTree()
@ -19,7 +20,7 @@ SceneTree::~SceneTree()
void SceneTree::setMainCamera(CameraNode *camera)
{
m_simplepipeline->setCamera(camera);
//m_pipeline->setCamera(camera);
}
SceneIterator<Light*>* SceneTree::getLights()

View File

@ -6,7 +6,7 @@
#include "scene.h"
#include "light.h"
#include "resourcemanager.h"
#include "camera.h"
#include <trackballcamera.h>
class SimplePipeline;
@ -19,8 +19,8 @@ class SceneNode
public:
SceneNode* m_parent;
virtual void update() = 0;
Light* getLight(){return nullptr;}
GeometryNode* getGeometryNode(){return nullptr;}
virtual Light* getLight() {return nullptr;}
virtual GeometryNode* getGeometryNode() {return nullptr;}
};
/**
@ -44,15 +44,30 @@ protected:
std::vector<SceneNode*> m_children;
};
/**
* @brief The MeshNode class holds a mesh
*/
class MeshNode : public SceneNode
{
GeometryNode geometry;
public:
MeshNode(Mesh* mesh) : geometry(mesh, glm::mat4()) {}
virtual void update() {}
void setTransform(const glm::mat4 &transform) { geometry.modelMatrix = transform; }
const glm::mat4& getTransform() { return geometry.modelMatrix; }
GeometryNode* getGeometryNode() { return &geometry; }
};
/**
* @brief The CameraNode class is a scene node that can be used by the renderer
*/
//TODO : Fix compil error here
class CameraNode : public Camera, SceneNode
class CameraNode : public TrackBallCamera, SceneNode
{
virtual glm::mat4 getProjectionMatrix(){return glm::mat4();}
virtual glm::mat4 getViewMatrix(){return glm::mat4();}
virtual void resize(int width, int height){}
public:
CameraNode() {}
};
class SceneTree : public Scene
@ -71,7 +86,7 @@ public:
void setMainCamera(CameraNode *camera);
ContainerNode getRootObject(){return m_root;}
ContainerNode* getRootObject(){return &m_root;}
void addObject(ContainerNode *parent, SceneNode *node);
void removeObject(ContainerNode* parent,SceneNode *node);
private:
@ -79,7 +94,6 @@ private:
std::vector<Light*> m_lights;
std::vector<GeometryNode*> m_geometries;
Texture* m_skybox;
SimplePipeline* m_simplepipeline;
};
// Additionnal Nodes :

View File

@ -268,35 +268,35 @@ bool Loader::loadMTL(const std::string &filename)
else if((tokens[0].substr(0,4) == "map_") && tokens.size() == 2)
{
if(tokens[0].compare("map_Ka") == 0){
mat->emission_texture = RESOURCE_GET(Texture,tokens[1]);
if (mat->emission_texture == NULL){
mat->emission_texture = new Texture(loadImage(tokens[1]));
RESOURCE_ADD(mat->emission_texture,Texture,tokens[1]);
mat->textures[PhongMaterial::EMISSION_SLOT] = RESOURCE_GET(Texture,tokens[1]);
if (mat->textures[PhongMaterial::EMISSION_SLOT] == NULL){
mat->textures[PhongMaterial::EMISSION_SLOT] = new Texture(loadImage(tokens[1]));
RESOURCE_ADD(mat->textures[PhongMaterial::EMISSION_SLOT], Texture, tokens[1]);
}
} else if(tokens[0].compare("map_Kd") == 0) {
mat->diffuse_texture = RESOURCE_GET(Texture,tokens[1]);
if (mat->diffuse_texture == NULL){
mat->diffuse_texture = new Texture(loadImage(tokens[1]));
RESOURCE_ADD(mat->diffuse_texture,Texture,tokens[1]);
mat->textures[PhongMaterial::DIFFUSE_SLOT] = RESOURCE_GET(Texture,tokens[1]);
if (mat->textures[PhongMaterial::DIFFUSE_SLOT] == NULL){
mat->textures[PhongMaterial::DIFFUSE_SLOT] = new Texture(loadImage(tokens[1]));
RESOURCE_ADD(mat->textures[PhongMaterial::DIFFUSE_SLOT],Texture,tokens[1]);
}
} else if(tokens[0].compare("map_Ks") == 0) {
mat->specular_texture = RESOURCE_GET(Texture,tokens[1]);
if (mat->specular_texture == NULL){
mat->specular_texture = new Texture(loadImage(tokens[1]));
RESOURCE_ADD(mat->specular_texture,Texture,tokens[1]);
mat->textures[PhongMaterial::SPECULAR_SLOT] = RESOURCE_GET(Texture,tokens[1]);
if (mat->textures[PhongMaterial::SPECULAR_SLOT] == NULL){
mat->textures[PhongMaterial::SPECULAR_SLOT] = new Texture(loadImage(tokens[1]));
RESOURCE_ADD(mat->textures[PhongMaterial::SPECULAR_SLOT],Texture,tokens[1]);
}
} else if(tokens[0].compare("map_Normal") == 0) {
mat->normal_map = RESOURCE_GET(Texture,tokens[1]);
if (mat->normal_map == NULL){
mat->normal_map = new Texture(loadImage(tokens[1]));
RESOURCE_ADD(mat->normal_map,Texture,tokens[1]);
mat->textures[PhongMaterial::NORMALS_SLOT] = RESOURCE_GET(Texture,tokens[1]);
if (mat->textures[PhongMaterial::NORMALS_SLOT] == NULL){
mat->textures[PhongMaterial::NORMALS_SLOT] = new Texture(loadImage(tokens[1]));
RESOURCE_ADD(mat->textures[PhongMaterial::NORMALS_SLOT],Texture,tokens[1]);
}
hasNormalMap = true;
} else if(tokens[0].compare("map_d") == 0) {
mat->alpha_mask = RESOURCE_GET(Texture,tokens[1]);
if (mat->alpha_mask == NULL){
mat->alpha_mask = new Texture(loadImage(tokens[1]));
RESOURCE_ADD(mat->alpha_mask,Texture,tokens[1]);
mat->textures[PhongMaterial::ALPHA_SLOT] = RESOURCE_GET(Texture,tokens[1]);
if (mat->textures[PhongMaterial::ALPHA_SLOT] == NULL){
mat->textures[PhongMaterial::ALPHA_SLOT] = new Texture(loadImage(tokens[1]));
RESOURCE_ADD(mat->textures[PhongMaterial::ALPHA_SLOT],Texture,tokens[1]);
}
} else
fprintf(stderr, "unsupported material property : \"%s\"\n", tokens[0].c_str());

View File

@ -1,74 +0,0 @@
#include "trackballcamera.h"
#include <glm/ext.hpp>
#define SCROLL_SPEED 0.998f
#define ROTATION_SPEED 0.01f
#define MOVE_SPEED 0.002f
TrackBallCamera::TrackBallCamera(float myFov, float myNear, float myFar) :
m_fov(myFov), m_near(myNear), m_far(myFar)
{
reset();
resize(800, 600);
}
void TrackBallCamera::resize(int width, int height)
{
if(width > 0 && height > 0)
m_projection = glm::perspectiveFov<float>(m_fov, float(width), float(height), m_near, m_far);
}
void TrackBallCamera::rotateCamera(float dx, float dy)
{
m_rotation.x += dx*ROTATION_SPEED;
m_rotation.y += dy*ROTATION_SPEED;
if(m_rotation.y > 1.57f)
m_rotation.y = 1.57f;
if(m_rotation.y < -1.57f)
m_rotation.y = -1.57f;
computeView();
}
void TrackBallCamera::reset()
{
m_center = glm::vec3(0, 4, 0);
m_target = m_center;
m_rotation = glm::vec2(0, 1);
m_dist = 20;
computeView();
}
void TrackBallCamera::computeView()
{
m_view = glm::translate(glm::mat4(), glm::vec3(0, 0, -m_dist));
m_view = glm::rotate(m_view, m_rotation.y, glm::vec3(1, 0, 0));
m_view = glm::rotate(m_view, m_rotation.x, glm::vec3(0, 1, 0));
m_view = glm::translate(m_view, -m_center);
}
void TrackBallCamera::mouseScroll(int nbScrolls)
{
while(nbScrolls != 0)
{
if(nbScrolls > 0)
{
m_dist *= SCROLL_SPEED;
--nbScrolls;
}
else
{
m_dist /= SCROLL_SPEED;
++nbScrolls;
}
}
computeView();
}
glm::vec3 TrackBallCamera::getDefaultPxInfo()
{
glm::vec4 viewPos(m_center, 1);
viewPos = (m_projection * m_view) * viewPos;
return glm::vec3(m_dist/m_far, 1/viewPos.w, 0);
}

View File

@ -1,43 +0,0 @@
#ifndef TRACKBALLCAMERA_H
#define TRACKBALLCAMERA_H
#include <camera.h>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
class TrackBallCamera : public Camera
{
glm::mat4 m_view;
glm::mat4 m_projection;
float m_fov;
float m_near;
float m_far;
// camera position
glm::vec3 m_center;
glm::vec2 m_rotation;
float m_dist;
// camera movement
glm::vec3 m_origin;
glm::vec3 m_target;
public:
TrackBallCamera(float myFov = 70.f, float myNear = 0.1f, float myFar = 1000.f);
glm::mat4 getProjectionMatrix() {return m_projection;}
glm::mat4 getViewMatrix() {return m_view;}
void resize(int width, int height);
void rotateCamera(float dx, float dy);
void moveCamera(float dx, float dy);
void mouseScroll(int nbScrolls);
void reset();
void computeView();
void setCenter(glm::vec3 pos) { m_center = pos; computeView(); }
glm::vec3 getDefaultPxInfo();
};
#endif // TRACKBALLCAMERA_H