gltest/src/main.cpp

121 lines
3.6 KiB
C++

#include "Shaders/ShaderProgram.h"
#include "GLObjects/WaveMesh.h"
#include "Audio/AudioPlayer.h"
#include "GLObjects/SpectralMesh.h"
#include <GLFW/glfw3.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <fstream>
#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() {
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);
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");
program.use();
// WaveMesh mesh(&program, 128);
// mesh.create();
SpectralMesh mesh(&program, 256);
mesh.create();
GLint view = glGetUniformLocation(program, "mView");
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);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
AudioPlayer player("/home/selim/dl/euphoria.wav");
player.setStreamListener([&mesh](void* data, std::size_t framesCount){
mesh.addLine((float*)data);
});
player.play();
float azimuth = 0.0f, elevation = 0.0f, scale = 1.0f;
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();
}