Browse Source

added mouse interaction

master
Joshua Moerman 13 years ago
parent
commit
7df0845872
  1. 85
      App.h
  2. 65
      GlutTest.cbp
  3. 24
      main.cpp
  4. 2
      resources/myTeaShader.frag
  5. 7
      resources/myTeaShader.vert

85
App.h

@ -15,12 +15,6 @@
#include "shader.h"
#include "fbo.h"
static const std::string filenames[] = {
"resources/attractor_2011-04-28_10-30-35-2.stf",
"resources/attractor_2011-04-28_10-05-11-1.stf",
"resources/attractor_2011-04-29_04-34-41-9.stf",
"resources/attractor_2011-04-29_15-19-03-9.stf" };
static const GLfloat quad[] = {
1.0, 1.0,
1.0, -1.0,
@ -34,6 +28,7 @@ static const GLfloat tex_quad[] = {
0.0, 1.0 };
class App {
public:
int counter;
unsigned int width, height;
@ -44,13 +39,9 @@ class App {
fbo fbo1;
fbo fbo2;
static constexpr size_t number_of_lines = 40000;
static constexpr size_t number_of_vertices = 2*number_of_lines;
std::array<GLfloat, 3*number_of_vertices> lines;
std::array<GLfloat, 7> parameters;
std::array<GLfloat, 7> random_parameters;
std::array<bool, 3> mouse_buttons;
std::array<GLfloat, 2> mouse;
public:
App(unsigned int w, unsigned int h) :
counter(0),
width(w),
@ -61,17 +52,12 @@ public:
blur_shader("resources/myHBlurShader.vert", "resources/myHBlurShader.frag"),
fbo1(width, height),
fbo2(width, height),
lines(),
parameters(),
random_parameters() {
mouse_buttons{{false, false, false}},
mouse{{0.0f, 0.0f}}{
set_points();
//glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
std::string filename = filenames[3];
set_parameters(filename);
}
~App() {
@ -83,12 +69,6 @@ public:
void update() {
++counter;
iterate();
if(counter % 5000 == 0) {
set_parameters(filenames[rand() % 4]);
set_points();
}
}
void draw() {
@ -148,39 +128,14 @@ public:
}
void scene() {
mshader.set_uniform("steps", counter/10);
glRotatef(0.09, std::sin(counter/13370.0), 1.0, 0.0);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &lines[0]);
glPointSize(1.0);
glDrawArrays(GL_POINTS, 0, number_of_vertices);
glDisableClientState(GL_VERTEX_ARRAY);
}
void iterate() {
for(size_t i = 0; i < number_of_lines; ++i) {
GLfloat* vectorNew = &lines[3*2*i];
GLfloat* vectorOld = &lines[3*2*i+3];
if(counter % 2) std::swap(vectorNew, vectorOld);
vectorNew[0] = parameters[0]*(vectorOld[2] + parameters[1]);
vectorNew[1] = parameters[2]*(vectorOld[0] + parameters[3]);
vectorNew[2] = parameters[4]*(vectorOld[1] + parameters[5]);
const double dist = vectorNew[0]*vectorNew[0] + vectorNew[1]*vectorNew[1] + vectorNew[2]*vectorNew[2];
if(dist > parameters[6]*parameters[6]) {
const double sqrtDist = std::sqrt(dist);
const double p = 1.0 - parameters[6] * (static_cast<int>(sqrtDist / parameters[6]) + 1.0) / sqrtDist;
vectorNew[0] *= p;
vectorNew[1] *= p;
vectorNew[2] *= p;
}
}
for(size_t i = 0; i < 7; ++i){
parameters[i] += random_parameters[i];
if(mouse_buttons[0]){
mshader.set_uniform("steps", counter/10);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, &mouse[0]);
glPointSize(50.0);
glDrawArrays(GL_POINTS, 0, 1);
glDisableClientState(GL_VERTEX_ARRAY);
}
}
@ -194,20 +149,6 @@ public:
pngwriter png(width, height, &data[0], filename);
png.close();
}
void set_parameters(std::string filename){
stfu::node file;
file.read(filename.c_str());
for(unsigned int i = 0; i < 7; ++i) {
stfu::node const attractorParameters = file.getChild("AttractorKernel").getChild("parameters");
parameters[i] = atof(attractorParameters.getValue(i).c_str());
random_parameters[i] = (rand() / (double)RAND_MAX - 0.5) * counter / 5000000.0;
}
}
void set_points(){
std::generate(lines.begin(), lines.end(), []() { return 2.0 * rand() / (double)RAND_MAX - 1.0; });
}
};
#endif // APP_H

65
GlutTest.cbp

@ -1,65 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="GlutTest" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/GlutTest" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/GlutTest" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
<Add option="-DNDEBUG" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Linker>
<Add library="GLEW" />
<Add library="glut" />
<Add library="png" />
</Linker>
<Unit filename="App.h" />
<Unit filename="fbo.h" />
<Unit filename="globals.h" />
<Unit filename="main.cpp" />
<Unit filename="pngwriter/pngwriter.cc" />
<Unit filename="pngwriter/pngwriter.h" />
<Unit filename="resources/myClearShader.frag" />
<Unit filename="resources/myClearShader.vert" />
<Unit filename="resources/myHBlurShader.frag" />
<Unit filename="resources/myHBlurShader.vert" />
<Unit filename="resources/myTeaShader.frag" />
<Unit filename="resources/myTeaShader.vert" />
<Unit filename="resources/myTextureShader.frag" />
<Unit filename="resources/myTextureShader.vert" />
<Unit filename="resources/myVBlurShader.frag" />
<Unit filename="resources/myVBlurShader.vert" />
<Unit filename="shader.h" />
<Unit filename="stfu/stf.cpp" />
<Unit filename="stfu/stf.hpp" />
<Extensions>
<code_completion />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>

24
main.cpp

@ -33,6 +33,15 @@ void end() {
delete app_ptr;
}
int mouse_button_mapping(int button){
switch(button){
case GLUT_LEFT_BUTTON: return 0;
case GLUT_MIDDLE_BUTTON: return 1;
case GLUT_RIGHT_BUTTON: return 2;
default: return 0;
}
}
int main(int argc, char** argv) {
// let glut create a window
@ -72,6 +81,21 @@ int main(int argc, char** argv) {
glutPostRedisplay();
});
glutReshapeFunc([](int w, int h) {app_ptr->resize(w, h);});
glutMouseFunc([](int button, int state, int x, int y) {
switch(state) {
case GLUT_DOWN: app_ptr->mouse_buttons[mouse_button_mapping(button)] = true; break;
case GLUT_UP: app_ptr->mouse_buttons[mouse_button_mapping(button)] = false; break;
default: break;
}
});
glutPassiveMotionFunc([](int x, int y) {
app_ptr->mouse[0] = x;
app_ptr->mouse[1] = y;
});
glutMotionFunc([](int x, int y) {
app_ptr->mouse[0] = x;
app_ptr->mouse[1] = y;
});
glutCloseFunc(end); // freeglut extension
// my app-object

2
resources/myTeaShader.frag

@ -4,6 +4,6 @@ uniform int steps;
void main( void ) {
gl_FragColor.rgb = sin(3.0*sin(normalize(color.rgb) + vec3(steps)*vec3(0.01, 0.017, 0.0093)))*0.5+0.5;
gl_FragColor.rgb *= 0.05;
gl_FragColor.rgb *= 0.5;
gl_FragColor.a = 1.0;
}

7
resources/myTeaShader.vert

@ -1,8 +1,9 @@
varying vec4 color;
void main( void ) {
gl_Position = ftransform();
gl_Position.xyz = gl_Position.xyz*0.6;
gl_Position.x *= 600.0/800.0;
gl_Position = gl_Vertex;
gl_Position.xy /= vec2(800, 600)*0.5;
gl_Position.xy -= 1.0;
gl_Position.y *= -1.0;
color = gl_Vertex;
}