egon-r пре 3 година
комит
f59316592a
5 измењених фајлова са 162 додато и 0 уклоњено
  1. 1 0
      .gitignore
  2. 50 0
      CMakeLists.txt
  3. 14 0
      README.md
  4. BIN
      attractor.png
  5. 97 0
      main.cpp

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+build/**

+ 50 - 0
CMakeLists.txt

@@ -0,0 +1,50 @@
+# Copyright (C) 2018 Tomasz Gałaj
+
+cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
+project(opengltest)
+
+# Add .lib files
+link_directories(${CMAKE_SOURCE_DIR}/lib)
+
+# Add source files
+file(GLOB_RECURSE SOURCE_FILES
+	${CMAKE_SOURCE_DIR}/src/*.c
+	${CMAKE_SOURCE_DIR}/src/*.cpp
+    main.cpp)
+	
+# Add header files
+file(GLOB_RECURSE HEADER_FILES 
+	${CMAKE_SOURCE_DIR}/src/*.h
+	${CMAKE_SOURCE_DIR}/src/*.hpp)
+
+# Configure assets header file
+include_directories(${CMAKE_BINARY_DIR}/src)
+	
+# Define the executable
+add_executable(opengltest ${HEADER_FILES} ${SOURCE_FILES})
+
+# We need a CMAKE_DIR with some code to find external dependencies
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
+
+#######################################
+# LOOK for the packages that we need! #
+#######################################
+
+# OpenGL
+find_package(OpenGL REQUIRED)
+
+# GLUT
+find_package(GLUT REQUIRED)
+message(STATUS "GLUT included at ${GLUT_INCLUDE_DIRS}")
+
+# Put all libraries into a variable
+set(LIBS ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})
+
+# Define the include DIRs
+include_directories(
+	"${CMAKE_SOURCE_DIR}/src"
+	"${CMAKE_SOURCE_DIR}/include"
+)
+
+# Define the link libraries
+target_link_libraries(${PROJECT_NAME} ${LIBS})

+ 14 - 0
README.md

@@ -0,0 +1,14 @@
+# Strange Attractor (OpenGL, GLUT)
+![attractor](attractor.png)
+
+# Requirements (Ubuntu)
+> apt install freeglut3-dev cmake build-essential
+
+# Ideas
+- change the colors
+- change the attractor’s coefficients (you may want to choose them randomly to start, although there are
+- some additional calculations you can do to make an educated guess at parameters that create interesting images)
+- make some changes to the strange attractor equations (substitute some known equations or make your own)
+- modify the code to generate 3d strange attractors
+- modify the code to make it interactive or animated
+- who says the coefficients have to stay the same for a million iterations? try randomizing or interpolating them.


+ 97 - 0
main.cpp

@@ -0,0 +1,97 @@
+#include <iostream>
+#include <math.h>
+#include <GL/freeglut.h>
+
+using std::cout;
+using std::endl;
+
+void initFunc();
+void displayFunc();
+void keyboardFunc(unsigned char key, int x, int y);
+
+// strange attractor
+float x = 0.1;
+float y = 0.1;
+float a = -0.966918; // ?
+float b = 2.879879; // ?!
+float c = 0.765145; // ??
+float d = 0.744728; // ??!
+int initialIterations = 100; // ???
+int iterations = 1000000;
+
+void attractor_iteration() {
+    float xnew = sin(y*b) + c*sin(x*b);
+    float ynew = sin(x*a) + d*sin(y*a);
+    x = xnew;
+    y = ynew;
+}
+
+
+int main(int argc, char const *argv[])
+{
+    cout << "main" << endl;
+
+    glutInit(&argc, (char**)argv);
+    // set up our display mode for color with alpha and double buffering
+    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
+    // create a 400px x 400px window
+    glutInitWindowSize(400, 400);
+    glutCreateWindow("Strange Attractors in C++ and OpenGL");
+
+    glutDisplayFunc(displayFunc);
+    glutKeyboardFunc(keyboardFunc);
+
+    initFunc();
+
+    glutMainLoop();
+    return 0;
+}
+
+void initFunc() {
+    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // background
+    glColor4f(1.0f, 1.0f, 1.0f, 0.02f); // foreground (pen) color
+
+    glViewport(0, 0, 400, 400);
+
+    // blending (transparency)
+    glEnable(GL_BLEND);
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+
+    // point smoothing
+    glEnable(GL_POINT_SMOOTH);
+    glPointSize(1.0f);
+
+    // setup projection matrix (camera)
+    glMatrixMode(GL_PROJECTION);
+    glLoadIdentity();
+    gluOrtho2D(-2.0f, 2.0f, -2.0f, 2.0f);
+
+    // setup model matrix (objects)
+    glMatrixMode(GL_MODELVIEW);
+    glLoadIdentity();
+
+    // strange attractor initial iterations to settle into an orbit (?)
+    for (int i = 0; i < initialIterations; i++) {
+        attractor_iteration();
+    }
+}
+
+void displayFunc() {
+    glClear(GL_COLOR_BUFFER_BIT); // clear screen
+
+    glBegin(GL_POINTS);
+    for(int i = 0; i < iterations; i++) {
+        attractor_iteration();
+        glVertex2f(x, y);
+    }
+    glEnd();
+
+    glutSwapBuffers(); 
+}
+
+void keyboardFunc(unsigned char key, int x, int y) {
+    cout << "pressed '" << key << "'" << endl;
+    if(key == 27) { //esc
+        exit(0);
+    }  
+}