From 416c05d4058177ed4f377a209d535aa7865e80b6 Mon Sep 17 00:00:00 2001 From: bobarna <barnabas.borcsok@gmail.com> Date: Sat, 23 Jan 2021 16:09:23 +0100 Subject: [PATCH] =?UTF-8?q?nem=20fordul=C3=B3=20projektek=20t=C3=B6r=C3=B6?= =?UTF-8?q?lve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FTL/CMakeLists.txt | 19 -- FTL/FTL.cpp | 115 ------- FTL/FTL.h | 35 --- animation-example/CMakeLists.txt | 19 -- animation-example/framework.cpp | 72 ----- animation-example/framework.h | 397 ----------------------- animation-example/pman.cpp | 523 ------------------------------- 7 files changed, 1180 deletions(-) delete mode 100644 FTL/CMakeLists.txt delete mode 100644 FTL/FTL.cpp delete mode 100644 FTL/FTL.h delete mode 100644 animation-example/CMakeLists.txt delete mode 100644 animation-example/framework.cpp delete mode 100644 animation-example/framework.h delete mode 100644 animation-example/pman.cpp diff --git a/FTL/CMakeLists.txt b/FTL/CMakeLists.txt deleted file mode 100644 index 7ff68b3..0000000 --- a/FTL/CMakeLists.txt +++ /dev/null @@ -1,19 +0,0 @@ -cmake_minimum_required(VERSION 3.13) -project(spacegame) - -set(CMAKE_CXX_STANDARD 17) -#set(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -pedantic") -set(CMAKE_CXX_FLAGS "-Wall -Wextra") - -find_package(OpenGL REQUIRED) - -link_directories(${GTKMM_LIBRARY_DIRS}) -include_directories(${GTKMM_INCLUDE_DIRS}) - -add_executable(spacegame - GameEngine.h - GameEngine.cpp - spacegame.cpp) - - -target_link_libraries(spacegame GL glut GLU GLEW glfw) diff --git a/FTL/FTL.cpp b/FTL/FTL.cpp deleted file mode 100644 index 3253af4..0000000 --- a/FTL/FTL.cpp +++ /dev/null @@ -1,115 +0,0 @@ -#include <glr/GL.h> -#include "FTL.h" - -using namespace gl; - -namespace ftl { - - Particle::Particle(Vec3 p, float m) - :position(p) - ,tmp_position(p) - ,enabled(true) - { - - if(m < 0.001) { - m = 0.001; - } - - mass = m; - inv_mass = 1.0 / mass; - } - - // ------------------------ - - FTL::FTL() - :len(10) - { - } - - void FTL::setup(int num, float d) { - float dim = 50; - len = d; - Vec3 pos(300, 300, 0); - float mass = rx_random(1.0f, 10.0f); - for(int i = 0; i < num; ++i) { - Particle* p = new Particle(pos, mass); //rx_random(1.0f, 10.0f)); - particles.push_back(p); - pos.y += d; - } - - particles[0]->enabled = false; - } - - void FTL::addForce(Vec3 f) { - for(std::vector<Particle*>::iterator it = particles.begin(); it != particles.end(); ++it) { - Particle* p = *it; - if(p->enabled) { - p->forces += f; - } - } - } - - void FTL::update() { - float dt = 1.0f/20.0f; - - // update velocities - for(std::vector<Particle*>::iterator it = particles.begin(); it != particles.end(); ++it) { - Particle* p = *it; - if(!p->enabled) { - p->tmp_position = p->position; - continue; - } - p->velocity = p->velocity + dt * (p->forces * p->inv_mass); - p->tmp_position += (p->velocity * dt); - p->forces = 0; - p->velocity *= 0.99; - } - - // solve constraints - Vec3 dir; - Vec3 curr_pos; - for(size_t i = 1; i < particles.size(); ++i) { - Particle* pa = particles[i - 1]; - Particle* pb = particles[i]; - curr_pos = pb->tmp_position; - dir = pb->tmp_position - pa->tmp_position; - dir.normalize(); - pb->tmp_position = pa->tmp_position + dir * len; - pb->d = curr_pos - pb->tmp_position; // - curr_pos; - } - - for(size_t i = 1; i < particles.size(); ++i) { - Particle* pa = particles[i-1]; - Particle* pb = particles[i]; - if(!pa->enabled) { - continue; - } - pa->velocity = ((pa->tmp_position - pa->position) / dt) + 0.9 * (pb->d / dt); - pa->position = pa->tmp_position; - } - - Particle* last = particles.back(); - last->position = last->tmp_position; - - } - - void FTL::draw() { - - glLineWidth(0.1f); - glr_begin(GL_LINE_STRIP); - for(std::vector<Particle*>::iterator it = particles.begin(); it != particles.end(); ++it) { - Particle* p = *it; - - if(!p->enabled) { - glr_color(217, 41, 41); - } - else { - glr_color(251, 251, 251); - } - - glr_vertex(p->position); - } - glr_end(); - } - -} diff --git a/FTL/FTL.h b/FTL/FTL.h deleted file mode 100644 index 8013f67..0000000 --- a/FTL/FTL.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef ROXLU_FTL_H -#define ROXLU_FTL_H - -#include <roxlu/Roxlu.h> - -namespace ftl { - - struct Particle { - Particle(Vec3 position, float m); - Vec3 position; - Vec3 tmp_position; - Vec3 forces; - Vec3 velocity; - Vec3 d; - float mass; - float inv_mass; - bool enabled; - - }; - - class FTL { - public: - FTL(); - void setup(int num, float d); - void addForce(Vec3 f); - void update(); - void draw(); - public: - float len; - std::vector<Particle*> particles; - Vec3 color; - }; - -} -#endif diff --git a/animation-example/CMakeLists.txt b/animation-example/CMakeLists.txt deleted file mode 100644 index 1ad86ce..0000000 --- a/animation-example/CMakeLists.txt +++ /dev/null @@ -1,19 +0,0 @@ -cmake_minimum_required(VERSION 3.13) -project(pman) - -set(CMAKE_CXX_STANDARD 17) -#set(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -pedantic") -set(CMAKE_CXX_FLAGS "-Wall -Wextra") - -find_package(OpenGL REQUIRED) - -link_directories(${GTKMM_LIBRARY_DIRS}) -include_directories(${GTKMM_INCLUDE_DIRS}) - -add_executable(pman - framework.h - framework.cpp - pman.cpp) - - -target_link_libraries(pman GL glut GLU GLEW glfw) diff --git a/animation-example/framework.cpp b/animation-example/framework.cpp deleted file mode 100644 index 09c1c82..0000000 --- a/animation-example/framework.cpp +++ /dev/null @@ -1,72 +0,0 @@ -//============================================================================================= -// Collection of classes from lecture slides. -// Framework for assignments. Valid from 2019. -// Do not change it if you want to submit a homework. -//============================================================================================= -#include "framework.h" - -// Initialization -void onInitialization(); - -// Window has become invalid: Redraw -void onDisplay(); - -// Key of ASCII code pressed -void onKeyboard(unsigned char key, int pX, int pY); - -// Key of ASCII code released -void onKeyboardUp(unsigned char key, int pX, int pY); - -// Move mouse with key pressed -void onMouseMotion(int pX, int pY); - -// Mouse click event -void onMouse(int button, int state, int pX, int pY); - -// Idle event indicating that some time elapsed: do animation here -void onIdle(); - -// Entry point of the application -int main(int argc, char * argv[]) { - // Initialize GLUT, Glew and OpenGL - glutInit(&argc, argv); - - // OpenGL major and minor versions - int majorVersion = 3, minorVersion = 3; -#if !defined(__APPLE__) - glutInitContextVersion(majorVersion, minorVersion); -#endif - glutInitWindowSize(windowWidth, windowHeight); // Application window is initially of resolution 600x600 - glutInitWindowPosition(100, 100); // Relative location of the application window -#if defined(__APPLE__) - glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_3_2_CORE_PROFILE); // 8 bit R,G,B,A + double buffer + depth buffer -#else - glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); -#endif - glutCreateWindow(argv[0]); - -#if !defined(__APPLE__) - glewExperimental = true; // magic - glewInit(); -#endif - printf("GL Vendor : %s\n", glGetString(GL_VENDOR)); - printf("GL Renderer : %s\n", glGetString(GL_RENDERER)); - printf("GL Version (string) : %s\n", glGetString(GL_VERSION)); - glGetIntegerv(GL_MAJOR_VERSION, &majorVersion); - glGetIntegerv(GL_MINOR_VERSION, &minorVersion); - printf("GL Version (integer) : %d.%d\n", majorVersion, minorVersion); - printf("GLSL Version : %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); - - // Initialize this program and create shaders - onInitialization(); - - glutDisplayFunc(onDisplay); // Register event handlers - glutMouseFunc(onMouse); - glutIdleFunc(onIdle); - glutKeyboardFunc(onKeyboard); - glutKeyboardUpFunc(onKeyboardUp); - glutMotionFunc(onMouseMotion); - - glutMainLoop(); - return 1; -} diff --git a/animation-example/framework.h b/animation-example/framework.h deleted file mode 100644 index 5ee4227..0000000 --- a/animation-example/framework.h +++ /dev/null @@ -1,397 +0,0 @@ -//============================================================================================= -// Collection of programs from lecture slides. -// Framework for assignments. Valid from 2020. -// -// Do not change it if you want to submit a homework. -// In the homework, file operations other than printf are prohibited. -//============================================================================================= -#define _USE_MATH_DEFINES // M_PI -#include <stdio.h> -#include <stdlib.h> -#include <math.h> -#include <vector> -#include <string> - -#if defined(__APPLE__) -#include <GLUT/GLUT.h> -#include <OpenGL/gl3.h> -#else -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) -#include <windows.h> -#endif -#include <GL/glew.h> // must be downloaded -#include <GL/freeglut.h> // must be downloaded unless you have an Apple -#endif - -// Resolution of screen -const unsigned int windowWidth = 600, windowHeight = 600; - -//-------------------------- -struct vec2 { -//-------------------------- - float x, y; - - vec2(float x0 = 0, float y0 = 0) { x = x0; y = y0; } - vec2 operator*(float a) const { return vec2(x * a, y * a); } - vec2 operator/(float a) const { return vec2(x / a, y / a); } - vec2 operator+(const vec2& v) const { return vec2(x + v.x, y + v.y); } - vec2 operator-(const vec2& v) const { return vec2(x - v.x, y - v.y); } - vec2 operator*(const vec2& v) const { return vec2(x * v.x, y * v.y); } - vec2 operator-() const { return vec2(-x, -y); } -}; - -inline float dot(const vec2& v1, const vec2& v2) { - return (v1.x * v2.x + v1.y * v2.y); -} - -inline float length(const vec2& v) { return sqrtf(dot(v, v)); } - -inline vec2 normalize(const vec2& v) { return v * (1 / length(v)); } - -inline vec2 operator*(float a, const vec2& v) { return vec2(v.x * a, v.y * a); } - -//-------------------------- -struct vec3 { -//-------------------------- - float x, y, z; - - vec3(float x0 = 0, float y0 = 0, float z0 = 0) { x = x0; y = y0; z = z0; } - vec3(vec2 v) { x = v.x; y = v.y; z = 0; } - - vec3 operator*(float a) const { return vec3(x * a, y * a, z * a); } - vec3 operator/(float a) const { return vec3(x / a, y / a, z / a); } - vec3 operator+(const vec3& v) const { return vec3(x + v.x, y + v.y, z + v.z); } - vec3 operator-(const vec3& v) const { return vec3(x - v.x, y - v.y, z - v.z); } - vec3 operator*(const vec3& v) const { return vec3(x * v.x, y * v.y, z * v.z); } - vec3 operator-() const { return vec3(-x, -y, -z); } -}; - -inline float dot(const vec3& v1, const vec3& v2) { return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z); } - -inline float length(const vec3& v) { return sqrtf(dot(v, v)); } - -inline vec3 normalize(const vec3& v) { return v * (1 / length(v)); } - -inline vec3 cross(const vec3& v1, const vec3& v2) { - return vec3(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x); -} - -inline vec3 operator*(float a, const vec3& v) { return vec3(v.x * a, v.y * a, v.z * a); } - -//-------------------------- -struct vec4 { -//-------------------------- - float x, y, z, w; - - vec4(float x0 = 0, float y0 = 0, float z0 = 0, float w0 = 0) { x = x0; y = y0; z = z0; w = w0; } - float& operator[](int j) { return *(&x + j); } - float operator[](int j) const { return *(&x + j); } - - vec4 operator*(float a) const { return vec4(x * a, y * a, z * a, w * a); } - vec4 operator/(float d) const { return vec4(x / d, y / d, z / d, w / d); } - vec4 operator+(const vec4& v) const { return vec4(x + v.x, y + v.y, z + v.z, w + v.w); } - vec4 operator-(const vec4& v) const { return vec4(x - v.x, y - v.y, z - v.z, w - v.w); } - vec4 operator*(const vec4& v) const { return vec4(x * v.x, y * v.y, z * v.z, w * v.w); } - void operator+=(const vec4 right) { x += right.x; y += right.y; z += right.z, w += right.z; } -}; - -inline float dot(const vec4& v1, const vec4& v2) { - return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w); -} - -inline vec4 operator*(float a, const vec4& v) { - return vec4(v.x * a, v.y * a, v.z * a, v.w * a); -} - -//--------------------------- -struct mat4 { // row-major matrix 4x4 -//--------------------------- - vec4 rows[4]; -public: - mat4() {} - mat4(float m00, float m01, float m02, float m03, - float m10, float m11, float m12, float m13, - float m20, float m21, float m22, float m23, - float m30, float m31, float m32, float m33) { - rows[0][0] = m00; rows[0][1] = m01; rows[0][2] = m02; rows[0][3] = m03; - rows[1][0] = m10; rows[1][1] = m11; rows[1][2] = m12; rows[1][3] = m13; - rows[2][0] = m20; rows[2][1] = m21; rows[2][2] = m22; rows[2][3] = m23; - rows[3][0] = m30; rows[3][1] = m31; rows[3][2] = m32; rows[3][3] = m33; - } - mat4(vec4 it, vec4 jt, vec4 kt, vec4 ot) { - rows[0] = it; rows[1] = jt; rows[2] = kt; rows[3] = ot; - } - - vec4& operator[](int i) { return rows[i]; } - vec4 operator[](int i) const { return rows[i]; } - operator float*() const { return (float*)this; } -}; - -inline vec4 operator*(const vec4& v, const mat4& mat) { - return v[0] * mat[0] + v[1] * mat[1] + v[2] * mat[2] + v[3] * mat[3]; -} - -inline mat4 operator*(const mat4& left, const mat4& right) { - mat4 result; - for (int i = 0; i < 4; i++) result.rows[i] = left.rows[i] * right; - return result; -} - -inline mat4 TranslateMatrix(vec3 t) { - return mat4(vec4(1, 0, 0, 0), - vec4(0, 1, 0, 0), - vec4(0, 0, 1, 0), - vec4(t.x, t.y, t.z, 1)); -} - -inline mat4 ScaleMatrix(vec3 s) { - return mat4(vec4(s.x, 0, 0, 0), - vec4(0, s.y, 0, 0), - vec4(0, 0, s.z, 0), - vec4(0, 0, 0, 1)); -} - -inline mat4 RotationMatrix(float angle, vec3 w) { - float c = cosf(angle), s = sinf(angle); - w = normalize(w); - return mat4(vec4(c * (1 - w.x*w.x) + w.x*w.x, w.x*w.y*(1 - c) + w.z*s, w.x*w.z*(1 - c) - w.y*s, 0), - vec4(w.x*w.y*(1 - c) - w.z*s, c * (1 - w.y*w.y) + w.y*w.y, w.y*w.z*(1 - c) + w.x*s, 0), - vec4(w.x*w.z*(1 - c) + w.y*s, w.y*w.z*(1 - c) - w.x*s, c * (1 - w.z*w.z) + w.z*w.z, 0), - vec4(0, 0, 0, 1)); -} - -//--------------------------- -class Texture { -//--------------------------- - std::vector<vec4> load(std::string pathname, bool transparent, int& width, int& height) { - FILE * file = fopen(pathname.c_str(), "r"); - if (!file) { - printf("%s does not exist\n", pathname.c_str()); - width = height = 0; - return std::vector<vec4>(); - } - unsigned short bitmapFileHeader[27]; // bitmap header - fread(&bitmapFileHeader, 27, 2, file); - if (bitmapFileHeader[0] != 0x4D42) printf("Not bmp file\n"); - if (bitmapFileHeader[14] != 24) printf("Only true color bmp files are supported\n"); - width = bitmapFileHeader[9]; - height = bitmapFileHeader[11]; - unsigned int size = (unsigned long)bitmapFileHeader[17] + (unsigned long)bitmapFileHeader[18] * 65536; - fseek(file, 54, SEEK_SET); - std::vector<unsigned char> bImage(size); - fread(&bImage[0], 1, size, file); // read the pixels - fclose(file); - std::vector<vec4> image(width * height); - int i = 0; - for (unsigned int idx = 0; idx < size; idx += 3) { // Swap R and B since in BMP, the order is BGR - float alpha = (transparent) ? (bImage[idx] + bImage[idx + 1] + bImage[idx + 2]) / 3.0f / 256.0f : 1.0f; - image[i++] = vec4(bImage[idx + 2] / 256.0f, bImage[idx + 1] / 256.0f, bImage[idx] / 256.0f, alpha); - } - return image; - } - -public: - unsigned int textureId = 0; - - Texture() { textureId = 0; } - - Texture(std::string pathname, bool transparent = false) { - textureId = 0; - create(pathname, transparent); - } - - Texture(int width, int height, const std::vector<vec4>& image, int sampling = GL_LINEAR) { - textureId = 0; - create(width, height, image, sampling); - } - - Texture(const Texture& texture) { - printf("\nError: Texture resource is not copied on GPU!!!\n"); - } - - void operator=(const Texture& texture) { - printf("\nError: Texture resource is not copied on GPU!!!\n"); - } - - void create(std::string pathname, bool transparent = false) { - int width, height; - std::vector<vec4> image = load(pathname, transparent, width, height); - if (image.size() > 0) create(width, height, image); - } - - void create(int width, int height, const std::vector<vec4>& image, int sampling = GL_LINEAR) { - if (textureId == 0) glGenTextures(1, &textureId); // id generation - glBindTexture(GL_TEXTURE_2D, textureId); // binding - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, &image[0]); // To GPU - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, sampling); // sampling - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, sampling); - } - - ~Texture() { - if (textureId > 0) glDeleteTextures(1, &textureId); - } -}; - -//--------------------------- -class GPUProgram { -//-------------------------- - unsigned int shaderProgramId = 0; - unsigned int vertexShader = 0, geometryShader = 0, fragmentShader = 0; - bool waitError = true; - - void getErrorInfo(unsigned int handle) { // shader error report - int logLen, written; - glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &logLen); - if (logLen > 0) { - std::string log(logLen, '\0'); - glGetShaderInfoLog(handle, logLen, &written, &log[0]); - printf("Shader log:\n%s", log.c_str()); - if (waitError) getchar(); - } - } - - bool checkShader(unsigned int shader, std::string message) { // check if shader could be compiled - int OK; - glGetShaderiv(shader, GL_COMPILE_STATUS, &OK); - if (!OK) { - printf("%s!\n", message.c_str()); - getErrorInfo(shader); - return false; - } - return true; - } - - bool checkLinking(unsigned int program) { // check if shader could be linked - int OK; - glGetProgramiv(program, GL_LINK_STATUS, &OK); - if (!OK) { - printf("Failed to link shader program!\n"); - getErrorInfo(program); - return false; - } - return true; - } - - int getLocation(const std::string& name) { // get the address of a GPU uniform variable - int location = glGetUniformLocation(shaderProgramId, name.c_str()); - if (location < 0) printf("uniform %s cannot be set\n", name.c_str()); - return location; - } - -public: - GPUProgram(bool _waitError = true) { shaderProgramId = 0; waitError = _waitError; } - - GPUProgram(const GPUProgram& program) { - if (program.shaderProgramId > 0) printf("\nError: GPU program is not copied on GPU!!!\n"); - } - - void operator=(const GPUProgram& program) { - if (program.shaderProgramId > 0) printf("\nError: GPU program is not copied on GPU!!!\n"); - } - - unsigned int getId() { return shaderProgramId; } - - bool create(const char * const vertexShaderSource, - const char * const fragmentShaderSource, const char * const fragmentShaderOutputName, - const char * const geometryShaderSource = nullptr) - { - // Create vertex shader from string - if (vertexShader == 0) vertexShader = glCreateShader(GL_VERTEX_SHADER); - if (!vertexShader) { - printf("Error in vertex shader creation\n"); - exit(1); - } - glShaderSource(vertexShader, 1, (const GLchar**)&vertexShaderSource, NULL); - glCompileShader(vertexShader); - if (!checkShader(vertexShader, "Vertex shader error")) return false; - - // Create geometry shader from string if given - if (geometryShaderSource != nullptr) { - if (geometryShader == 0) geometryShader = glCreateShader(GL_GEOMETRY_SHADER); - if (!geometryShader) { - printf("Error in geometry shader creation\n"); - exit(1); - } - glShaderSource(geometryShader, 1, (const GLchar**)&geometryShaderSource, NULL); - glCompileShader(geometryShader); - if (!checkShader(geometryShader, "Geometry shader error")) return false; - } - - // Create fragment shader from string - if (fragmentShader == 0) fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); - if (!fragmentShader) { - printf("Error in fragment shader creation\n"); - exit(1); - } - - glShaderSource(fragmentShader, 1, (const GLchar**)&fragmentShaderSource, NULL); - glCompileShader(fragmentShader); - if (!checkShader(fragmentShader, "Fragment shader error")) return false; - - shaderProgramId = glCreateProgram(); - if (!shaderProgramId) { - printf("Error in shader program creation\n"); - exit(1); - } - glAttachShader(shaderProgramId, vertexShader); - glAttachShader(shaderProgramId, fragmentShader); - if (geometryShader > 0) glAttachShader(shaderProgramId, geometryShader); - - // Connect the fragmentColor to the frame buffer memory - glBindFragDataLocation(shaderProgramId, 0, fragmentShaderOutputName); // this output goes to the frame buffer memory - - // program packaging - glLinkProgram(shaderProgramId); - if (!checkLinking(shaderProgramId)) return false; - - // make this program run - glUseProgram(shaderProgramId); - return true; - } - - void Use() { // make this program run - glUseProgram(shaderProgramId); - } - - void setUniform(int i, const std::string& name) { - int location = getLocation(name); - if (location >= 0) glUniform1i(location, i); - } - - void setUniform(float f, const std::string& name) { - int location = getLocation(name); - if (location >= 0) glUniform1f(location, f); - } - - void setUniform(const vec2& v, const std::string& name) { - int location = getLocation(name); - if (location >= 0) glUniform2fv(location, 1, &v.x); - } - - void setUniform(const vec3& v, const std::string& name) { - int location = getLocation(name); - if (location >= 0) glUniform3fv(location, 1, &v.x); - } - - void setUniform(const vec4& v, const std::string& name) { - int location = getLocation(name); - if (location >= 0) glUniform4fv(location, 1, &v.x); - } - - void setUniform(const mat4& mat, const std::string& name) { - int location = getLocation(name); - if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, mat); - } - - void setUniform(const Texture& texture, const std::string& samplerName, unsigned int textureUnit = 0) { - int location = getLocation(samplerName); - if (location >= 0) { - glUniform1i(location, textureUnit); - glActiveTexture(GL_TEXTURE0 + textureUnit); - glBindTexture(GL_TEXTURE_2D, texture.textureId); - } - } - - ~GPUProgram() { if (shaderProgramId > 0) glDeleteProgram(shaderProgramId); } -}; diff --git a/animation-example/pman.cpp b/animation-example/pman.cpp deleted file mode 100644 index 82b22d9..0000000 --- a/animation-example/pman.cpp +++ /dev/null @@ -1,523 +0,0 @@ -//============================================================================================= -// Primitive Man -//============================================================================================= -#include "framework.h" - -//--------------------------- -class PhongShader : public GPUProgram { -//--------------------------- - const char * vertexSource = R"( - #version 330 - precision highp float; - - uniform mat4 MVP, M, Minv; // MVP, Model, Model-inverse - uniform vec3 wLiDir; // light source direction - uniform vec3 wEye; // pos of eye - - layout(location = 0) in vec3 vtxPos; // pos in modeling space - layout(location = 1) in vec3 vtxNorm; // normal in modeling space - layout(location = 2) in vec2 vtxUV; - - out vec3 wNormal; // normal in world space - out vec3 wView; // view in world space - out vec3 wLight; // light dir in world space - - void main() { - gl_Position = vec4(vtxPos, 1) * MVP; // to NDC - vec4 wPos = vec4(vtxPos, 1) * M; - wLight = wLiDir; - wView = wEye - wPos.xyz; - wNormal = (Minv * vec4(vtxNorm, 0)).xyz; - } - )"; - - // fragment shader in GLSL - const char * fragmentSource = R"( - #version 330 - precision highp float; - - uniform vec3 kd, ks, ka; // diffuse, specular, ambient ref - uniform vec3 La, Le; // ambient and point sources - uniform float shine; // shininess for specular ref - - in vec3 wNormal; // interpolated world sp normal - in vec3 wView; // interpolated world sp view - in vec3 wLight; // interpolated world sp illum dir - in vec2 texcoord; - out vec4 fragmentColor; // output goes to frame buffer - - void main() { - vec3 N = normalize(wNormal); - vec3 V = normalize(wView); - vec3 L = normalize(wLight); - vec3 H = normalize(L + V); - float cost = max(dot(N,L), 0), cosd = max(dot(N,H), 0); - vec3 color = ka * La + (kd * cost + ks * pow(cosd,shine)) * Le; - fragmentColor = vec4(color, 1); - } - )"; -public: - PhongShader() { Create(vertexSource, fragmentSource, "fragmentColor"); } -}; - -PhongShader * gpuProgram; // vertex and fragment shaders - -//--------------------------- -struct Camera { // 3D camera -//--------------------------- - vec3 wEye, wLookat, wVup; - float fov, asp, fp, bp; -public: - Camera() { - asp = 1; - fov = 80.0f * (float)M_PI / 180.0f; - fp = 0.1; bp = 100; - } - mat4 V() { // view matrix: translates the center to the origin - vec3 w = normalize(wEye - wLookat); - vec3 u = normalize(cross(wVup, w)); - vec3 v = cross(w, u); - return TranslateMatrix(-wEye) * mat4(u.x, v.x, w.x, 0, - u.y, v.y, w.y, 0, - u.z, v.z, w.z, 0, - 0, 0, 0, 1); - } - mat4 P() { // projection matrix - return mat4(1 / (tan(fov / 2)*asp), 0, 0, 0, - 0, 1 / tan(fov / 2), 0, 0, - 0, 0, -(fp + bp) / (bp - fp), -1, - 0, 0, -2 * fp*bp / (bp - fp), 0); - } - void SetUniform() { - int location = glGetUniformLocation(gpuProgram->getId(), "wEye"); - if (location >= 0) glUniform3fv(location, 1, &wEye.x); - else printf("uniform wEye cannot be set\n"); - } -}; - -Camera camera; // 3D camera - -//-------------------------- - -struct Material { -//--------------------------- - vec3 kd, ks, ka; - float shininess; - - void SetUniform() { - kd.SetUniform(gpuProgram->getId(), "kd"); - ks.SetUniform(gpuProgram->getId(), "ks"); - ka.SetUniform(gpuProgram->getId(), "ka"); - int location = glGetUniformLocation(gpuProgram->getId(), "shine"); - if (location >= 0) glUniform1f(location, shininess); else printf("uniform shininess cannot be set\n"); - } -}; - -//--------------------------- -struct Light { -//--------------------------- - vec3 La, Le; - vec3 wLightDir; - - Light() : La(1, 1, 1), Le(3, 3, 3) { } - void SetUniform(bool enable) { - if (enable) { - La.SetUniform(gpuProgram->getId(), "La"); - Le.SetUniform(gpuProgram->getId(), "Le"); - } - else { - vec3(0, 0, 0).SetUniform(gpuProgram->getId(), "La"); - vec3(0, 0, 0).SetUniform(gpuProgram->getId(), "Le"); - } - wLightDir.SetUniform(gpuProgram->getId(), "wLiDir"); - } -}; - - -//--------------------------- -struct VertexData { -//--------------------------- - vec3 position, normal; - vec2 texcoord; -}; - -//--------------------------- -class Geometry { -//--------------------------- - unsigned int vao, type; // vertex array object -protected: - int nVertices; -public: - Geometry(unsigned int _type) { - type = _type; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - } - void Draw(mat4 M, mat4 Minv) { - mat4 MVP = M * camera.V() * camera.P(); - MVP.SetUniform(gpuProgram->getId(), "MVP"); - M.SetUniform(gpuProgram->getId(), "M"); - Minv.SetUniform(gpuProgram->getId(), "Minv"); - glBindVertexArray(vao); - glDrawArrays(type, 0, nVertices); - } -}; - -//--------------------------- -class ParamSurface : public Geometry { -//--------------------------- -public: - ParamSurface() : Geometry(GL_TRIANGLES) {} - - virtual VertexData GenVertexData(float u, float v) = 0; - - void Create(int N = 16, int M = 16) { - unsigned int vbo; - glGenBuffers(1, &vbo); // Generate 1 vertex buffer object - glBindBuffer(GL_ARRAY_BUFFER, vbo); - nVertices = N * M * 6; - std::vector<VertexData> vtxData; // vertices on the CPU - for (int i = 0; i < N; i++) { - for (int j = 0; j < M; j++) { - vtxData.push_back(GenVertexData((float)i / N, (float)j / M)); - vtxData.push_back(GenVertexData((float)(i + 1) / N, (float)j / M)); - vtxData.push_back(GenVertexData((float)i / N, (float)(j + 1) / M)); - vtxData.push_back(GenVertexData((float)(i + 1) / N, (float)j / M)); - vtxData.push_back(GenVertexData((float)(i + 1) / N, (float)(j + 1) / M)); - vtxData.push_back(GenVertexData((float)i / N, (float)(j + 1) / M)); - } - } - glBufferData(GL_ARRAY_BUFFER, nVertices * sizeof(VertexData), &vtxData[0], GL_STATIC_DRAW); - // Enable the vertex attribute arrays - glEnableVertexAttribArray(0); // attribute array 0 = POSITION - glEnableVertexAttribArray(1); // attribute array 1 = NORMAL - glEnableVertexAttribArray(2); // attribute array 2 = TEXCOORD0 - // attribute array, components/attribute, component type, normalize?, stride, offset - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, position)); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, normal)); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, texcoord)); - } -}; - -//--------------------------- -class Sphere : public ParamSurface { -//--------------------------- - float r; -public: - Sphere(float _r) { - r = _r; - Create(20, 20); - } - - VertexData GenVertexData(float u, float v) { - VertexData vd; - vd.normal = vec3(cosf(u * 2.0f * M_PI) * sin(v*M_PI), sinf(u * 2.0f * M_PI) * sinf(v*M_PI), cosf(v*M_PI)); - vd.position = vd.normal * r; - vd.texcoord = vec2(u, v); - return vd; - } -}; - -//--------------------------- -class TruncatedCone : public ParamSurface { -//--------------------------- - float rStart, rEnd; -public: - TruncatedCone(float _rStart, float _rEnd) { - rStart = _rStart, rEnd = _rEnd; - Create(20, 20); - } - - VertexData GenVertexData(float u, float v) { - VertexData vd; - float U = u * 2.0f * M_PI; - vec3 circle = vec3(cosf(U), sinf(U), 0); - vd.position = circle * (rStart * (1 - v) + rEnd * v) + vec3(0, 0, v); - vec3 drdU = vec3(-sinf(U), cosf(U)); - vec3 drdv = circle * (rEnd - rStart) + vec3(0, 0, 1); - vd.normal = cross(drdU, drdv); - vd.texcoord = vec2(u, v); - return vd; - } -}; - -//--------------------------- -class Quad : public ParamSurface { -//--------------------------- - float size; -public: - Quad() { - size = 100; - Create(20, 20); - } - - VertexData GenVertexData(float u, float v) { - VertexData vd; - vd.normal = vec3(0, 1, 0); - vd.position = vec3((u - 0.5) * 2, 0, (v - 0.5) * 2) * size; - vd.texcoord = vec2(u, v); - return vd; - } -}; - -//--------------------------- -class Floor { -//--------------------------- - Material * material; - Geometry * quad; -public: - Floor(Material * _m) { - material = _m; - quad = new Quad(); - } - void Draw(mat4 M, mat4 Minv) { - material->SetUniform(); - quad->Draw(M, Minv); - } -}; - -const float boneRadius = 0.5; -const float legLength = 5; - -#define INVERSE_KINEMATICS -//=============================================================== -class PrimitiveMan { -//=============================================================== - Material * material; - Sphere * head; - TruncatedCone * torso; - Sphere * joint; - TruncatedCone * bone; - - float dleftarm_angle, drightarm_angle, dleftleg_angle, drightleg_angle; - float leftLegAngle, rightLegAngle, leftArmAngle, rightArmAngle, leftToeAngle, rightToeAngle; - float forward, up; // movement -public: - PrimitiveMan(Material * _m) { - material = _m; - head = new Sphere(1.5); - torso = new TruncatedCone(1.0, 0.8); - joint = new Sphere(boneRadius); - bone = new TruncatedCone(boneRadius, boneRadius/5); - forward = 0; - up = legLength + boneRadius; - - dleftarm_angle = -6; drightarm_angle = 6; - dleftleg_angle = 3; drightleg_angle = -3; - - rightLegAngle = 120; - rightToeAngle = -120; - leftLegAngle = 60; - leftToeAngle = -60; - rightArmAngle = 30; - leftArmAngle = 150; - } - float Forward() { return forward; } - - void Animate(float dt) { - if (forward < 105) { - float oldleg_angle = rightLegAngle; - - leftArmAngle += dleftarm_angle * dt; - rightArmAngle += drightarm_angle * dt; - leftLegAngle += dleftleg_angle * dt; - rightLegAngle += drightleg_angle * dt; - if (leftArmAngle > 150) { dleftarm_angle = -6; drightarm_angle = 6; } - if (rightArmAngle > 150) { dleftarm_angle = 6; drightarm_angle = -6; } - if (leftLegAngle > 120) { dleftleg_angle = -3; drightleg_angle = 3; } - if (rightLegAngle > 120) { dleftleg_angle = 3; drightleg_angle = -3; } - // "inverse kinematics" -#ifdef INVERSE_KINEMATICS - forward += fabs(legLength * (sin((rightLegAngle - 90) * M_PI / 180) -sin((oldleg_angle - 90) * M_PI / 180))); - up = legLength * cos((rightLegAngle - 90) * M_PI / 180) + boneRadius; - leftToeAngle = -leftLegAngle; - rightToeAngle = -rightLegAngle; -#else - forward += 0.3 * dt; -#endif - } - else { - up -= 2 * dt; - } - } - void DrawHead(mat4 M, mat4 Minv) { - M = TranslateMatrix(vec3(0, 6.5f, 0)) * M; - Minv = Minv * TranslateMatrix(-vec3(0, 6.5f, 0)); - head->Draw(M, Minv); - } - void DrawTorso(mat4 M, mat4 Minv) { - M = ScaleMatrix(vec3(2, 1, 5)) * RotationMatrix(90 * M_PI / 180, vec3(1, 0, 0)) * TranslateMatrix(vec3(0, 5, 0)) * M; - Minv = Minv * TranslateMatrix(-vec3(0, 5, 0)) * RotationMatrix(-90 * M_PI / 180, vec3(1, 0, 0)) * ScaleMatrix(vec3(0.5, 1, 0.2)); - torso->Draw(M, Minv); - } - void DrawLeftLeg(mat4 M, mat4 Minv) { - joint->Draw(M, Minv); - - M = RotationMatrix(leftLegAngle * M_PI / 180, vec3(1, 0, 0)) * M; - Minv = Minv * RotationMatrix(-leftLegAngle * M_PI / 180, vec3(1, 0, 0)); - bone->Draw(ScaleMatrix(vec3(1, 1, legLength)) * TranslateMatrix(vec3(0, 0, boneRadius)) * M, - Minv * TranslateMatrix(-vec3(0, 0, boneRadius)) * ScaleMatrix(vec3(1, 1, 1/legLength))); - - DrawToe(RotationMatrix(-leftLegAngle * M_PI / 180, vec3(1, 0, 0)) * TranslateMatrix(vec3(0, 0, legLength)) * M, - Minv * TranslateMatrix(-vec3(0, 0, legLength)) * RotationMatrix(leftLegAngle * M_PI / 180, vec3(1, 0, 0))); - } - - void DrawRightLeg(mat4 M, mat4 Minv) { - joint->Draw(M, Minv); - - M = RotationMatrix(rightLegAngle * M_PI / 180, vec3(1, 0, 0)) * M; - Minv = Minv * RotationMatrix(-rightLegAngle * M_PI / 180, vec3(1, 0, 0)); - const float legLength = 5; - bone->Draw(ScaleMatrix(vec3(1, 1, legLength)) * TranslateMatrix(vec3(0, 0, boneRadius)) * M, - Minv * TranslateMatrix(-vec3(0, 0, boneRadius)) * ScaleMatrix(vec3(1, 1, 1 / legLength))); - - DrawToe(RotationMatrix(-rightLegAngle * M_PI / 180, vec3(1, 0, 0)) * TranslateMatrix(vec3(0, 0, legLength)) *M, - Minv * TranslateMatrix(-vec3(0, 0, legLength))* RotationMatrix(rightLegAngle * M_PI / 180, vec3(1, 0, 0))); - } - - void DrawToe(mat4 M, mat4 Minv) { - joint->Draw(M, Minv); - const float toeLength = 1; - bone->Draw(ScaleMatrix(vec3(1, 1, toeLength)) * TranslateMatrix(vec3(0, 0, boneRadius)) * M, - Minv * TranslateMatrix(-vec3(0, 0, boneRadius)) * ScaleMatrix(vec3(1, 1, 1 / toeLength))); - } - - void DrawArm(mat4 M, mat4 Minv) { - joint->Draw(M, Minv); - const float toeLength = 1; - bone->Draw(ScaleMatrix(vec3(1, 1, 4)) * TranslateMatrix(vec3(0, 0, boneRadius)) * M, - Minv * TranslateMatrix(-vec3(0, 0, boneRadius)) * ScaleMatrix(vec3(1, 1, 1.0 / 4))); - } - - void Draw(mat4 M, mat4 Minv) { // Draw the hierarchy - M = TranslateMatrix(vec3(0, up, forward)) * M; - Minv = Minv * TranslateMatrix(-vec3(0, up, forward)); - material->SetUniform(); - DrawHead(M, Minv); - DrawTorso(M, Minv); - - vec3 rightLegJoint(-2, 0, 0); - DrawRightLeg(TranslateMatrix(rightLegJoint) * M, Minv * TranslateMatrix(-rightLegJoint)); - - vec3 leftLegJoint( 2, 0, 0); - DrawLeftLeg(TranslateMatrix(leftLegJoint) * M, Minv * TranslateMatrix(-leftLegJoint)); - - vec3 rightArmJoint(-2.4, 5, 0); - DrawArm(RotationMatrix(rightArmAngle * M_PI / 180, vec3(1, 0, 0)) * TranslateMatrix(rightArmJoint) * M, - Minv * TranslateMatrix(-rightArmJoint) * RotationMatrix(-rightArmAngle * M_PI / 180, vec3(1, 0, 0))); - - vec3 leftArmJoint(2.4, 5, 0); - DrawArm(RotationMatrix(leftArmAngle * M_PI / 180, vec3(1, 0, 0)) * TranslateMatrix(leftArmJoint) * M, - Minv * TranslateMatrix(-leftArmJoint) * RotationMatrix(-leftArmAngle * M_PI / 180, vec3(1, 0, 0))); - } -}; - -//--------------------------- -class Scene { -//--------------------------- - PrimitiveMan * pman; - Floor * floor; -public: - Light light; - - void Build() { - // Materials - Material * material0 = new Material; - material0->kd = vec3(0.2f, 0.3f, 1); - material0->ks = vec3(1, 1, 1); - material0->ka = vec3(0.2f, 0.3f, 1); - material0->shininess = 20; - - Material * material1 = new Material; - material1->kd = vec3(0, 1, 1); - material1->ks = vec3(2, 2, 2); - material1->ka = vec3(0.2f, 0.2f, 0.2f); - material1->shininess = 200; - - // Geometries - pman = new PrimitiveMan(material0); - floor = new Floor(material1); - - // Camera - camera.wEye = vec3(0, 0, 4); - camera.wLookat = vec3(0, 0, 0); - camera.wVup = vec3(0, 1, 0); - - // Light - light.wLightDir = vec3(5, 5, 4); - - } - void Render() { - camera.SetUniform(); - light.SetUniform(true); - - mat4 unit = TranslateMatrix(vec3(0, 0, 0)); - floor->Draw(unit, unit); - - pman->Draw(unit, unit); - // shadow matrix that projects the man onto the floor - - light.SetUniform(false); - - mat4 shadowMatrix = { 1, 0, 0, 0, - -light.wLightDir.x / light.wLightDir.y, 0, -light.wLightDir.z / light.wLightDir.y, 0, - 0, 0, 1, 0, - 0, 0.001f, 0, 1 }; - pman->Draw(shadowMatrix, shadowMatrix); - } - - void Animate(float t) { - static float tprev = 0; - float dt = t - tprev; - tprev = t; - - pman->Animate(dt); - - static float cam_angle = 0; - cam_angle += 0.01 * dt; // camera rotate - - const float camera_rad = 30; - camera.wEye = vec3(cos(cam_angle) * camera_rad, 10, sin(cam_angle) * camera_rad + pman->Forward()); - camera.wLookat = vec3(0, 0, pman->Forward()); - } -}; - -Scene scene; - -// Initialization, create an OpenGL context -void onInitialization() { - glViewport(0, 0, windowWidth, windowHeight); - glEnable(GL_DEPTH_TEST); - glDisable(GL_CULL_FACE); - - gpuProgram = new PhongShader(); - scene.Build(); -} - -// Window has become invalid: Redraw -void onDisplay() { - glClearColor(0.5f, 0.5f, 0.8f, 1.0f); // background color - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the screen - scene.Render(); - glutSwapBuffers(); // exchange the two buffers -} - -// Key of ASCII code pressed -void onKeyboard(unsigned char key, int pX, int pY) { } - -// Key of ASCII code released -void onKeyboardUp(unsigned char key, int pX, int pY) { } - -// Mouse click event -void onMouse(int button, int state, int pX, int pY) { } - -// Move mouse with key pressed -void onMouseMotion(int pX, int pY) { -} - -// Idle event indicating that some time elapsed: do animation here -void onIdle() { - long time = glutGet(GLUT_ELAPSED_TIME); // elapsed time since the start of the program - float sec = time / 30.0f; // convert msec to sec - scene.Animate(sec); // animate the camera - glutPostRedisplay(); // redraw the scene -} -- GitLab