This commit is contained in:
Jasper 2014-11-14 14:31:32 +01:00
commit 0d9300193c
15 changed files with 209 additions and 56 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 223 KiB

After

Width:  |  Height:  |  Size: 35 KiB

View File

@ -1,7 +1,8 @@
#version 150
uniform mat4 uViewMatrix;
uniform mat4 uProjectionMatrix;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
in vec3 aNormal;
in vec3 aPosition;
@ -13,8 +14,8 @@ out vec4 fragPosition;
void main()
{
fragPosition = uViewMatrix * vec4(aPosition, 1.0);
vNormal = inverse(transpose(mat3(uViewMatrix))) * aNormal;
fragPosition = modelMatrix * vec4(aPosition, 1.0);
vNormal = inverse(transpose(mat3(modelMatrix))) * aNormal;
vTexCoord = aTexCoord;
gl_Position = uProjectionMatrix * uViewMatrix * vec4(aPosition, 1.0);
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(aPosition, 1.0);
}

34
camera.cc Normal file
View File

@ -0,0 +1,34 @@
#include "camera.hh"
Camera::Camera(glm::vec3 rotation, float distance) {
this->rotation = rotation;
this->distance = distance;
}
Camera::Camera() {
rotation = glm::vec3(0.0f, 0.0f, 0.0f);
distance = 1.0f;
}
Camera::~Camera() {
}
float Camera::getDistance() {
return distance;
}
void Camera::setDistance(float distance) {
this->distance = distance;
}
glm::vec3 Camera::getRotation() {
return rotation;
}
void Camera::setRotation(glm::vec3 rotation) {
this->rotation = rotation;
}
void Camera::updateRotation(glm::vec3 rotation) {
this->rotation += rotation;;
}

21
camera.hh Normal file
View File

@ -0,0 +1,21 @@
#ifndef CAMERA_HH_INCLUDED
#define CAMERA_HH_INCLUDED
#include <ACGL/Math/Math.hh>
class Camera {
public:
Camera(glm::vec3 rotation, float distance);
Camera();
~Camera();
float getDistance();
void setDistance(float distance);
glm::vec3 getRotation();
void setRotation(glm::vec3 rotation);
void updateRotation(glm::vec3 rotation); //adds to current rotation
private:
float distance;
glm::vec3 rotation;
};
#endif

View File

