diff --git a/CMakeLists.txt b/CMakeLists.txt index 46a8b72..b8aadc5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,4 @@ TARGET_LINK_LIBRARIES(LorenzAttractor ${CONAN_LIBS}) set (CMAKE_CXX_STANDARD 11) #Set the project for VS to use. -set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT "LorenzAttractor") - -#Perform prebuild -add_custom_command(TARGET LorenzAttractor PRE_BUILD COMMAND "pre_build_Script.bat") \ No newline at end of file +set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT "LorenzAttractor") \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5cea159..35f465e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,4 @@ #pragma once - #include #include #include @@ -12,48 +11,50 @@ #include #include #include - -//GLEW #include #include "windows.h" - -//OGL Maths #define GLM_FORCE_RADIANS // use radians #include #include #include - #include "SDL.h" - #undef main +//variables used to store window/gl states SDL_Window* win; SDL_GLContext context; 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 points; std::vector colors; -//constants +//constants used in algorithm later const float sigma = 10; const float beta = 8.0 / 3.0; const float rho = 28; 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() { - 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()); system("pause"); return 1; } + //get display information 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); if (win == nullptr) { @@ -62,27 +63,32 @@ int init() return 1; } + //initialize glew glew = glewInit(); + //create OpenGL context context = SDL_GL_CreateContext(win); + //add starter point points.push_back({ 1.0, 1.0, 1.0 }); + //scale size for viewport float scalesize = 0.015; + //scale by scalesize glScalef(scalesize,scalesize,scalesize); - - //glPointSize(2.5); return 0; } + +//amount to increase color value by float amount = 0.01; glm::vec3 getCol() { - r += amount; - if (r > 1) + r += amount; //increment by amount + if (r > 1) //if r is more than 1 { - r = 1; + r = 1; //hold r at 1 g += amount; if (g > 1) { @@ -90,29 +96,30 @@ glm::vec3 getCol() b += amount; if (b > 1) { + //if all are 1, reset r = 0; g = 0; b = 0; } } } - return { r, g, b }; + return { r, g, b }; //return data } void process() { - SDL_Event evnt; - if (SDL_PollEvent(&evnt)) + SDL_Event evnt; //gets event + 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; else rotate = true; @@ -123,57 +130,57 @@ void process() 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 dy = (point.x * (rho - point.z) - point.y) * dt; - float dz = (point.x * point.y - beta * point.z) * dt; + float dx = (sigma * (point.y - point.x)) * dt; //calculate delta x + float dy = (point.x * (rho - point.z) - point.y) * dt; //calculates delta y + float dz = (point.x * point.y - beta * point.z) * dt; //calculates delta z - x = x + dx; - y = y + dy; - z = z + dz; + x = x + dx; //increment global x by x + delta x + y = y + dy; //increment global y by y + delta y + z = z + dz; //increment global z by z + delta z - glm::vec3 col = getCol(); - points.push_back(glm::vec3(x,y,z)); - colors.push_back(glm::vec4(col, 1)); + glm::vec3 col = getCol(); //get the next color value + points.push_back(glm::vec3(x,y,z)); //push new point to dynamic array + 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("\n"); + 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"); //new line } 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_COLOR_ARRAY); - glVertexPointer(3, GL_FLOAT, 0, &points[0]); - glColorPointer(4, GL_FLOAT, 0, &colors[0]); - glDrawArrays(GL_LINE_LOOP, 0, points.capacity()); - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_COLOR_ARRAY); - - if(rotate) - glRotatef(1, 0, 1, 0); - - SDL_GL_SwapWindow(win); + glEnableClientState(GL_VERTEX_ARRAY); //enable vertex array + glEnableClientState(GL_COLOR_ARRAY); //enable color array + glVertexPointer(3, GL_FLOAT, 0, &points[0]); //feed vertex array data + glColorPointer(4, GL_FLOAT, 0, &colors[0]); //feed color array data + glDrawArrays(GL_LINE_LOOP, 0, points.capacity()); //draw everything in the vertex array + glDisableClientState(GL_VERTEX_ARRAY); //disable vertex array + glDisableClientState(GL_COLOR_ARRAY); //disable color array + + if(rotate) //if rotateable + glRotatef(1, 0, 1, 0); //rotate + + SDL_GL_SwapWindow(win); //swap window with new data } int main() { - if (init() != 0) - return 1; + if (init() != 0) //initialize + return 1; //if error, close - while (run) + while (run) //while able to run { - process(); - update(); - draw(); + process(); //process input + update(); //update simulation + 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 } \ No newline at end of file