From 233dceb002d774aa8034cabcead9210a04e4a60d Mon Sep 17 00:00:00 2001 From: exsersewo Date: Mon, 9 Apr 2018 23:58:54 +0100 Subject: [PATCH] Hello World --- .gitignore | 1 + CMakeLists.txt | 25 +++++++ README.md | 1 + build-vs2015.bat | 30 ++++++++ build-vs2017.bat | 30 ++++++++ conanfile.txt | 15 ++++ src/main.cpp | 179 +++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 281 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 build-vs2015.bat create mode 100644 build-vs2017.bat create mode 100644 conanfile.txt create mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ada0c28 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +[Bb]in/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..46a8b72 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,25 @@ +#Set the project name +project(LorenzAttractor) + +#Set the CMake version +cmake_minimum_required(VERSION 3.0) + +#Use Conan +include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) +conan_basic_setup() + +#Import all source files +file(GLOB src_files "src/*.c" "src/*.cpp" ) + +#Make a project, link in libraries from conan +ADD_EXECUTABLE(LorenzAttractor ${src_files}) +TARGET_LINK_LIBRARIES(LorenzAttractor ${CONAN_LIBS}) + +#Set the C++ version +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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..58e90c5 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Lorenz Attractor diff --git a/build-vs2015.bat b/build-vs2015.bat new file mode 100644 index 0000000..ab825c4 --- /dev/null +++ b/build-vs2015.bat @@ -0,0 +1,30 @@ +@echo off + +:start +if exist src goto changetobin +if exist .\CMakeLists.txt xcopy /q /i /e /y .\CMakeLists.txt .\bin\CMakeLists.txt +if exist .\conanfile.txt xcopy /q /i /e /y .\conanfile.txt .\bin\conanfile.txt + +:conan +conan install --build missing +goto cmake + +:cmake +cmake .. -G "Visual Studio 14 2015 Win64" +goto end + +:changetobin +cd bin +goto start + +:end +if exist bin goto quit +cd .. +goto quit + +:quit +echo. +echo. +echo INFO: Conan and CMake ran successfully. Open up your solution file (listed below) to open your project. +echo. +dir /b *.sln \ No newline at end of file diff --git a/build-vs2017.bat b/build-vs2017.bat new file mode 100644 index 0000000..a6035ed --- /dev/null +++ b/build-vs2017.bat @@ -0,0 +1,30 @@ +@echo off + +:start +if exist src goto changetobin +if exist .\CMakeLists.txt xcopy /q /i /e /y .\CMakeLists.txt .\bin\CMakeLists.txt +if exist .\conanfile.txt xcopy /q /i /e /y .\conanfile.txt .\bin\conanfile.txt + +:conan +conan install --build missing +goto cmake + +:cmake +cmake .. -G "Visual Studio 15 2017 Win64" +goto end + +:changetobin +cd bin +goto start + +:end +if exist bin goto quit +cd .. +goto quit + +:quit +echo. +echo. +echo INFO: Conan and CMake ran successfully. Open up your solution file (listed below) to open your project. +echo. +dir /b *.sln \ No newline at end of file diff --git a/conanfile.txt b/conanfile.txt new file mode 100644 index 0000000..a96d497 --- /dev/null +++ b/conanfile.txt @@ -0,0 +1,15 @@ +[requires] +SDL2/2.0.5@dotfloat/stable +glew/2.0.0@coding3d/stable +glm/0.9.7.6@dlarudgus20/stable + +[options] +SDL2:shared=True +glew:shared=False + +[generators] +cmake + +[imports] +bin, *.dll -> ./bin # Copies all dll files from packages bin folder to my "bin" folder +lib, *.dylib* -> ./bin # Copies all dylib files from packages lib folder to my "bin" folder \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..5cea159 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,179 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +//GLEW +#include +#include "windows.h" + +//OGL Maths +#define GLM_FORCE_RADIANS // use radians +#include +#include +#include + +#include "SDL.h" + +#undef main + +SDL_Window* win; +SDL_GLContext context; +GLenum glew; +std::vector points; +std::vector colors; +//constants +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) + { + printf("%s\n", SDL_GetError()); + system("pause"); + return 1; + } + + SDL_GetDesktopDisplayMode(0, &DM); + + win = SDL_CreateWindow("Lorenz Attractor", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, DM.w/2, DM.h/2, SDL_WINDOW_OPENGL); + if (win == nullptr) + { + printf("%s\n", SDL_GetError()); + system("pause"); + return 1; + } + + glew = glewInit(); + + context = SDL_GL_CreateContext(win); + + points.push_back({ 1.0, 1.0, 1.0 }); + + float scalesize = 0.015; + + glScalef(scalesize,scalesize,scalesize); + + //glPointSize(2.5); + + return 0; +} +float amount = 0.01; +glm::vec3 getCol() +{ + r += amount; + if (r > 1) + { + r = 1; + g += amount; + if (g > 1) + { + g = 0; + b += amount; + if (b > 1) + { + r = 0; + g = 0; + b = 0; + } + } + } + return { r, g, b }; +} + +void process() +{ + SDL_Event evnt; + if (SDL_PollEvent(&evnt)) + { + if (evnt.type == SDL_QUIT) + { + run = false; + } + if (evnt.type == SDL_KEYDOWN) + { + if (evnt.key.keysym.sym == SDLK_SPACE) + { + if (rotate) + rotate = false; + else + rotate = true; + } + } + } +} + +void update() +{ + glm::vec3 point = points.back(); + + 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; + + x = x + dx; + y = y + dy; + z = z + dz; + + glm::vec3 col = getCol(); + points.push_back(glm::vec3(x,y,z)); + colors.push_back(glm::vec4(col, 1)); + + printf("x: %f , y: %f, z: %f , r: %f , g: %f , b: %f", x, y, z, col.x, col.y, col.z); + printf("\n"); +} + +void draw() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + 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); +} + +int main() +{ + if (init() != 0) + return 1; + + while (run) + { + process(); + update(); + draw(); + } + + SDL_GL_DeleteContext(context); + + SDL_Quit(); + + return 0; +} \ No newline at end of file