1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
#include <iostream>
#include <chrono>
#include <algorithm>
#include <cpplocate/cpplocate.h>
#include <cpplocate/ModuleInfo.h>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glbinding/gl/gl.h>
#include <glbinding/ContextInfo.h>
#include <glbinding/Version.h>
#include <GLFW/glfw3.h>
#include <globjects/globjects.h>
#include <globjects/base/File.h>
#include <globjects/logging.h>
#include <globjects/Uniform.h>
#include <globjects/Program.h>
#include <globjects/Shader.h>
#include "Icosahedron.h"
#include "datapath.inl"
using namespace gl;
namespace
{
globjects::Program * g_sphere = nullptr;
Icosahedron * g_icosahedron = nullptr;
glm::mat4 g_viewProjection;
const std::chrono::high_resolution_clock::time_point g_starttime = std::chrono::high_resolution_clock::now();
auto g_size = glm::ivec2{};
}
void resize()
{
static const auto fovy = glm::radians(40.f);
static const auto zNear = 1.f;
static const auto zFar = 16.f;
static const auto eye = glm::vec3{ 0.f, 1.f, 4.f };
static const auto center = glm::vec3{ 0.0, 0.0, 0.0 };
static const auto up = glm::vec3{ 0.0, 1.0, 0.0 };
const auto aspect = static_cast<float>(g_size.x) / glm::max(static_cast<float>(g_size.y), 1.f);
g_viewProjection = glm::perspective(fovy, aspect, zNear, zFar) * glm::lookAt(eye, center, up);
}
void initialize()
{
g_sphere = new globjects::Program();
g_sphere->ref();
const auto dataPath = common::retrieveDataPath("globjects", "dataPath");
g_sphere->attach(
globjects::Shader::fromFile(GL_VERTEX_SHADER, dataPath + "tessellation/sphere.vert")
, globjects::Shader::fromFile(GL_TESS_CONTROL_SHADER, dataPath + "tessellation/sphere.tcs")
, globjects::Shader::fromFile(GL_TESS_EVALUATION_SHADER, dataPath + "tessellation/sphere.tes")
, globjects::Shader::fromFile(GL_GEOMETRY_SHADER, dataPath + "tessellation/sphere.geom")
, globjects::Shader::fromFile(GL_FRAGMENT_SHADER, dataPath + "tessellation/sphere.frag")
, globjects::Shader::fromFile(GL_FRAGMENT_SHADER, dataPath + "tessellation/phong.frag"));
g_icosahedron = new Icosahedron();
g_icosahedron->ref();
resize();
}
void deinitialize()
{
g_sphere->unref();
g_icosahedron->unref();
}
void draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const auto t_elapsed = std::chrono::high_resolution_clock::now() - g_starttime;
const auto t = static_cast<float>(t_elapsed.count()) * 4e-10f;
glm::mat4 R = glm::rotate(t * 1.f, glm::vec3(sin(t * 0.321f), cos(t * 0.234f), sin(t * 0.123f)));
g_sphere->setUniform("transform", g_viewProjection);
g_sphere->setUniform("rotation", R);
const auto level = static_cast<int>((sin(t) * 0.5f + 0.5f) * 16) + 1;
g_sphere->setUniform("level", level);
g_sphere->use();
glViewport(0, 0, g_size.x, g_size.y);
glPatchParameteri(GL_PATCH_VERTICES, 3);
g_icosahedron->draw(GL_PATCHES);
g_sphere->release();
}
void error(int errnum, const char * errmsg)
{
globjects::critical() << errnum << ": " << errmsg << std::endl;
}
void framebuffer_size_callback(GLFWwindow * /*window*/, int width, int height)
{
g_size = glm::ivec2{ width, height };
resize();
}
void key_callback(GLFWwindow * window, int key, int /*scancode*/, int action, int /*modes*/)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
glfwSetWindowShouldClose(window, true);
if (key == GLFW_KEY_F5 && action == GLFW_RELEASE)
globjects::File::reloadAll();
}
int main(int /*argc*/, char * /*argv*/[])
{
// Initialize GLFW
if (!glfwInit())
return 1;
glfwSetErrorCallback(error);
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
// Create a context and, if valid, make it current
GLFWwindow * window = glfwCreateWindow(640, 480, "globjects Tessellation", NULL, NULL);
if (window == nullptr)
{
globjects::critical() << "Context creation failed. Terminate execution.";
glfwTerminate();
return -1;
}
glfwSetKeyCallback(window, key_callback);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwMakeContextCurrent(window);
// Initialize globjects (internally initializes glbinding, and registers the current context)
globjects::init();
std::cout << std::endl
<< "OpenGL Version: " << glbinding::ContextInfo::version() << std::endl
<< "OpenGL Vendor: " << glbinding::ContextInfo::vendor() << std::endl
<< "OpenGL Renderer: " << glbinding::ContextInfo::renderer() << std::endl << std::endl;
globjects::info() << "Press F5 to reload shaders." << std::endl << std::endl;
glfwGetFramebufferSize(window, &g_size[0], &g_size[1]);
initialize();
// Main loop
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
draw();
glfwSwapBuffers(window);
}
deinitialize();
// Properly shutdown GLFW
glfwTerminate();
return 0;
}
|