Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLFW 3 and save PGM #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ using namespace glm;

Camera::Camera()
{
_position = vec3(0.0);
_forward = fquat(1.0f,0.0f,0.0f,0.0f);
SetProjection();
}

Expand Down
1 change: 1 addition & 0 deletions Math/PerlinNoise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "vector"

#include <random>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtx/compatibility.hpp>
#include "MathUtil.h"
Expand Down
4 changes: 2 additions & 2 deletions TerrainFluid.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 50 additions & 21 deletions TerrainFluidSimulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,35 @@ void TerrainFluidSimulation::Stop()
_finished = true;
}

void TerrainFluidSimulation::SavePGM( const char* file )
{
const Grid2D<float>& 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;
Expand Down Expand Up @@ -100,24 +129,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;
}
Expand All @@ -133,20 +162,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)
Expand All @@ -167,7 +196,7 @@ void TerrainFluidSimulation::render()
// Resize
int w,h;
bool resize = false;
glfwGetWindowSize(&w,&h);
glfwGetWindowSize(win,&w,&h);
if (w != _width)
{
_width = w;
Expand Down Expand Up @@ -214,7 +243,7 @@ void TerrainFluidSimulation::render()
_testShader->UnBind();

// finish
glfwSwapBuffers();
glfwSwapBuffers(win);
glFinish();
}

Expand Down
1 change: 1 addition & 0 deletions TerrainFluidSimulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class TerrainFluidSimulation
void Run();

void Stop();
void SavePGM( const char* file );

protected:

Expand Down
43 changes: 33 additions & 10 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,24 @@

using namespace std;

GLFWwindow* win = 0;
TerrainFluidSimulation* simulationPtr = 0;

int onWindowClose()
void onWindowClose( GLFWwindow* )
{
if (simulationPtr) simulationPtr->Stop();
return GL_TRUE;
}

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)
Expand Down Expand Up @@ -63,24 +75,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)
Expand All @@ -101,12 +119,17 @@ int main(int argc, char** argv)
// Start Simulation ////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

glfwSetWindowCloseCallback(onWindowClose);
glfwSetWindowCloseCallback(win, onWindowClose);
glfwSetKeyCallback(win, keyHandler);


simulationPtr = new TerrainFluidSimulation(terrainDim);
simulationPtr->Run();

delete simulationPtr;
glfwDestroyWindow( win );
glfwTerminate();

cout << "The end." << endl;
return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion platform_includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include <GL/glew.h> // brew install glew

#include <GL/glx.h>
#include <GL/glfw.h>



Expand All @@ -47,6 +46,9 @@
// No support, sorry
#endif

#include <GLFW/glfw3.h>
extern GLFWwindow* win;

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned short ushort;
Expand Down