Compare commits
4 Commits
570ad74885
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e4adf675e3 | |||
| 5c5e1a83a6 | |||
| 9cf523fb6c | |||
| 0e19ef3610 |
33
src/Input.c
33
src/Input.c
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
extern float pos[3];
|
extern float pos[3];
|
||||||
|
|
||||||
/* 0: escape
|
/* 0: nothing
|
||||||
* 1: W
|
* 1: W
|
||||||
* 2: S
|
* 2: S
|
||||||
* 3: A
|
* 3: A
|
||||||
@@ -10,27 +10,26 @@ extern float pos[3];
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static bool pressed[5];
|
static bool pressed[5];
|
||||||
|
static int index;
|
||||||
void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods){
|
void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods){
|
||||||
if ( (key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q )&& action == GLFW_PRESS) glfwSetWindowShouldClose(window, true);
|
if ( key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, true);
|
||||||
|
if ( key == GLFW_KEY_Q && action == GLFW_PRESS ) glfwSetWindowShouldClose(window, true);
|
||||||
|
|
||||||
if ( key == GLFW_KEY_W && action == GLFW_PRESS ) pressed[1] = true;
|
index = 0;
|
||||||
if ( key == GLFW_KEY_W && action == GLFW_RELEASE ) pressed[1] = false;
|
if ( key == GLFW_KEY_W ) index = 1;
|
||||||
|
if ( key == GLFW_KEY_S ) index = 2;
|
||||||
|
if ( key == GLFW_KEY_A ) index = 3;
|
||||||
|
if ( key == GLFW_KEY_D ) index = 4;
|
||||||
|
|
||||||
if ( key == GLFW_KEY_S && action == GLFW_PRESS ) pressed[2] = true;
|
if ( action == GLFW_PRESS ) pressed[index] = true;
|
||||||
if ( key == GLFW_KEY_S && action == GLFW_RELEASE ) pressed[2] = false;
|
if ( action == GLFW_RELEASE ) pressed[index] = false;
|
||||||
|
|
||||||
|
|
||||||
if ( key == GLFW_KEY_A && action == GLFW_PRESS ) pressed[3] = true;
|
|
||||||
if ( key == GLFW_KEY_A && action == GLFW_RELEASE ) pressed[3] = false;
|
|
||||||
|
|
||||||
if ( key == GLFW_KEY_D && action == GLFW_PRESS ) pressed[4] = true;
|
|
||||||
if ( key == GLFW_KEY_D && action == GLFW_RELEASE ) pressed[4] = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void processInput(){
|
void processInput(){
|
||||||
if (pressed[1]) pos[1] += 0.01f;
|
if (pressed[1] == true) pos[1] += 0.01f;
|
||||||
if (pressed[2]) pos[1] -= 0.01f;
|
if (pressed[2] == true) pos[1] -= 0.01f;
|
||||||
if (pressed[3]) pos[0] -= 0.01f;
|
if (pressed[3] == true) pos[0] -= 0.01f;
|
||||||
if (pressed[4]) pos[0] += 0.01f;
|
if (pressed[4] == true) pos[0] += 0.01f;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -152,4 +152,10 @@ unsigned int createTexture(char* path){
|
|||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void buffercpy(int VBO, void* ptr, int size){
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
void* buffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
|
||||||
|
memcpy(buffer, ptr, size);
|
||||||
|
glUnmapBuffer(GL_ARRAY_BUFFER);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,4 +20,6 @@ unsigned int createShader(char* vertexPath, char* fragmentPath);
|
|||||||
|
|
||||||
unsigned int createTexture(char* path);
|
unsigned int createTexture(char* path);
|
||||||
|
|
||||||
|
|
||||||
|
void buffercpy(int VBO, void* ptr, int size);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
97
src/lorenz.c
97
src/lorenz.c
@@ -7,29 +7,40 @@
|
|||||||
#include <linmath.h>
|
#include <linmath.h>
|
||||||
#include "Renderer.h"
|
#include "Renderer.h"
|
||||||
#include "Input.h"
|
#include "Input.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
float pos[3] = {0.0f, 0.0f, 0.0f};
|
float pos[3] = {0.0f, 0.0f, 0.0f};
|
||||||
|
|
||||||
float rho=28.0;
|
const int number_points = 10000;
|
||||||
float sigma=10.0;
|
|
||||||
float beta=8.0/3.0;
|
float rho=28.0f;
|
||||||
void lorenzStep(float next[3], vec3 prev, float dt) {
|
float sigma=10.0f;
|
||||||
|
float beta=8.0f/3.0f;
|
||||||
|
|
||||||
|
void lorenzSolution(vec3* solution, vec3 initial, float dt) {
|
||||||
float x, y, z, dx, dy, dz;
|
float x, y, z, dx, dy, dz;
|
||||||
x = prev[0];
|
x = initial[0];
|
||||||
y = prev[1];
|
y = initial[1];
|
||||||
z = prev[2];
|
z = initial[2];
|
||||||
|
|
||||||
dx = (sigma * (y-x))*dt;
|
for (int i=0; i < number_points; ++i) {
|
||||||
dy = (x * (rho - z) -y)*dt;
|
dx = (sigma * (y-x))*dt;
|
||||||
dz = (x*y - beta*z)*dt;
|
dy = (x * (rho - z) -y)*dt;
|
||||||
|
dz = (x*y - beta*z)*dt;
|
||||||
next[0] = prev[0] + dx;
|
|
||||||
next[1] = prev[1] + dy;
|
x += dx;
|
||||||
next[2] = prev[2] + dz;
|
y += dy;
|
||||||
|
z += dz;
|
||||||
|
|
||||||
|
vec3 p = {x, y, z};
|
||||||
|
printf("%f %f %f\n", p[0], p[1], p[2]);
|
||||||
|
memcpy(solution[i], p, sizeof(vec3));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
GLFWwindow* window = initOpenGL();
|
GLFWwindow* window = initOpenGL();
|
||||||
glfwSetKeyCallback(window, &key_pressed);
|
glfwSetKeyCallback(window, &key_pressed);
|
||||||
|
|
||||||
@@ -43,47 +54,47 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
glBufferData(GL_ARRAY_BUFFER, 3000* sizeof(vec3), NULL, GL_STREAM_DRAW);
|
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, number_points * sizeof(vec3), NULL, GL_DYNAMIC_DRAW);
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0 );
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0 );
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
// Projection Matrix
|
// Projection and View Matrix
|
||||||
mat4x4 proj;
|
mat4x4 proj;
|
||||||
mat4x4_identity(proj);
|
mat4x4_ortho(proj, -100.0, 100.0, -100.0, 100.0, -100.0, 100.0);
|
||||||
mat4x4_ortho(proj, 0.0, 400.0, 0.0, 400.0, 0.1, 100.0);
|
mat4x4 view;
|
||||||
|
vec3 camera_position = {0.0f, 0.0f, 0.3f};
|
||||||
|
vec3 look_at = {0.0f,0.0f,0.0f};
|
||||||
|
vec3 up = {0.0f, 1.0f, 0.0f};
|
||||||
|
mat4x4_look_at(view, camera_position, look_at, up);
|
||||||
|
|
||||||
// Uniform Projection Matrix
|
|
||||||
glUseProgram(shader1);
|
glUseProgram(shader1);
|
||||||
int projectionLoc = glGetUniformLocation(shader1, "proj");
|
int projLoc = glGetUniformLocation(shader1, "proj");
|
||||||
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, *proj);
|
glUniformMatrix4fv(projLoc, 1, GL_FALSE, *proj);
|
||||||
|
int viewLoc = glGetUniformLocation(shader1, "view");
|
||||||
|
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, *view);
|
||||||
|
|
||||||
vec3* lorenzPoints = malloc(3000 * sizeof(vec3));
|
// PointsArray
|
||||||
memset(lorenzPoints, 0, 3000 * sizeof(vec3));
|
vec3* lorenzPoints = malloc(number_points * sizeof(vec3));
|
||||||
vec3 initial = {0.1f, 0.1f, 0.1f};
|
memset(lorenzPoints, 0, number_points * sizeof(vec3));
|
||||||
memcpy(lorenzPoints, initial, sizeof(vec3));
|
|
||||||
vec3 sec = {1.0f, 1.0f, 1.0f};
|
|
||||||
memcpy(lorenzPoints, sec, sizeof(vec3));
|
|
||||||
|
|
||||||
|
vec3 initial = {1.0f,1.0f,0.0f};
|
||||||
|
float dt = 0.01f;
|
||||||
|
|
||||||
|
lorenzSolution(lorenzPoints, initial, dt);
|
||||||
|
|
||||||
|
buffercpy(VBO, lorenzPoints, sizeof(vec3) * number_points);
|
||||||
|
|
||||||
int i=1;
|
|
||||||
while(!glfwWindowShouldClose(window)) {
|
while(!glfwWindowShouldClose(window)) {
|
||||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
glUseProgram(shader1);
|
|
||||||
|
|
||||||
if(i<3000){
|
|
||||||
float increment = 0.1* (float)i;
|
|
||||||
float next[3] = {increment, increment, increment};
|
|
||||||
//lorenzStep(next, lorenzPoints[i], 0.01f);
|
|
||||||
memcpy(lorenzPoints[i++], next, sizeof(vec3));
|
|
||||||
|
|
||||||
|
|
||||||
float* buff = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
|
||||||
memcpy(buff, lorenzPoints, i * sizeof(vec3));
|
|
||||||
glUnmapBuffer(GL_ARRAY_BUFFER);
|
|
||||||
}
|
|
||||||
|
|
||||||
glDrawArrays(GL_POINTS, 10, 2);
|
glUseProgram(shader1);
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
|
||||||
|
glDrawArrays(GL_LINE_STRIP, 0, number_points);
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
processInput();
|
processInput();
|
||||||
|
|||||||
14
src/utils.c
Normal file
14
src/utils.c
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
float lerp(float a, float b, float t) {
|
||||||
|
return (1.0f - t) * a + b * t;
|
||||||
|
}
|
||||||
|
|
||||||
|
float invLerp(float a, float b, float v){
|
||||||
|
return (v - a) / (b - a);
|
||||||
|
}
|
||||||
|
|
||||||
|
float remap(float a, float b, float A, float B, float value) {
|
||||||
|
float t = invLerp(a,b,value);
|
||||||
|
return lerp(A,B,t);
|
||||||
|
}
|
||||||
8
src/utils.h
Normal file
8
src/utils.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef UTILS
|
||||||
|
#define UTILS
|
||||||
|
|
||||||
|
float lerp(float a, float b, float t);
|
||||||
|
float invLerp(float a, float b, float value);
|
||||||
|
float remap(float a,float b, float A, float B, float value);
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user