From 11f2a757e848f5cb08eb2adfd3b408bb7fbc309f Mon Sep 17 00:00:00 2001 From: Karl Robillard Date: Mon, 26 Oct 2020 21:55:40 -0400 Subject: [PATCH 1/2] Fixes for glfw3 and glm on Fedora 30. --- Camera.cpp | 2 ++ Math/PerlinNoise.cpp | 1 + TerrainFluid.pro | 4 ++-- TerrainFluidSimulation.cpp | 42 +++++++++++++++++++------------------- main.cpp | 30 ++++++++++++++++++--------- platform_includes.h | 4 +++- 6 files changed, 49 insertions(+), 34 deletions(-) diff --git a/Camera.cpp b/Camera.cpp index c1b2e05..40e423a 100644 --- a/Camera.cpp +++ b/Camera.cpp @@ -17,6 +17,8 @@ using namespace glm; Camera::Camera() { + _position = vec3(0.0); + _forward = fquat(1.0f,0.0f,0.0f,0.0f); SetProjection(); } diff --git a/Math/PerlinNoise.cpp b/Math/PerlinNoise.cpp index f617b76..39e1c95 100644 --- a/Math/PerlinNoise.cpp +++ b/Math/PerlinNoise.cpp @@ -13,6 +13,7 @@ #include "vector" #include +#define GLM_ENABLE_EXPERIMENTAL #include #include #include "MathUtil.h" diff --git a/TerrainFluid.pro b/TerrainFluid.pro index fa87652..67739e5 100644 --- a/TerrainFluid.pro +++ b/TerrainFluid.pro @@ -51,10 +51,10 @@ mac { QMAKE_CXXFLAGS += -fopenmp QMAKE_LFLAGS += -fopenmp CONFIG += link_pkgconfig - PKGCONFIG += libglfw + # PKGCONFIG += libglfw LIBS+=-lboost_system LIBS+=-lboost_filesystem - LIBS+=-lGLEW + LIBS+=-lglfw -lGLEW LIBS+=-lGL copydata.commands = $(COPY_DIR) $$PWD/Resources $$OUT_PWD diff --git a/TerrainFluidSimulation.cpp b/TerrainFluidSimulation.cpp index 83df6ba..ea65c48 100644 --- a/TerrainFluidSimulation.cpp +++ b/TerrainFluidSimulation.cpp @@ -100,24 +100,24 @@ void TerrainFluidSimulation::runMainloop() void TerrainFluidSimulation::checkInput() { // exit - if (glfwGetKey(GLFW_KEY_ESC)) + if (glfwGetKey(win,GLFW_KEY_ESCAPE)) { _finished = true; } - if (glfwGetKey('O')) _rain = true; - if (glfwGetKey('P')) _rain = false; + if (glfwGetKey(win,'O')) _rain = true; + if (glfwGetKey(win,'P')) _rain = false; - if (glfwGetKey('K')) _flood = true; - if (glfwGetKey('L')) _flood = false; + if (glfwGetKey(win,'K')) _flood = true; + if (glfwGetKey(win,'L')) _flood = false; // move rain position float d = 1.0f; - if (glfwGetKey(GLFW_KEY_UP)) _rainPos.y += d; - if (glfwGetKey(GLFW_KEY_DOWN)) _rainPos.y -= d; - if (glfwGetKey(GLFW_KEY_RIGHT)) _rainPos.x += d; - if (glfwGetKey(GLFW_KEY_LEFT)) _rainPos.x -= d; + if (glfwGetKey(win,GLFW_KEY_UP)) _rainPos.y += d; + if (glfwGetKey(win,GLFW_KEY_DOWN)) _rainPos.y -= d; + if (glfwGetKey(win,GLFW_KEY_RIGHT)) _rainPos.x += d; + if (glfwGetKey(win,GLFW_KEY_LEFT)) _rainPos.x -= d; _simulation.rainPos = _rainPos; } @@ -133,20 +133,20 @@ void TerrainFluidSimulation::cameraMovement(double dt) vec3 yAxis(0,1,0); vec3 xAxis(1,0,0); - if (glfwGetKey('D')) _cam.TranslateLocal(vec3(camSpeed,0,0)); - if (glfwGetKey('A')) _cam.TranslateLocal(vec3(-camSpeed,0,0)); + if (glfwGetKey(win,'D')) _cam.TranslateLocal(vec3(camSpeed,0,0)); + if (glfwGetKey(win,'A')) _cam.TranslateLocal(vec3(-camSpeed,0,0)); - if (glfwGetKey('R')) _cam.TranslateLocal(vec3(0,camSpeed,0)); - if (glfwGetKey('F')) _cam.TranslateLocal(vec3(0,-camSpeed,0)); + if (glfwGetKey(win,'R')) _cam.TranslateLocal(vec3(0,camSpeed,0)); + if (glfwGetKey(win,'F')) _cam.TranslateLocal(vec3(0,-camSpeed,0)); - if (glfwGetKey('W')) _cam.TranslateLocal(vec3(0,0,-camSpeed)); - if (glfwGetKey('S')) _cam.TranslateLocal(vec3(0,0,camSpeed)); + if (glfwGetKey(win,'W')) _cam.TranslateLocal(vec3(0,0,-camSpeed)); + if (glfwGetKey(win,'S')) _cam.TranslateLocal(vec3(0,0,camSpeed)); - if (glfwGetKey('Q')) _cam.GlobalRotate(yAxis,-rotSpeed); - if (glfwGetKey('E')) _cam.GlobalRotate(yAxis,rotSpeed); + if (glfwGetKey(win,'Q')) _cam.GlobalRotate(yAxis,-rotSpeed); + if (glfwGetKey(win,'E')) _cam.GlobalRotate(yAxis,rotSpeed); - if (glfwGetKey('T')) _cam.LocalRotate(xAxis,-rotSpeed); - if (glfwGetKey('G')) _cam.LocalRotate(xAxis,rotSpeed); + if (glfwGetKey(win,'T')) _cam.LocalRotate(xAxis,-rotSpeed); + if (glfwGetKey(win,'G')) _cam.LocalRotate(xAxis,rotSpeed); } void TerrainFluidSimulation::updatePhysics(double dt) @@ -167,7 +167,7 @@ void TerrainFluidSimulation::render() // Resize int w,h; bool resize = false; - glfwGetWindowSize(&w,&h); + glfwGetWindowSize(win,&w,&h); if (w != _width) { _width = w; @@ -214,7 +214,7 @@ void TerrainFluidSimulation::render() _testShader->UnBind(); // finish - glfwSwapBuffers(); + glfwSwapBuffers(win); glFinish(); } diff --git a/main.cpp b/main.cpp index 775f7e9..69841b6 100644 --- a/main.cpp +++ b/main.cpp @@ -19,12 +19,12 @@ using namespace std; +GLFWwindow* win = 0; TerrainFluidSimulation* simulationPtr = 0; -int onWindowClose() +void onWindowClose( GLFWwindow* ) { if (simulationPtr) simulationPtr->Stop(); - return GL_TRUE; } int main(int argc, char** argv) @@ -63,24 +63,30 @@ int main(int argc, char** argv) } - glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4); // 4x antialiasing + glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing // Use OpenGL Core v3.2 - glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); - glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); - glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Open an OpenGL window - if( !glfwOpenWindow( windowWidth,windowHeight, 8,8,8,8,8,8, GLFW_WINDOW ) ) + win = glfwCreateWindow( windowWidth, windowHeight, "Terrain", NULL, NULL ); + if( ! win ) { std::cerr << "[GLFW] Error opening window" << std::endl; glfwTerminate(); exit(1); } + glfwMakeContextCurrent( win ); + int major, minor, rev; - glfwGetGLVersion(&major, &minor, &rev); + major = glfwGetWindowAttrib( win, GLFW_CONTEXT_VERSION_MAJOR ); + minor = glfwGetWindowAttrib( win, GLFW_CONTEXT_VERSION_MINOR ); + rev = glfwGetWindowAttrib( win, GLFW_CONTEXT_REVISION ); + fprintf(stdout, "OpenGL version recieved: %d.%d.%d\n", major, minor, rev); // Init Glew (OpenGL Extension Loading) @@ -101,12 +107,16 @@ int main(int argc, char** argv) // Start Simulation //////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// - glfwSetWindowCloseCallback(onWindowClose); + glfwSetWindowCloseCallback(win, onWindowClose); + simulationPtr = new TerrainFluidSimulation(terrainDim); simulationPtr->Run(); delete simulationPtr; + glfwDestroyWindow( win ); + glfwTerminate(); + cout << "The end." << endl; return 0; } diff --git a/platform_includes.h b/platform_includes.h index e2f78cb..0c6f809 100644 --- a/platform_includes.h +++ b/platform_includes.h @@ -38,7 +38,6 @@ #include // brew install glew #include -#include @@ -47,6 +46,9 @@ // No support, sorry #endif +#include +extern GLFWwindow* win; + typedef unsigned int uint; typedef unsigned long ulong; typedef unsigned short ushort; From d0f651fe48d49e3ccde61600380d0453a36dc6dc Mon Sep 17 00:00:00 2001 From: Karl Robillard Date: Sun, 15 Jun 2014 21:28:37 -0700 Subject: [PATCH 2/2] Save PGM when F4 key is pressed. --- TerrainFluidSimulation.cpp | 29 +++++++++++++++++++++++++++++ TerrainFluidSimulation.h | 1 + main.cpp | 13 +++++++++++++ 3 files changed, 43 insertions(+) diff --git a/TerrainFluidSimulation.cpp b/TerrainFluidSimulation.cpp index ea65c48..17f72dc 100644 --- a/TerrainFluidSimulation.cpp +++ b/TerrainFluidSimulation.cpp @@ -39,6 +39,35 @@ void TerrainFluidSimulation::Stop() _finished = true; } +void TerrainFluidSimulation::SavePGM( const char* file ) +{ + const Grid2D& grid = _simulation.terrain; + const float* it = grid.ptr(); + const float* end = it + grid.size(); + float low = 9999.0f; + float high = -9999.0f; + float scale; + + for( ; it != end; ++it ) + { + float n = *it; + if( low > n ) + low = n; + if( high < n ) + high = n; + } + scale = 255.0f / (high - low); + + FILE* fp = fopen( file, "w" ); + if( fp ) + { + fprintf( fp, "P5 %d %d 255\n", grid.width(), grid.height() ); + for( it = grid.ptr(); it != end; ++it ) + fputc( int((*it - low) * scale), fp ); + fclose( fp ); + } +} + void TerrainFluidSimulation::runMainloop() { using namespace std::chrono; diff --git a/TerrainFluidSimulation.h b/TerrainFluidSimulation.h index ed863a6..bfa9286 100644 --- a/TerrainFluidSimulation.h +++ b/TerrainFluidSimulation.h @@ -37,6 +37,7 @@ class TerrainFluidSimulation void Run(); void Stop(); + void SavePGM( const char* file ); protected: diff --git a/main.cpp b/main.cpp index 69841b6..64358d6 100644 --- a/main.cpp +++ b/main.cpp @@ -27,6 +27,18 @@ void onWindowClose( GLFWwindow* ) if (simulationPtr) simulationPtr->Stop(); } +void keyHandler( GLFWwindow*, int key, int /*code*/, int action, int mods ) +{ + if( action == GLFW_PRESS ) + { + //if( mods & GLFW_MOD_CONTROL && key == GLFW_KEY_S ) + if( key == GLFW_KEY_F4 ) + { + simulationPtr->SavePGM( "erosion.pgm" ); + } + } +} + int main(int argc, char** argv) { // Settings //////////////////////////////////////////////////////////// @@ -108,6 +120,7 @@ int main(int argc, char** argv) //////////////////////////////////////////////////////////////////////// glfwSetWindowCloseCallback(win, onWindowClose); + glfwSetKeyCallback(win, keyHandler); simulationPtr = new TerrainFluidSimulation(terrainDim);