OBJ Loader
This commit is contained in:
51
OpenGL/playground/src/init.h
Normal file
51
OpenGL/playground/src/init.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#ifndef SRC_INIT_H_
|
||||
#define SRC_INIT_H_
|
||||
|
||||
// General includes
|
||||
#include <chrono>
|
||||
|
||||
// GL includes
|
||||
#if _WIN32
|
||||
#include <SDL.h>
|
||||
#else
|
||||
#include <SDL2/SDL.h>
|
||||
#endif
|
||||
|
||||
// Custom includes
|
||||
#include <logger.h>
|
||||
|
||||
bool init(Logger& logger, SDL_Window* window, SDL_GLContext glContext) {
|
||||
std::cout << "-----------------------------" << std::endl;
|
||||
std::cout << "----- OpenGL Playground -----" << std::endl;
|
||||
std::cout << "-------- Version 1.0 --------" << std::endl;
|
||||
std::cout << "----- <20>Benjamin Kyd 2019 ----" << std::endl;
|
||||
std::cout << "-----------------------------" << std::endl;
|
||||
std::cout << std::endl;
|
||||
|
||||
SDL_Init(SDL_INIT_EVERYTHING);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
|
||||
|
||||
window = SDL_CreateWindow("OpenGL Playground V1.0",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
1280, 720,
|
||||
SDL_WINDOW_OPENGL);
|
||||
glContext = SDL_GL_CreateContext(window);
|
||||
SDL_GL_SetSwapInterval(0);
|
||||
|
||||
gladLoadGLLoader(SDL_GL_GetProcAddress);
|
||||
|
||||
logger << LOGGER_INFO << "OpenGL and SDL initialized" << LOGGER_ENDL;
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,6 @@
|
||||
// General includes
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
|
||||
// GL includes
|
||||
#include <glad/glad.h>
|
||||
@@ -19,78 +20,38 @@
|
||||
|
||||
// Custom includes
|
||||
#include <logger.h>
|
||||
#include "init.h"
|
||||
#include "timers.h"
|
||||
#include "object.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
std::cout << "----- OpenGL Playground -----" << std::endl;
|
||||
std::cout << "-------- Version 1.0 --------" << std::endl;
|
||||
std::cout << "----- ©Benjamin Kyd 2019 ----" << std::endl << std::endl;
|
||||
|
||||
// Get global variables ready
|
||||
Logger logger;
|
||||
SDL_Window* window = nullptr;
|
||||
SDL_GLContext glContext;
|
||||
bool isWindowClosed = true;
|
||||
|
||||
logger << LOGGER_DEBUG << "debug" << LOGGER_ENDL;
|
||||
logger << LOGGER_INFO << "info" << LOGGER_ENDL;
|
||||
logger << LOGGER_WARN << "warn" << LOGGER_ENDL;
|
||||
logger << LOGGER_ERROR << "error" << LOGGER_ENDL;
|
||||
logger << LOGGER_PANIC << "panic" << LOGGER_ENDL << LOGGER_ENDL;
|
||||
bool isWindowOpen = false;
|
||||
|
||||
// Initialize SDL and OpenGL
|
||||
SDL_Init(SDL_INIT_EVERYTHING);
|
||||
isWindowOpen = init(logger, window, glContext);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
|
||||
|
||||
window = SDL_CreateWindow("OpenGL Playground V1.0",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
1000, 1000,
|
||||
SDL_WINDOW_OPENGL);
|
||||
glContext = SDL_GL_CreateContext(window);
|
||||
SDL_GL_SetSwapInterval(0);
|
||||
|
||||
gladLoadGLLoader(SDL_GL_GetProcAddress);
|
||||
isWindowClosed = false;
|
||||
|
||||
logger << LOGGER_INFO << "OpenGL and SDL initialized" << LOGGER_ENDL;
|
||||
|
||||
|
||||
|
||||
// Various timers needed for FPS counting
|
||||
std::chrono::high_resolution_clock timer;
|
||||
auto FPSCalculateLast = timer.now();
|
||||
auto FPSClock = SDL_GetTicks();
|
||||
|
||||
auto UpdateClock = SDL_GetTicks();
|
||||
std::vector<glm::vec4> vertices;
|
||||
std::vector<glm::vec3> normals;
|
||||
std::vector<GLushort> elements;
|
||||
LoadOBJ(logger, "./resources/dragon.obj", vertices, normals, elements);
|
||||
|
||||
SDL_Event event;
|
||||
while (!isWindowClosed) {
|
||||
// Calculate and display framerate
|
||||
if (SDL_GetTicks() - FPSClock >= 1000) {
|
||||
auto deltaTime = std::chrono::duration_cast<std::chrono::nanoseconds>(timer.now() - FPSCalculateLast).count();
|
||||
logger << LOGGER_INFO << "FPS: " << (int)(1 / ((float)deltaTime * 1e-9)) << LOGGER_ENDL;
|
||||
FPSClock = SDL_GetTicks();
|
||||
}
|
||||
FPSCalculateLast = timer.now();
|
||||
while (isWindowOpen) {
|
||||
FPSCount(logger);
|
||||
|
||||
// Update tick (60ups)
|
||||
if (SDL_GetTicks() - UpdateClock >= 10) {
|
||||
if (UPSTimer()) {
|
||||
|
||||
}
|
||||
|
||||
// Handle events
|
||||
while (SDL_PollEvent(&event) != 0)
|
||||
if (event.type == SDL_QUIT)
|
||||
isWindowClosed = true;
|
||||
isWindowOpen = false;
|
||||
|
||||
// Swap GL frame buffers
|
||||
SDL_GL_SwapWindow(window);
|
||||
|
||||
40
OpenGL/playground/src/object.cpp
Normal file
40
OpenGL/playground/src/object.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "object.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
// https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Load_OBJ
|
||||
void LoadOBJ(Logger& logger, std::string file, std::vector<glm::vec4>& vertices, std::vector<glm::vec3>& normals, std::vector<GLushort>& elements) {
|
||||
std::ifstream in(file, std::ios::in);
|
||||
if (!in) {
|
||||
logger << LOGGER_ERROR << "Cannot open " << file << LOGGER_ENDL;
|
||||
}
|
||||
|
||||
std::string line;
|
||||
while (getline(in, line)) {
|
||||
if (line.substr(0,2) == "v ") {
|
||||
std::istringstream s(line.substr(2));
|
||||
glm::vec4 v; s >> v.x; s >> v.y; s >> v.z; v.w = 1.0f;
|
||||
vertices.push_back(v);
|
||||
} else if (line.substr(0,2) == "f ") {
|
||||
std::istringstream s(line.substr(2));
|
||||
GLushort a,b,c;
|
||||
s >> a; s >> b; s >> c;
|
||||
a--; b--; c--;
|
||||
elements.push_back(a); elements.push_back(b); elements.push_back(c);
|
||||
} else if (line[0] == '#') { }
|
||||
else {}
|
||||
}
|
||||
|
||||
normals.resize(vertices.size(), glm::vec3(0.0, 0.0, 0.0));
|
||||
for (int i = 0; i < elements.size(); i += 3) {
|
||||
GLushort ia = elements[i];
|
||||
GLushort ib = elements[i+1];
|
||||
GLushort ic = elements[i+2];
|
||||
glm::vec3 normal = glm::normalize(glm::cross(
|
||||
glm::vec3(vertices[ib]) - glm::vec3(vertices[ia]),
|
||||
glm::vec3(vertices[ic]) - glm::vec3(vertices[ia])));
|
||||
normals[ia] = normals[ib] = normals[ic] = normal;
|
||||
}
|
||||
|
||||
logger << LOGGER_INFO << "Loaded OBJ: " << file << LOGGER_ENDL;
|
||||
}
|
||||
29
OpenGL/playground/src/object.h
Normal file
29
OpenGL/playground/src/object.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef SRC_OBJECT_H_
|
||||
#define SRC_OBJECT_H_
|
||||
|
||||
// General includes
|
||||
#include <vector>
|
||||
|
||||
// GL includes
|
||||
#include <glad/glad.h>
|
||||
|
||||
#if _WIN32
|
||||
#include <SDL.h>
|
||||
#else
|
||||
#include <SDL2/SDL.h>
|
||||
#endif
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
// Custom includes
|
||||
#include <logger.h>
|
||||
|
||||
void LoadOBJ(Logger& logger,
|
||||
std::string file,
|
||||
std::vector<glm::vec4>& vertices,
|
||||
std::vector<glm::vec3>& normals,
|
||||
std::vector<GLushort>& elements);
|
||||
|
||||
#endif
|
||||
36
OpenGL/playground/src/timers.h
Normal file
36
OpenGL/playground/src/timers.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef SRC_TIMERS_H_
|
||||
#define SRC_TIMERS_H_
|
||||
|
||||
// General includes
|
||||
#include <chrono>
|
||||
|
||||
// GL includes
|
||||
#if _WIN32
|
||||
#include <SDL.h>
|
||||
#else
|
||||
#include <SDL2/SDL.h>
|
||||
#endif
|
||||
|
||||
// Custom includes
|
||||
#include <logger.h>
|
||||
|
||||
std::chrono::high_resolution_clock timer;
|
||||
auto FPSCalculateLast = timer.now();
|
||||
auto FPSClock = SDL_GetTicks();
|
||||
|
||||
auto UpdateClock = SDL_GetTicks();
|
||||
|
||||
void FPSCount(Logger& logger) {
|
||||
if (SDL_GetTicks() - FPSClock >= 1000) {
|
||||
auto deltaTime = std::chrono::duration_cast<std::chrono::nanoseconds>(timer.now() - FPSCalculateLast).count();
|
||||
logger << LOGGER_INFO << "FPS: " << (int)(1 / ((float)deltaTime * 1e-9)) << LOGGER_ENDL;
|
||||
FPSClock = SDL_GetTicks();
|
||||
}
|
||||
FPSCalculateLast = timer.now();
|
||||
}
|
||||
|
||||
bool UPSTimer() {
|
||||
return (SDL_GetTicks() - UpdateClock >= 10);
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user