basic camera support
This commit is contained in:
parent
a3ebd84cfc
commit
d8c8365690
@ -1,7 +1,7 @@
|
|||||||
cmake_minimum_required(VERSION 3.2)
|
cmake_minimum_required(VERSION 3.2)
|
||||||
project(glTest)
|
project(glTest)
|
||||||
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3 -g")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O0 -g")
|
||||||
set(CMAKE_BUILD_TYPE DEBUG)
|
set(CMAKE_BUILD_TYPE DEBUG)
|
||||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
|
||||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
|
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
|
||||||
|
|||||||
@ -1,10 +1,22 @@
|
|||||||
#include "Camera.h"
|
#include "Camera.h"
|
||||||
#include <GL/glew.h>
|
#include "OGL.h"
|
||||||
|
#include <glm/gtx/rotate_vector.hpp>
|
||||||
|
|
||||||
Camera::Camera() {
|
Camera::Camera() {
|
||||||
glm::vec3 eye(0.0f, 0.0f, 4.0f), at(0.0f, 0.0f, 0.0f), up(0.0f, 1.0f, 0.0f);
|
_viewDistance = 4.0f;
|
||||||
_view = glm::lookAt(eye, at, up);
|
_eye = glm::vec3(0.0f, 0.0f, _viewDistance);
|
||||||
_projection = glm::perspective(45.0f, 1.0f, 2.0f, -2.0f);
|
_at = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||||
|
_up = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||||
|
_view = glm::lookAt(_eye, _at, _up);
|
||||||
|
_projection = glm::perspective(45.0f, 1.0f, -2.0f, 2.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::init() {
|
||||||
|
ShaderProgram* sp = OGL::instance()->getCurShaderProgram();
|
||||||
|
GLint view = glGetUniformLocation(*sp, "mView");
|
||||||
|
glUniformMatrix4fv(view, 1, GL_FALSE, glm::value_ptr(_view));
|
||||||
|
GLint projection = glGetUniformLocation(*sp, "mProjection");
|
||||||
|
glUniformMatrix4fv(projection, 1, GL_FALSE, glm::value_ptr(_projection));
|
||||||
}
|
}
|
||||||
|
|
||||||
Camera *Camera::instance() {
|
Camera *Camera::instance() {
|
||||||
@ -12,9 +24,33 @@ Camera *Camera::instance() {
|
|||||||
return &camera;
|
return &camera;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::storeUniformVars() const {
|
void Camera::updateView() {
|
||||||
// GLint view = glGetUniformLocation(program, "mView");
|
_view = glm::lookAt(_eye, _at, _up);
|
||||||
// glUniformMatrix4fv(view, 1, GL_FALSE, glm::value_ptr(mView));
|
ShaderProgram* sp = OGL::instance()->getCurShaderProgram();
|
||||||
// GLint projection = glGetUniformLocation(program, "mProjection");
|
GLint view = glGetUniformLocation(*sp, "mView");
|
||||||
// glUniformMatrix4fv(projection, 1, GL_FALSE, glm::value_ptr(mProjection));
|
glUniformMatrix4fv(view, 1, GL_FALSE, glm::value_ptr(_view));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::turnLeft(float angle) {
|
||||||
|
_up = glm::rotateY(_up, angle);
|
||||||
|
_eye = glm::rotateY(_eye, angle);
|
||||||
|
updateView();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::turnRight(float angle) {
|
||||||
|
_up = glm::rotateY(_up, -angle);
|
||||||
|
_eye = glm::rotateY(_eye, -angle);
|
||||||
|
updateView();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::turnUp(float angle) {
|
||||||
|
_up = glm::rotateX(_up, angle);
|
||||||
|
_eye = glm::rotateX(_eye, angle);
|
||||||
|
updateView();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::turnDown(float angle) {
|
||||||
|
_up = glm::rotateX(_up, -angle);
|
||||||
|
_eye = glm::rotateX(_eye, -angle);
|
||||||
|
updateView();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
class Camera {
|
class Camera {
|
||||||
private:
|
private:
|
||||||
|
float _viewDistance;
|
||||||
|
glm::vec3 _eye, _at, _up;
|
||||||
glm::mat4 _view;
|
glm::mat4 _view;
|
||||||
glm::mat4 _projection;
|
glm::mat4 _projection;
|
||||||
|
|
||||||
@ -13,10 +15,15 @@ private:
|
|||||||
Camera();
|
Camera();
|
||||||
Camera(const Camera&) = delete;
|
Camera(const Camera&) = delete;
|
||||||
void operator=(const Camera&) = delete;
|
void operator=(const Camera&) = delete;
|
||||||
void storeUniformVars() const;
|
void updateView();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static Camera* instance();
|
static Camera* instance();
|
||||||
|
void init();
|
||||||
|
void turnLeft(float angle);
|
||||||
|
void turnRight(float angle);
|
||||||
|
void turnUp(float angle);
|
||||||
|
void turnDown(float angle);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,57 +0,0 @@
|
|||||||
#include <GL/glew.h>
|
|
||||||
#include "GLApp.h"
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
bool wiredFlag = false;
|
|
||||||
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
|
||||||
{
|
|
||||||
if (key == GLFW_KEY_M && action == GLFW_PRESS) {
|
|
||||||
glPolygonMode(GL_FRONT_AND_BACK, wiredFlag ? GL_FILL : GL_LINE);
|
|
||||||
wiredFlag = !wiredFlag;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
GLApp::GLApp() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
GLApp *GLApp::instance() {
|
|
||||||
static GLApp app;
|
|
||||||
return &app;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GLApp::updateFpsCounter() const {
|
|
||||||
static double previous_seconds = glfwGetTime ();
|
|
||||||
static int frame_count;
|
|
||||||
double current_seconds = glfwGetTime ();
|
|
||||||
double elapsed_seconds = current_seconds - previous_seconds;
|
|
||||||
if (elapsed_seconds > 0.25) {
|
|
||||||
previous_seconds = current_seconds;
|
|
||||||
double fps = (double)frame_count / elapsed_seconds;
|
|
||||||
char tmp[128];
|
|
||||||
sprintf (tmp, "opengl @ fps: %.2f", fps);
|
|
||||||
glfwSetWindowTitle (_window, tmp);
|
|
||||||
frame_count = 0;
|
|
||||||
}
|
|
||||||
frame_count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GLApp::init() {
|
|
||||||
glfwInit();
|
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
||||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
||||||
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
|
|
||||||
glfwWindowHint (GLFW_SAMPLES, 4);
|
|
||||||
|
|
||||||
_window = glfwCreateWindow(1024, 1024, "OpenGL", nullptr, nullptr); // Windowed
|
|
||||||
glfwMakeContextCurrent(_window);
|
|
||||||
glfwSetKeyCallback(_window, keyCallback);
|
|
||||||
|
|
||||||
glewExperimental = GL_TRUE;
|
|
||||||
GLenum err = glewInit();
|
|
||||||
if(err != GLEW_OK) {
|
|
||||||
std::cout << "GLEW error: " << err << ": " << glewGetErrorString(err) << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
22
src/GLApp.h
22
src/GLApp.h
@ -1,22 +0,0 @@
|
|||||||
#ifndef GLTEST_GLAPP_H
|
|
||||||
#define GLTEST_GLAPP_H
|
|
||||||
|
|
||||||
#include <GLFW/glfw3.h>
|
|
||||||
|
|
||||||
class GLApp {
|
|
||||||
private:
|
|
||||||
GLFWwindow* _window;
|
|
||||||
|
|
||||||
private:
|
|
||||||
GLApp();
|
|
||||||
GLApp(const GLApp&) = delete;
|
|
||||||
void operator=(const GLApp&) = delete;
|
|
||||||
void updateFpsCounter() const;
|
|
||||||
|
|
||||||
public:
|
|
||||||
static GLApp* instance();
|
|
||||||
void init();
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#endif //GLTEST_GLAPP_H
|
|
||||||
@ -40,6 +40,7 @@ void GLObject::create() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GLObject::rotate(glm::vec3 angles) {
|
void GLObject::rotate(glm::vec3 angles) {
|
||||||
|
// FIXME: Change transformation code
|
||||||
GLint world = glGetUniformLocation(*_sp, "mWorld");
|
GLint world = glGetUniformLocation(*_sp, "mWorld");
|
||||||
_worldMatrix = glm::rotate(_worldMatrix, angles.x, glm::vec3(1.0f, 0.0f, 0.0f));
|
_worldMatrix = glm::rotate(_worldMatrix, angles.x, glm::vec3(1.0f, 0.0f, 0.0f));
|
||||||
_worldMatrix = glm::rotate(_worldMatrix, angles.y, glm::vec3(0.0f, 1.0f, 0.0f));
|
_worldMatrix = glm::rotate(_worldMatrix, angles.y, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
@ -48,6 +49,7 @@ void GLObject::rotate(glm::vec3 angles) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GLObject::scale(glm::vec3 scaleFactor) {
|
void GLObject::scale(glm::vec3 scaleFactor) {
|
||||||
|
// FIXME: Change transformation code
|
||||||
GLint world = glGetUniformLocation(*_sp, "mWorld");
|
GLint world = glGetUniformLocation(*_sp, "mWorld");
|
||||||
_worldMatrix = glm::scale(_worldMatrix, scaleFactor);
|
_worldMatrix = glm::scale(_worldMatrix, scaleFactor);
|
||||||
glUniformMatrix4fv(world, 1, GL_FALSE, glm::value_ptr(_worldMatrix));
|
glUniformMatrix4fv(world, 1, GL_FALSE, glm::value_ptr(_worldMatrix));
|
||||||
@ -58,7 +60,8 @@ void GLObject::translate(glm::vec3 point) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GLObject::draw() {
|
void GLObject::draw() {
|
||||||
_worldMatrix = glm::mat4();
|
GLint world = glGetUniformLocation(*_sp, "mWorld");
|
||||||
|
glUniformMatrix4fv(world, 1, GL_FALSE, glm::value_ptr(_worldMatrix));
|
||||||
glBindVertexArray(_vao);
|
glBindVertexArray(_vao);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVbo);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVbo);
|
||||||
glDrawElements(GL_TRIANGLES, _trianglesCount, GL_UNSIGNED_INT, nullptr);
|
glDrawElements(GL_TRIANGLES, _trianglesCount, GL_UNSIGNED_INT, nullptr);
|
||||||
|
|||||||
@ -26,7 +26,7 @@ public:
|
|||||||
void scale(glm::vec3 scaleFactor) override final;
|
void scale(glm::vec3 scaleFactor) override final;
|
||||||
void translate(glm::vec3 point) override final;
|
void translate(glm::vec3 point) override final;
|
||||||
void draw() override final;
|
void draw() override final;
|
||||||
void update();
|
void update() override final;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual std::tuple<Vertex*, std::size_t> generateVertices() const = 0;
|
virtual std::tuple<Vertex*, std::size_t> generateVertices() const = 0;
|
||||||
|
|||||||
@ -9,6 +9,7 @@ struct IGLObject {
|
|||||||
virtual void scale(glm::vec3 scaleFactor) = 0;
|
virtual void scale(glm::vec3 scaleFactor) = 0;
|
||||||
virtual void translate(glm::vec3 point) = 0;
|
virtual void translate(glm::vec3 point) = 0;
|
||||||
virtual void draw() = 0;
|
virtual void draw() = 0;
|
||||||
|
virtual void update() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //GLTEST_IGLOBJECT_H
|
#endif //GLTEST_IGLOBJECT_H
|
||||||
|
|||||||
@ -13,7 +13,7 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
SpectralMesh(ShaderProgram* shader, std::size_t size);
|
SpectralMesh(ShaderProgram* shader, std::size_t size);
|
||||||
~SpectralMesh();
|
virtual ~SpectralMesh();
|
||||||
void addLine(float* line);
|
void addLine(float* line);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@ -13,3 +13,7 @@ void WaveMesh::setShift(float shift) {
|
|||||||
WaveMesh::WaveMesh(ShaderProgram *shader, std::size_t size): Mesh(shader, size), _shift(0) {
|
WaveMesh::WaveMesh(ShaderProgram *shader, std::size_t size): Mesh(shader, size), _shift(0) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WaveMesh::~WaveMesh() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@ -9,6 +9,7 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
WaveMesh(ShaderProgram* shader, std::size_t size);
|
WaveMesh(ShaderProgram* shader, std::size_t size);
|
||||||
|
virtual ~WaveMesh();
|
||||||
void setShift(float shift);
|
void setShift(float shift);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
112
src/OGL.cpp
Normal file
112
src/OGL.cpp
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
#include "OGL.h"
|
||||||
|
#include "Camera.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
bool wiredFlag = false;
|
||||||
|
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
||||||
|
{
|
||||||
|
const float turnStep = 0.1f; // in radians
|
||||||
|
|
||||||
|
if (key == GLFW_KEY_M && action == GLFW_PRESS) {
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, wiredFlag ? GL_FILL : GL_LINE);
|
||||||
|
wiredFlag = !wiredFlag;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(action == GLFW_PRESS || action == GLFW_REPEAT) {
|
||||||
|
switch (key) {
|
||||||
|
case GLFW_KEY_LEFT:
|
||||||
|
Camera::instance()->turnLeft(turnStep);
|
||||||
|
break;
|
||||||
|
case GLFW_KEY_RIGHT:
|
||||||
|
Camera::instance()->turnRight(turnStep);
|
||||||
|
break;
|
||||||
|
case GLFW_KEY_UP:
|
||||||
|
Camera::instance()->turnUp(turnStep);
|
||||||
|
break;
|
||||||
|
case GLFW_KEY_DOWN:
|
||||||
|
Camera::instance()->turnDown(turnStep);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
OGL::OGL() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
OGL::~OGL() {
|
||||||
|
glfwTerminate();
|
||||||
|
}
|
||||||
|
|
||||||
|
OGL *OGL::instance() {
|
||||||
|
static OGL app;
|
||||||
|
return &app;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OGL::updateFpsCounter() const {
|
||||||
|
static double previous_seconds = glfwGetTime ();
|
||||||
|
static int frame_count;
|
||||||
|
double current_seconds = glfwGetTime ();
|
||||||
|
double elapsed_seconds = current_seconds - previous_seconds;
|
||||||
|
if (elapsed_seconds > 0.25) {
|
||||||
|
previous_seconds = current_seconds;
|
||||||
|
double fps = (double)frame_count / elapsed_seconds;
|
||||||
|
char tmp[128];
|
||||||
|
sprintf (tmp, "opengl @ fps: %.2f", fps);
|
||||||
|
glfwSetWindowTitle (_window, tmp);
|
||||||
|
frame_count = 0;
|
||||||
|
}
|
||||||
|
frame_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OGL::init() {
|
||||||
|
glfwInit();
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||||
|
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
|
||||||
|
glfwWindowHint (GLFW_SAMPLES, 4);
|
||||||
|
|
||||||
|
_window = glfwCreateWindow(1024, 1024, "OpenGL", nullptr, nullptr); // Windowed
|
||||||
|
glfwMakeContextCurrent(_window);
|
||||||
|
glfwSetKeyCallback(_window, keyCallback);
|
||||||
|
|
||||||
|
glewExperimental = GL_TRUE;
|
||||||
|
GLenum err = glewInit();
|
||||||
|
if(err != GLEW_OK) {
|
||||||
|
std::string errMsg = std::string("GLEW init error: ") + (char*)glewGetErrorString(err);
|
||||||
|
throw new std::runtime_error(errMsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void OGL::run() {
|
||||||
|
while(!glfwWindowShouldClose(_window)) {
|
||||||
|
updateFpsCounter();
|
||||||
|
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
for(IGLObject* obj: _glObjects) {
|
||||||
|
//obj->scale(glm::vec3(1.0f, 1.0f, 1.0f));
|
||||||
|
obj->update();
|
||||||
|
obj->draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
glfwPollEvents();
|
||||||
|
glfwSwapBuffers(_window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void OGL::addObject(IGLObject *object) {
|
||||||
|
_glObjects.push_back(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
ShaderProgram *OGL::getCurShaderProgram() {
|
||||||
|
return _curShaderProgram;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OGL::setCurShaderProgram(ShaderProgram *sp) {
|
||||||
|
_curShaderProgram = sp;
|
||||||
|
_curShaderProgram->use();
|
||||||
|
}
|
||||||
33
src/OGL.h
Normal file
33
src/OGL.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#ifndef GLTEST_GLAPP_H
|
||||||
|
#define GLTEST_GLAPP_H
|
||||||
|
|
||||||
|
#include "GLObjects/IGLObject.h"
|
||||||
|
#include "Shaders/ShaderProgram.h"
|
||||||
|
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
class OGL {
|
||||||
|
private:
|
||||||
|
GLFWwindow* _window;
|
||||||
|
std::list<IGLObject*> _glObjects;
|
||||||
|
ShaderProgram* _curShaderProgram;
|
||||||
|
|
||||||
|
private:
|
||||||
|
OGL();
|
||||||
|
~OGL();
|
||||||
|
OGL(const OGL &) = delete;
|
||||||
|
void operator=(const OGL &) = delete;
|
||||||
|
void updateFpsCounter() const;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static OGL * instance();
|
||||||
|
void init();
|
||||||
|
void run();
|
||||||
|
void addObject(IGLObject* object);
|
||||||
|
ShaderProgram* getCurShaderProgram();
|
||||||
|
void setCurShaderProgram(ShaderProgram* sp);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //GLTEST_GLAPP_H
|
||||||
100
src/main.cpp
100
src/main.cpp
@ -2,76 +2,24 @@
|
|||||||
#include "GLObjects/WaveMesh.h"
|
#include "GLObjects/WaveMesh.h"
|
||||||
#include "Audio/AudioPlayer.h"
|
#include "Audio/AudioPlayer.h"
|
||||||
#include "GLObjects/SpectralMesh.h"
|
#include "GLObjects/SpectralMesh.h"
|
||||||
|
#include "OGL.h"
|
||||||
#include <GLFW/glfw3.h>
|
#include "Camera.h"
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
glm::vec3 eye(0.0f, 0.0f, 4.0f), at(0.0f, 0.0f, 0.0f), up(0.0f, 1.0f, 0.0f);
|
|
||||||
glm::mat4 mView = glm::lookAt(eye, at, up);
|
|
||||||
glm::mat4 mProjection = glm::perspective(45.0f, 1.0f, -2.0f, 2.0f); //glm::perspective(45.0f, 1.0f, 2.0f, -2.0f);
|
|
||||||
bool wired = false;
|
|
||||||
|
|
||||||
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
|
||||||
{
|
|
||||||
if (key == GLFW_KEY_M && action == GLFW_PRESS) {
|
|
||||||
glPolygonMode(GL_FRONT_AND_BACK, wired ? GL_FILL : GL_LINE);
|
|
||||||
wired = !wired;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void _update_fps_counter (GLFWwindow* window) {
|
|
||||||
static double previous_seconds = glfwGetTime ();
|
|
||||||
static int frame_count;
|
|
||||||
double current_seconds = glfwGetTime ();
|
|
||||||
double elapsed_seconds = current_seconds - previous_seconds;
|
|
||||||
if (elapsed_seconds > 0.25) {
|
|
||||||
previous_seconds = current_seconds;
|
|
||||||
double fps = (double)frame_count / elapsed_seconds;
|
|
||||||
char tmp[128];
|
|
||||||
sprintf (tmp, "opengl @ fps: %.2f", fps);
|
|
||||||
glfwSetWindowTitle (window, tmp);
|
|
||||||
frame_count = 0;
|
|
||||||
}
|
|
||||||
frame_count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
glfwInit();
|
OGL::instance()->init();
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
||||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
||||||
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
|
|
||||||
glfwWindowHint (GLFW_SAMPLES, 4);
|
|
||||||
|
|
||||||
GLFWwindow* window = glfwCreateWindow(1024, 1024, "OpenGL", nullptr, nullptr); // Windowed
|
|
||||||
glfwMakeContextCurrent(window);
|
|
||||||
glfwSetKeyCallback(window, key_callback);
|
|
||||||
|
|
||||||
glewExperimental = GL_TRUE;
|
|
||||||
GLenum err = glewInit();
|
|
||||||
if(err != GLEW_OK) {
|
|
||||||
std::cout << "GLEW error: " << err << ": " << glewGetErrorString(err) << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
ShaderProgram program("../shaders/vertex.glsl", "../shaders/fragment.glsl");
|
ShaderProgram program("../shaders/vertex.glsl", "../shaders/fragment.glsl");
|
||||||
program.use();
|
OGL::instance()->setCurShaderProgram(&program);
|
||||||
|
|
||||||
// WaveMesh mesh(&program, 128);
|
|
||||||
// mesh.create();
|
|
||||||
|
|
||||||
SpectralMesh mesh(&program, 256);
|
SpectralMesh mesh(&program, 256);
|
||||||
mesh.create();
|
mesh.create();
|
||||||
|
OGL::instance()->addObject(&mesh);
|
||||||
|
|
||||||
GLint view = glGetUniformLocation(program, "mView");
|
Camera::instance()->init();
|
||||||
glUniformMatrix4fv(view, 1, GL_FALSE, glm::value_ptr(mView));
|
|
||||||
GLint projection = glGetUniformLocation(program, "mProjection");
|
|
||||||
glUniformMatrix4fv(projection, 1, GL_FALSE, glm::value_ptr(mProjection));
|
|
||||||
|
|
||||||
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
glDepthFunc(GL_LEQUAL);
|
glDepthFunc(GL_LEQUAL);
|
||||||
@ -83,39 +31,5 @@ int main() {
|
|||||||
});
|
});
|
||||||
player.play();
|
player.play();
|
||||||
|
|
||||||
float azimuth = 0.0f, elevation = 0.0f, scale = 1.0f;
|
OGL::instance()->run();
|
||||||
|
|
||||||
while(!glfwWindowShouldClose(window)) {
|
|
||||||
_update_fps_counter (window);
|
|
||||||
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
||||||
|
|
||||||
if(glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
|
|
||||||
azimuth -= 0.05f;
|
|
||||||
} else if(glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
|
|
||||||
azimuth += 0.05f;
|
|
||||||
} else if(glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
|
|
||||||
elevation -= 0.05f;
|
|
||||||
} else if(glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
|
|
||||||
elevation += 0.05f;
|
|
||||||
} else if(glfwGetKey(window, GLFW_KEY_KP_ADD) == GLFW_PRESS) {
|
|
||||||
scale += 0.05f;
|
|
||||||
} else if(glfwGetKey(window, GLFW_KEY_KP_SUBTRACT) == GLFW_PRESS) {
|
|
||||||
scale -= 0.05f;
|
|
||||||
}
|
|
||||||
|
|
||||||
// double time = glfwGetTime();
|
|
||||||
// float shift = (float)fmod(time, 2*3.1415926);
|
|
||||||
// mesh.setShift(shift*2);
|
|
||||||
mesh.update();
|
|
||||||
|
|
||||||
|
|
||||||
mesh.scale(glm::vec3(scale, scale, scale));
|
|
||||||
mesh.rotate(glm::vec3(elevation, azimuth, 0));
|
|
||||||
mesh.draw();
|
|
||||||
|
|
||||||
glfwPollEvents();
|
|
||||||
glfwSwapBuffers(window);
|
|
||||||
}
|
|
||||||
|
|
||||||
glfwTerminate();
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user