Skip to content

Commit

Permalink
vao, vbo and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mohlek committed Aug 13, 2017
1 parent c9c0723 commit f7e17fe
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 32 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ set(SOURCE
src/engine/Buffer.cpp
src/engine/VertexArrayObject.cpp
src/engine/Object.cpp
src/engine/Camera.cpp
src/engine/shader/ShaderLoader.cpp
src/engine/shader/Shader.cpp
src/engine/shader/ShaderProgram.cpp
src/engine/model/3ds/Model3DS.cpp
src/shader/MainShader.cpp
src/main.cpp
)

Expand Down
17 changes: 15 additions & 2 deletions src/engine/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

#include <cstring>

#define MEMCPY

using namespace Engine;

Buffer::Buffer(GLenum bufferType) : bufferType(bufferType) {
glGenBuffers(1, &this->bufferId);
glCreateBuffers(1, &this->bufferId);
}

Buffer::~Buffer() {
Expand Down Expand Up @@ -33,9 +35,20 @@ void* Buffer::map(GLenum access = GL_READ_WRITE) {

void Buffer::pushData(void* data, int size) {
this->size = size;

#ifdef MEMCPY
void* addr = map(GL_WRITE_ONLY);

std::memcpy(addr, data, this->size);
unmap();
#else
if (glNamedBufferSubData) {
glNamedBufferSubData(this->bufferId, 0, size, data);
}

bind();
glBufferSubData(this->bufferType, 0, size, data);
#endif

}

bool Buffer::unmap() {
Expand Down
5 changes: 5 additions & 0 deletions src/engine/Buffer.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef BUFFER_H
#define BUFFER_H

#include <GL/glew.h>

namespace Engine {
Expand All @@ -24,3 +27,5 @@ namespace Engine {
bool unmap();
};
}

#endif
25 changes: 25 additions & 0 deletions src/engine/Camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "Camera.h"

using namespace Engine;

Camera::Camera() {
glGenBuffers(1, &this->cameraBuffer);
}

Camera::~Camera() {
glBindBuffer(GL_UNIFORM_BUFFER, this->cameraBuffer);
glDeleteBuffers(1, &this->cameraBuffer);
}

void Camera::setCamera(glm::vec3 cameraPos, glm::vec3 lookAt) {
if (!this->cameraBuffer) {
return;
}

float realMatrix[] = {cameraPos.x, cameraPos.y, cameraPos.z, 0.0f,
lookAt.x, lookAt.y, lookAt.z, 0.0f};

glBindBuffer(GL_UNIFORM_BUFFER, this->cameraBuffer);
glBufferData(GL_UNIFORM_BUFFER, sizeof(float) * 8, realMatrix, GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
16 changes: 16 additions & 0 deletions src/engine/Camera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <GL/glew.h>

#include <glm/glm.hpp>

namespace Engine {

class Camera {
private:
GLuint cameraBuffer;
public:
Camera();
~Camera();

void setCamera(glm::vec3 cameraPos, glm::vec3 lookAt);
};
}
20 changes: 18 additions & 2 deletions src/engine/VertexArrayObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
using namespace Engine;

VertexArrayObject::VertexArrayObject() {
if (glCreateVertexArrays) {
glCreateVertexArrays(1, &this->vaoId);
return;
}
glGenVertexArrays(1, &this->vaoId);
}

Expand All @@ -13,7 +17,19 @@ VertexArrayObject::~VertexArrayObject() {

void VertexArrayObject::addBuffer(std::shared_ptr<Buffer>& buffer) {
const int index = this->buffers.size();
glEnableVertexAttribArray(index);
glVertexAttribPointer(index, buffer->size, buffer->dataType, GL_FALSE, buffer->stride, 0);

if (glEnableVertexArrayAttrib) {
glEnableVertexArrayAttrib(this->vaoId, index);
} else {
bind();
glEnableVertexAttribArray(index);
}

buffer->bind();
glVertexAttribPointer(index, 3, buffer->dataType, GL_FALSE, buffer->stride, 0);
this->buffers.push_back(buffer);
}

void VertexArrayObject::bind() {
glBindVertexArray(this->vaoId);
}
2 changes: 2 additions & 0 deletions src/engine/VertexArrayObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ namespace Engine {
virtual ~VertexArrayObject();

void addBuffer(std::shared_ptr<Buffer>& buffer);

void bind();
};
}
10 changes: 6 additions & 4 deletions src/engine/model/3ds/Model3DS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using namespace Engine;

Model3DS::Model3DS(std::string&& filename) {
this->vertexVBO = std::make_shared<Buffer>();
this->normalsVBO = std::make_shared<Buffer>();
this->model = lib3ds_file_load(filename.c_str());

if (!model) {
Expand Down Expand Up @@ -56,11 +58,11 @@ void Model3DS::createVBO() {

unsigned int size = sizeof(Lib3dsVector) * 3 * totalFaces;

this->vertexVBO.stride = sizeof(Lib3dsVector) * 3;
this->vertexVBO.pushData(vertices, size);
this->vertexVBO->stride = sizeof(Lib3dsVector) * 3;
this->vertexVBO->pushData(vertices, size);

this->normalsVBO.stride = sizeof(Lib3dsVector) * 3;
this->normalsVBO.pushData(normals, size);
this->normalsVBO->stride = sizeof(Lib3dsVector) * 3;
this->normalsVBO->pushData(normals, size);

delete vertices;
delete normals;
Expand Down
14 changes: 10 additions & 4 deletions src/engine/model/3ds/Model3DS.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#ifndef MODEL3DS_H
#define MODEL3DS_H

#include <lib3ds/file.h>
#include <GL/glew.h>

#include "../../Buffer.h"

#include <string>
#include <memory>

#include "../../Buffer.h"

namespace Engine {
class Model3DS {
Expand All @@ -12,12 +16,14 @@ namespace Engine {

unsigned int getTotalFaces();
public:
Engine::Buffer vertexVBO;
Engine::Buffer normalsVBO;
std::shared_ptr<Buffer> vertexVBO;
std::shared_ptr<Buffer> normalsVBO;

Model3DS(std::string&& filename);
~Model3DS();
void render();
void createVBO();
};
}

#endif
3 changes: 3 additions & 0 deletions src/engine/shader/ShaderProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ void printProgramInfoLog(GLuint obj) {
}
}

GLint ShaderProgram::getUniformLocation(std::string&& name) {
return glGetUniformLocation(this->programId, name.c_str());
}

void ShaderProgram::link() {
this->programId = glCreateProgram();
Expand Down
7 changes: 6 additions & 1 deletion src/engine/shader/ShaderProgram.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <GL/glew.h>

#include <string>
#include <vector>
#include <map>
#include <memory>

#include "Shader.h"
Expand All @@ -13,10 +15,13 @@ namespace Engine {

public:
std::vector<std::shared_ptr<Shader>> shaders;

std::map<std::string, GLuint> uniforms;

ShaderProgram();
virtual ~ShaderProgram();

GLint getUniformLocation(std::string&& name);

void link();
void use();
void end();
Expand Down
45 changes: 27 additions & 18 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#include <stdio.h>
#include <stdlib.h>

#include <glm/glm.hpp>
#include <memory>

#include "engine/Window.h"
#include "engine/shader/ShaderProgram.h"
#include "engine/shader/ShaderLoader.h"
#include "engine/Camera.h"

#include "engine/model/3ds/Model3DS.h"
#include "engine/VertexArrayObject.h"

#include "shader/MainShader.h"

using namespace Engine;

Expand All @@ -14,27 +19,31 @@ int main(int argc, char** argv) {
Window* win = new Window();

win->create();

ShaderProgram p1;

ShaderLoader loadMainVertex(GL_VERTEX_SHADER, "shader/main.vert");
Shader mainVertex(loadMainVertex);
p1.shaders.push_back(std::make_shared<Shader>(mainVertex));

ShaderLoader loadMainFrag(GL_FRAGMENT_SHADER, "shader/main.frag");
Shader mainFrag(loadMainFrag);
p1.shaders.push_back(std::make_shared<Shader>(mainFrag));

p1.link();
Game::Shader::MainShader mainShader;

mainShader.link();

Model3DS dragon("resource/dragon.3ds");

while (win->loop()) {
glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT);

float points[] = {
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};

p1.use();
VertexArrayObject vao;
std::shared_ptr<Buffer> vbo = std::make_shared<Buffer>();
vbo->pushData(points, 9 * sizeof(float));
vbo->stride = 3 * sizeof(float);

vao.addBuffer(vbo);

while (win->loop()) {
mainShader.use();

p1.end();
vao.bind();
glDrawArrays(GL_TRIANGLES, 0, 3);
}
win->close();

Expand Down
17 changes: 17 additions & 0 deletions src/shader/MainShader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "MainShader.h"

#include <memory>
#include "../engine/shader/Shader.h"
#include "../engine/shader/ShaderLoader.h"

using namespace Game::Shader;

MainShader::MainShader() {
Engine::ShaderLoader loadMainFragment(GL_FRAGMENT_SHADER, "shader/main.frag");
Engine::Shader mainFragment(loadMainFragment);
this->shaders.push_back(std::make_shared<Engine::Shader>(mainFragment));

Engine::ShaderLoader loadMainVertex(GL_VERTEX_SHADER, "shader/main.vert");
Engine::Shader mainVertex(loadMainVertex);
this->shaders.push_back(std::make_shared<Engine::Shader>(mainVertex));
}
15 changes: 15 additions & 0 deletions src/shader/MainShader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "../engine/shader/ShaderProgram.h"

#include <GL/glew.h>

namespace Game {
namespace Shader {
class MainShader : public Engine::ShaderProgram {

public:
GLint positionLocation = getUniformLocation("position");

MainShader();
};
}
}
2 changes: 1 addition & 1 deletion src/shader/main.vert
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#version 450 core

layout (location = 0) in vec3 aPos;
in vec3 aPos;

void main() {
gl_Position = vec4(aPos, 1.0);
Expand Down

0 comments on commit f7e17fe

Please sign in to comment.