More seperate cc files, font & overlay rendering
This commit is contained in:
60
main.cc
60
main.cc
@@ -4,6 +4,7 @@
|
||||
#include <unordered_map>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
|
||||
#include <glbinding/gl/gl.h>
|
||||
#include <glbinding/Binding.h>
|
||||
@@ -14,6 +15,7 @@
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/gtx/transform.hpp>
|
||||
#include <glm/gtc/packing.hpp>
|
||||
|
||||
#include "common.hh"
|
||||
#include "shaders.hh"
|
||||
@@ -21,6 +23,8 @@
|
||||
#include "VBOManager.hh"
|
||||
#include "objectParser.hh"
|
||||
#include "Object.hh"
|
||||
#include "font.hh"
|
||||
#include "Overlay.hh"
|
||||
|
||||
using namespace gl;
|
||||
|
||||
@@ -50,6 +54,13 @@ int main(int argc, char *argv[])
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(TTF_Init() == -1) {
|
||||
printf("Could not init SDL_ttf: %s\n", TTF_GetError());
|
||||
IMG_Quit();
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
||||
@@ -106,7 +117,10 @@ int main(int argc, char *argv[])
|
||||
0.1f, 100.0f);
|
||||
glm::mat4 view = glm::lookAt(glm::vec3(2.0f, 2.0f, 10),
|
||||
glm::vec3(0.0f, 0.0f, 0),
|
||||
glm::vec3(0, 1, 0));
|
||||
glm::vec3(0, 1, 0));
|
||||
|
||||
Font font{"DejaVuSans.ttf", 12};
|
||||
|
||||
// sf::Font font;
|
||||
// if (!font.loadFromFile("DejaVuSans.ttf")) {
|
||||
// std::printf("Error loading font\n");
|
||||
@@ -120,7 +134,8 @@ int main(int argc, char *argv[])
|
||||
|
||||
// sf::Clock clock;
|
||||
// sf::Time last = clock.getElapsedTime();
|
||||
|
||||
auto last = SDL_GetTicks();
|
||||
|
||||
VBOManager vboManager;
|
||||
|
||||
Texture2D cubeTex("textures/Wood_Box_Texture.jpg");
|
||||
@@ -135,7 +150,8 @@ int main(int argc, char *argv[])
|
||||
const unsigned lights = 2;
|
||||
const unsigned shadowMapSize = 512;
|
||||
glm::mat4 shadowProj = glm::perspectiveFov(90.0f, static_cast<float>(shadowMapSize),
|
||||
static_cast<float>(shadowMapSize), 1.0f, 128.0f);
|
||||
static_cast<float>(shadowMapSize), 1.0f, 128.0f);
|
||||
//glm::mat4 shadowProj = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, 1.0f, 128.0f);
|
||||
std::vector<TextureCubeMap> shadowMaps;
|
||||
for(unsigned i = 0;i < lights;++i) {
|
||||
shadowMaps.emplace_back(shadowMapSize);
|
||||
@@ -160,6 +176,10 @@ int main(int argc, char *argv[])
|
||||
FragmentShader shadowFs{fileToString("shaders/shadow.fs")};
|
||||
Program shadowProg{shadowVs, shadowFs};
|
||||
|
||||
VertexShader ovlVs{fileToString("shaders/overlay.vs")};
|
||||
FragmentShader ovlFs{fileToString("shaders/overlay.fs")};
|
||||
Program ovlProg{ovlVs, ovlFs};
|
||||
|
||||
prog.use();
|
||||
glUniformMatrix4fv(prog.getUniformLocation("projection_matrix"), 1, GL_FALSE,
|
||||
glm::value_ptr(proj));
|
||||
@@ -171,12 +191,28 @@ int main(int argc, char *argv[])
|
||||
glUniform1iv(prog.getUniformLocation("texShadowMaps"), 2, shadowMapTUs);
|
||||
|
||||
shadowProg.use();
|
||||
glUniformMatrix4fv(prog.getUniformLocation("projection_matrix"), 1, GL_FALSE,
|
||||
glUniformMatrix4fv(shadowProg.getUniformLocation("projection_matrix"), 1, GL_FALSE,
|
||||
glm::value_ptr(shadowProj));
|
||||
|
||||
|
||||
ovlProg.use();
|
||||
glUniform1i(ovlProg.getUniformLocation("texBase"), 0);
|
||||
|
||||
Object box(vboManager, "objects/woodbox.obj", prog);
|
||||
Object pyramid(vboManager, "objects/pyramid.obj", prog);
|
||||
Object plane(vboManager, "objects/plane.obj", prog);
|
||||
|
||||
float px20_width = 2.0f*(20.0f/width);
|
||||
float px20_height = 2.0f*(20.0f/height);
|
||||
|
||||
std::vector<ovlVertexAttribs> ovlAttribs{
|
||||
{{glm::packSnorm1x16(1.0-px20_width), glm::packSnorm1x16(1.0)}, {0, 0}},
|
||||
{{glm::packSnorm1x16(1.0), glm::packSnorm1x16(1.0-px20_height)}, {65535u, 65535u}},
|
||||
{{glm::packSnorm1x16(1.0), glm::packSnorm1x16(1.0)}, {65535u, 0}},
|
||||
{{glm::packSnorm1x16(1.0-px20_width), glm::packSnorm1x16(1.0)}, {0, 0}},
|
||||
{{glm::packSnorm1x16(1.0-px20_width), glm::packSnorm1x16(1.0-px20_height)}, {0, 65535u}},
|
||||
{{glm::packSnorm1x16(1.0), glm::packSnorm1x16(1.0-px20_height)}, {65535u, 65535u}}};
|
||||
|
||||
Overlay ovl{vboManager, ovlAttribs, ovlProg};
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
|
||||
@@ -288,6 +324,20 @@ int main(int argc, char *argv[])
|
||||
// fpsText.setPosition({1680-fpsText.getLocalBounds().width, 0});
|
||||
// window.draw(fpsText);
|
||||
|
||||
auto now = SDL_GetTicks();
|
||||
auto elapsed = now - last;
|
||||
last = now;
|
||||
std::string fpsText = std::to_string(static_cast<unsigned>(1000.0/elapsed));
|
||||
auto fpsTex = font.render(fpsText);
|
||||
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
fpsTex.bind();
|
||||
//redTex.bind();
|
||||
ovl.draw();
|
||||
glDisable(GL_BLEND);
|
||||
|
||||
SDL_GL_SwapWindow(window);
|
||||
}
|
||||
}catch(Exception &ex) {
|
||||
|
||||
Reference in New Issue
Block a user