added lots of things, but it crashes
This commit is contained in:
commit
2bd9205c37
@ -1,6 +1,6 @@
|
|||||||
#include "gridmesh.h"
|
#include "gridmesh.h"
|
||||||
|
|
||||||
GridMesh::GridMesh(int width, int height, bool alternate) : Mesh()
|
GridMesh::GridMesh(int width, int height, bool alternate) : Mesh(), m_width(width), m_height(height)
|
||||||
{
|
{
|
||||||
for(int i=0; i<=width; ++i)
|
for(int i=0; i<=width; ++i)
|
||||||
{
|
{
|
||||||
@ -38,5 +38,5 @@ GridMesh::GridMesh(int width, int height, bool alternate) : Mesh()
|
|||||||
|
|
||||||
int GridMesh::getVertexId(int i, int j)
|
int GridMesh::getVertexId(int i, int j)
|
||||||
{
|
{
|
||||||
return i*(height+1) + j;
|
return i*(m_height+1) + j;
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,8 @@ class GridMesh : public Mesh
|
|||||||
public:
|
public:
|
||||||
GridMesh(int width, int height, bool alternate);
|
GridMesh(int width, int height, bool alternate);
|
||||||
private:
|
private:
|
||||||
|
int m_height;
|
||||||
|
int m_width;
|
||||||
int getVertexId(int i, int j);
|
int getVertexId(int i, int j);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
12
material.cpp
12
material.cpp
@ -1,12 +0,0 @@
|
|||||||
#include "material.h"
|
|
||||||
|
|
||||||
Material::Material()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Material::~Material()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
11
material.h
11
material.h
@ -1,12 +1,19 @@
|
|||||||
#ifndef MATERIAL_H
|
#ifndef MATERIAL_H
|
||||||
#define MATERIAL_H
|
#define MATERIAL_H
|
||||||
|
|
||||||
|
#include "shader.h"
|
||||||
|
#include "glm/fwd.hpp"
|
||||||
|
|
||||||
class Material
|
class Material
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Material();
|
Material(Shader* myShader) : shader(myShader) {}
|
||||||
~Material();
|
Shader* getShader() {return shader;}
|
||||||
|
|
||||||
|
virtual void bindAttributes() = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Shader* shader;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // MATERIAL_H
|
#endif // MATERIAL_H
|
||||||
|
9
phongmaterial.cpp
Normal file
9
phongmaterial.cpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#include "phongmaterial.h"
|
||||||
|
|
||||||
|
void PhongMaterial::bindAttributes()
|
||||||
|
{
|
||||||
|
shader->bindVec3(shader->getLocation("materialKd"), kd);
|
||||||
|
shader->bindVec3(shader->getLocation("materialKs"), ks);
|
||||||
|
shader->bindFloat(shader->getLocation("materialNs"), ns);
|
||||||
|
}
|
||||||
|
|
18
phongmaterial.h
Normal file
18
phongmaterial.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#ifndef PHONGMATERIAL_H
|
||||||
|
#define PHONGMATERIAL_H
|
||||||
|
|
||||||
|
#include "material.h"
|
||||||
|
#include "glm/vec3.hpp"
|
||||||
|
|
||||||
|
class PhongMaterial : public Material
|
||||||
|
{
|
||||||
|
glm::vec3 kd;
|
||||||
|
glm::vec3 ks;
|
||||||
|
float ns;
|
||||||
|
public:
|
||||||
|
PhongMaterial(Shader* myShader) : Material(myShader), kd(0.5f), ks(0.5f), ns(10) {}
|
||||||
|
PhongMaterial(Shader* myShader, glm::vec3 myKd, glm::vec3 myKs, float myNs) : Material(myShader), kd(myKd), ks(myKs), ns(myNs) {}
|
||||||
|
virtual void bindAttributes();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // PHONGMATERIAL_H
|
24
scene.cpp
24
scene.cpp
@ -15,25 +15,27 @@ Scene::~Scene()
|
|||||||
delete(e);
|
delete(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::addEntity(Mesh& mesh, Material& mat)
|
void Scene::addEntity(Mesh* mesh, Material* mat)
|
||||||
{
|
{
|
||||||
entities.push_back(new Entity(NULL, mesh, mat));
|
entities.push_back(new Entity(NULL, mesh, mat));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::addEntity(Mesh& mesh)
|
void Scene::drawEntities()
|
||||||
{
|
|
||||||
Material default_material = Material();
|
|
||||||
entities.push_back(new Entity(NULL, mesh, default_material));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Scene::drawEntities(GLuint programId)
|
|
||||||
{
|
{
|
||||||
glm::mat4 viewMatrix = camera->getViewMatrix();
|
glm::mat4 viewMatrix = camera->getViewMatrix();
|
||||||
glm::mat4 projectionMatrix = camera->getProjectionMatrix();
|
glm::mat4 projectionMatrix = camera->getProjectionMatrix();
|
||||||
glAssert(GLuint viewMatrixUniform = glGetUniformLocation(programId, "viewMatrix"));
|
|
||||||
glAssert(glUniformMatrix4fv(viewMatrixUniform, 1, GL_FALSE, glm::value_ptr(viewMatrix)));
|
|
||||||
for(Entity* e : entities)
|
for(Entity* e : entities)
|
||||||
e->mesh.draw();
|
{
|
||||||
|
glm::mat4 modelViewMatrix = viewMatrix * e->modelMatrix;
|
||||||
|
glm::mat4 mvp = projectionMatrix * modelViewMatrix;
|
||||||
|
Shader* shader = e->mat->getShader();
|
||||||
|
shader->bind();
|
||||||
|
e->mat->bindAttributes();
|
||||||
|
shader->bindMatrix(shader->getLocation("viewMatrix"), viewMatrix);
|
||||||
|
shader->bindMatrix(shader->getLocation("modelViewMatrix"), modelViewMatrix);
|
||||||
|
shader->bindMatrix(shader->getLocation("MVP"), mvp);
|
||||||
|
e->mesh->draw();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::setCamera(Camera* myCamera)
|
void Scene::setCamera(Camera* myCamera)
|
||||||
|
11
scene.h
11
scene.h
@ -11,11 +11,11 @@ class Scene
|
|||||||
{
|
{
|
||||||
typedef struct s_entity
|
typedef struct s_entity
|
||||||
{
|
{
|
||||||
Material mat;
|
Material* mat;
|
||||||
Mesh mesh;
|
Mesh* mesh;
|
||||||
s_entity* parent;
|
s_entity* parent;
|
||||||
glm::mat4 modelMatrix;
|
glm::mat4 modelMatrix;
|
||||||
s_entity(s_entity* myParent, Mesh &myMesh, Material &myMat)
|
s_entity(s_entity* myParent, Mesh* myMesh, Material* myMat)
|
||||||
{
|
{
|
||||||
parent = myParent;
|
parent = myParent;
|
||||||
mesh = myMesh;
|
mesh = myMesh;
|
||||||
@ -29,9 +29,8 @@ class Scene
|
|||||||
public:
|
public:
|
||||||
Scene();
|
Scene();
|
||||||
~Scene();
|
~Scene();
|
||||||
void addEntity(Mesh& mesh, Material& mat);
|
void addEntity(Mesh* mesh, Material* mat);
|
||||||
void addEntity(Mesh& mesh);
|
void drawEntities();
|
||||||
void drawEntities(GLuint programId);
|
|
||||||
void setCamera(Camera* myCamera);
|
void setCamera(Camera* myCamera);
|
||||||
Camera* getCamera();
|
Camera* getCamera();
|
||||||
};
|
};
|
||||||
|
31
shader.cpp
31
shader.cpp
@ -4,6 +4,7 @@
|
|||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "glassert.h"
|
#include "glassert.h"
|
||||||
|
#include <glm/ext.hpp>
|
||||||
|
|
||||||
const std::string Shader::DEFAULT_VERT = "#version 330\nlayout(location = 0)in vec3 inPosition;\nvoid main(){gl_Position = vec4(inPosition, 1.0);}";
|
const std::string Shader::DEFAULT_VERT = "#version 330\nlayout(location = 0)in vec3 inPosition;\nvoid main(){gl_Position = vec4(inPosition, 1.0);}";
|
||||||
const std::string Shader::DEFAULT_FRAG = "#version 330\nlayout(location = 0)out vec4 outColor;\nvoid main(){outColor = vec4(1, 0, 0, 1);}";
|
const std::string Shader::DEFAULT_FRAG = "#version 330\nlayout(location = 0)out vec4 outColor;\nvoid main(){outColor = vec4(1, 0, 0, 1);}";
|
||||||
@ -108,7 +109,33 @@ void Shader::printProgramInfoLog(GLuint programId)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GLuint Shader::getProgramId()
|
GLuint Shader::getLocation(std::string attribName)
|
||||||
{
|
{
|
||||||
return program;
|
glAssert(GLuint loc = glGetUniformLocation(program, attribName.c_str()));
|
||||||
|
return loc;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shader::bind()
|
||||||
|
{
|
||||||
|
glAssert(glUseProgram(program));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shader::unbind()
|
||||||
|
{
|
||||||
|
glAssert(glUseProgram(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shader::bindFloat(GLuint location, float val)
|
||||||
|
{
|
||||||
|
glAssert(glUniform1f(location, val));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shader::bindMatrix(GLuint location, glm::mat4 mat)
|
||||||
|
{
|
||||||
|
glAssert(glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(mat)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shader::bindVec3(GLuint location, glm::vec3 vec)
|
||||||
|
{
|
||||||
|
glAssert(glUniform3fv(location, 1, glm::value_ptr(vec)));
|
||||||
}
|
}
|
||||||
|
9
shader.h
9
shader.h
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <glew/glew.h>
|
#include <glew/glew.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <glm/fwd.hpp>
|
||||||
|
|
||||||
class QString;
|
class QString;
|
||||||
|
|
||||||
@ -19,7 +20,13 @@ class Shader
|
|||||||
public:
|
public:
|
||||||
Shader(const QString &vertFilename, const QString &fragFilename);
|
Shader(const QString &vertFilename, const QString &fragFilename);
|
||||||
~Shader();
|
~Shader();
|
||||||
GLuint getProgramId();
|
GLuint getLocation(std::string attribName);
|
||||||
|
|
||||||
|
void bind();
|
||||||
|
void unbind();
|
||||||
|
void bindFloat(GLuint location, float val);
|
||||||
|
void bindMatrix(GLuint location, glm::mat4 mat);
|
||||||
|
void bindVec3(GLuint location, glm::vec3 vec);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SHADER_H
|
#endif // SHADER_H
|
||||||
|
@ -27,8 +27,9 @@ SOURCES += main.cpp\
|
|||||||
camera.cpp \
|
camera.cpp \
|
||||||
sparrowrenderer.cpp \
|
sparrowrenderer.cpp \
|
||||||
scene.cpp \
|
scene.cpp \
|
||||||
material.cpp \
|
gridmesh.cpp \
|
||||||
gridmesh.cpp
|
texture.cpp \
|
||||||
|
phongmaterial.cpp
|
||||||
|
|
||||||
HEADERS += mainwindow.h \
|
HEADERS += mainwindow.h \
|
||||||
myglwidget.h \
|
myglwidget.h \
|
||||||
@ -39,7 +40,9 @@ HEADERS += mainwindow.h \
|
|||||||
glassert.h \
|
glassert.h \
|
||||||
scene.h \
|
scene.h \
|
||||||
material.h \
|
material.h \
|
||||||
gridmesh.h
|
gridmesh.h \
|
||||||
|
texture.h \
|
||||||
|
phongmaterial.h
|
||||||
|
|
||||||
FORMS += mainwindow.ui
|
FORMS += mainwindow.ui
|
||||||
|
|
||||||
|
@ -1,22 +1,27 @@
|
|||||||
#include "sparrowrenderer.h"
|
#include "sparrowrenderer.h"
|
||||||
|
#include "phongmaterial.h"
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
#include "scene.h"
|
#include "scene.h"
|
||||||
|
#include "gridmesh.h"
|
||||||
#include "glassert.h"
|
#include "glassert.h"
|
||||||
#include <glew/glew.h>
|
#include <glew/glew.h>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
SparrowRenderer::SparrowRenderer(int width, int height)
|
SparrowRenderer::SparrowRenderer(int width, int height)
|
||||||
{
|
{
|
||||||
shader = new Shader(QString("../phong.vert"), QString("../phong.frag"));
|
Shader* shader = new Shader(QString("../phong.vert"), QString("../phong.frag"));
|
||||||
|
Material* mat = new PhongMaterial(shader);
|
||||||
scene = new Scene();
|
scene = new Scene();
|
||||||
scene->setCamera(new Camera(width, height));
|
scene->setCamera(new Camera(width, height));
|
||||||
|
Mesh* myGrid = new GridMesh(10, 10, false);
|
||||||
|
myGrid->initGL();
|
||||||
|
scene->addEntity(myGrid, mat);
|
||||||
}
|
}
|
||||||
|
|
||||||
SparrowRenderer::~SparrowRenderer()
|
SparrowRenderer::~SparrowRenderer()
|
||||||
{
|
{
|
||||||
delete(scene->getCamera());
|
delete(scene->getCamera());
|
||||||
delete(shader);
|
|
||||||
delete(scene);
|
delete(scene);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,9 +32,6 @@ void SparrowRenderer::resize(int width, int height)
|
|||||||
|
|
||||||
void SparrowRenderer::render()
|
void SparrowRenderer::render()
|
||||||
{
|
{
|
||||||
// bind shader
|
|
||||||
GLuint programId = shader->getProgramId();
|
|
||||||
glAssert(glUseProgram(programId));
|
|
||||||
// draw geometry
|
// draw geometry
|
||||||
scene->drawEntities(programId);
|
scene->drawEntities();
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ class Scene;
|
|||||||
|
|
||||||
class SparrowRenderer
|
class SparrowRenderer
|
||||||
{
|
{
|
||||||
Shader* shader;
|
|
||||||
Scene* scene;
|
Scene* scene;
|
||||||
public:
|
public:
|
||||||
SparrowRenderer(int width, int height);
|
SparrowRenderer(int width, int height);
|
||||||
|
12
texture.cpp
Normal file
12
texture.cpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#include "texture.h"
|
||||||
|
|
||||||
|
Texture::Texture()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Texture::~Texture()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user