added lots of things, but it crashes

This commit is contained in:
unknown 2015-06-26 14:56:43 +02:00
commit 2bd9205c37
15 changed files with 133 additions and 46 deletions

View File

@ -1,6 +1,6 @@
#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)
{
@ -38,5 +38,5 @@ GridMesh::GridMesh(int width, int height, bool alternate) : Mesh()
int GridMesh::getVertexId(int i, int j)
{
return i*(height+1) + j;
return i*(m_height+1) + j;
}

View File

@ -8,6 +8,8 @@ class GridMesh : public Mesh
public:
GridMesh(int width, int height, bool alternate);
private:
int m_height;
int m_width;
int getVertexId(int i, int j);
};

View File

@ -1,12 +0,0 @@
#include "material.h"
Material::Material()
{
}
Material::~Material()
{
}

View File

@ -1,12 +1,19 @@
#ifndef MATERIAL_H
#define MATERIAL_H
#include "shader.h"
#include "glm/fwd.hpp"
class Material
{
public:
Material();
~Material();
Material(Shader* myShader) : shader(myShader) {}
Shader* getShader() {return shader;}
virtual void bindAttributes() = 0;
protected:
Shader* shader;
};
#endif // MATERIAL_H

9
phongmaterial.cpp Normal file
View 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
View 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

View File

@ -15,25 +15,27 @@ Scene::~Scene()
delete(e);
}
void Scene::addEntity(Mesh& mesh, Material& mat)
void Scene::addEntity(Mesh* mesh, Material* mat)
{
entities.push_back(new Entity(NULL, mesh, mat));
}
void Scene::addEntity(Mesh& mesh)
{
Material default_material = Material();
entities.push_back(new Entity(NULL, mesh, default_material));
}
void Scene::drawEntities(GLuint programId)
void Scene::drawEntities()
{
glm::mat4 viewMatrix = camera->getViewMatrix();
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)
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)

11
scene.h
View File

@ -11,11 +11,11 @@ class Scene
{
typedef struct s_entity
{
Material mat;
Mesh mesh;
Material* mat;
Mesh* mesh;
s_entity* parent;
glm::mat4 modelMatrix;
s_entity(s_entity* myParent, Mesh &myMesh, Material &myMat)
s_entity(s_entity* myParent, Mesh* myMesh, Material* myMat)
{
parent = myParent;
mesh = myMesh;
@ -29,9 +29,8 @@ class Scene
public:
Scene();
~Scene();
void addEntity(Mesh& mesh, Material& mat);
void addEntity(Mesh& mesh);
void drawEntities(GLuint programId);
void addEntity(Mesh* mesh, Material* mat);
void drawEntities();
void setCamera(Camera* myCamera);
Camera* getCamera();
};

View File

@ -4,6 +4,7 @@
#include <QTextStream>
#include <iostream>
#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_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)));
}

View File

@ -3,6 +3,7 @@
#include <glew/glew.h>
#include <string>
#include <glm/fwd.hpp>
class QString;
@ -19,7 +20,13 @@ class Shader
public:
Shader(const QString &vertFilename, const QString &fragFilename);
~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

View File

@ -27,8 +27,9 @@ SOURCES += main.cpp\
camera.cpp \
sparrowrenderer.cpp \
scene.cpp \
material.cpp \
gridmesh.cpp
gridmesh.cpp \
texture.cpp \
phongmaterial.cpp
HEADERS += mainwindow.h \
myglwidget.h \
@ -39,7 +40,9 @@ HEADERS += mainwindow.h \
glassert.h \
scene.h \
material.h \
gridmesh.h
gridmesh.h \
texture.h \
phongmaterial.h
FORMS += mainwindow.ui

View File

@ -1,22 +1,27 @@
#include "sparrowrenderer.h"
#include "phongmaterial.h"
#include "shader.h"
#include "camera.h"
#include "scene.h"
#include "gridmesh.h"
#include "glassert.h"
#include <glew/glew.h>
#include <QString>
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->setCamera(new Camera(width, height));
Mesh* myGrid = new GridMesh(10, 10, false);
myGrid->initGL();
scene->addEntity(myGrid, mat);
}
SparrowRenderer::~SparrowRenderer()
{
delete(scene->getCamera());
delete(shader);
delete(scene);
}
@ -27,9 +32,6 @@ void SparrowRenderer::resize(int width, int height)
void SparrowRenderer::render()
{
// bind shader
GLuint programId = shader->getProgramId();
glAssert(glUseProgram(programId));
// draw geometry
scene->drawEntities(programId);
scene->drawEntities();
}

View File

@ -7,7 +7,6 @@ class Scene;
class SparrowRenderer
{
Shader* shader;
Scene* scene;
public:
SparrowRenderer(int width, int height);

12
texture.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "texture.h"
Texture::Texture()
{
}
Texture::~Texture()
{
}

12
texture.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef TEXTURE_H
#define TEXTURE_H
class Texture
{
public:
Texture();
~Texture();
};
#endif // TEXTURE_H