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

@ -101,5 +101,5 @@ void main() {
} else {
color = texture(diffuseTexture, UV);
}
if (isInverted) color.rgb = 1 - color.rgb;
if (isInverted) color.rgb = 1- color.rgb;
}

View File

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

View File

@ -6,34 +6,35 @@
using std::vector;
using glm::vec3;
using glm::vec2;
typedef unsigned int uint;
unsigned int generateBuffer(Mesh &mesh, bool isNormalMapped) {
unsigned int vaoID;
uint generateBuffer(Mesh &mesh, bool isNormalMapped) {
uint vaoID;
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
unsigned int vertexBufferID;
uint vertexBufferID;
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
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);
glEnableVertexAttribArray(0);
unsigned int normalBufferID;
uint normalBufferID;
glGenBuffers(1, &normalBufferID);
glBindBuffer(GL_ARRAY_BUFFER, normalBufferID);
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);
glEnableVertexAttribArray(1);
unsigned int indexBufferID;
uint indexBufferID;
glGenBuffers(1, &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;
unsigned int textureBufferID;
uint textureBufferID;
glGenBuffers(1, &textureBufferID);
glBindBuffer(GL_ARRAY_BUFFER, textureBufferID);
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;
}
void addTangents(unsigned int vaoID, Mesh& mesh) {
void addTangents(uint vaoID, Mesh& mesh) {
vector<vec3> tangents(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& pos2 = mesh.vertices[mesh.indices[i+1]];
const vec3& pos3 = mesh.vertices[mesh.indices[i+2]];
@ -88,14 +89,14 @@ void addTangents(unsigned int vaoID, Mesh& mesh) {
glBindVertexArray(vaoID);
unsigned int tangentBufferID;
uint tangentBufferID;
glGenBuffers(1, &tangentBufferID);
glBindBuffer(GL_ARRAY_BUFFER, tangentBufferID);
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);
glEnableVertexAttribArray(3);
unsigned int bitangentBufferID;
uint bitangentBufferID;
glGenBuffers(1, &bitangentBufferID);
glBindBuffer(GL_ARRAY_BUFFER, bitangentBufferID);
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);
}
unsigned int generateTexture(PNGImage& texture) {
unsigned int id;
uint generateTexture(PNGImage& texture) {
uint id;
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);

View File

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