Updated code handout for assignment 2
This commit is contained in:
parent
5047f2b506
commit
4645e40b62
|
@ -25,3 +25,6 @@
|
||||||
[submodule "arrrgh"]
|
[submodule "arrrgh"]
|
||||||
path = arrrgh
|
path = arrrgh
|
||||||
url = https://github.com/ElectricToy/arrrgh.git
|
url = https://github.com/ElectricToy/arrrgh.git
|
||||||
|
[submodule "lib/lodepng"]
|
||||||
|
path = lib/lodepng
|
||||||
|
url = https://github.com/lvandeve/lodepng
|
||||||
|
|
|
@ -43,6 +43,7 @@ add_subdirectory(lib/SFML)
|
||||||
include_directories (src/
|
include_directories (src/
|
||||||
lib/glad/include/
|
lib/glad/include/
|
||||||
lib/glfw/include/
|
lib/glfw/include/
|
||||||
|
lib/lodepng/
|
||||||
lib/glm/
|
lib/glm/
|
||||||
lib/stb/
|
lib/stb/
|
||||||
lib/arrrgh/
|
lib/arrrgh/
|
||||||
|
@ -51,7 +52,8 @@ include_directories (src/
|
||||||
#
|
#
|
||||||
# Add files
|
# Add files
|
||||||
#
|
#
|
||||||
file (GLOB VENDORS_SOURCES lib/glad/src/glad.c)
|
file (GLOB VENDORS_SOURCES lib/glad/src/glad.c
|
||||||
|
lib/lodepng/lodepng.cpp)
|
||||||
file (GLOB_RECURSE PROJECT_HEADERS src/*.hpp
|
file (GLOB_RECURSE PROJECT_HEADERS src/*.hpp
|
||||||
src/*.h)
|
src/*.h)
|
||||||
file (GLOB_RECURSE PROJECT_SOURCES src/*.cpp
|
file (GLOB_RECURSE PROJECT_SOURCES src/*.cpp
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
./*.png
|
||||||
|
|
|
@ -99,8 +99,8 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
|
||||||
shader->makeBasicShader("../res/shaders/simple.vert", "../res/shaders/simple.frag");
|
shader->makeBasicShader("../res/shaders/simple.vert", "../res/shaders/simple.frag");
|
||||||
shader->activate();
|
shader->activate();
|
||||||
|
|
||||||
Mesh box = generateBox(boxDimensions.x, boxDimensions.y, boxDimensions.z, false);
|
Mesh box = generateBox(boxDimensions.x, boxDimensions.y, boxDimensions.z, true);
|
||||||
Mesh pad = generateBox(padDimensions.x, padDimensions.y, padDimensions.z, true);
|
Mesh pad = generateBox(padDimensions.x, padDimensions.y, padDimensions.z, false);
|
||||||
Mesh sphere = generateSphere(1.0, 40, 40);
|
Mesh sphere = generateSphere(1.0, 40, 40);
|
||||||
|
|
||||||
unsigned int ballVAO = generateBuffer(sphere);
|
unsigned int ballVAO = generateBuffer(sphere);
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include "glfont.h"
|
||||||
|
|
||||||
|
Mesh generateTextGeometryBuffer(std::string text, float characterHeightOverWidth, float totalTextWidth) {
|
||||||
|
float characterWidth = totalTextWidth / float(text.length());
|
||||||
|
float characterHeight = characterHeightOverWidth * characterWidth;
|
||||||
|
|
||||||
|
unsigned int vertexCount = 4 * text.length();
|
||||||
|
unsigned int indexCount = 6 * text.length();
|
||||||
|
|
||||||
|
Mesh mesh;
|
||||||
|
|
||||||
|
mesh.vertices.resize(vertexCount);
|
||||||
|
mesh.indices.resize(indexCount);
|
||||||
|
|
||||||
|
for(unsigned int i = 0; i < text.length(); i++)
|
||||||
|
{
|
||||||
|
float baseXCoordinate = float(i) * characterWidth;
|
||||||
|
|
||||||
|
mesh.vertices.at(4 * i + 0) = {baseXCoordinate, 0, 0};
|
||||||
|
mesh.vertices.at(4 * i + 1) = {baseXCoordinate + characterWidth, 0, 0};
|
||||||
|
mesh.vertices.at(4 * i + 2) = {baseXCoordinate + characterWidth, characterHeight, 0};
|
||||||
|
|
||||||
|
mesh.vertices.at(4 * i + 0) = {baseXCoordinate, 0, 0};
|
||||||
|
mesh.vertices.at(4 * i + 2) = {baseXCoordinate + characterWidth, characterHeight, 0};
|
||||||
|
mesh.vertices.at(4 * i + 3) = {baseXCoordinate, characterHeight, 0};
|
||||||
|
|
||||||
|
|
||||||
|
mesh.indices.at(6 * i + 0) = 4 * i + 0;
|
||||||
|
mesh.indices.at(6 * i + 1) = 4 * i + 1;
|
||||||
|
mesh.indices.at(6 * i + 2) = 4 * i + 2;
|
||||||
|
mesh.indices.at(6 * i + 3) = 4 * i + 0;
|
||||||
|
mesh.indices.at(6 * i + 4) = 4 * i + 2;
|
||||||
|
mesh.indices.at(6 * i + 5) = 4 * i + 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
return mesh;
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include "mesh.h"
|
||||||
|
|
||||||
|
Mesh generateTextGeometryBuffer(std::string text, float characterHeightOverWidth, float totalTextWidth);
|
|
@ -0,0 +1,41 @@
|
||||||
|
#include "imageLoader.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// Original source: https://raw.githubusercontent.com/lvandeve/lodepng/master/examples/example_decode.cpp
|
||||||
|
PNGImage loadPNGFile(std::string fileName)
|
||||||
|
{
|
||||||
|
std::vector<unsigned char> png;
|
||||||
|
std::vector<unsigned char> pixels; //the raw pixels
|
||||||
|
unsigned int width, height;
|
||||||
|
|
||||||
|
//load and decode
|
||||||
|
unsigned error = lodepng::load_file(png, fileName);
|
||||||
|
if(!error) error = lodepng::decode(pixels, width, height, png);
|
||||||
|
|
||||||
|
//if there's an error, display it
|
||||||
|
if(error) std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
|
||||||
|
|
||||||
|
//the pixels are now in the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ...
|
||||||
|
|
||||||
|
// Unfortunately, images usually have their origin at the top left.
|
||||||
|
// OpenGL instead defines the origin to be on the _bottom_ left instead, so
|
||||||
|
// here's the world's most inefficient way to flip the image vertically.
|
||||||
|
|
||||||
|
// You're welcome :)
|
||||||
|
|
||||||
|
unsigned int widthBytes = 4 * width;
|
||||||
|
|
||||||
|
for(unsigned int row = 0; row < (height / 2); row++) {
|
||||||
|
for(unsigned int col = 0; col < widthBytes; col++) {
|
||||||
|
std::swap(pixels[row * widthBytes + col], pixels[(height - 1 - row) * widthBytes + col]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PNGImage image;
|
||||||
|
image.width = width;
|
||||||
|
image.height = height;
|
||||||
|
image.pixels = pixels;
|
||||||
|
|
||||||
|
return image;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "lodepng.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
typedef struct PNGImage {
|
||||||
|
unsigned int width, height;
|
||||||
|
std::vector<unsigned char> pixels;
|
||||||
|
} PNGImage;
|
||||||
|
|
||||||
|
PNGImage loadPNGFile(std::string fileName);
|
|
@ -8,52 +8,53 @@ Mesh generateBox(float width, float height, float depth, bool flipFaces) {
|
||||||
|
|
||||||
std::vector<glm::vec3> vertices = {
|
std::vector<glm::vec3> vertices = {
|
||||||
{0, 0, 0},
|
{0, 0, 0},
|
||||||
{0, height, depth},
|
|
||||||
{0, 0, depth},
|
|
||||||
|
|
||||||
{0, 0, 0},
|
|
||||||
{0, height, 0},
|
|
||||||
{0, height, depth},
|
|
||||||
|
|
||||||
{width, 0, 0},
|
|
||||||
{width, height, depth},
|
|
||||||
{width, height, 0},
|
|
||||||
|
|
||||||
{width, 0, 0},
|
|
||||||
{width, 0, depth},
|
|
||||||
{width, height, depth},
|
|
||||||
|
|
||||||
{0, 0, 0},
|
|
||||||
{width, 0, 0},
|
|
||||||
{width, height, 0},
|
|
||||||
|
|
||||||
{0, 0, 0},
|
|
||||||
{width, height, 0},
|
|
||||||
{0, height, 0},
|
|
||||||
|
|
||||||
{0, 0, depth},
|
|
||||||
{width, height, depth},
|
|
||||||
{width, 0, depth},
|
|
||||||
|
|
||||||
{0, 0, depth},
|
{0, 0, depth},
|
||||||
{0, height, depth},
|
{0, height, depth},
|
||||||
{width, height, depth},
|
|
||||||
|
|
||||||
{0, 0, 0},
|
{0, 0, 0},
|
||||||
{width, 0, depth},
|
|
||||||
{width, 0, 0},
|
|
||||||
|
|
||||||
{0, 0, 0},
|
|
||||||
{0, 0, depth},
|
|
||||||
{width, 0, depth},
|
|
||||||
|
|
||||||
{width, height, 0},
|
|
||||||
{0, height, depth},
|
{0, height, depth},
|
||||||
{0, height, 0},
|
{0, height, 0},
|
||||||
|
|
||||||
|
{width, 0, 0},
|
||||||
|
{width, height, depth},
|
||||||
|
{width, 0, depth},
|
||||||
|
|
||||||
|
{width, 0, 0},
|
||||||
{width, height, 0},
|
{width, height, 0},
|
||||||
{width, height, depth},
|
{width, height, depth},
|
||||||
{0, height, depth}};
|
|
||||||
|
{0, 0, 0},
|
||||||
|
{width, height, 0},
|
||||||
|
{width, 0, 0},
|
||||||
|
|
||||||
|
{0, 0, 0},
|
||||||
|
{0, height, 0},
|
||||||
|
{width, height, 0},
|
||||||
|
|
||||||
|
{0, 0, depth},
|
||||||
|
{width, 0, depth},
|
||||||
|
{width, height, depth},
|
||||||
|
|
||||||
|
{0, 0, depth},
|
||||||
|
{width, height, depth},
|
||||||
|
{0, height, depth},
|
||||||
|
|
||||||
|
{0, 0, 0},
|
||||||
|
{width, 0, 0},
|
||||||
|
{width, 0, depth},
|
||||||
|
|
||||||
|
{0, 0, 0},
|
||||||
|
{width, 0, depth},
|
||||||
|
{0, 0, depth},
|
||||||
|
|
||||||
|
{width, height, 0},
|
||||||
|
{0, height, 0},
|
||||||
|
{0, height, depth},
|
||||||
|
|
||||||
|
{width, height, 0},
|
||||||
|
{0, height, depth},
|
||||||
|
{width, height, depth},
|
||||||
|
};
|
||||||
|
|
||||||
// These are technically inverted relative to the vertex coordinates.
|
// These are technically inverted relative to the vertex coordinates.
|
||||||
// But for some strange reason the faces are rendered inverted.
|
// But for some strange reason the faces are rendered inverted.
|
||||||
|
@ -109,6 +110,62 @@ Mesh generateBox(float width, float height, float depth, bool flipFaces) {
|
||||||
{0.0, -1.0, 0.0},
|
{0.0, -1.0, 0.0},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
float texScaleFactorX = depth / height;
|
||||||
|
float texScaleFactorY = width / depth;
|
||||||
|
float texScaleFactorZ = width / height;
|
||||||
|
|
||||||
|
std::vector<glm::vec2> textureCoordinates = {
|
||||||
|
{0, 0},
|
||||||
|
{texScaleFactorX, 0},
|
||||||
|
{texScaleFactorX, 1},
|
||||||
|
|
||||||
|
{0, 0},
|
||||||
|
{texScaleFactorX, 1},
|
||||||
|
{0, 1},
|
||||||
|
|
||||||
|
{0, 0},
|
||||||
|
{texScaleFactorX, 1},
|
||||||
|
{texScaleFactorX, 0},
|
||||||
|
|
||||||
|
{0, 0},
|
||||||
|
{0, 1},
|
||||||
|
{texScaleFactorX, 1},
|
||||||
|
|
||||||
|
{0, 0},
|
||||||
|
{texScaleFactorZ, 0},
|
||||||
|
{texScaleFactorZ, 1},
|
||||||
|
|
||||||
|
{0, 0},
|
||||||
|
{texScaleFactorZ, 1},
|
||||||
|
{0, 1},
|
||||||
|
|
||||||
|
{0, 0},
|
||||||
|
{texScaleFactorZ, 0},
|
||||||
|
{texScaleFactorZ, 1},
|
||||||
|
|
||||||
|
{0, 0},
|
||||||
|
{texScaleFactorZ, 1},
|
||||||
|
{0, 1},
|
||||||
|
|
||||||
|
{0, 0},
|
||||||
|
{texScaleFactorY, 0},
|
||||||
|
{texScaleFactorY, 1},
|
||||||
|
|
||||||
|
{0, 0},
|
||||||
|
{texScaleFactorY, 1},
|
||||||
|
{0, 1},
|
||||||
|
|
||||||
|
{0, 0},
|
||||||
|
{texScaleFactorY, 0},
|
||||||
|
{texScaleFactorY, 1},
|
||||||
|
|
||||||
|
{0, 0},
|
||||||
|
{texScaleFactorY, 1},
|
||||||
|
{0, 1},
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
std::vector<unsigned int> indices = {
|
std::vector<unsigned int> indices = {
|
||||||
0, 1, 2,
|
0, 1, 2,
|
||||||
3, 4, 5,
|
3, 4, 5,
|
||||||
|
@ -139,6 +196,7 @@ Mesh generateBox(float width, float height, float depth, bool flipFaces) {
|
||||||
Mesh mesh;
|
Mesh mesh;
|
||||||
mesh.vertices = vertices;
|
mesh.vertices = vertices;
|
||||||
mesh.normals = normals;
|
mesh.normals = normals;
|
||||||
|
mesh.textureCoordinates = textureCoordinates;
|
||||||
mesh.indices = indices;
|
mesh.indices = indices;
|
||||||
|
|
||||||
return mesh;
|
return mesh;
|
||||||
|
|
Loading…
Reference in New Issue