This commit is contained in:
Peder Bergebakken Sundt 2019-03-15 21:22:41 +01:00
parent eac8fcdf9e
commit 06e6ce7494
5 changed files with 25 additions and 25 deletions

View File

@ -72,7 +72,6 @@ const float debug_startTime = 45;
double totalElapsedTime = debug_startTime; double totalElapsedTime = debug_startTime;
// textures // textures
PNGImage t_charmap; PNGImage t_charmap;
PNGImage t_cobble_diff; PNGImage t_cobble_diff;
PNGImage t_cobble_normal; PNGImage t_cobble_normal;
@ -166,8 +165,7 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
Mesh hello_world = generateTextGeometryBuffer("Skjer'a bagera?", 1.3, 2); Mesh hello_world = generateTextGeometryBuffer("Skjer'a bagera?", 1.3, 2);
textNode->position = vec3(-1.0, 0.0, 0.0); textNode->position = vec3(-1.0, 0.0, 0.0);
textNode->rotation = vec3(0.0, 0.0, 0.0); textNode->rotation = vec3(0.0, 0.0, 0.0);
textNode->vertexArrayObjectID = generateBuffer(hello_world); textNode->setMesh(&hello_world);
textNode->VAOIndexCount = hello_world.indices.size();
textNode->setTexture(&t_charmap); textNode->setTexture(&t_charmap);
textNode->isIlluminated = false; textNode->isIlluminated = false;
textNode->isInverted = true; textNode->isInverted = true;

View File

