#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "Fluid.h"
using namespace std;
const int WINDOW_WIDTH = ViewportWidth;
const int WINDOW_HEIGHT = ViewportHeight;
const char* WINDOW_TITLE = "Fluid 2D - GLSL";
// Callback Functions
void ErrorCallback(int error, const char* description);
void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
int main(int argc, char **argv)
{
glfwSetErrorCallback(ErrorCallback);
//************************************
// Initialize GLFW
//************************************
if (!glfwInit())
exit(EXIT_FAILURE);
//************************************
// Create Window
//************************************
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
//glfwWindowHint(GLFW_SAMPLES, 4);
//glfwWindowHint(GLFW_RED_BITS, 8);
//glfwWindowHint(GLFW_GREEN_BITS, 8);
//glfwWindowHint(GLFW_BLUE_BITS, 8);
//glfwWindowHint(GLFW_ALPHA_BITS, 8);
//glfwWindowHint(GLFW_DEPTH_BITS, 24);
GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, NULL, NULL);
if (!window)
{
cout << "Fail to create window.\n";
glfwTerminate();
exit(EXIT_FAILURE);
}
//const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
//const float ScreenWidth = (float)(mode->width);
//glfwSetWindowPos(window, (ScreenWidth - (float)WINDOW_WIDTH)*0.5, 0);
glfwSetWindowPos(window, 0, 0);
glfwMakeContextCurrent(window);
//************************************
// Display Information
//************************************
cout << "Renderer \t\t:\t" << glGetString(GL_RENDERER) << endl;
cout << "OpenGL Version \t:\t" << glGetString(GL_VERSION) << endl;
cout << "GLSL Version \t:\t" << glGetString(GL_SHADING_LANGUAGE_VERSION) << endl;
cout << "GLFW Version \t:\t" << glfwGetVersionString() << endl;
//************************************
// Initialize GLEW
//************************************
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err){
// Problem: glewInit failed, something is seriously wrong.
printf("Error: %s\n", glewGetErrorString(err));
return 1;
}
printf("GLEW Version \t:\t%s\n", glewGetString(GLEW_VERSION));
if (glewIsSupported("GL_VERSION_3_2"))
cout << "OpenGL 3.2 API is available." << endl;
else {
cout << "OpenGL 3.2 API is not available." << endl;
return 1;
}
//************************************
// Setup Callback Functions
//************************************
glfwSetKeyCallback(window, KeyCallback);
//************************************
// Enter Main Loop
//************************************
float previousTime = 0;
// Fluid Object
Fluid FluidSim;
FluidSim.Initialize();
while (!glfwWindowShouldClose(window))
{
float currentTime = (float)glfwGetTime();
float inverseFPS = currentTime - previousTime;
previousTime = currentTime;
FluidSim.Update(inverseFPS);
FluidSim.Render();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
void ErrorCallback(int error, const char* description)
{
cout << description << endl;
}
void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}