Compare commits

..

4 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
6 changed files with 100 additions and 60 deletions

View File

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

View File

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

View File

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

View File

@@ -7,26 +7,37 @@
#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) {
float x, y, z, dx, dy, dz;
x = prev[0];
y = prev[1];
z = prev[2];
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 = initial[0];
y = initial[1];
z = initial[2];
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;
next[0] = prev[0] + dx;
next[1] = prev[1] + dy;
next[2] = prev[2] + dz;
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[]) {
@@ -43,47 +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));
vec3 sec = {1.0f, 1.0f, 1.0f};
memcpy(lorenzPoints, sec, 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);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
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));
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_POINTS, 10, 2);
glfwSwapBuffers(window);
glfwPollEvents();
processInput();

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