@ -14,3 +14,15 @@ Entity::~Entity(){
glm::vec3 Entity::getPosition() {
return position;
}
glm::vec3 Entity::getRotation() {
return rotation;
}
void Entity::setPosition(glm::vec3 position) {
this->position = position;
}
void Entity::setRotation(glm::vec3 rotation) {
this->rotation = rotation;
}

View File

@ -38,15 +38,18 @@ void deleteCustomResources()
// we have memory management via reference counting, so nothing to do here
}
void draw( float runTime )
void draw(float runTime)
{
// update Level first TODO: move this with the rest of the stuff that doesn't belong here to main
level.update(runTime);
// clear the framebuffer:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set view and projection matrix:
glm::mat4 viewMatrix = glm::translate(glm::vec3(0.0f, -1.0f, -2.0f)) * glm::rotate<float>(1.0472f * runTime, glm::vec3(0.0f, 1.0f, 0.0f)) * glm::scale<float>(glm::vec3(0.25f));
shader->setUniform( "uViewMatrix", viewMatrix );
shader->setUniform( "uProjectionMatrix", buildFrustum(75.0, 0.1, 100.0, (float)g_windowSize.x/(float)g_windowSize.y) );
//set view and projection matrix
shader->setUniform("projectionMatrix", buildFrustum(75.0, 0.1, 100.0, (float)g_windowSize.x/(float)g_windowSize.y) );
// the + (0,1,0) compensates bunny doesn't have its center at it's center
shader->setUniform("viewMatrix", buildViewMatrix());
//set lighting parameters
if (level.getLights().size() > 0) {
@ -101,3 +104,13 @@ glm::mat4 buildFrustum( float phiInDegree, float _near, float _far, float aspect
return glm::frustum(left, right, bottom, top, _near, _far);
}
glm::mat4 buildViewMatrix() {
glm::vec4 cameraVector = glm::vec4(0.0f, 0.0f, level.getCamera().getDistance(), 0.0f);
// rotate vector
glm::mat4 rotationMatrix = glm::rotate<float>(level.getCamera().getRotation()[0], glm::vec3(1.0f, 0.0f, 0.0f)) *
glm::rotate<float>(level.getCamera().getRotation()[1], glm::vec3(0.0f, 1.0f, 0.0f)) * glm::rotate<float>(level.getCamera().getRotation()[2], glm::vec3(0.0f, 0.0f, 1.0f));
cameraVector = rotationMatrix * cameraVector;
//construct lookAt (cameraPosition = cameraCenter + cameraVector
return glm::lookAt(level.getCameraCenter()->getPosition() + glm::vec3(cameraVector), level.getCameraCenter()->getPosition(), glm::vec3(0.0f, 1.0f, 0.0f));
}

View File

@ -13,7 +13,7 @@ void initCustomResources();
void deleteCustomResources();
// gets called ech frame, runTime is in seconds:
void draw( float runTime );
void draw(float runTime);
// gets called at window resize:
void resizeCallback( GLFWwindow *, int newWidth, int newHeight );
@ -21,4 +21,6 @@ void resizeCallback( GLFWwindow *, int newWidth, int newHeight );
// to build the projection matrix:
glm::mat4 buildFrustum( float phiInDegree, float near, float far, float aspectRatio);
glm::mat4 buildViewMatrix();
#endif

View File

@ -12,33 +12,38 @@ Level::~Level() {
}
void Level::load(ACGL::OpenGL::SharedShaderProgram shader) {
//this->terrain.load();
// currently hard coded should later read this stuff out of a file
this->camera = Camera(glm::vec3(-0.8f, 0.0f, 0.0f), 3.0f);
// load the geometry of the stanford bunny and build a VAO:
Model model = Model("Bunny.obj");
Model model = Model("Bunny.obj", 0.25f);
// load a texture:
Material material = Material("clownfishBunny.png", 0.1f, 0.5f, 0.5f, 3.0f);
//Create object
Object object = Object(model, material, glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f),
Object object = Object(model, material, glm::vec3(0.0f, -1.0f, -2.0f),
glm::vec3(0.0f, 1.0472f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f), shader);
objects.push_back(object);
cameraCenter = &objects[0];
//set lighting parameters
ambientLight = glm::vec3(1.0f, 1.0f, 1.0f);
Light light = Light(glm::vec3(-3.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f), 2.0f);
lights.push_back(light);
Light light2 = Light(glm::vec3(3.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), 2.0f);
lights.push_back(light2);
// load terrain
this->terrain.load();
}
void Level::render() {
for(unsigned int i = 0; i<objects.size(); i++) {
objects[i].render();
}
//this->terrain.render();
this->terrain.render();
}
void Level::update(float runTime) {
// rotate bunny
cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f));
}
glm::vec3 Level::getAmbientLight() {
@ -48,3 +53,11 @@ glm::vec3 Level::getAmbientLight() {
std::vector<Light> Level::getLights() {
return lights;
}
Camera Level::getCamera() {
return camera;
}
Object* Level::getCameraCenter() {
return cameraCenter;
}

View File

@ -7,6 +7,7 @@
#include "entity.hh"
#include "terrain.hh"
#include "material.hh"
#include "camera.hh"
class Level {
public:
@ -14,14 +15,19 @@ class Level {
Level();
~Level();
void load(ACGL::OpenGL::SharedShaderProgram shader); // Shader is necessary for correct texture assigning
void update(float runTime);
void render();
glm::vec3 getAmbientLight();
std::vector<Light> getLights();
Object* getCameraCenter();
Camera getCamera();
private:
std::string filePath;
std::vector<Object> objects;
std::vector<Light> lights;
glm::vec3 ambientLight;
Object* cameraCenter;
Camera camera;
Terrain terrain;
};

View File

@ -1,8 +1,15 @@
#include "model.hh"
Model::Model(std::string filePath, float scale) {
reference = ACGL::OpenGL::VertexArrayObjectCreator(filePath).create();
reference->bind();
this->scale = scale;
}
Model::Model(std::string filePath) {
reference = ACGL::OpenGL::VertexArrayObjectCreator(filePath).create();
reference->bind();
this->scale = 1.0f;
}
Model::Model(){
@ -14,3 +21,11 @@ Model::~Model() {
ACGL::OpenGL::SharedVertexArrayObject Model::getReference() {
return reference;
}
void Model::setScale(float scale) {
this->scale = scale;
}
float Model::getScale() {
return scale;
}

View File

@ -6,12 +6,16 @@
class Model {
public:
Model(std::string filePath, float scale);
Model(std::string filePath);
Model();
~Model();
ACGL::OpenGL::SharedVertexArrayObject getReference();
void setScale(float scale);
float getScale();
private:
ACGL::OpenGL::SharedVertexArrayObject reference;
float scale;
};
#endif

View File

@ -3,7 +3,7 @@
Object::Object(Model model, Material material, glm::vec3 position, glm::vec3 rotation,
glm::vec3 velocity, glm::vec3 angularVelocity, ACGL::OpenGL::SharedShaderProgram shader) :
Entity(position, rotation) {
this->model = model.getReference();
this->model = model;
this->material = material;
this->velocity = velocity;
this->angularVelocity = angularVelocity;
@ -17,10 +17,17 @@ Object::~Object() {
}
void Object::render() {
// set lightning parameters for this object
shader->setUniform("ambientFactor", material.getAmbientFactor());
shader->setUniform("diffuseFactor", material.getDiffuseFactor());
shader->setUniform("specularFactor", material.getSpecularFactor());
shader->setUniform("shininess", material.getShininess());
shader->setTexture("uTexture", material.getReference(), 0);
model->render();
// set model matrix
glm::mat4 rotationMatrix = glm::rotate<float>(this->getRotation()[0], glm::vec3(1.0f, 0.0f, 0.0f)) *
glm::rotate<float>(this->getRotation()[1], glm::vec3(0.0f, 1.0f, 0.0f)) * glm::rotate<float>(this->getRotation()[2], glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 modelMatrix = glm::translate(this->getPosition()) * rotationMatrix * glm::scale<float>(glm::vec3(model.getScale()));
shader->setUniform( "modelMatrix", modelMatrix);
// draw
model.getReference()->render();
}

View File

@ -18,7 +18,7 @@ class Object : public Entity {
~Object();
void render();
private:
ACGL::OpenGL::SharedVertexArrayObject model;
Model model;
Material material;
glm::vec3 velocity;
glm::vec3 angularVelocity;

View File

@ -12,29 +12,25 @@ Terrain::~Terrain() {
void Terrain::load() {
std::ifstream terrain_png(this->filePath + "/heightmap.png"); //TODO: filepath organization
this->filePath = "../Levels/LevelTest/terrain"; //TODO remove this, its only for testing
std::ifstream terrain_png(this->filePath + "/heightmap.png");
unsigned int rowNum, columnNum, heightmapValue;
terrain_png.seekg(16); //skip part of the header
terrain_png.read((char *)&this->heightmapWidth, 4); //read width
terrain_png.read((char *)&this->heightmapHeight, 4); //read height
this->heightmapWidth = ntohl(this->heightmapWidth); //convert from network to host byte order
this->heightmapHeight = ntohl(this->heightmapHeight);
/* //alternate implementation that does NOT work at all
char temp[2];4???
char temp[4];
terrain_png.read(temp, 4); //read width
this->heightmapWidth = (temp[1]<<0) | (temp[0]<<8); //convert from network to host byte order
this->heightmapWidth = (temp[3]<<0) | (temp[2]<<8) | (temp[1]<<16) | (temp[0]<<24); //convert from network to host byte order
terrain_png.read(temp, 4); //read height
this->heightmapHeight = (temp[1]<<0) | (temp[0]<<8); //convert from network to host byte order
*/
this->heightmapHeight = (temp[3]<<0) | (temp[2]<<8) | (temp[1]<<16) | (temp[0]<<24); //convert from network to host byte order
heightmap = new float*[this->heightmapHeight]; //initialize the heightmap
for(rowNum = 0; rowNum < this->heightmapHeight; rowNum++){ //read in the heightmap
heightmap[rowNum] = new float[this->heightmapWidth];
for(columnNum = 0; columnNum < this->heightmapWidth; columnNum++){
terrain_png.read((char *)&heightmapValue, 1);
heightmap[rowNum][columnNum] = (float)heightmapValue / 5;
heightmap[rowNum][columnNum] = (float)heightmapValue / 256;
}
}
@ -45,24 +41,32 @@ void Terrain::load() {
void Terrain::makeTriangleMesh(){
ACGL::OpenGL::SharedArrayBuffer arrayBuffer = ACGL::OpenGL::SharedArrayBuffer();
arrayBuffer->defineAttribute("pos", GL_FLOAT, 3); //TODO: ArrayBuffer for the texture coordinates
ACGL::OpenGL::SharedArrayBuffer ab = std::make_shared<ACGL::OpenGL::ArrayBuffer>();
ab->defineAttribute("pos", GL_FLOAT, 3); //TODO: ArrayBuffer for the texture coordinates
unsigned int rowNum=0, columnNum=0; //initializing:
unsigned int rowNum=0, columnNum=0, dataCount=0; //initializing:
bool movingRight = true, isUp = true;
int numVertices = (this->heightmapHeight - 1) * (this->heightmapWidth * 2 + 1) + 1;
float* abData = new float[numVertices * 3];
while(rowNum < this->heightmapHeight){ //traversing the Triangle Strip!
float newPos[3];
newPos[0] = (float)rowNum;
newPos[1] = (float)columnNum;
newPos[2] = heightmap[rowNum][columnNum];
arrayBuffer->setDataElements(1, &newPos);
abData[dataCount] = (float)rowNum;
abData[dataCount+1] = heightmap[rowNum][columnNum];
abData[dataCount+2] = (float)columnNum;
dataCount += 3;
if (isUp){
rowNum = rowNum + 1;
isUp = false;
}else if (movingRight){
if (columnNum == this->heightmapWidth - 1){
arrayBuffer->setDataElements(1, &newPos);
arrayBuffer->setDataElements(1, &newPos);
abData[dataCount] = (float)rowNum;
abData[dataCount+1] = heightmap[rowNum][columnNum];
abData[dataCount+2] = (float)columnNum;
dataCount += 3;
abData[dataCount] = (float)rowNum;
abData[dataCount+1] = heightmap[rowNum][columnNum];
abData[dataCount+2] = (float)columnNum;
dataCount += 3;
movingRight = false;
rowNum = rowNum + 1;
} else{
@ -72,8 +76,14 @@ void Terrain::makeTriangleMesh(){
}
}else{
if (columnNum == 0){
arrayBuffer->setDataElements(1, &newPos);
arrayBuffer->setDataElements(1, &newPos);
abData[dataCount] = (float)rowNum;
abData[dataCount+1] = heightmap[rowNum][columnNum];
abData[dataCount+2] = (float)columnNum;
dataCount += 3;
abData[dataCount] = (float)rowNum;
abData[dataCount+1] = heightmap[rowNum][columnNum];
abData[dataCount+2] = (float)columnNum;
dataCount += 3;
movingRight = true;
rowNum = rowNum + 1;
}else{
@ -84,11 +94,27 @@ void Terrain::makeTriangleMesh(){
}
}
this->triangleMesh = ACGL::OpenGL::SharedVertexArrayObject();
ab->setDataElements(numVertices, abData);
this->triangleMesh = std::make_shared<ACGL::OpenGL::VertexArrayObject>();
this->triangleMesh->bind();
this->triangleMesh->setMode(GL_TRIANGLE_STRIP);
this->triangleMesh->attachAllAttributes(arrayBuffer);
this->triangleMesh->attachAllAttributes(ab);
//TODO unbind?
//TODO remove this TestCode (that doesnt even work yet...):
/* ACGL::OpenGL::SharedArrayBuffer tex = std::make_shared<ACGL::OpenGL::ArrayBuffer>();
tex->defineAttribute("color", GL_FLOAT, 3);
float* texData = new float[numVertices*3];
for (int i=0; i<numVertices*3; i++){
texData[i] = 1.0;
}
tex->setDataElements(numVertices, texData);
this->triangleMesh->attachAllAttributes(tex);
*/
}
void Terrain::render() {

View File

@ -4,7 +4,6 @@
#include <string>
#include "material.hh"
#include <fstream>
#include <netinet/in.h>
#include <ACGL/OpenGL/Objects/VertexArrayObject.hh>
class Terrain {