comments lol

master
exsersewo 7 years ago
parent 233dceb002
commit 76b9c81152
  1. 5
      CMakeLists.txt
  2. 129
      src/main.cpp

@ -19,7 +19,4 @@ TARGET_LINK_LIBRARIES(LorenzAttractor ${CONAN_LIBS})
set (CMAKE_CXX_STANDARD 11) set (CMAKE_CXX_STANDARD 11)
#Set the project for VS to use. #Set the project for VS to use.
set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT "LorenzAttractor") set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT "LorenzAttractor")
#Perform prebuild
add_custom_command(TARGET LorenzAttractor PRE_BUILD COMMAND "pre_build_Script.bat")

@ -1,5 +1,4 @@
#pragma once #pragma once
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
@ -12,48 +11,50 @@
#include <iomanip> #include <iomanip>
#include <stdexcept> #include <stdexcept>
#include <map> #include <map>
//GLEW
#include <GL/glew.h> #include <GL/glew.h>
#include "windows.h" #include "windows.h"
//OGL Maths
#define GLM_FORCE_RADIANS // use radians #define GLM_FORCE_RADIANS // use radians
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include "SDL.h" #include "SDL.h"
#undef main #undef main
//variables used to store window/gl states
SDL_Window* win; SDL_Window* win;
SDL_GLContext context; SDL_GLContext context;
GLenum glew; GLenum glew;
SDL_DisplayMode DM;
//simulation states
bool run = true;
bool rotate = false;
//variables used to handle color and location positions
float x, y, z;
float r = 0, g = 0, b = 0;
//dynamic sized arrays used to store point location and color data
std::vector<glm::vec3> points; std::vector<glm::vec3> points;
std::vector<glm::vec4> colors; std::vector<glm::vec4> colors;
//constants //constants used in algorithm later
const float sigma = 10; const float sigma = 10;
const float beta = 8.0 / 3.0; const float beta = 8.0 / 3.0;
const float rho = 28; const float rho = 28;
const float dt = 0.02; const float dt = 0.02;
bool run = true;
SDL_DisplayMode DM;
float x, y, z;
float r = 0, g = 0, b = 0;
bool rotate = false;
int init() int init()
{ {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) //load video and events
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_EVENTS) != 0)
{ {
//if errored print error and wait for user input
printf("%s\n", SDL_GetError()); printf("%s\n", SDL_GetError());
system("pause"); system("pause");
return 1; return 1;
} }
//get display information
SDL_GetDesktopDisplayMode(0, &DM); SDL_GetDesktopDisplayMode(0, &DM);
//create window, if didn't load successfully, print error and wait for user input
win = SDL_CreateWindow("Lorenz Attractor", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, DM.w/2, DM.h/2, SDL_WINDOW_OPENGL); win = SDL_CreateWindow("Lorenz Attractor", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, DM.w/2, DM.h/2, SDL_WINDOW_OPENGL);
if (win == nullptr) if (win == nullptr)
{ {
@ -62,27 +63,32 @@ int init()
return 1; return 1;
} }
//initialize glew
glew = glewInit(); glew = glewInit();
//create OpenGL context
context = SDL_GL_CreateContext(win); context = SDL_GL_CreateContext(win);
//add starter point
points.push_back({ 1.0, 1.0, 1.0 }); points.push_back({ 1.0, 1.0, 1.0 });
//scale size for viewport
float scalesize = 0.015; float scalesize = 0.015;
//scale by scalesize
glScalef(scalesize,scalesize,scalesize); glScalef(scalesize,scalesize,scalesize);
//glPointSize(2.5);
return 0; return 0;
} }
//amount to increase color value by
float amount = 0.01; float amount = 0.01;
glm::vec3 getCol() glm::vec3 getCol()
{ {
r += amount; r += amount; //increment by amount
if (r > 1) if (r > 1) //if r is more than 1
{ {
r = 1; r = 1; //hold r at 1
g += amount; g += amount;
if (g > 1) if (g > 1)
{ {
@ -90,29 +96,30 @@ glm::vec3 getCol()
b += amount; b += amount;
if (b > 1) if (b > 1)
{ {
//if all are 1, reset
r = 0; r = 0;
g = 0; g = 0;
b = 0; b = 0;
} }
} }
} }
return { r, g, b }; return { r, g, b }; //return data
} }
void process() void process()
{ {
SDL_Event evnt; SDL_Event evnt; //gets event
if (SDL_PollEvent(&evnt)) if (SDL_PollEvent(&evnt)) //gets event information
{ {
if (evnt.type == SDL_QUIT) if (evnt.type == SDL_QUIT) //if quit event
{ {
run = false; run = false; //close app
} }
if (evnt.type == SDL_KEYDOWN) if (evnt.type == SDL_KEYDOWN) //if key pressed
{ {
if (evnt.key.keysym.sym == SDLK_SPACE) if (evnt.key.keysym.sym == SDLK_SPACE) //if space key pressed
{ {
if (rotate) if (rotate) //toggle rotate boolean
rotate = false; rotate = false;
else else
rotate = true; rotate = true;
@ -123,57 +130,57 @@ void process()
void update() void update()
{ {
glm::vec3 point = points.back(); glm::vec3 point = points.back(); //gets last item in point array
float dx = (sigma * (point.y - point.x)) * dt; float dx = (sigma * (point.y - point.x)) * dt; //calculate delta x
float dy = (point.x * (rho - point.z) - point.y) * dt; float dy = (point.x * (rho - point.z) - point.y) * dt; //calculates delta y
float dz = (point.x * point.y - beta * point.z) * dt; float dz = (point.x * point.y - beta * point.z) * dt; //calculates delta z
x = x + dx; x = x + dx; //increment global x by x + delta x
y = y + dy; y = y + dy; //increment global y by y + delta y
z = z + dz; z = z + dz; //increment global z by z + delta z
glm::vec3 col = getCol(); glm::vec3 col = getCol(); //get the next color value
points.push_back(glm::vec3(x,y,z)); points.push_back(glm::vec3(x,y,z)); //push new point to dynamic array
colors.push_back(glm::vec4(col, 1)); colors.push_back(glm::vec4(col, 1)); //push new color value to dynamic array
printf("x: %f , y: %f, z: %f , r: %f , g: %f , b: %f", x, y, z, col.x, col.y, col.z); printf("x: %f , y: %f, z: %f , r: %f , g: %f , b: %f", x, y, z, col.x, col.y, col.z); //print data ~~debug information~~
printf("\n"); printf("\n"); //new line
} }
void draw() void draw()
{ {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear background
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); //enable vertex array
glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_COLOR_ARRAY); //enable color array
glVertexPointer(3, GL_FLOAT, 0, &points[0]); glVertexPointer(3, GL_FLOAT, 0, &points[0]); //feed vertex array data
glColorPointer(4, GL_FLOAT, 0, &colors[0]); glColorPointer(4, GL_FLOAT, 0, &colors[0]); //feed color array data
glDrawArrays(GL_LINE_LOOP, 0, points.capacity()); glDrawArrays(GL_LINE_LOOP, 0, points.capacity()); //draw everything in the vertex array
glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); //disable vertex array
glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_COLOR_ARRAY); //disable color array
if(rotate) if(rotate) //if rotateable
glRotatef(1, 0, 1, 0); glRotatef(1, 0, 1, 0); //rotate
SDL_GL_SwapWindow(win); SDL_GL_SwapWindow(win); //swap window with new data
} }
int main() int main()
{ {
if (init() != 0) if (init() != 0) //initialize
return 1; return 1; //if error, close
while (run) while (run) //while able to run
{ {
process(); process(); //process input
update(); update(); //update simulation
draw(); draw(); //draw simulation
} }
SDL_GL_DeleteContext(context); SDL_GL_DeleteContext(context); //upon close, delete OpenGL context
SDL_Quit(); SDL_Quit(); //quit SDL
return 0; return 0; //clean close
} }
Loading…
Cancel
Save