@ -6,34 +6,35 @@
using std::vector; using std::vector;
using glm::vec3; using glm::vec3;
using glm::vec2; using glm::vec2;
typedef unsigned int uint;
unsigned int generateBuffer(Mesh &mesh, bool isNormalMapped) { uint generateBuffer(Mesh &mesh, bool isNormalMapped) {
unsigned int vaoID; uint vaoID;
glGenVertexArrays(1, &vaoID); glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID); glBindVertexArray(vaoID);
unsigned int vertexBufferID; uint vertexBufferID;
glGenBuffers(1, &vertexBufferID); glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID); glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, mesh.vertices.size() * sizeof(vec3), mesh.vertices.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, mesh.vertices.size() * sizeof(vec3), mesh.vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
unsigned int normalBufferID; uint normalBufferID;
glGenBuffers(1, &normalBufferID); glGenBuffers(1, &normalBufferID);
glBindBuffer(GL_ARRAY_BUFFER, normalBufferID); glBindBuffer(GL_ARRAY_BUFFER, normalBufferID);
glBufferData(GL_ARRAY_BUFFER, mesh.normals.size() * sizeof(vec3), mesh.normals.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, mesh.normals.size() * sizeof(vec3), mesh.normals.data(), GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_TRUE, 3 * sizeof(float), 0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_TRUE, 3 * sizeof(float), 0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
unsigned int indexBufferID; uint indexBufferID;
glGenBuffers(1, &indexBufferID); glGenBuffers(1, &indexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh.indices.size() * sizeof(unsigned int), mesh.indices.data(), GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh.indices.size() * sizeof(uint), mesh.indices.data(), GL_STATIC_DRAW);
if (mesh.textureCoordinates.empty()) return vaoID; if (mesh.textureCoordinates.empty()) return vaoID;
unsigned int textureBufferID; uint textureBufferID;
glGenBuffers(1, &textureBufferID); glGenBuffers(1, &textureBufferID);
glBindBuffer(GL_ARRAY_BUFFER, textureBufferID); glBindBuffer(GL_ARRAY_BUFFER, textureBufferID);
glBufferData(GL_ARRAY_BUFFER, mesh.textureCoordinates.size() * sizeof(vec2), mesh.textureCoordinates.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, mesh.textureCoordinates.size() * sizeof(vec2), mesh.textureCoordinates.data(), GL_STATIC_DRAW);
@ -45,11 +46,11 @@ unsigned int generateBuffer(Mesh &mesh, bool isNormalMapped) {
return vaoID; return vaoID;
} }
void addTangents(unsigned int vaoID, Mesh& mesh) { void addTangents(uint vaoID, Mesh& mesh) {
vector<vec3> tangents(mesh.vertices.size()); vector<vec3> tangents(mesh.vertices.size());
vector<vec3> bitangents(mesh.vertices.size()); vector<vec3> bitangents(mesh.vertices.size());
for (unsigned int i = 0; i < mesh.indices.size(); i+=3) { for (uint i = 0; i < mesh.indices.size(); i+=3) {
const vec3& pos1 = mesh.vertices[mesh.indices[i+0]]; const vec3& pos1 = mesh.vertices[mesh.indices[i+0]];
const vec3& pos2 = mesh.vertices[mesh.indices[i+1]]; const vec3& pos2 = mesh.vertices[mesh.indices[i+1]];
const vec3& pos3 = mesh.vertices[mesh.indices[i+2]]; const vec3& pos3 = mesh.vertices[mesh.indices[i+2]];
@ -88,14 +89,14 @@ void addTangents(unsigned int vaoID, Mesh& mesh) {
glBindVertexArray(vaoID); glBindVertexArray(vaoID);
unsigned int tangentBufferID; uint tangentBufferID;
glGenBuffers(1, &tangentBufferID); glGenBuffers(1, &tangentBufferID);
glBindBuffer(GL_ARRAY_BUFFER, tangentBufferID); glBindBuffer(GL_ARRAY_BUFFER, tangentBufferID);
glBufferData(GL_ARRAY_BUFFER, mesh.vertices.size() * sizeof(vec3), tangents.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, mesh.vertices.size() * sizeof(vec3), tangents.data(), GL_STATIC_DRAW);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0); glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexAttribArray(3); glEnableVertexAttribArray(3);
unsigned int bitangentBufferID; uint bitangentBufferID;
glGenBuffers(1, &bitangentBufferID); glGenBuffers(1, &bitangentBufferID);
glBindBuffer(GL_ARRAY_BUFFER, bitangentBufferID); glBindBuffer(GL_ARRAY_BUFFER, bitangentBufferID);
glBufferData(GL_ARRAY_BUFFER, mesh.vertices.size() * sizeof(vec3), bitangents.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, mesh.vertices.size() * sizeof(vec3), bitangents.data(), GL_STATIC_DRAW);
@ -103,8 +104,8 @@ void addTangents(unsigned int vaoID, Mesh& mesh) {
glEnableVertexAttribArray(4); glEnableVertexAttribArray(4);
} }
unsigned int generateTexture(PNGImage& texture) { uint generateTexture(PNGImage& texture) {
unsigned int id; uint id;
glGenTextures(1, &id); glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);

View File

@ -5,15 +5,16 @@
using glm::vec2; using glm::vec2;
using std::vector; using std::vector;
typedef unsigned int uint;
// Original source: https://raw.githubusercontent.com/lvandeve/lodepng/master/examples/example_decode.cpp // Original source: https://raw.githubusercontent.com/lvandeve/lodepng/master/examples/example_decode.cpp
PNGImage loadPNGFile(std::string fileName) { PNGImage loadPNGFile(std::string fileName) {
vector<unsigned char> png; vector<unsigned char> png;
vector<unsigned char> pixels; //the raw pixels vector<unsigned char> pixels; //the raw pixels
unsigned int width, height; uint width, height;
//load and decode //load and decode
unsigned error = lodepng::load_file(png, fileName); uint error = lodepng::load_file(png, fileName);
if(!error) error = lodepng::decode(pixels, width, height, png); if(!error) error = lodepng::decode(pixels, width, height, png);
//if there's an error, display it //if there's an error, display it
@ -27,10 +28,10 @@ PNGImage loadPNGFile(std::string fileName) {
// You're welcome :) // You're welcome :)
unsigned int widthBytes = 4 * width; uint widthBytes = 4 * width;
for(unsigned int row = 0; row < (height / 2); row++) { for(uint row = 0; row < (height / 2); row++) {
for(unsigned int col = 0; col < widthBytes; col++) { for(uint col = 0; col < widthBytes; col++) {
std::swap(pixels[row * widthBytes + col], pixels[(height - 1 - row) * widthBytes + col]); std::swap(pixels[row * widthBytes + col], pixels[(height - 1 - row) * widthBytes + col]);
} }
} }

View File

@ -6,9 +6,9 @@
typedef unsigned int uint; typedef unsigned int uint;
typedef struct PNGImage { struct PNGImage {
unsigned int width, height; uint width, height;
std::vector<unsigned char> pixels; // RGBA std::vector<unsigned char> pixels; // RGBA
} PNGImage; };
PNGImage loadPNGFile(std::string fileName); PNGImage loadPNGFile(std::string fileName);