diff --git a/src/lorenz.c b/src/lorenz.c index 441903f..f9a00cd 100644 --- a/src/lorenz.c +++ b/src/lorenz.c @@ -11,24 +11,34 @@ 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 = initOpenGL(); @@ -44,54 +54,47 @@ int main(int argc, char *argv[]) { glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, 100* 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); glUseProgram(shader1); - //int projLoc = glGetUniformLocation(shader1, "proj"); - //glUniformMatrix4fv(projLoc, 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); // PointsArray - vec3* lorenzPoints = malloc(3000 * sizeof(vec3)); - memset(lorenzPoints, 0, 3000 * sizeof(vec3)); - vec3 initial[] = {{0.1f, 0.1f, 0.1f},{ - 0.2f, 0.7f, 0.7f }}; - memcpy(lorenzPoints, initial, 2 * sizeof(vec3)); - int i = 2; - for (i=2; i<10; i++){ - vec3 next; - lorenzStep(next, lorenzPoints[i-1], 0.01); + vec3* lorenzPoints = malloc(number_points * sizeof(vec3)); + memset(lorenzPoints, 0, number_points * sizeof(vec3)); - memcpy(lorenzPoints[i], next, sizeof(vec3)); - //printf("%f, %f\n", lorenzPoints[i][0], lorenzPoints[i][1]); - } - - vec3* mapped = malloc(3000 * sizeof(vec3)); - memset(mapped, 0, 3000 * sizeof(vec3)); - for(int j=0; j>=i; j++){ - mapped[j][0] = remap( -20.0f, 20.0f, -1.0f, 1.0f, lorenzPoints[j][0]); - mapped[j][1] = remap( -20.0f, 20.0f, -1.0f, 1.0f, lorenzPoints[j][1]); - mapped[j][2] = remap( -20.0f, 20.0f, -1.0f, 1.0f, lorenzPoints[j][2]); + vec3 initial = {1.0f,1.0f,0.0f}; + float dt = 0.01f; - } - printf("%f \n", mapped[0][0]); + lorenzSolution(lorenzPoints, initial, dt); - buffercpy(VBO, lorenzPoints, sizeof(vec3) * 100); + buffercpy(VBO, lorenzPoints, sizeof(vec3) * number_points); 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); + glDrawArrays(GL_LINE_STRIP, 0, number_points); - glDrawArrays(GL_LINE_STRIP, 0, 10); glfwSwapBuffers(window); glfwPollEvents(); processInput(); @@ -100,7 +103,6 @@ int main(int argc, char *argv[]) { glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); free(lorenzPoints); - free(mapped); glfwTerminate(); return 0; }