Compare commits
6 Commits
31820dd7cd
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e4adf675e3 | |||
| 5c5e1a83a6 | |||
| 9cf523fb6c | |||
| 0e19ef3610 | |||
| 570ad74885 | |||
| 328945e6fc |
33
src/Input.c
33
src/Input.c
@@ -2,7 +2,7 @@
|
||||
|
||||
extern float pos[3];
|
||||
|
||||
/* 0: escape
|
||||
/* 0: nothing
|
||||
* 1: W
|
||||
* 2: S
|
||||
* 3: A
|
||||
@@ -10,27 +10,26 @@ extern float pos[3];
|
||||
*/
|
||||
|
||||
static bool pressed[5];
|
||||
|
||||
static int index;
|
||||
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;
|
||||
if ( key == GLFW_KEY_W && action == GLFW_RELEASE ) pressed[1] = false;
|
||||
index = 0;
|
||||
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 ( key == GLFW_KEY_S && action == GLFW_RELEASE ) pressed[2] = false;
|
||||
if ( action == GLFW_PRESS ) pressed[index] = true;
|
||||
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(){
|
||||
if (pressed[1]) pos[1] += 0.01f;
|
||||
if (pressed[2]) pos[1] -= 0.01f;
|
||||
if (pressed[3]) pos[0] -= 0.01f;
|
||||
if (pressed[4]) pos[0] += 0.01f;
|
||||
|
||||
if (pressed[1] == true) pos[1] += 0.01f;
|
||||
if (pressed[2] == true) pos[1] -= 0.01f;
|
||||
if (pressed[3] == true) pos[0] -= 0.01f;
|
||||
if (pressed[4] == true) pos[0] += 0.01f;
|
||||
}
|
||||
|
||||
@@ -109,8 +109,8 @@ unsigned int createShader(char* vertexPath, char* fragmentPath) {
|
||||
fread(vertex_src, sizeof(char), vertex_length, vertex_file );
|
||||
fread(fragment_src, sizeof(char), fragment_length, fragment_file );
|
||||
|
||||
vertex_src[vertex_length+1] = '\0';
|
||||
fragment_src[fragment_length+1] = '\0';
|
||||
vertex_src[vertex_length] = '\0';
|
||||
fragment_src[fragment_length] = '\0';
|
||||
// printf("%s", vertex_src);
|
||||
// printf("%s", fragment_src);
|
||||
|
||||
@@ -152,4 +152,10 @@ unsigned int createTexture(char* path){
|
||||
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);
|
||||
|
||||
|
||||
void buffercpy(int VBO, void* ptr, int size);
|
||||
#endif
|
||||
|
||||
97
src/lorenz.c
97
src/lorenz.c
@@ -7,36 +7,45 @@
|
||||
#include <linmath.h>
|
||||
#include "Renderer.h"
|
||||
#include "Input.h"
|
||||
#include "utils.h"
|
||||
|
||||
float pos[3] = {0.0f, 0.0f, 0.0f};
|
||||
|
||||
float rho=28.0;
|
||||
float sigma=10.0;
|
||||
float beta=8.0/3.0;
|
||||
void lorenzStep(float next[3], vec3 prev, float dt) {
|
||||
const int number_points = 10000;
|
||||
|
||||
float rho=28.0f;
|
||||
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;
|
||||
x = prev[0];
|
||||
y = prev[1];
|
||||
z = prev[2];
|
||||
x = initial[0];
|
||||
y = initial[1];
|
||||
z = initial[2];
|
||||
|
||||
dx = (sigma * (y-x))*dt;
|
||||
dy = (x * (rho - z) -y)*dt;
|
||||
dz = (x*y - beta*z)*dt;
|
||||
|
||||
next[0] = prev[0] + dx;
|
||||
next[1] = prev[1] + dy;
|
||||
next[2] = prev[2] + dz;
|
||||
for (int i=0; i < number_points; ++i) {
|
||||
dx = (sigma * (y-x))*dt;
|
||||
dy = (x * (rho - z) -y)*dt;
|
||||
dz = (x*y - beta*z)*dt;
|
||||
|
||||
x += dx;
|
||||
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[]) {
|
||||
|
||||
GLFWwindow* window;
|
||||
window = initOpenGL();
|
||||
|
||||
GLFWwindow* window = initOpenGL();
|
||||
glfwSetKeyCallback(window, &key_pressed);
|
||||
|
||||
// CreateShader
|
||||
unsigned int shader1;
|
||||
shader1 = createShader("assets/shaders/vertex.glsl\0", "assets/shaders/fragment.glsl\0");
|
||||
unsigned int shader1 = createShader("assets/shaders/vertex.glsl\0", "assets/shaders/fragment.glsl\0");
|
||||
|
||||
// Initialise VBO and VAO
|
||||
unsigned int VAO, VBO;
|
||||
@@ -45,42 +54,47 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
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 );
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// Projection Matrix
|
||||
// Projection and View Matrix
|
||||
mat4x4 proj;
|
||||
mat4x4_identity(proj);
|
||||
mat4x4_ortho(proj, 0.0, 400.0, 0.0, 400.0, 0.1, 100.0);
|
||||
mat4x4_ortho(proj, -100.0, 100.0, -100.0, 100.0, -100.0, 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);
|
||||
int projectionLoc = glGetUniformLocation(shader1, "proj");
|
||||
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, *proj);
|
||||
int projLoc = glGetUniformLocation(shader1, "proj");
|
||||
glUniformMatrix4fv(projLoc, 1, GL_FALSE, *proj);
|
||||
int viewLoc = glGetUniformLocation(shader1, "view");
|
||||
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, *view);
|
||||
|
||||
vec3* lorenzPoints = malloc(3000 * sizeof(vec3));
|
||||
memset(lorenzPoints, 0, 3000 * sizeof(vec3));
|
||||
vec3 initial = {0.1f, 0.1f, 0.1f};
|
||||
memcpy(lorenzPoints, initial, sizeof(vec3));
|
||||
// PointsArray
|
||||
vec3* lorenzPoints = malloc(number_points * sizeof(vec3));
|
||||
memset(lorenzPoints, 0, number_points * 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)) {
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glUseProgram(shader1);
|
||||
|
||||
float increment = 0.1* (float)i;
|
||||
float next[3] = {increment, increment, increment};
|
||||
//lorenzStep(next, lorenzPoints[i], 0.01f);
|
||||
memcpy(lorenzPoints[i++], next, sizeof(vec3));
|
||||
glBindVertexArray(VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
|
||||
glDrawArrays(GL_LINE_STRIP, 0, number_points);
|
||||
|
||||
float* buff = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
||||
memcpy(buff, lorenzPoints, i * sizeof(vec3));
|
||||
glUnmapBuffer(GL_ARRAY_BUFFER);
|
||||
|
||||
glDrawArrays(GL_LINE_STRIP, 0, i);
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
processInput();
|
||||
@@ -88,6 +102,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
free(lorenzPoints);
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
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