Compare commits

...

6 Commits

Author SHA1 Message Date
e4adf675e3 refactoring how Input works 2022-07-10 00:13:54 +02:00
5c5e1a83a6 FUCKING FINALLY 2022-06-05 17:59:20 +02:00
9cf523fb6c Hehehehe it still doesn't work 2022-05-27 00:05:18 +02:00
0e19ef3610 having fun messin round 2022-04-18 12:25:26 +02:00
570ad74885 Hehe i can feel my sanity diappearing 2022-04-15 23:51:53 +02:00
328945e6fc *Leaks Memory* 2022-04-15 23:51:11 +02:00
6 changed files with 104 additions and 60 deletions

View File

@@ -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;
} }

View File

@@ -109,8 +109,8 @@ unsigned int createShader(char* vertexPath, char* fragmentPath) {
fread(vertex_src, sizeof(char), vertex_length, vertex_file ); fread(vertex_src, sizeof(char), vertex_length, vertex_file );
fread(fragment_src, sizeof(char), fragment_length, fragment_file ); fread(fragment_src, sizeof(char), fragment_length, fragment_file );
vertex_src[vertex_length+1] = '\0'; vertex_src[vertex_length] = '\0';
fragment_src[fragment_length+1] = '\0'; fragment_src[fragment_length] = '\0';
// printf("%s", vertex_src); // printf("%s", vertex_src);
// printf("%s", fragment_src); // printf("%s", fragment_src);
@@ -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);
}

View File

@@ -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

View File

@@ -7,36 +7,45 @@
#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;
void lorenzStep(float next[3], vec3 prev, float dt) {
float x, y, z, dx, dy, dz;
x = prev[0];
y = prev[1];
z = prev[2];
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 = initial[0];
y = initial[1];
z = initial[2];
for (int i=0; i < number_points; ++i) {
dx = (sigma * (y-x))*dt; dx = (sigma * (y-x))*dt;
dy = (x * (rho - z) -y)*dt; dy = (x * (rho - z) -y)*dt;
dz = (x*y - beta*z)*dt; dz = (x*y - beta*z)*dt;
next[0] = prev[0] + dx; x += dx;
next[1] = prev[1] + dy; y += dy;
next[2] = prev[2] + dz; 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; GLFWwindow* window = initOpenGL();
window = initOpenGL();
glfwSetKeyCallback(window, &key_pressed); glfwSetKeyCallback(window, &key_pressed);
// CreateShader // CreateShader
unsigned int shader1; unsigned int shader1 = createShader("assets/shaders/vertex.glsl\0", "assets/shaders/fragment.glsl\0");
shader1 = createShader("assets/shaders/vertex.glsl\0", "assets/shaders/fragment.glsl\0");
// Initialise VBO and VAO // Initialise VBO and VAO
unsigned int VAO, VBO; unsigned int VAO, VBO;
@@ -45,42 +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 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); glUseProgram(shader1);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
float increment = 0.1* (float)i; glDrawArrays(GL_LINE_STRIP, 0, number_points);
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_LINE_STRIP, 0, i);
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
processInput(); processInput();
@@ -88,6 +102,7 @@ int main(int argc, char *argv[]) {
glDeleteVertexArrays(1, &VAO); glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO); glDeleteBuffers(1, &VBO);
free(lorenzPoints);
glfwTerminate(); glfwTerminate();
return 0; return 0;
} }

14
src/utils.c Normal file
View 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
View 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