GDB Adventures
This commit is contained in:
72
src/lorenz.c
72
src/lorenz.c
@@ -13,7 +13,20 @@ 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];
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
@@ -21,14 +34,9 @@ int main(int argc, char *argv[]) {
|
||||
window = initOpenGL();
|
||||
glfwSetKeyCallback(window, &key_pressed);
|
||||
|
||||
|
||||
// CreateShader
|
||||
char* vertex = "assets/shaders/vertex.glsl\0";
|
||||
char* fragment = "assets/shaders/fragment.glsl\0";
|
||||
unsigned int shader1;
|
||||
shader1 = createShader(vertex, fragment);
|
||||
|
||||
int projectionLoc = glGetUniformLocation(shader1, "proj");
|
||||
shader1 = createShader("assets/shaders/vertex.glsl\0", "assets/shaders/fragment.glsl\0");
|
||||
|
||||
// Initialise VBO and VAO
|
||||
unsigned int VAO, VBO;
|
||||
@@ -37,50 +45,42 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vec3), NULL, GL_DYNAMIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, 3000* sizeof(vec3), NULL, GL_STREAM_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0 );
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
vec3 vertices[3000];
|
||||
int i=0;
|
||||
float dx, dy, dz, dt=0.001;
|
||||
vertices[0][0]=0.01;
|
||||
vertices[0][1]=0.01;
|
||||
vertices[0][2]=0.01;
|
||||
|
||||
|
||||
// Projection Matrix
|
||||
mat4x4 proj;
|
||||
mat4x4_identity(proj);
|
||||
mat4x4_ortho(proj, 0.0, 400.0, 0.0, 400.0, 0.1, 100.0);
|
||||
|
||||
// Uniform Projection Matrix
|
||||
glUseProgram(shader1);
|
||||
int projectionLoc = glGetUniformLocation(shader1, "proj");
|
||||
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, *proj);
|
||||
|
||||
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));
|
||||
|
||||
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);
|
||||
|
||||
//glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, *proj);
|
||||
float increment = 0.1* (float)i;
|
||||
float next[3] = {increment, increment, increment};
|
||||
//lorenzStep(next, lorenzPoints[i], 0.01f);
|
||||
memcpy(lorenzPoints[i++], next, sizeof(vec3));
|
||||
|
||||
if(i<3000) {
|
||||
float x, y, z;
|
||||
x = vertices[i][0];
|
||||
y = vertices[i][1];
|
||||
z = vertices[i][2];
|
||||
|
||||
printf("%i %f %f %f\n", i, x, y, z);
|
||||
|
||||
dx = (sigma * (y-x))*dt;
|
||||
dy = (x * (rho - z) -y)*dt;
|
||||
dz = (x*y - beta*z)*dt;
|
||||
|
||||
i++;
|
||||
vertices[i][0] = x+dx;
|
||||
vertices[i][1] = y+dy;
|
||||
vertices[i][2] = z+dz;
|
||||
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vec3), vertices[i]);
|
||||
}
|
||||
glDrawArrays(GL_POINTS, 0, 1);
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user