208 lines
6.0 KiB
C++
208 lines
6.0 KiB
C++
#include <cstdio>
|
|
#include <cmath>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <SDL2/SDL.h>
|
|
#include <SDL2/SDL_image.h>
|
|
|
|
#include <glbinding/gl/gl.h>
|
|
#include <glbinding/Binding.h>
|
|
#include <glbinding/callbacks.h>
|
|
|
|
#define GLM_FORCE_RADIANS
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
#include <glm/gtx/transform.hpp>
|
|
|
|
#include "common.hh"
|
|
#include "shaders.hh"
|
|
#include "texture.hh"
|
|
#include "VBOManager.hh"
|
|
#include "objectParser.hh"
|
|
#include "Object.hh"
|
|
|
|
using namespace gl;
|
|
|
|
void glAfterCallback(glbinding::FunctionCall const& fc)
|
|
{
|
|
glbinding::setCallbackMask(glbinding::CallbackMask::None);
|
|
GLenum error = glGetError();
|
|
glbinding::setCallbackMask(glbinding::CallbackMask::After);
|
|
if (error != GL_NO_ERROR)
|
|
throw GLException(error);
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
SDL_Window *window;
|
|
int retcode = 0;
|
|
|
|
glbinding::Binding::initialize();
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
|
std::printf("Could not init SDL: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
if (IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG) == 0) {
|
|
std::printf("Could not init SDL_image: %s\n", SDL_GetError());
|
|
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);
|
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
|
|
|
window = SDL_CreateWindow("SDL2 Test",
|
|
SDL_WINDOWPOS_UNDEFINED,
|
|
SDL_WINDOWPOS_UNDEFINED,
|
|
640,
|
|
480,
|
|
/*SDL_WINDOW_FULLSCREEN_DESKTOP |*/ SDL_WINDOW_OPENGL);
|
|
|
|
if (!window) {
|
|
std::printf("Could not create window: %s\n", SDL_GetError());
|
|
SDL_Quit();
|
|
return 1;
|
|
}
|
|
|
|
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
|
|
if (!glcontext) {
|
|
std::printf("Could not create GL context: %s\n", SDL_GetError());
|
|
SDL_DestroyWindow(window);
|
|
SDL_Quit();
|
|
return 1;
|
|
}
|
|
|
|
// Clear GL errors
|
|
while(glGetError() != GL_NO_ERROR);
|
|
|
|
try {
|
|
glbinding::setCallbackMask(glbinding::CallbackMask::After);
|
|
glbinding::setAfterCallback(glAfterCallback);
|
|
|
|
{
|
|
int depth, stencil, aa, major, minor;
|
|
SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depth);
|
|
SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &stencil);
|
|
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &aa);
|
|
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
|
|
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
|
|
|
|
std::printf("Depth: %d, Stencil: %d, AA: %d, GL %d.%d\n",
|
|
depth, stencil, aa, major, minor);
|
|
}
|
|
|
|
int width, height;
|
|
SDL_GetWindowSize(window, &width, &height);
|
|
glm::mat4 proj = glm::perspectiveFov(75.0f, static_cast<float>(width), static_cast<float>(height),
|
|
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));
|
|
// sf::Font font;
|
|
// if (!font.loadFromFile("DejaVuSans.ttf")) {
|
|
// std::printf("Error loading font\n");
|
|
// return 1;
|
|
// }
|
|
|
|
// sf::Text fpsText{"0", font, 12};
|
|
// fpsText.setPosition({200, 200});
|
|
|
|
// window.setFramerateLimit(60);
|
|
|
|
// sf::Clock clock;
|
|
// sf::Time last = clock.getElapsedTime();
|
|
|
|
VBOManager vboManager;
|
|
|
|
Texture2D cubeTex("textures/Wood_Box_Texture.jpg");
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, static_cast<int>(GL_LINEAR_MIPMAP_LINEAR));
|
|
if (SDL_GL_ExtensionSupported("GL_EXT_texture_filter_anisotropic"))
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 4.0f);
|
|
else
|
|
std::printf("Warning: extension GL_EXT_texture_filter_anisotropic not supported\n");
|
|
Texture2D redTex("textures/red.png");
|
|
Texture2D whiteTex("textures/white.png");
|
|
|
|
VertexShader vs{fileToString("shaders/textured.vs")};
|
|
FragmentShader fs{fileToString("shaders/textured.fs")};
|
|
Program prog(vs, fs);
|
|
|
|
prog.use();
|
|
glUniformMatrix4fv(prog.getUniformLocation("projection_matrix"), 1, GL_FALSE,
|
|
glm::value_ptr(proj));
|
|
glUniformMatrix4fv(prog.getUniformLocation("view_matrix"), 1, GL_FALSE,
|
|
glm::value_ptr(view));
|
|
glUniform1i(prog.getUniformLocation("texBase"), 0);
|
|
|
|
Object box(vboManager, "objects/woodbox.obj", prog);
|
|
Object pyramid(vboManager, "objects/pyramid.obj", prog);
|
|
Object plane(vboManager, "objects/plane.obj", prog);
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
bool close = false;
|
|
|
|
while (!close) {
|
|
SDL_Event event;
|
|
while (SDL_PollEvent(&event)) {
|
|
switch(event.type) {
|
|
case SDL_KEYDOWN:
|
|
close = true;
|
|
break;
|
|
case SDL_WINDOWEVENT:
|
|
if (event.window.event == SDL_WINDOWEVENT_CLOSE)
|
|
close = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
glEnable(GL_DEPTH_TEST);
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glm::mat4 model = glm::translate(glm::vec3(0.5f, 0.5f, -0.5f)) *
|
|
glm::rotate(SDL_GetTicks()*0.001f, glm::vec3(1.0f, 0.0f, 0.0f)) *
|
|
glm::translate(glm::vec3(-0.5f, -0.5f, 0.5f));
|
|
cubeTex.bind();
|
|
box.draw(model);
|
|
whiteTex.bind();
|
|
plane.draw(glm::translate(glm::vec3(2.0f, -2.5f, 0.0f))*
|
|
glm::rotate(0.35f, glm::vec3(0.0f, 1.0f, 0.0f)));
|
|
redTex.bind();
|
|
pyramid.draw(glm::translate(glm::vec3(-2.0f, 0.0f, 0.0f)));
|
|
|
|
// glUseProgram(0);
|
|
// sf::Time now = clock.getElapsedTime();
|
|
// sf::Time elapsed = now - last;
|
|
// last = now;
|
|
// fpsText.setString(std::to_string(static_cast<int>(std::roundf(1.0f/elapsed.asSeconds()))));
|
|
// fpsText.setPosition({1680-fpsText.getLocalBounds().width, 0});
|
|
// window.draw(fpsText);
|
|
|
|
SDL_GL_SwapWindow(window);
|
|
}
|
|
}catch(Exception &ex) {
|
|
std::printf("%s\n", ex.toString().c_str());
|
|
retcode = 1;
|
|
}
|
|
|
|
SDL_GL_DeleteContext(glcontext);
|
|
|
|
SDL_DestroyWindow(window);
|
|
|
|
// Clean up
|
|
IMG_Quit();
|
|
SDL_Quit();
|
|
|
|
return retcode;
|
|
}
|