From 8144e44adbaa377ae5e2083b61ff39bd2c6b6cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Klime=C5=A1?= Date: Sat, 20 Jul 2019 11:32:41 +0200 Subject: [PATCH] first skeleton working (?) --- .gitignore | 1 + CMakeLists.txt | 85 +++++++++++++++++----------------------------- shader/shader.frag | 25 ++++++++++++++ shader/shader.vert | 26 ++++++++++++++ 4 files changed, 83 insertions(+), 54 deletions(-) create mode 100644 .gitignore create mode 100644 shader/shader.frag create mode 100644 shader/shader.vert diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index a4350cb..62579b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,54 +1,31 @@ -cmake_minimum_required(VERSION 3.2 FATAL_ERROR) -project(openglapp VERSION 0.1.0) - -include(CTest) -enable_testing() - -# Variables -if(CMAKE_SIZEOF_VOID_P EQUAL 8) - set(PLATFORM_ARCH "64") -elseif(CMAKE_SIZEOF_VOID_P EQUAL 4) - set(PLATFORM_ARCH "32") -endif() - -# Global -set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src") -set(INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include") -set(LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lib") - -# Includes -include_directories(${INCLUDE_DIR}) - -# Source -file(GLOB SOURCES "${SRC_DIR}/*.cpp") - -add_executable(${PROJECT_NAME} ${SOURCES}) - -# GLAD -set(GLAD_DIR "${LIB_DIR}/glad") -add_library("glad" "${GLAD_DIR}/src/glad.c") -target_include_directories("glad" PRIVATE "${GLAD_DIR}/include") -target_include_directories(${PROJECT_NAME} PRIVATE "${GLAD_DIR}/include") -target_link_libraries(${PROJECT_NAME} "glad" "${CMAKE_DL_LIBS}") - -# GLFW -set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE) -set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE) -set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) -set(GLFW_DIR "${LIB_DIR}/glfw-3.2.1") -add_subdirectory("${GLFW_DIR}") -target_include_directories(${PROJECT_NAME} PRIVATE "${GLFW_DIR}/include") - -# GLM -set(GLM_TEST_ENABLE OFF CACHE BOOL "" FORCE) -set(GLM_DIR "${LIB_DIR}/glm") -add_subdirectory("${GLM_DIR}") -target_include_directories(${PROJECT_NAME} PRIVATE "${GLM_DIR}/glm") - -set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11) -target_link_libraries(${PROJECT_NAME} glfw) -target_link_libraries(${PROJECT_NAME} glm) - -set(CPACK_PROJECT_NAME ${PROJECT_NAME}) -set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) -include(CPack) +# Projects Settings +cmake_minimum_required (VERSION 3.1) +project (opengl-skeleton) + +# The main executable +add_executable(main + src/Application.cpp + src/Application.cpp + src/MyApplication.cpp + src/MyApplication.cpp + src/glError.hpp + src/glError.cpp + src/main.cpp + src/Shader.hpp + src/Shader.cpp +) +set_property(TARGET main PROPERTY CXX_STANDARD 11) +target_compile_options(main PRIVATE -Wall) + +# glfw +add_subdirectory(lib/glfw EXCLUDE_FROM_ALL) +target_link_libraries(main PRIVATE glfw) + +# glew +add_definitions(-DGLEW_STATIC) +add_subdirectory(lib/glew EXCLUDE_FROM_ALL) +target_link_libraries(main PRIVATE libglew_static) + +# glm +add_subdirectory(lib/glm EXCLUDE_FROM_ALL) +target_link_libraries(main PRIVATE glm) diff --git a/shader/shader.frag b/shader/shader.frag new file mode 100644 index 0000000..7f74c19 --- /dev/null +++ b/shader/shader.frag @@ -0,0 +1,25 @@ +#version 150 + +in vec4 fPosition; +in vec4 fColor; +in vec4 fLightPosition; +in vec3 fNormal; + +// output +out vec4 color; + +void main(void) +{ + vec3 o =-normalize(fPosition.xyz); + vec3 n = normalize(fNormal); + vec3 r = reflect(o,n); + vec3 l = normalize(fLightPosition.xyz-fPosition.xyz); + + float ambient = 0.1; + float diffus = 0.7*max(0.0,dot(n,l)); + float specular = 0.6*pow(max(0.0,-dot(r,l)),4.0); + + color = fColor * ( ambient + diffus + specular ); + + /*color = vec3(1,0,0);*/ +} diff --git a/shader/shader.vert b/shader/shader.vert new file mode 100644 index 0000000..fa13657 --- /dev/null +++ b/shader/shader.vert @@ -0,0 +1,26 @@ +#version 150 + +in vec3 position; +in vec3 normal; +in vec4 color; + +uniform mat4 projection; +uniform mat4 view; + +out vec4 fPosition; +out vec4 fColor; +out vec4 fLightPosition; +out vec3 fNormal; + +void main(void) +{ + fPosition = view * vec4(position,1.0); + fLightPosition = view * vec4(0.0,0.0,1.0,1.0); + + fColor = color; + fNormal = vec3(view * vec4(normal,0.0)); + + gl_Position = projection * fPosition; + /*gl_Position.x *= 1000.0f;*/ + /*gl_Position.y = 0.0;*/ +}