Now have multiple lights.
This commit is contained in:
parent
a1d2c7768d
commit
1b7066757a
63 changed files with 13886 additions and 17 deletions
|
|
@ -24,19 +24,6 @@ struct Config {
|
|||
Shader shader;
|
||||
Shader lightShader;
|
||||
|
||||
Light light{
|
||||
.position={1.2f, 1.0f, 2.0f},
|
||||
.direction={-0.2f, -1.0f, -0.3f},
|
||||
.ambient={0.2f, 0.2f, 0.2f},
|
||||
.diffuse={0.8f, 0.8f, 0.8f},
|
||||
.specular={1.0f, 1.0f, 1.0f},
|
||||
.constant=1.0f,
|
||||
.linear=0.09f,
|
||||
.quadratic=0.032f,
|
||||
.cutOff=12.5f,
|
||||
.outerCutOff=17.5f,
|
||||
};
|
||||
|
||||
glm::vec3 cubePos{2.0f, 0.0f, 0.0f};
|
||||
Camera camera{.movementSpeed=2.0f};
|
||||
|
||||
|
|
@ -243,15 +230,15 @@ void draw_cube(size_t pos, Config& config, glm::mat4& projection, glm::mat4& vie
|
|||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
||||
|
||||
void draw_light(Config& config, glm::mat4& projection, glm::mat4& view) {
|
||||
void draw_light(Config& config, Light& light, glm::mat4& projection, glm::mat4& view) {
|
||||
config.lightShader.use();
|
||||
|
||||
config.lightShader.applyLight(config.light);
|
||||
config.lightShader.setVec3(light.diffuse);
|
||||
config.lightShader.setMat4("projection", projection);
|
||||
config.lightShader.setMat4("view", view);
|
||||
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, config.light.position);
|
||||
model = glm::translate(model, light.position);
|
||||
model = glm::scale(model, glm::vec3(0.2f));
|
||||
config.lightShader.setMat4("model", model);
|
||||
|
||||
|
|
@ -276,7 +263,9 @@ void render(GLFWwindow* window, Config& config) {
|
|||
// fov, aspect, near plane, far plane
|
||||
projection = glm::perspective(glm::radians(config.camera.fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||
|
||||
// draw_light(config, projection, view);
|
||||
for(auto& light : config.light.positioned) {
|
||||
draw_light(config, light, projection, view);
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < CUBE_COUNT; i++) {
|
||||
draw_cube(i, config, projection, view);
|
||||
|
|
|
|||
10
13-multiple-lights/.gitignore
vendored
Normal file
10
13-multiple-lights/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
.*.sw*
|
||||
.DS_Store
|
||||
*.sqlite3
|
||||
*.sqlite3-wal
|
||||
*.sqlite3-shm
|
||||
debug
|
||||
coverage/
|
||||
.coverage
|
||||
builddir
|
||||
subprojects
|
||||
1
13-multiple-lights/.vimrc_proj
Normal file
1
13-multiple-lights/.vimrc_proj
Normal file
|
|
@ -0,0 +1 @@
|
|||
set makeprg=meson\ compile\ -C\ .
|
||||
311
13-multiple-lights/KHR/khrplatform.h
Normal file
311
13-multiple-lights/KHR/khrplatform.h
Normal file
|
|
@ -0,0 +1,311 @@
|
|||
#ifndef __khrplatform_h_
|
||||
#define __khrplatform_h_
|
||||
|
||||
/*
|
||||
** Copyright (c) 2008-2018 The Khronos Group Inc.
|
||||
**
|
||||
** Permission is hereby granted, free of charge, to any person obtaining a
|
||||
** copy of this software and/or associated documentation files (the
|
||||
** "Materials"), to deal in the Materials without restriction, including
|
||||
** without limitation the rights to use, copy, modify, merge, publish,
|
||||
** distribute, sublicense, and/or sell copies of the Materials, and to
|
||||
** permit persons to whom the Materials are furnished to do so, subject to
|
||||
** the following conditions:
|
||||
**
|
||||
** The above copyright notice and this permission notice shall be included
|
||||
** in all copies or substantial portions of the Materials.
|
||||
**
|
||||
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
|
||||
*/
|
||||
|
||||
/* Khronos platform-specific types and definitions.
|
||||
*
|
||||
* The master copy of khrplatform.h is maintained in the Khronos EGL
|
||||
* Registry repository at https://github.com/KhronosGroup/EGL-Registry
|
||||
* The last semantic modification to khrplatform.h was at commit ID:
|
||||
* 67a3e0864c2d75ea5287b9f3d2eb74a745936692
|
||||
*
|
||||
* Adopters may modify this file to suit their platform. Adopters are
|
||||
* encouraged to submit platform specific modifications to the Khronos
|
||||
* group so that they can be included in future versions of this file.
|
||||
* Please submit changes by filing pull requests or issues on
|
||||
* the EGL Registry repository linked above.
|
||||
*
|
||||
*
|
||||
* See the Implementer's Guidelines for information about where this file
|
||||
* should be located on your system and for more details of its use:
|
||||
* http://www.khronos.org/registry/implementers_guide.pdf
|
||||
*
|
||||
* This file should be included as
|
||||
* #include <KHR/khrplatform.h>
|
||||
* by Khronos client API header files that use its types and defines.
|
||||
*
|
||||
* The types in khrplatform.h should only be used to define API-specific types.
|
||||
*
|
||||
* Types defined in khrplatform.h:
|
||||
* khronos_int8_t signed 8 bit
|
||||
* khronos_uint8_t unsigned 8 bit
|
||||
* khronos_int16_t signed 16 bit
|
||||
* khronos_uint16_t unsigned 16 bit
|
||||
* khronos_int32_t signed 32 bit
|
||||
* khronos_uint32_t unsigned 32 bit
|
||||
* khronos_int64_t signed 64 bit
|
||||
* khronos_uint64_t unsigned 64 bit
|
||||
* khronos_intptr_t signed same number of bits as a pointer
|
||||
* khronos_uintptr_t unsigned same number of bits as a pointer
|
||||
* khronos_ssize_t signed size
|
||||
* khronos_usize_t unsigned size
|
||||
* khronos_float_t signed 32 bit floating point
|
||||
* khronos_time_ns_t unsigned 64 bit time in nanoseconds
|
||||
* khronos_utime_nanoseconds_t unsigned time interval or absolute time in
|
||||
* nanoseconds
|
||||
* khronos_stime_nanoseconds_t signed time interval in nanoseconds
|
||||
* khronos_boolean_enum_t enumerated boolean type. This should
|
||||
* only be used as a base type when a client API's boolean type is
|
||||
* an enum. Client APIs which use an integer or other type for
|
||||
* booleans cannot use this as the base type for their boolean.
|
||||
*
|
||||
* Tokens defined in khrplatform.h:
|
||||
*
|
||||
* KHRONOS_FALSE, KHRONOS_TRUE Enumerated boolean false/true values.
|
||||
*
|
||||
* KHRONOS_SUPPORT_INT64 is 1 if 64 bit integers are supported; otherwise 0.
|
||||
* KHRONOS_SUPPORT_FLOAT is 1 if floats are supported; otherwise 0.
|
||||
*
|
||||
* Calling convention macros defined in this file:
|
||||
* KHRONOS_APICALL
|
||||
* KHRONOS_APIENTRY
|
||||
* KHRONOS_APIATTRIBUTES
|
||||
*
|
||||
* These may be used in function prototypes as:
|
||||
*
|
||||
* KHRONOS_APICALL void KHRONOS_APIENTRY funcname(
|
||||
* int arg1,
|
||||
* int arg2) KHRONOS_APIATTRIBUTES;
|
||||
*/
|
||||
|
||||
#if defined(__SCITECH_SNAP__) && !defined(KHRONOS_STATIC)
|
||||
# define KHRONOS_STATIC 1
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* Definition of KHRONOS_APICALL
|
||||
*-------------------------------------------------------------------------
|
||||
* This precedes the return type of the function in the function prototype.
|
||||
*/
|
||||
#if defined(KHRONOS_STATIC)
|
||||
/* If the preprocessor constant KHRONOS_STATIC is defined, make the
|
||||
* header compatible with static linking. */
|
||||
# define KHRONOS_APICALL
|
||||
#elif defined(_WIN32)
|
||||
# define KHRONOS_APICALL __declspec(dllimport)
|
||||
#elif defined (__SYMBIAN32__)
|
||||
# define KHRONOS_APICALL IMPORT_C
|
||||
#elif defined(__ANDROID__)
|
||||
# define KHRONOS_APICALL __attribute__((visibility("default")))
|
||||
#else
|
||||
# define KHRONOS_APICALL
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* Definition of KHRONOS_APIENTRY
|
||||
*-------------------------------------------------------------------------
|
||||
* This follows the return type of the function and precedes the function
|
||||
* name in the function prototype.
|
||||
*/
|
||||
#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(__SCITECH_SNAP__)
|
||||
/* Win32 but not WinCE */
|
||||
# define KHRONOS_APIENTRY __stdcall
|
||||
#else
|
||||
# define KHRONOS_APIENTRY
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* Definition of KHRONOS_APIATTRIBUTES
|
||||
*-------------------------------------------------------------------------
|
||||
* This follows the closing parenthesis of the function prototype arguments.
|
||||
*/
|
||||
#if defined (__ARMCC_2__)
|
||||
#define KHRONOS_APIATTRIBUTES __softfp
|
||||
#else
|
||||
#define KHRONOS_APIATTRIBUTES
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* basic type definitions
|
||||
*-----------------------------------------------------------------------*/
|
||||
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__GNUC__) || defined(__SCO__) || defined(__USLC__)
|
||||
|
||||
|
||||
/*
|
||||
* Using <stdint.h>
|
||||
*/
|
||||
#include <stdint.h>
|
||||
typedef int32_t khronos_int32_t;
|
||||
typedef uint32_t khronos_uint32_t;
|
||||
typedef int64_t khronos_int64_t;
|
||||
typedef uint64_t khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
/*
|
||||
* To support platform where unsigned long cannot be used interchangeably with
|
||||
* inptr_t (e.g. CHERI-extended ISAs), we can use the stdint.h intptr_t.
|
||||
* Ideally, we could just use (u)intptr_t everywhere, but this could result in
|
||||
* ABI breakage if khronos_uintptr_t is changed from unsigned long to
|
||||
* unsigned long long or similar (this results in different C++ name mangling).
|
||||
* To avoid changes for existing platforms, we restrict usage of intptr_t to
|
||||
* platforms where the size of a pointer is larger than the size of long.
|
||||
*/
|
||||
#if defined(__SIZEOF_LONG__) && defined(__SIZEOF_POINTER__)
|
||||
#if __SIZEOF_POINTER__ > __SIZEOF_LONG__
|
||||
#define KHRONOS_USE_INTPTR_T
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#elif defined(__VMS ) || defined(__sgi)
|
||||
|
||||
/*
|
||||
* Using <inttypes.h>
|
||||
*/
|
||||
#include <inttypes.h>
|
||||
typedef int32_t khronos_int32_t;
|
||||
typedef uint32_t khronos_uint32_t;
|
||||
typedef int64_t khronos_int64_t;
|
||||
typedef uint64_t khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif defined(_WIN32) && !defined(__SCITECH_SNAP__)
|
||||
|
||||
/*
|
||||
* Win32
|
||||
*/
|
||||
typedef __int32 khronos_int32_t;
|
||||
typedef unsigned __int32 khronos_uint32_t;
|
||||
typedef __int64 khronos_int64_t;
|
||||
typedef unsigned __int64 khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif defined(__sun__) || defined(__digital__)
|
||||
|
||||
/*
|
||||
* Sun or Digital
|
||||
*/
|
||||
typedef int khronos_int32_t;
|
||||
typedef unsigned int khronos_uint32_t;
|
||||
#if defined(__arch64__) || defined(_LP64)
|
||||
typedef long int khronos_int64_t;
|
||||
typedef unsigned long int khronos_uint64_t;
|
||||
#else
|
||||
typedef long long int khronos_int64_t;
|
||||
typedef unsigned long long int khronos_uint64_t;
|
||||
#endif /* __arch64__ */
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif 0
|
||||
|
||||
/*
|
||||
* Hypothetical platform with no float or int64 support
|
||||
*/
|
||||
typedef int khronos_int32_t;
|
||||
typedef unsigned int khronos_uint32_t;
|
||||
#define KHRONOS_SUPPORT_INT64 0
|
||||
#define KHRONOS_SUPPORT_FLOAT 0
|
||||
|
||||
#else
|
||||
|
||||
/*
|
||||
* Generic fallback
|
||||
*/
|
||||
#include <stdint.h>
|
||||
typedef int32_t khronos_int32_t;
|
||||
typedef uint32_t khronos_uint32_t;
|
||||
typedef int64_t khronos_int64_t;
|
||||
typedef uint64_t khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Types that are (so far) the same on all platforms
|
||||
*/
|
||||
typedef signed char khronos_int8_t;
|
||||
typedef unsigned char khronos_uint8_t;
|
||||
typedef signed short int khronos_int16_t;
|
||||
typedef unsigned short int khronos_uint16_t;
|
||||
|
||||
/*
|
||||
* Types that differ between LLP64 and LP64 architectures - in LLP64,
|
||||
* pointers are 64 bits, but 'long' is still 32 bits. Win64 appears
|
||||
* to be the only LLP64 architecture in current use.
|
||||
*/
|
||||
#ifdef KHRONOS_USE_INTPTR_T
|
||||
typedef intptr_t khronos_intptr_t;
|
||||
typedef uintptr_t khronos_uintptr_t;
|
||||
#elif defined(_WIN64)
|
||||
typedef signed long long int khronos_intptr_t;
|
||||
typedef unsigned long long int khronos_uintptr_t;
|
||||
#else
|
||||
typedef signed long int khronos_intptr_t;
|
||||
typedef unsigned long int khronos_uintptr_t;
|
||||
#endif
|
||||
|
||||
#if defined(_WIN64)
|
||||
typedef signed long long int khronos_ssize_t;
|
||||
typedef unsigned long long int khronos_usize_t;
|
||||
#else
|
||||
typedef signed long int khronos_ssize_t;
|
||||
typedef unsigned long int khronos_usize_t;
|
||||
#endif
|
||||
|
||||
#if KHRONOS_SUPPORT_FLOAT
|
||||
/*
|
||||
* Float type
|
||||
*/
|
||||
typedef float khronos_float_t;
|
||||
#endif
|
||||
|
||||
#if KHRONOS_SUPPORT_INT64
|
||||
/* Time types
|
||||
*
|
||||
* These types can be used to represent a time interval in nanoseconds or
|
||||
* an absolute Unadjusted System Time. Unadjusted System Time is the number
|
||||
* of nanoseconds since some arbitrary system event (e.g. since the last
|
||||
* time the system booted). The Unadjusted System Time is an unsigned
|
||||
* 64 bit value that wraps back to 0 every 584 years. Time intervals
|
||||
* may be either signed or unsigned.
|
||||
*/
|
||||
typedef khronos_uint64_t khronos_utime_nanoseconds_t;
|
||||
typedef khronos_int64_t khronos_stime_nanoseconds_t;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Dummy value used to pad enum types to 32 bits.
|
||||
*/
|
||||
#ifndef KHRONOS_MAX_ENUM
|
||||
#define KHRONOS_MAX_ENUM 0x7FFFFFFF
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Enumerated boolean type
|
||||
*
|
||||
* Values other than zero should be considered to be true. Therefore
|
||||
* comparisons should not be made against KHRONOS_TRUE.
|
||||
*/
|
||||
typedef enum {
|
||||
KHRONOS_FALSE = 0,
|
||||
KHRONOS_TRUE = 1,
|
||||
KHRONOS_BOOLEAN_ENUM_FORCE_SIZE = KHRONOS_MAX_ENUM
|
||||
} khronos_boolean_enum_t;
|
||||
|
||||
#endif /* __khrplatform_h_ */
|
||||
16
13-multiple-lights/LICENSE
Normal file
16
13-multiple-lights/LICENSE
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
MIT No Attribution
|
||||
|
||||
Copyright <YEAR> <COPYRIGHT HOLDER>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||
software and associated documentation files (the "Software"), to deal in the Software
|
||||
without restriction, including without limitation the rights to use, copy, modify,
|
||||
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
30
13-multiple-lights/Makefile
Normal file
30
13-multiple-lights/Makefile
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
all: build
|
||||
|
||||
reset:
|
||||
powershell -executionpolicy bypass .\scripts\reset_build.ps1
|
||||
|
||||
build:
|
||||
meson compile -j 4 -C builddir
|
||||
|
||||
release_build:
|
||||
meson --wipe builddir -Db_ndebug=true --buildtype release
|
||||
meson compile -j 4 -C builddir
|
||||
|
||||
debug_build:
|
||||
meson setup --wipe builddir -Db_ndebug=true --buildtype debugoptimized
|
||||
meson compile -j 4 -C builddir
|
||||
|
||||
run: test
|
||||
./builddir/hellogl.exe
|
||||
|
||||
test: build
|
||||
./builddir/fuc2it
|
||||
|
||||
debug_test: build
|
||||
gdb --nx -x .gdbinit --ex run --args builddir/fuc2it.exe
|
||||
|
||||
debug_run: build
|
||||
gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/hellogl.exe
|
||||
|
||||
clean:
|
||||
meson compile --clean -C builddir
|
||||
BIN
13-multiple-lights/assets/popcorn_model_a.glb
Normal file
BIN
13-multiple-lights/assets/popcorn_model_a.glb
Normal file
Binary file not shown.
1140
13-multiple-lights/glad/glad.cpp
Normal file
1140
13-multiple-lights/glad/glad.cpp
Normal file
File diff suppressed because it is too large
Load diff
2129
13-multiple-lights/glad/glad.h
Normal file
2129
13-multiple-lights/glad/glad.h
Normal file
File diff suppressed because it is too large
Load diff
100
13-multiple-lights/meson.build
Normal file
100
13-multiple-lights/meson.build
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
project('hellogl', 'cpp',
|
||||
version: '0.1.0',
|
||||
default_options: [
|
||||
'cpp_std=c++23',
|
||||
'cpp_args=-D_GLIBCXX_DEBUG=1 -D_GLIBCXX_DEBUG_PEDANTIC=1',
|
||||
])
|
||||
|
||||
# use this for common options only for our executables
|
||||
cpp_args=[
|
||||
'-Wno-unused-parameter',
|
||||
'-Wno-unused-function',
|
||||
'-Wno-unused-variable',
|
||||
'-Wno-unused-but-set-variable',
|
||||
'-Wno-deprecated-declarations',
|
||||
]
|
||||
link_args=[]
|
||||
# these are passed as override_defaults
|
||||
exe_defaults = [ 'warning_level=2' ]
|
||||
|
||||
cc = meson.get_compiler('cpp')
|
||||
dependencies = []
|
||||
|
||||
if build_machine.system() == 'windows'
|
||||
add_global_link_arguments(
|
||||
'-static-libgcc',
|
||||
'-static-libstdc++',
|
||||
'-static',
|
||||
'-lstdc++exp',
|
||||
language: 'cpp',
|
||||
)
|
||||
|
||||
opengl32 = cc.find_library('opengl32', required: true)
|
||||
winmm = cc.find_library('winmm', required: true)
|
||||
gdi32 = cc.find_library('gdi32', required: true)
|
||||
|
||||
dependencies += [
|
||||
opengl32, winmm, gdi32
|
||||
]
|
||||
exe_defaults += ['werror=true']
|
||||
|
||||
elif build_machine.system() == 'darwin'
|
||||
add_global_link_arguments(
|
||||
language: 'cpp',
|
||||
)
|
||||
|
||||
opengl = dependency('OpenGL')
|
||||
corefoundation = dependency('CoreFoundation')
|
||||
carbon = dependency('Carbon')
|
||||
cocoa = dependency('Cocoa')
|
||||
iokit = dependency('IOKit')
|
||||
corevideo = dependency('CoreVideo')
|
||||
|
||||
link_args += ['-ObjC']
|
||||
exe_defaults += ['werror=false']
|
||||
dependencies += [
|
||||
opengl, corefoundation, carbon, cocoa, iokit, corevideo
|
||||
]
|
||||
endif
|
||||
|
||||
glfw3 = subproject('glfw').get_variable('glfw_dep')
|
||||
fuc2 = subproject('fuc2').get_variable('fuc2_dep')
|
||||
fmt = subproject('fmt').get_variable('fmt_dep')
|
||||
glm = subproject('glm').get_variable('glm_dep')
|
||||
box2d = subproject('box2d').get_variable('box2d_dep')
|
||||
|
||||
assimp = subproject('assimp').get_variable('assimp_dep')
|
||||
|
||||
dependencies += [
|
||||
glfw3, fuc2, fmt, glm, box2d, assimp
|
||||
]
|
||||
|
||||
inc_dirs = ['src']
|
||||
|
||||
sources = [
|
||||
'glad/glad.cpp',
|
||||
'src/dbc.cpp',
|
||||
'src/stb_image.cpp',
|
||||
'src/shader.cpp',
|
||||
'src/physics.cpp',
|
||||
'src/mesh.cpp',
|
||||
'src/model.cpp',
|
||||
'src/camera.cpp',
|
||||
]
|
||||
|
||||
subdir('tests')
|
||||
|
||||
executable('fuc2it', sources + fuc2_tests,
|
||||
cpp_args: cpp_args,
|
||||
link_args: link_args,
|
||||
include_directories: inc_dirs,
|
||||
override_options: exe_defaults,
|
||||
dependencies: dependencies + [fuc2])
|
||||
|
||||
executable('hellogl',
|
||||
sources + [ 'src/main.cpp' ],
|
||||
cpp_args: cpp_args,
|
||||
include_directories: inc_dirs,
|
||||
link_args: link_args,
|
||||
override_options: exe_defaults,
|
||||
dependencies: dependencies)
|
||||
BIN
13-multiple-lights/resources/textures/awesomeface.png
Normal file
BIN
13-multiple-lights/resources/textures/awesomeface.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 58 KiB |
BIN
13-multiple-lights/resources/textures/container.jpg
Normal file
BIN
13-multiple-lights/resources/textures/container.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 181 KiB |
BIN
13-multiple-lights/resources/textures/container2.png
Normal file
BIN
13-multiple-lights/resources/textures/container2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 457 KiB |
BIN
13-multiple-lights/resources/textures/container2_specular.png
Normal file
BIN
13-multiple-lights/resources/textures/container2_specular.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 141 KiB |
BIN
13-multiple-lights/resources/textures/popcorn.jpg
Normal file
BIN
13-multiple-lights/resources/textures/popcorn.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.9 KiB |
7
13-multiple-lights/scripts/reset_build.ps1
Normal file
7
13-multiple-lights/scripts/reset_build.ps1
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
mv .\subprojects\packagecache .
|
||||
rm -recurse -force .\subprojects\,.\builddir\
|
||||
mkdir subprojects
|
||||
mv .\packagecache .\subprojects\
|
||||
mkdir builddir
|
||||
cp wraps\*.wrap subprojects\
|
||||
meson setup --default-library=static --prefer-static builddir
|
||||
10
13-multiple-lights/scripts/reset_build.sh
Normal file
10
13-multiple-lights/scripts/reset_build.sh
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
mv -f ./subprojects/packagecache .
|
||||
rm -rf subprojects builddir
|
||||
mkdir subprojects
|
||||
mv -f packagecache ./subprojects/ && true
|
||||
mkdir builddir
|
||||
cp wraps/*.wrap subprojects/
|
||||
# on OSX you can't do this with static
|
||||
meson setup --default-library=static --prefer-static builddir
|
||||
4
13-multiple-lights/scripts/setup.ps1
Normal file
4
13-multiple-lights/scripts/setup.ps1
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
mkdir builddir
|
||||
mkdir subprojects
|
||||
cp wraps/*.wrap subprojects
|
||||
meson setup -Ddefault_library=static builddir
|
||||
7
13-multiple-lights/scripts/setup.sh
Normal file
7
13-multiple-lights/scripts/setup.sh
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#!/usr/bin/env bash
|
||||
set -ex
|
||||
|
||||
mkdir subprojects
|
||||
mkdir builddir
|
||||
cp wraps/*.wrap subprojects/
|
||||
meson setup builddir
|
||||
12
13-multiple-lights/scripts/watch_build.sh
Normal file
12
13-multiple-lights/scripts/watch_build.sh
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
fswatch -o *.cpp | while read num
|
||||
do echo ">>>>>>>>>>>>>>>>>>>>>> `date`"
|
||||
if meson compile -C builddir
|
||||
then
|
||||
./builddir/sfmldemo
|
||||
else
|
||||
echo "^^^^^^^^^^^^^^^^^^^^^ ERROR `date`"
|
||||
fi
|
||||
done
|
||||
255
13-multiple-lights/scripts/windows_setup.ps1
Normal file
255
13-multiple-lights/scripts/windows_setup.ps1
Normal file
|
|
@ -0,0 +1,255 @@
|
|||
function Test-WinUtilPackageManager {
|
||||
<#
|
||||
|
||||
.SYNOPSIS
|
||||
Checks if Winget and/or Choco are installed
|
||||
|
||||
.PARAMETER winget
|
||||
Check if Winget is installed
|
||||
|
||||
.PARAMETER choco
|
||||
Check if Chocolatey is installed
|
||||
|
||||
#>
|
||||
|
||||
Param(
|
||||
[System.Management.Automation.SwitchParameter]$winget,
|
||||
[System.Management.Automation.SwitchParameter]$choco
|
||||
)
|
||||
|
||||
$status = "not-installed"
|
||||
|
||||
if ($winget) {
|
||||
# Check if Winget is available while getting it's Version if it's available
|
||||
$wingetExists = $true
|
||||
try {
|
||||
$wingetVersionFull = winget --version
|
||||
} catch [System.Management.Automation.CommandNotFoundException], [System.Management.Automation.ApplicationFailedException] {
|
||||
Write-Warning "Winget was not found due to un-availablity reasons"
|
||||
$wingetExists = $false
|
||||
} catch {
|
||||
Write-Warning "Winget was not found due to un-known reasons, The Stack Trace is:`n$($psitem.Exception.StackTrace)"
|
||||
$wingetExists = $false
|
||||
}
|
||||
|
||||
# If Winget is available, Parse it's Version and give proper information to Terminal Output.
|
||||
# If it isn't available, the return of this funtion will be "not-installed", indicating that
|
||||
# Winget isn't installed/available on The System.
|
||||
if ($wingetExists) {
|
||||
# Check if Preview Version
|
||||
if ($wingetVersionFull.Contains("-preview")) {
|
||||
$wingetVersion = $wingetVersionFull.Trim("-preview")
|
||||
$wingetPreview = $true
|
||||
} else {
|
||||
$wingetVersion = $wingetVersionFull
|
||||
$wingetPreview = $false
|
||||
}
|
||||
|
||||
# Check if Winget's Version is too old.
|
||||
$wingetCurrentVersion = [System.Version]::Parse($wingetVersion.Trim('v'))
|
||||
# Grabs the latest release of Winget from the Github API for version check process.
|
||||
$response = Invoke-RestMethod -Uri "https://api.github.com/repos/microsoft/Winget-cli/releases/latest" -Method Get -ErrorAction Stop
|
||||
$wingetLatestVersion = [System.Version]::Parse(($response.tag_name).Trim('v')) #Stores version number of latest release.
|
||||
$wingetOutdated = $wingetCurrentVersion -lt $wingetLatestVersion
|
||||
Write-Host "===========================================" -ForegroundColor Green
|
||||
Write-Host "--- Winget is installed ---" -ForegroundColor Green
|
||||
Write-Host "===========================================" -ForegroundColor Green
|
||||
Write-Host "Version: $wingetVersionFull" -ForegroundColor White
|
||||
|
||||
if (!$wingetPreview) {
|
||||
Write-Host " - Winget is a release version." -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host " - Winget is a preview version. Unexpected problems may occur." -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
if (!$wingetOutdated) {
|
||||
Write-Host " - Winget is Up to Date" -ForegroundColor Green
|
||||
$status = "installed"
|
||||
}
|
||||
else {
|
||||
Write-Host " - Winget is Out of Date" -ForegroundColor Red
|
||||
$status = "outdated"
|
||||
}
|
||||
} else {
|
||||
Write-Host "===========================================" -ForegroundColor Red
|
||||
Write-Host "--- Winget is not installed ---" -ForegroundColor Red
|
||||
Write-Host "===========================================" -ForegroundColor Red
|
||||
$status = "not-installed"
|
||||
}
|
||||
}
|
||||
|
||||
if ($choco) {
|
||||
if ((Get-Command -Name choco -ErrorAction Ignore) -and ($chocoVersion = (Get-Item "$env:ChocolateyInstall\choco.exe" -ErrorAction Ignore).VersionInfo.ProductVersion)) {
|
||||
Write-Host "===========================================" -ForegroundColor Green
|
||||
Write-Host "--- Chocolatey is installed ---" -ForegroundColor Green
|
||||
Write-Host "===========================================" -ForegroundColor Green
|
||||
Write-Host "Version: v$chocoVersion" -ForegroundColor White
|
||||
$status = "installed"
|
||||
} else {
|
||||
Write-Host "===========================================" -ForegroundColor Red
|
||||
Write-Host "--- Chocolatey is not installed ---" -ForegroundColor Red
|
||||
Write-Host "===========================================" -ForegroundColor Red
|
||||
$status = "not-installed"
|
||||
}
|
||||
}
|
||||
|
||||
return $status
|
||||
}
|
||||
|
||||
function Get-WinUtilWingetPrerequisites {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Downloads the Winget Prereqs.
|
||||
.DESCRIPTION
|
||||
Downloads Prereqs for Winget. Version numbers are coded as variables and can be updated as uncommonly as Microsoft updates the prereqs.
|
||||
#>
|
||||
|
||||
# I don't know of a way to detect the prereqs automatically, so if someone has a better way of defining these, that would be great.
|
||||
# Microsoft.VCLibs version rarely changes, but for future compatibility I made it a variable.
|
||||
$versionVCLibs = "14.00"
|
||||
$fileVCLibs = "https://aka.ms/Microsoft.VCLibs.x64.${versionVCLibs}.Desktop.appx"
|
||||
# Write-Host "$fileVCLibs"
|
||||
# Microsoft.UI.Xaml version changed recently, so I made the version numbers variables.
|
||||
$versionUIXamlMinor = "2.8"
|
||||
$versionUIXamlPatch = "2.8.6"
|
||||
$fileUIXaml = "https://github.com/microsoft/microsoft-ui-xaml/releases/download/v${versionUIXamlPatch}/Microsoft.UI.Xaml.${versionUIXamlMinor}.x64.appx"
|
||||
# Write-Host "$fileUIXaml"
|
||||
|
||||
Try{
|
||||
Write-Host "Downloading Microsoft.VCLibs Dependency..."
|
||||
Invoke-WebRequest -Uri $fileVCLibs -OutFile $ENV:TEMP\Microsoft.VCLibs.x64.Desktop.appx
|
||||
Write-Host "Downloading Microsoft.UI.Xaml Dependency...`n"
|
||||
Invoke-WebRequest -Uri $fileUIXaml -OutFile $ENV:TEMP\Microsoft.UI.Xaml.x64.appx
|
||||
}
|
||||
Catch{
|
||||
throw [WingetFailedInstall]::new('Failed to install prerequsites')
|
||||
}
|
||||
}
|
||||
|
||||
function Get-WinUtilWingetLatest {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Uses GitHub API to check for the latest release of Winget.
|
||||
.DESCRIPTION
|
||||
This function grabs the latest version of Winget and returns the download path to Install-WinUtilWinget for installation.
|
||||
#>
|
||||
# Invoke-WebRequest is notoriously slow when the byte progress is displayed. The following lines disable the progress bar and reset them at the end of the function
|
||||
$PreviousProgressPreference = $ProgressPreference
|
||||
$ProgressPreference = "silentlyContinue"
|
||||
Try{
|
||||
# Grabs the latest release of Winget from the Github API for the install process.
|
||||
$response = Invoke-RestMethod -Uri "https://api.github.com/repos/microsoft/Winget-cli/releases/latest" -Method Get -ErrorAction Stop
|
||||
$latestVersion = $response.tag_name #Stores version number of latest release.
|
||||
$licenseWingetUrl = $response.assets.browser_download_url | Where-Object {$_ -like "*License1.xml"} #Index value for License file.
|
||||
Write-Host "Latest Version:`t$($latestVersion)`n"
|
||||
Write-Host "Downloading..."
|
||||
$assetUrl = $response.assets.browser_download_url | Where-Object {$_ -like "*Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"}
|
||||
Invoke-WebRequest -Uri $licenseWingetUrl -OutFile $ENV:TEMP\License1.xml
|
||||
# The only pain is that the msixbundle for winget-cli is 246MB. In some situations this can take a bit, with slower connections.
|
||||
Invoke-WebRequest -Uri $assetUrl -OutFile $ENV:TEMP\Microsoft.DesktopAppInstaller.msixbundle
|
||||
}
|
||||
Catch{
|
||||
throw [WingetFailedInstall]::new('Failed to get latest Winget release and license')
|
||||
}
|
||||
$ProgressPreference = $PreviousProgressPreference
|
||||
}
|
||||
|
||||
function Install-WinUtilWinget {
|
||||
<#
|
||||
|
||||
.SYNOPSIS
|
||||
Installs Winget if it is not already installed.
|
||||
|
||||
.DESCRIPTION
|
||||
This function will download the latest version of Winget and install it. If Winget is already installed, it will do nothing.
|
||||
#>
|
||||
$isWingetInstalled = Test-WinUtilPackageManager -winget
|
||||
|
||||
Try {
|
||||
if ($isWingetInstalled -eq "installed") {
|
||||
Write-Host "`nWinget is already installed.`r" -ForegroundColor Green
|
||||
return
|
||||
} elseif ($isWingetInstalled -eq "outdated") {
|
||||
Write-Host "`nWinget is Outdated. Continuing with install.`r" -ForegroundColor Yellow
|
||||
} else {
|
||||
Write-Host "`nWinget is not Installed. Continuing with install.`r" -ForegroundColor Red
|
||||
}
|
||||
|
||||
# Gets the computer's information
|
||||
if ($null -eq $sync.ComputerInfo){
|
||||
$ComputerInfo = Get-ComputerInfo -ErrorAction Stop
|
||||
} else {
|
||||
$ComputerInfo = $sync.ComputerInfo
|
||||
}
|
||||
|
||||
if (($ComputerInfo.WindowsVersion) -lt "1809") {
|
||||
# Checks if Windows Version is too old for Winget
|
||||
Write-Host "Winget is not supported on this version of Windows (Pre-1809)" -ForegroundColor Red
|
||||
return
|
||||
}
|
||||
|
||||
# Install Winget via GitHub method.
|
||||
# Used part of my own script with some modification: ruxunderscore/windows-initialization
|
||||
Write-Host "Downloading Winget Prerequsites`n"
|
||||
Get-WinUtilWingetPrerequisites
|
||||
Write-Host "Downloading Winget and License File`r"
|
||||
Get-WinUtilWingetLatest
|
||||
Write-Host "Installing Winget w/ Prerequsites`r"
|
||||
Add-AppxProvisionedPackage -Online -PackagePath $ENV:TEMP\Microsoft.DesktopAppInstaller.msixbundle -DependencyPackagePath $ENV:TEMP\Microsoft.VCLibs.x64.Desktop.appx, $ENV:TEMP\Microsoft.UI.Xaml.x64.appx -LicensePath $ENV:TEMP\License1.xml
|
||||
Write-Host "Manually adding Winget Sources, from Winget CDN."
|
||||
Add-AppxPackage -Path https://cdn.winget.microsoft.com/cache/source.msix #Seems some installs of Winget don't add the repo source, this should makes sure that it's installed every time.
|
||||
Write-Host "Winget Installed" -ForegroundColor Green
|
||||
Write-Host "Enabling NuGet and Module..."
|
||||
Install-PackageProvider -Name NuGet -Force
|
||||
Install-Module -Name Microsoft.WinGet.Client -Force
|
||||
# Winget only needs a refresh of the environment variables to be used.
|
||||
Write-Output "Refreshing Environment Variables...`n"
|
||||
$ENV:PATH = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
|
||||
} Catch {
|
||||
Write-Host "Failure detected while installing via GitHub method. Continuing with Chocolatey method as fallback." -ForegroundColor Red
|
||||
# In case install fails via GitHub method.
|
||||
Try {
|
||||
# Install Choco if not already present
|
||||
Install-WinUtilChoco
|
||||
Start-Process -Verb runas -FilePath powershell.exe -ArgumentList "choco install winget-cli"
|
||||
Write-Host "Winget Installed" -ForegroundColor Green
|
||||
Write-Output "Refreshing Environment Variables...`n"
|
||||
$ENV:PATH = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
|
||||
} Catch {
|
||||
throw [WingetFailedInstall]::new('Failed to install!')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$isAdmin = [System.Security.Principal.WindowsPrincipal]::new(
|
||||
[System.Security.Principal.WindowsIdentity]::GetCurrent()).
|
||||
IsInRole('Administrators')
|
||||
|
||||
if(-not $isAdmin) {
|
||||
$params = @{
|
||||
FilePath = 'powershell' # or pwsh if Core
|
||||
Verb = 'RunAs'
|
||||
ArgumentList = @(
|
||||
'-ExecutionPolicy ByPass'
|
||||
'-File "{0}"' -f $PSCommandPath
|
||||
)
|
||||
}
|
||||
|
||||
Start-Process -Wait @params
|
||||
Write-Host "Admin stuff done..."
|
||||
} else {
|
||||
Write-Host "In Admin stuff..."
|
||||
Install-WinUtilWinget
|
||||
return
|
||||
}
|
||||
|
||||
Start-Process -NoNewWindow -Wait winget -ArgumentList 'install','chocolatey'
|
||||
Start-Process -NoNewWindow -Wait winget -ArgumentList 'install','Git.Git'
|
||||
Start-Process -NoNewWindow -Wait winget -ArgumentList 'install','Microsoft.WindowsTerminal'
|
||||
Start-Process -NoNewWindow -Wait winget -ArgumentList 'install','Python.Python.3.12'
|
||||
Start-Process -NoNewWindow -Wait winget -ArgumentList 'install','AntibodySoftware.WizFile'
|
||||
Start-Process -NoNewWindow -Wait winget -ArgumentList 'install','Kitware.CMake'
|
||||
Start-Process -NoNewWindow -Wait winget -ArgumentList 'install','Microsoft.VCRedist.2015+.x64'
|
||||
|
||||
Start-Process -Verb RunAs -Wait powershell -argumentlist 'C:\ProgramData\chocolatey\bin\choco.exe','install','geany','geany-plugins','winlibs','conan','meson'
|
||||
43
13-multiple-lights/shaders/08-frag.glsl
Normal file
43
13-multiple-lights/shaders/08-frag.glsl
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#version 330 core
|
||||
struct Material {
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
float shininess;
|
||||
};
|
||||
|
||||
struct Light {
|
||||
vec3 position;
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
};
|
||||
|
||||
uniform Light light;
|
||||
|
||||
uniform Material material;
|
||||
out vec4 FragColor;
|
||||
in vec3 FragPos;
|
||||
in vec3 Normal;
|
||||
|
||||
uniform vec3 viewPos;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 ambient = light.ambient * material.ambient;
|
||||
|
||||
vec3 norm = normalize(Normal);
|
||||
vec3 lightDir = normalize(light.position - FragPos);
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = light.diffuse * (diff * material.diffuse);
|
||||
|
||||
vec3 viewDir = normalize(viewPos - FragPos);
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||
vec3 specular = light.specular * (material.specular * spec);
|
||||
|
||||
vec3 result = ambient + diffuse + specular;
|
||||
|
||||
FragColor = vec4(result, 1.0);
|
||||
}
|
||||
16
13-multiple-lights/shaders/08-lightsource.frag.glsl
Normal file
16
13-multiple-lights/shaders/08-lightsource.frag.glsl
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
struct Light {
|
||||
vec3 position;
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
};
|
||||
|
||||
uniform Light light;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(light.diffuse, 1.0); // set all 4 vector values to 1.0
|
||||
}
|
||||
17
13-multiple-lights/shaders/08-vert.glsl
Normal file
17
13-multiple-lights/shaders/08-vert.glsl
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
|
||||
out vec3 FragPos;
|
||||
out vec3 Normal;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0f);
|
||||
FragPos = vec3(model * vec4(aPos, 1.0));
|
||||
Normal = mat3(transpose(inverse(model))) * aNormal;
|
||||
}
|
||||
46
13-multiple-lights/shaders/11-frag.glsl
Normal file
46
13-multiple-lights/shaders/11-frag.glsl
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#version 330 core
|
||||
struct Material {
|
||||
sampler2D diffuse;
|
||||
sampler2D specular;
|
||||
float shininess;
|
||||
};
|
||||
|
||||
struct Light {
|
||||
vec3 position;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
vec3 ambient;
|
||||
};
|
||||
|
||||
out vec4 FragColor;
|
||||
in vec3 FragPos;
|
||||
in vec3 Normal;
|
||||
in vec2 TexCoords;
|
||||
|
||||
uniform vec3 viewPos;
|
||||
uniform Light light;
|
||||
uniform Material material;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 mat_tex = vec3(texture(material.diffuse, TexCoords));
|
||||
vec3 spec_tex = vec3(texture(material.specular, TexCoords));
|
||||
|
||||
vec3 ambient = light.ambient * mat_tex;
|
||||
|
||||
vec3 norm = normalize(Normal);
|
||||
vec3 lightDir = normalize(light.position - FragPos);
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = light.diffuse * diff * mat_tex;
|
||||
|
||||
vec3 viewDir = normalize(viewPos - FragPos);
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||
|
||||
vec3 specular = light.specular * spec * spec_tex;
|
||||
|
||||
vec3 result = ambient + diffuse + specular;
|
||||
|
||||
FragColor = vec4(result, 1.0);
|
||||
}
|
||||
16
13-multiple-lights/shaders/11-lightsource.frag.glsl
Normal file
16
13-multiple-lights/shaders/11-lightsource.frag.glsl
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
struct Light {
|
||||
vec3 position;
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
};
|
||||
|
||||
uniform Light light;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(light.diffuse, 1.0); // set all 4 vector values to 1.0
|
||||
}
|
||||
20
13-multiple-lights/shaders/11-vert.glsl
Normal file
20
13-multiple-lights/shaders/11-vert.glsl
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
layout (location = 2) in vec2 aTexCoords;
|
||||
|
||||
out vec3 FragPos;
|
||||
out vec3 Normal;
|
||||
out vec2 TexCoords;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0f);
|
||||
FragPos = vec3(model * vec4(aPos, 1.0));
|
||||
Normal = mat3(transpose(inverse(model))) * aNormal;
|
||||
TexCoords = aTexCoords;
|
||||
}
|
||||
67
13-multiple-lights/shaders/12-frag.glsl
Normal file
67
13-multiple-lights/shaders/12-frag.glsl
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#version 330 core
|
||||
struct Material {
|
||||
sampler2D diffuse;
|
||||
sampler2D specular;
|
||||
float shininess;
|
||||
};
|
||||
|
||||
struct Light {
|
||||
vec3 direction;
|
||||
vec3 position;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
vec3 ambient;
|
||||
|
||||
float constant;
|
||||
float linear;
|
||||
float quadratic;
|
||||
float cutOff;
|
||||
float outerCutOff;
|
||||
};
|
||||
|
||||
out vec4 FragColor;
|
||||
in vec3 FragPos;
|
||||
in vec3 Normal;
|
||||
in vec2 TexCoords;
|
||||
|
||||
uniform vec3 viewPos;
|
||||
uniform Light light;
|
||||
uniform Material material;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 lightDir = normalize(light.position - FragPos);
|
||||
|
||||
vec3 mat_tex = vec3(texture(material.diffuse, TexCoords));
|
||||
vec3 spec_tex = vec3(texture(material.specular, TexCoords));
|
||||
vec3 ambient = light.ambient * mat_tex;
|
||||
|
||||
vec3 norm = normalize(Normal);
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = light.diffuse * diff * mat_tex;
|
||||
|
||||
vec3 viewDir = normalize(viewPos - FragPos);
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||
|
||||
vec3 specular = light.specular * spec * spec_tex;
|
||||
|
||||
// splotlight
|
||||
float theta = dot(lightDir, normalize(-light.direction));
|
||||
float epsilon = light.cutOff - light.outerCutOff;
|
||||
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
|
||||
diffuse *= intensity;
|
||||
specular *= intensity;
|
||||
|
||||
// determine attenuation based on light distance
|
||||
float distance = length(light.position - FragPos);
|
||||
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
|
||||
|
||||
ambient *= attenuation;
|
||||
diffuse *= attenuation;
|
||||
specular *= attenuation;
|
||||
|
||||
vec3 result = ambient + diffuse + specular;
|
||||
FragColor = vec4(result, 1.0);
|
||||
}
|
||||
16
13-multiple-lights/shaders/12-lightsource.frag.glsl
Normal file
16
13-multiple-lights/shaders/12-lightsource.frag.glsl
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
struct Light {
|
||||
vec3 position;
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
};
|
||||
|
||||
uniform Light light;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(light.diffuse, 1.0); // set all 4 vector values to 1.0
|
||||
}
|
||||
20
13-multiple-lights/shaders/12-vert.glsl
Normal file
20
13-multiple-lights/shaders/12-vert.glsl
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
layout (location = 2) in vec2 aTexCoords;
|
||||
|
||||
out vec3 FragPos;
|
||||
out vec3 Normal;
|
||||
out vec2 TexCoords;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0f);
|
||||
FragPos = vec3(model * vec4(aPos, 1.0));
|
||||
Normal = mat3(transpose(inverse(model))) * aNormal;
|
||||
TexCoords = aTexCoords;
|
||||
}
|
||||
150
13-multiple-lights/shaders/13-frag.glsl
Normal file
150
13-multiple-lights/shaders/13-frag.glsl
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
#version 330 core
|
||||
struct Material {
|
||||
sampler2D diffuse;
|
||||
sampler2D specular;
|
||||
float shininess;
|
||||
};
|
||||
|
||||
out vec4 FragColor;
|
||||
in vec3 FragPos;
|
||||
in vec3 Normal;
|
||||
in vec2 TexCoords;
|
||||
|
||||
uniform vec3 viewPos;
|
||||
uniform Material material;
|
||||
|
||||
struct DirLight {
|
||||
vec3 direction;
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
};
|
||||
|
||||
struct PointLight {
|
||||
vec3 position;
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
float constant;
|
||||
float linear;
|
||||
float quadratic;
|
||||
};
|
||||
|
||||
struct SpotLight {
|
||||
vec3 direction;
|
||||
vec3 position;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
vec3 ambient;
|
||||
|
||||
float constant;
|
||||
float linear;
|
||||
float quadratic;
|
||||
float cutOff;
|
||||
float outerCutOff;
|
||||
};
|
||||
|
||||
|
||||
#define NR_POINT_LIGHTS 4
|
||||
uniform int pointLightCount;
|
||||
uniform PointLight pointLights[NR_POINT_LIGHTS];
|
||||
uniform DirLight dirLight;
|
||||
uniform SpotLight spotLight;
|
||||
|
||||
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
|
||||
{
|
||||
vec3 mat_tex = vec3(texture(material.diffuse, TexCoords));
|
||||
vec3 spec_tex = vec3(texture(material.specular, TexCoords));
|
||||
|
||||
vec3 lightDir = normalize(-light.direction);
|
||||
// diffuse shading
|
||||
float diff = max(dot(normal, lightDir), 0.0);
|
||||
|
||||
// specular shading
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||
|
||||
vec3 ambient = light.ambient * mat_tex;
|
||||
vec3 diffuse = light.diffuse * diff * mat_tex;
|
||||
vec3 specular = light.specular * spec * spec_tex;
|
||||
|
||||
return ambient + diffuse + specular;
|
||||
}
|
||||
|
||||
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
|
||||
{
|
||||
vec3 mat_tex = vec3(texture(material.diffuse, TexCoords));
|
||||
vec3 spec_tex = vec3(texture(material.specular, TexCoords));
|
||||
vec3 lightDir = normalize(light.position - fragPos);
|
||||
|
||||
// diffuse shading
|
||||
float diff = max(dot(normal, lightDir), 0.0);
|
||||
|
||||
// specular shading
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||
|
||||
// attenuation
|
||||
float distance = length(light.position - fragPos);
|
||||
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
|
||||
|
||||
vec3 ambient = light.ambient * mat_tex;
|
||||
vec3 diffuse = light.diffuse * diff * mat_tex;
|
||||
vec3 specular = light.specular * spec * spec_tex;
|
||||
|
||||
ambient *= attenuation;
|
||||
diffuse *= attenuation;
|
||||
specular *= attenuation;
|
||||
|
||||
return ambient + diffuse + specular;
|
||||
}
|
||||
|
||||
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
|
||||
{
|
||||
vec3 mat_tex = vec3(texture(material.diffuse, TexCoords));
|
||||
vec3 spec_tex = vec3(texture(material.specular, TexCoords));
|
||||
vec3 lightDir = normalize(light.position - fragPos);
|
||||
|
||||
// diffuse shading
|
||||
float diff = max(dot(normal, lightDir), 0.0);
|
||||
|
||||
// specular shading
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||
|
||||
// attenuation
|
||||
float distance = length(light.position - fragPos);
|
||||
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
|
||||
|
||||
vec3 ambient = light.ambient * mat_tex;
|
||||
vec3 diffuse = light.diffuse * diff * mat_tex;
|
||||
vec3 specular = light.specular * spec * spec_tex;
|
||||
|
||||
float theta = dot(lightDir, normalize(-light.direction));
|
||||
float epsilon = light.cutOff - light.outerCutOff;
|
||||
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
|
||||
diffuse *= intensity;
|
||||
specular *= intensity;
|
||||
|
||||
ambient *= attenuation;
|
||||
diffuse *= attenuation;
|
||||
specular *= attenuation;
|
||||
|
||||
return ambient + diffuse + specular;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 norm = normalize(Normal);
|
||||
vec3 viewDir = normalize(viewPos - FragPos);
|
||||
|
||||
vec3 result = CalcDirLight(dirLight, norm, viewDir);
|
||||
|
||||
for(int i = 0; i < pointLightCount; i++) {
|
||||
result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);
|
||||
}
|
||||
|
||||
result += CalcSpotLight(spotLight, norm, FragPos, viewDir);
|
||||
|
||||
FragColor = vec4(result, 1.0);
|
||||
}
|
||||
9
13-multiple-lights/shaders/13-lightsource.frag.glsl
Normal file
9
13-multiple-lights/shaders/13-lightsource.frag.glsl
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 diffuse;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(diffuse, 1.0); // set all 4 vector values to 1.0
|
||||
}
|
||||
20
13-multiple-lights/shaders/13-vert.glsl
Normal file
20
13-multiple-lights/shaders/13-vert.glsl
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
layout (location = 2) in vec2 aTexCoords;
|
||||
|
||||
out vec3 FragPos;
|
||||
out vec3 Normal;
|
||||
out vec2 TexCoords;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0f);
|
||||
FragPos = vec3(model * vec4(aPos, 1.0));
|
||||
Normal = mat3(transpose(inverse(model))) * aNormal;
|
||||
TexCoords = aTexCoords;
|
||||
}
|
||||
8
13-multiple-lights/shaders/3.3.shader.fs
Normal file
8
13-multiple-lights/shaders/3.3.shader.fs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
in vec3 ourColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(ourColor, 1.0);
|
||||
}
|
||||
11
13-multiple-lights/shaders/3.3.shader.vs
Normal file
11
13-multiple-lights/shaders/3.3.shader.vs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos; // the position variable has attribute position 0
|
||||
layout (location = 1) in vec3 aColor;
|
||||
|
||||
out vec3 ourColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(aPos, 1.0); // see how we directly give a vec3 to vec4's constructor
|
||||
ourColor = aColor;
|
||||
}
|
||||
11
13-multiple-lights/shaders/4.2.texture.fs
Normal file
11
13-multiple-lights/shaders/4.2.texture.fs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
in vec2 TexCoord;
|
||||
|
||||
uniform sampler2D texture1;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = texture(texture1, TexCoord);
|
||||
}
|
||||
15
13-multiple-lights/shaders/4.2.texture.vs
Normal file
15
13-multiple-lights/shaders/4.2.texture.vs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec2 aTexCoord;
|
||||
|
||||
out vec2 TexCoord;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0f);
|
||||
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
|
||||
}
|
||||
63
13-multiple-lights/src/camera.cpp
Normal file
63
13-multiple-lights/src/camera.cpp
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
#include "camera.hpp"
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
glm::mat4 Camera::lookAt() {
|
||||
return glm::lookAt(pos, pos + front, up);
|
||||
}
|
||||
|
||||
void Camera::forward() {
|
||||
pos += speed * front;
|
||||
}
|
||||
|
||||
void Camera::back() {
|
||||
pos -= speed * front;
|
||||
}
|
||||
|
||||
void Camera::left() {
|
||||
pos -= glm::normalize(glm::cross(front, up)) * speed;
|
||||
}
|
||||
|
||||
void Camera::right() {
|
||||
pos += glm::normalize(glm::cross(front, up)) * speed;
|
||||
}
|
||||
|
||||
void Camera::update(float deltaTime) {
|
||||
speed = movementSpeed * deltaTime;
|
||||
}
|
||||
|
||||
void Camera::mouse_move(double xpos, double ypos) {
|
||||
if(firstMouse) {
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
firstMouse = false;
|
||||
}
|
||||
|
||||
float xoffset = xpos - lastX;
|
||||
float yoffset = lastY - ypos;
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
const float sensitivity = 0.1f;
|
||||
xoffset *= sensitivity;
|
||||
yoffset *= sensitivity;
|
||||
|
||||
yaw += xoffset;
|
||||
pitch += yoffset;
|
||||
|
||||
if(pitch > 89.0f) pitch = 89.0f;
|
||||
if(pitch < -89.0f) pitch = -89.0f;
|
||||
|
||||
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
direction.y = sin(glm::radians(pitch));
|
||||
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
|
||||
front = glm::normalize(direction);
|
||||
}
|
||||
|
||||
void Camera::mouse_scroll(double xoffset, double yoffset) {
|
||||
fov -= (float)yoffset;
|
||||
|
||||
if(fov < 1.0f) fov = 1.0f;
|
||||
if(fov > 90.0f) fov = 90.0f;
|
||||
}
|
||||
27
13-multiple-lights/src/camera.hpp
Normal file
27
13-multiple-lights/src/camera.hpp
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
struct Camera {
|
||||
glm::vec3 pos = glm::vec3(0.0f, 0.0f, 3.0f);
|
||||
glm::vec3 front = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||
glm::vec3 direction = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
float movementSpeed = 20.0f;
|
||||
|
||||
float pitch = 0.0f;
|
||||
float yaw = -90.0f;
|
||||
float speed = 0.05f;
|
||||
float lastX = 400;
|
||||
float lastY = 300;
|
||||
float fov = 45.0f;
|
||||
bool firstMouse = true;
|
||||
|
||||
glm::mat4 lookAt();
|
||||
void forward();
|
||||
void back();
|
||||
void left();
|
||||
void right();
|
||||
void update(float deltaTime);
|
||||
void mouse_move(double xpos, double ypos);
|
||||
void mouse_scroll(double xoffset, double yoffset);
|
||||
};
|
||||
63
13-multiple-lights/src/data.hpp
Normal file
63
13-multiple-lights/src/data.hpp
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#define CUBE_COUNT 10
|
||||
|
||||
float vertices[] = {
|
||||
// positions // normals // texture coords
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
|
||||
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
|
||||
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
|
||||
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
|
||||
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
|
||||
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
|
||||
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
|
||||
};
|
||||
|
||||
// positions all containers
|
||||
glm::vec3 cube_positions[] = {
|
||||
glm::vec3( 0.0f, 0.0f, 0.0f),
|
||||
glm::vec3( 2.0f, 5.0f, -15.0f),
|
||||
glm::vec3(-1.5f, -2.2f, -2.5f),
|
||||
glm::vec3(-3.8f, -2.0f, -12.3f),
|
||||
glm::vec3( 2.4f, -0.4f, -3.5f),
|
||||
glm::vec3(-1.7f, 3.0f, -7.5f),
|
||||
glm::vec3( 1.3f, -2.0f, -2.5f),
|
||||
glm::vec3( 1.5f, 2.0f, -2.5f),
|
||||
glm::vec3( 1.5f, 0.2f, -1.5f),
|
||||
glm::vec3(-1.3f, 1.0f, -1.5f)
|
||||
};
|
||||
47
13-multiple-lights/src/dbc.cpp
Normal file
47
13-multiple-lights/src/dbc.cpp
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#include "dbc.hpp"
|
||||
#include <iostream>
|
||||
|
||||
void dbc::log(const string &message, const std::source_location location) {
|
||||
std::cout << '[' << location.file_name() << ':'
|
||||
<< location.line() << "|"
|
||||
<< location.function_name() << "] "
|
||||
<< message << std::endl;
|
||||
}
|
||||
|
||||
void dbc::sentinel(const string &message, const std::source_location location) {
|
||||
string err = $F("[SENTINEL!] {}", message);
|
||||
dbc::log(err, location);
|
||||
throw dbc::SentinelError(err);
|
||||
}
|
||||
|
||||
void dbc::pre(const string &message, bool test, const std::source_location location) {
|
||||
if(!test) {
|
||||
string err = $F("[PRE!] {}", message);
|
||||
dbc::log(err, location);
|
||||
throw dbc::PreCondError(err);
|
||||
}
|
||||
}
|
||||
|
||||
void dbc::pre(const string &message, std::function<bool()> tester, const std::source_location location) {
|
||||
dbc::pre(message, tester(), location);
|
||||
}
|
||||
|
||||
void dbc::post(const string &message, bool test, const std::source_location location) {
|
||||
if(!test) {
|
||||
string err = $F("[POST!] {}", message);
|
||||
dbc::log(err, location);
|
||||
throw dbc::PostCondError(err);
|
||||
}
|
||||
}
|
||||
|
||||
void dbc::post(const string &message, std::function<bool()> tester, const std::source_location location) {
|
||||
dbc::post(message, tester(), location);
|
||||
}
|
||||
|
||||
void dbc::check(bool test, const string &message, const std::source_location location) {
|
||||
if(!test) {
|
||||
string err = $F("[CHECK!] {}\n", message);
|
||||
dbc::log(err, location);
|
||||
throw dbc::CheckError(err);
|
||||
}
|
||||
}
|
||||
46
13-multiple-lights/src/dbc.hpp
Normal file
46
13-multiple-lights/src/dbc.hpp
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <fmt/core.h>
|
||||
#include <functional>
|
||||
#include <source_location>
|
||||
|
||||
// AKA the Fuckit macro
|
||||
#define $F(FMT, ...) fmt::format(FMT, ##__VA_ARGS__)
|
||||
|
||||
namespace dbc {
|
||||
using std::string;
|
||||
|
||||
using CheckError = std::runtime_error;
|
||||
using SentinelError = std::runtime_error;
|
||||
using PreCondError = std::runtime_error;
|
||||
using PostCondError = std::runtime_error;
|
||||
|
||||
void log(const string &message,
|
||||
const std::source_location location =
|
||||
std::source_location::current());
|
||||
|
||||
[[noreturn]] void sentinel(const string &message,
|
||||
const std::source_location location =
|
||||
std::source_location::current());
|
||||
|
||||
void pre(const string &message, bool test,
|
||||
const std::source_location location =
|
||||
std::source_location::current());
|
||||
|
||||
void pre(const string &message, std::function<bool()> tester,
|
||||
const std::source_location location =
|
||||
std::source_location::current());
|
||||
|
||||
void post(const string &message, bool test,
|
||||
const std::source_location location =
|
||||
std::source_location::current());
|
||||
|
||||
void post(const string &message, std::function<bool()> tester,
|
||||
const std::source_location location =
|
||||
std::source_location::current());
|
||||
|
||||
void check(bool test, const string &message,
|
||||
const std::source_location location =
|
||||
std::source_location::current());
|
||||
}
|
||||
362
13-multiple-lights/src/main.cpp
Normal file
362
13-multiple-lights/src/main.cpp
Normal file
|
|
@ -0,0 +1,362 @@
|
|||
#define _USE_MATH_DEFINES
|
||||
#include <math.h>
|
||||
#include "dbc.hpp"
|
||||
#include <print>
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include "shader.hpp"
|
||||
#include <stb_image.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include "data.hpp"
|
||||
#include "model.hpp"
|
||||
#include "camera.hpp"
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
||||
|
||||
const unsigned int SCR_WIDTH = 800;
|
||||
const unsigned int SCR_HEIGHT = 600;
|
||||
|
||||
const float AMBIENT_LEVEL = 0.25f;
|
||||
|
||||
struct Config {
|
||||
Shader shader;
|
||||
Shader lightShader;
|
||||
|
||||
Lighting light{
|
||||
.directional={
|
||||
.direction={-0.2f, -1.0f, -0.3f},
|
||||
.ambient={0.2f, 0.2f, 0.2f},
|
||||
.diffuse={0.8f, 0.8f, 0.8f},
|
||||
.specular={1.0f, 1.0f, 1.0f},
|
||||
},
|
||||
.positioned={
|
||||
{
|
||||
.position={1.2f, 1.0f, 2.0f},
|
||||
.direction={-0.2f, -1.0f, -0.3f},
|
||||
.ambient={0.2f, 0.2f, 0.2f},
|
||||
.diffuse={0.8f, 0.8f, 0.8f},
|
||||
.specular={1.0f, 1.0f, 1.0f},
|
||||
.constant=1.0f,
|
||||
.linear=0.09f,
|
||||
.quadratic=0.032f,
|
||||
.cutOff=12.5f,
|
||||
.outerCutOff=17.5f,
|
||||
},
|
||||
{
|
||||
.position={-1.2f, -1.0f, -2.0f},
|
||||
.direction={0.2f, 1.0f, 0.3f},
|
||||
.ambient={0.2f, 0.2f, 0.2f},
|
||||
.diffuse={0.8f, 0.8f, 0.8f},
|
||||
.specular={1.0f, 1.0f, 1.0f},
|
||||
.constant=1.0f,
|
||||
.linear=0.09f,
|
||||
.quadratic=0.032f,
|
||||
.cutOff=12.5f,
|
||||
.outerCutOff=17.5f,
|
||||
},
|
||||
},
|
||||
.spot={
|
||||
.position={2.2f, 1.0f, 2.0f},
|
||||
.direction={0.0f, 0.0f, -1.0f},
|
||||
.ambient={0.2f, 0.2f, 0.2f},
|
||||
.diffuse={0.8f, 0.8f, 0.8f},
|
||||
.specular={1.0f, 1.0f, 1.0f},
|
||||
.constant=1.0f,
|
||||
.linear=0.09f,
|
||||
.quadratic=0.032f,
|
||||
.cutOff=12.5f,
|
||||
.outerCutOff=17.5f,
|
||||
},
|
||||
};
|
||||
|
||||
glm::vec3 cubePos{2.0f, 0.0f, 0.0f};
|
||||
Camera camera{
|
||||
.pos={2.2f, 1.0f, 2.0f},
|
||||
.movementSpeed=2.0f
|
||||
};
|
||||
|
||||
Material cubeMaterial{
|
||||
.ambient = {1.0f, 0.5f, 0.31f},
|
||||
.shininess = 32.0f,
|
||||
.diffuseMap = 0,
|
||||
.specularMap = 0,
|
||||
};
|
||||
|
||||
unsigned int cubeVAO = 0;
|
||||
unsigned int lightCubeVAO = 0;
|
||||
unsigned int VBO = 0;
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
|
||||
void updateDelta() {
|
||||
float currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
}
|
||||
};
|
||||
|
||||
void init_glfw() {
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
}
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow *, int width, int height) {
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void mouse_callback(GLFWwindow *window, double xpos, double ypos) {
|
||||
Camera* camera = static_cast<Camera*>(glfwGetWindowUserPointer(window));
|
||||
camera->mouse_move(xpos, ypos);
|
||||
}
|
||||
|
||||
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) {
|
||||
Camera* camera = static_cast<Camera*>(glfwGetWindowUserPointer(window));
|
||||
camera->mouse_scroll(xoffset, yoffset);
|
||||
}
|
||||
|
||||
GLFWwindow* create_window() {
|
||||
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
||||
dbc::check(window != NULL, "failed to open window");
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
|
||||
auto good = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
||||
dbc::check(good, "failed to load GLAD");
|
||||
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
|
||||
return window;
|
||||
}
|
||||
|
||||
|
||||
void processInput(GLFWwindow *window, Config& config) {
|
||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
||||
config.camera.forward();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
||||
config.camera.back();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
||||
config.camera.left();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
||||
config.camera.right();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
|
||||
config.light.directional.adjust(-0.01f);
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
|
||||
config.light.directional.adjust(0.01f);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int load_texture(const std::string& image_file) {
|
||||
unsigned int texture_id;
|
||||
glGenTextures(1, &texture_id);
|
||||
glBindTexture(GL_TEXTURE_2D, texture_id);
|
||||
|
||||
// set the texture wrapping parameters
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
|
||||
// set texture filtering parameters
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
int nrChannels = 0;
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
|
||||
unsigned char *data = stbi_load(image_file.c_str(), &width, &height, &nrChannels, 0);
|
||||
|
||||
dbc::check(data != nullptr, std::format("Failed to load texture: {}", image_file));
|
||||
|
||||
dbc::check(nrChannels == 3 || nrChannels == 4,
|
||||
std::format("Image {} has invalid channels {} should be 3 or 4.", image_file, nrChannels));
|
||||
|
||||
auto rgb_or_a = nrChannels == 3 ? GL_RGB : GL_RGBA;
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, rgb_or_a, width, height, 0, rgb_or_a, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
stbi_image_free(data);
|
||||
|
||||
return texture_id;
|
||||
}
|
||||
|
||||
Config setup() {
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
Config config{
|
||||
.shader={"shaders/13-vert.glsl", "shaders/13-frag.glsl"},
|
||||
.lightShader={"shaders/13-vert.glsl", "shaders/13-lightsource.frag.glsl"},
|
||||
};
|
||||
|
||||
unsigned int VBO = 0;
|
||||
unsigned int lightCubeVAO = 0;
|
||||
unsigned int cubeVAO = 0;
|
||||
|
||||
// cube vao configure, this is the light target
|
||||
glGenVertexArrays(1, &cubeVAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
glBindVertexArray(cubeVAO);
|
||||
|
||||
// position attribute
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
// light vao configure, this is the light source
|
||||
glGenVertexArrays(1, &lightCubeVAO);
|
||||
glBindVertexArray(lightCubeVAO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
config.lightCubeVAO = lightCubeVAO;
|
||||
config.cubeVAO = cubeVAO;
|
||||
config.VBO = VBO;
|
||||
|
||||
config.cubeMaterial.diffuseMap = load_texture("resources/textures/container2.png");
|
||||
config.cubeMaterial.specularMap = load_texture("resources/textures/container2_specular.png");
|
||||
|
||||
config.shader.use();
|
||||
config.shader.setInt("material.diffuse", 0);
|
||||
config.shader.setInt("material.specular", 1);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
void draw_cube(size_t pos, Config& config, glm::mat4& projection, glm::mat4& view) {
|
||||
config.shader.setMat4("view", view);
|
||||
config.shader.setMat4("projection", projection);
|
||||
config.shader.setVec3("viewPos", config.camera.pos);
|
||||
|
||||
config.shader.applyMaterial(config.cubeMaterial);
|
||||
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, cube_positions[pos]);
|
||||
|
||||
float angle = glfwGetTime() * (float)pos;
|
||||
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
||||
|
||||
config.shader.setMat4("model", model);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, config.cubeMaterial.diffuseMap);
|
||||
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, config.cubeMaterial.specularMap);
|
||||
|
||||
glBindVertexArray(config.cubeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
||||
|
||||
void draw_light(Config& config, Light& light, glm::mat4& projection, glm::mat4& view) {
|
||||
config.lightShader.use();
|
||||
|
||||
config.lightShader.setVec3("diffuse", light.diffuse);
|
||||
config.lightShader.setMat4("projection", projection);
|
||||
config.lightShader.setMat4("view", view);
|
||||
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, light.position);
|
||||
model = glm::scale(model, glm::vec3(0.2f));
|
||||
config.lightShader.setMat4("model", model);
|
||||
|
||||
glBindVertexArray(config.lightCubeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
||||
|
||||
void render(GLFWwindow* window, Config& config) {
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
config.shader.use();
|
||||
|
||||
// time is used for fake 3d rotation
|
||||
float time = glfwGetTime();
|
||||
|
||||
config.camera.update(config.deltaTime);
|
||||
|
||||
glm::mat4 view = config.camera.lookAt();
|
||||
glm::mat4 projection = glm::mat4(1.0f);
|
||||
|
||||
// fov, aspect, near plane, far plane
|
||||
projection = glm::perspective(glm::radians(config.camera.fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||
|
||||
config.shader.use();
|
||||
config.light.spot.position = config.camera.pos;
|
||||
config.light.spot.direction = config.camera.front;
|
||||
config.shader.applyLighting(config.light);
|
||||
|
||||
for(size_t i = 0; i < CUBE_COUNT; i++) {
|
||||
draw_cube(i, config, projection, view);
|
||||
}
|
||||
|
||||
for(auto& light : config.light.positioned) {
|
||||
draw_light(config, light, projection, view);
|
||||
}
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
void cleanup(Config& config) {
|
||||
glDeleteVertexArrays(1, &config.lightCubeVAO);
|
||||
glDeleteBuffers(1, &config.lightCubeVAO);
|
||||
glDeleteBuffers(1, &config.VBO);
|
||||
config.shader.cleanup();
|
||||
}
|
||||
|
||||
int main() {
|
||||
init_glfw();
|
||||
|
||||
auto window = create_window();
|
||||
auto config = setup();
|
||||
|
||||
glfwSetWindowUserPointer(window, &config.camera);
|
||||
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
processInput(window, config);
|
||||
render(window, config);
|
||||
config.updateDelta();
|
||||
}
|
||||
|
||||
cleanup(config);
|
||||
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
57
13-multiple-lights/src/mesh.cpp
Normal file
57
13-multiple-lights/src/mesh.cpp
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#include "mesh.hpp"
|
||||
|
||||
void Mesh::Draw(Shader &shader) {
|
||||
unsigned int diffuseNr = 1;
|
||||
unsigned int specularNr = 1;
|
||||
unsigned int normalNr = 1;
|
||||
unsigned int heightNr = 1;
|
||||
|
||||
for(unsigned int i = 0; i < textures.size(); i++) {
|
||||
glActiveTexture(GL_TEXTURE0 + i);
|
||||
std::string number;
|
||||
std::string name = textures[i].type;
|
||||
|
||||
if(name == "texture_diffuse") {
|
||||
number = std::to_string(diffuseNr++);
|
||||
} else if(name == "texture_specular") {
|
||||
number = std::to_string(specularNr++);
|
||||
} else if(name == "texture_normal") {
|
||||
number = std::to_string(normalNr++);
|
||||
} else if(name == "texture_height") {
|
||||
number = std::to_string(heightNr++);
|
||||
}
|
||||
|
||||
// TODO: Get this uniform ID once and cache it?
|
||||
glUniform1i(glGetUniformLocation(shader.ID, (name + number).c_str()), i);
|
||||
glBindTexture(GL_TEXTURE_2D, textures[i].id);
|
||||
}
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(indices.size()), GL_UNSIGNED_INT, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
}
|
||||
|
||||
|
||||
void Mesh::setupMesh() {
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glGenBuffers(1, &EBO);
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Position));
|
||||
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
|
||||
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
|
||||
}
|
||||
42
13-multiple-lights/src/mesh.hpp
Normal file
42
13-multiple-lights/src/mesh.hpp
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#pragma once
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
#include "shader.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct Vertex {
|
||||
glm::vec3 Position;
|
||||
glm::vec3 Normal;
|
||||
glm::vec2 TexCoords;
|
||||
};
|
||||
|
||||
struct Texture {
|
||||
unsigned int id;
|
||||
std::string type;
|
||||
std::string path;
|
||||
};
|
||||
|
||||
struct Mesh {
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
std::vector<Texture> textures;
|
||||
unsigned int VAO;
|
||||
unsigned int VBO;
|
||||
unsigned int EBO;
|
||||
|
||||
Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices, std::vector<Texture> textures) :
|
||||
vertices(vertices),
|
||||
indices(indices),
|
||||
textures(textures)
|
||||
{
|
||||
setupMesh();
|
||||
}
|
||||
|
||||
void Draw(Shader &shader);
|
||||
void setupMesh();
|
||||
};
|
||||
171
13-multiple-lights/src/model.cpp
Normal file
171
13-multiple-lights/src/model.cpp
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
#include "model.hpp"
|
||||
#include "dbc.hpp"
|
||||
|
||||
unsigned int TextureFromFile(const std::string& path, const std::string& directory, bool gamma)
|
||||
{
|
||||
std::string filename = path + "/" + filename;
|
||||
|
||||
unsigned int textureID;
|
||||
glGenTextures(1, &textureID);
|
||||
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
int nrComponents = 0;
|
||||
|
||||
unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 0);
|
||||
dbc::check(data != nullptr, $F("Failed to load texture {}", filename));
|
||||
|
||||
GLenum format = GL_RGB;
|
||||
|
||||
if (nrComponents == 1) {
|
||||
format = GL_RED;
|
||||
} else if (nrComponents == 3) {
|
||||
format = GL_RGB;
|
||||
} else if (nrComponents == 4) {
|
||||
format = GL_RGBA;
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
stbi_image_free(data);
|
||||
|
||||
return textureID;
|
||||
}
|
||||
|
||||
void Model::Draw(Shader &shader) {
|
||||
for(unsigned int i = 0; i < meshes.size(); i++) {
|
||||
meshes[i].Draw(shader);
|
||||
}
|
||||
}
|
||||
|
||||
void Model::loadModel(std::string path) {
|
||||
Assimp::Importer importer;
|
||||
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
|
||||
|
||||
dbc::check(scene != nullptr, "Assimp ReadFile return null");
|
||||
dbc::check(!(scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE), "Assimp says incomplete.");
|
||||
dbc::check(scene->mRootNode != nullptr, "Assimp loaded scene doesn't have a root node");
|
||||
|
||||
directory = path.substr(0, path.find_last_of('/'));
|
||||
|
||||
processNode(scene->mRootNode, scene);
|
||||
}
|
||||
|
||||
void Model::processNode(aiNode *node, const aiScene *scene) {
|
||||
for(unsigned int i = 0; i < node->mNumMeshes; i++) {
|
||||
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
|
||||
meshes.push_back(processMesh(mesh, scene));
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < node->mNumChildren; i++) {
|
||||
processNode(node->mChildren[i], scene);
|
||||
}
|
||||
}
|
||||
|
||||
Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene) {
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
std::vector<Texture> textures;
|
||||
|
||||
for(unsigned int i = 0; i < mesh->mNumVertices; i++) {
|
||||
Vertex vertex;
|
||||
vertex.Position = glm::vec3(
|
||||
mesh->mVertices[i].x,
|
||||
mesh->mVertices[i].y,
|
||||
mesh->mVertices[i].z);
|
||||
|
||||
if(mesh->HasNormals()) {
|
||||
vertex.Normal = glm::vec3(
|
||||
mesh->mNormals[i].x,
|
||||
mesh->mNormals[i].y,
|
||||
mesh->mNormals[i].z);
|
||||
}
|
||||
|
||||
if(mesh->mTextureCoords[0]) {
|
||||
vertex.TexCoords = glm::vec2(
|
||||
mesh->mTextureCoords[0][i].x,
|
||||
mesh->mTextureCoords[0][i].y
|
||||
);
|
||||
} else {
|
||||
vertex.TexCoords = glm::vec2(0.0f, 0.0f);
|
||||
}
|
||||
|
||||
vertices.push_back(vertex);
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < mesh->mNumFaces; i++) {
|
||||
aiFace face = mesh->mFaces[i];
|
||||
|
||||
for(unsigned int j = 0; j < face.mNumIndices; j++) {
|
||||
indices.push_back(face.mIndices[j]);
|
||||
}
|
||||
}
|
||||
|
||||
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
|
||||
|
||||
// we assume a convention for sampler names in the shaders. Each diffuse texture should be named
|
||||
// as 'texture_diffuseN' where N is a sequential number ranging from 1 to MAX_SAMPLER_NUMBER.
|
||||
// Same applies to other texture as the following list summarizes:
|
||||
// diffuse: texture_diffuseN
|
||||
// specular: texture_specularN
|
||||
// normal: texture_normalN
|
||||
|
||||
// 1. diffuse maps
|
||||
std::vector<Texture> diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse");
|
||||
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
|
||||
|
||||
// 2. specular maps
|
||||
std::vector<Texture> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular");
|
||||
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
|
||||
|
||||
// 3. normal maps
|
||||
std::vector<Texture> normalMaps = loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_normal");
|
||||
textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
|
||||
|
||||
// 4. height maps
|
||||
std::vector<Texture> heightMaps = loadMaterialTextures(material, aiTextureType_AMBIENT, "texture_height");
|
||||
textures.insert(textures.end(), heightMaps.begin(), heightMaps.end());
|
||||
|
||||
return Mesh(vertices, indices, textures);
|
||||
}
|
||||
|
||||
std::vector<Texture> Model::loadMaterialTextures(aiMaterial *mat, aiTextureType type, std::string typeName)
|
||||
{
|
||||
fmt::println("TEXTURE COUNT: {}, {}, {}", (int)type, typeName, mat->GetTextureCount(type));
|
||||
|
||||
std::vector<Texture> textures;
|
||||
for(unsigned int i = 0; i < mat->GetTextureCount(type); i++) {
|
||||
aiString str;
|
||||
mat->GetTexture(type, i, &str);
|
||||
std::string tx_str{str.C_Str()};
|
||||
|
||||
bool skip = false;
|
||||
for(unsigned int j = 0; j < textures_loaded.size(); j++) {
|
||||
if(textures_loaded[j].path == tx_str) {
|
||||
textures.push_back(textures_loaded[j]);
|
||||
skip = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!skip) {
|
||||
Texture texture{
|
||||
.id = TextureFromFile(tx_str, directory),
|
||||
.type = typeName,
|
||||
.path = tx_str,
|
||||
};
|
||||
|
||||
textures.push_back(texture);
|
||||
textures_loaded.push_back(texture);
|
||||
}
|
||||
}
|
||||
|
||||
return textures;
|
||||
}
|
||||
40
13-multiple-lights/src/model.hpp
Normal file
40
13-multiple-lights/src/model.hpp
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <stb_image.h>
|
||||
#include <assimp/Importer.hpp>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
|
||||
#include <mesh.hpp>
|
||||
#include <shader.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
unsigned int TextureFromFile(const std::string& path, const std::string& directory, bool gamma = false);
|
||||
|
||||
struct Model {
|
||||
std::vector<Texture> textures_loaded;
|
||||
std::vector<Mesh> meshes;
|
||||
std::string directory;
|
||||
bool gammaCorrection;
|
||||
|
||||
Model(const char *path) {
|
||||
loadModel(path);
|
||||
}
|
||||
|
||||
void Draw(Shader &shader);
|
||||
|
||||
void loadModel(std::string path);
|
||||
void processNode(aiNode *node, const aiScene *scene);
|
||||
Mesh processMesh(aiMesh *mesh, const aiScene *scene);
|
||||
|
||||
std::vector<Texture> loadMaterialTextures(aiMaterial *mat, aiTextureType type, std::string typeName);
|
||||
};
|
||||
49
13-multiple-lights/src/physics.cpp
Normal file
49
13-multiple-lights/src/physics.cpp
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#include "physics.hpp"
|
||||
|
||||
struct BoxTest Box2d_setup(b2World &world) {
|
||||
BoxTest box;
|
||||
float wall_x[4] = {0.0f, 0.0f, -0.99f, 0.99f};
|
||||
float wall_y[4] = {-0.99, 0.99f, 0.0f, 0.0f};
|
||||
float bound_w[4] = {1.0f, 1.0f, 0.1f, 0.1f};
|
||||
float bound_h[4] = {0.1f, 0.1f, 1.0f, 1.0f};
|
||||
|
||||
for(size_t i = 0; i < 4; i++) {
|
||||
b2BodyDef groundBodyDef;
|
||||
groundBodyDef.position.Set(wall_x[i], wall_y[i]);
|
||||
b2Body *groundBody = world.CreateBody(&groundBodyDef);
|
||||
|
||||
b2PolygonShape groundBox;
|
||||
groundBox.SetAsBox(bound_w[i], bound_h[i]);
|
||||
groundBody->CreateFixture(&groundBox, 1.0f);
|
||||
|
||||
box.walls[i] = groundBody;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < BODY_COUNT; i++) {
|
||||
b2BodyDef bodyDef;
|
||||
bodyDef.type = b2_dynamicBody;
|
||||
bodyDef.position.Set(0.0f, 0.5f);
|
||||
box.bodies[i] = world.CreateBody(&bodyDef);
|
||||
|
||||
b2PolygonShape dynamicBox;
|
||||
dynamicBox.SetAsBox(0.1f, 0.1f);
|
||||
b2FixtureDef fixtureDef;
|
||||
fixtureDef.shape = &dynamicBox;
|
||||
|
||||
fixtureDef.density = 1.0f;
|
||||
fixtureDef.friction = 0.3f;
|
||||
|
||||
box.bodies[i]->CreateFixture(&fixtureDef);
|
||||
}
|
||||
|
||||
return box;
|
||||
}
|
||||
|
||||
void Box2d_step_world(b2World& world) {
|
||||
float timeStep = 1.0f / 60.0f;
|
||||
int velocityIterations = 6;
|
||||
int positionIterations = 2;
|
||||
|
||||
// step the world
|
||||
world.Step(timeStep, velocityIterations, positionIterations);
|
||||
}
|
||||
13
13-multiple-lights/src/physics.hpp
Normal file
13
13-multiple-lights/src/physics.hpp
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
#include <box2d/box2d.h>
|
||||
|
||||
#define BODY_COUNT 1
|
||||
|
||||
struct BoxTest {
|
||||
b2Body *walls[4];
|
||||
b2Body *bodies[BODY_COUNT];
|
||||
};
|
||||
|
||||
struct BoxTest Box2d_setup(b2World &world);
|
||||
void Box2d_step_world(b2World& world);
|
||||
126
13-multiple-lights/src/shader.cpp
Normal file
126
13-multiple-lights/src/shader.cpp
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
#include "shader.hpp"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <print>
|
||||
#include <filesystem>
|
||||
#include "dbc.hpp"
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
inline std::string read_file(const std::string& filename) {
|
||||
// load the file
|
||||
std::ifstream in_file{filename, std::ios::binary};
|
||||
|
||||
// get the size of the file
|
||||
std::stringstream in_str;
|
||||
in_str << in_file.rdbuf();
|
||||
return in_str.str();
|
||||
}
|
||||
|
||||
void check_error(const std::string& what, unsigned int thing, GLenum check_type) {
|
||||
int success = 0;
|
||||
char infoLog[512] = {0};
|
||||
|
||||
if(check_type == GL_LINK_STATUS) {
|
||||
glGetProgramiv(thing, check_type, &success);
|
||||
|
||||
if(!success) {
|
||||
glGetProgramInfoLog(thing, 512, NULL, infoLog);
|
||||
dbc::sentinel(std::format("ERROR: Program {} compile failed: {}", what, infoLog));
|
||||
}
|
||||
} else {
|
||||
glGetShaderiv(thing, check_type, &success);
|
||||
|
||||
if(!success) {
|
||||
glGetShaderInfoLog(thing, 512, NULL, infoLog);
|
||||
dbc::sentinel(std::format("ERROR: Shader {} compile failed: {}", what, infoLog));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int Shader::load_shader(const std::string& filename, GLenum shader_type) {
|
||||
dbc::check(fs::exists(filename),
|
||||
std::format("shader file {} does not exist", filename));
|
||||
|
||||
// create the shader
|
||||
std::string shader_code = read_file(filename);
|
||||
const char* shader_code_ptr = shader_code.c_str();
|
||||
|
||||
int shader_id = glCreateShader(shader_type);
|
||||
glShaderSource(shader_id, 1, &shader_code_ptr, NULL);
|
||||
glCompileShader(shader_id);
|
||||
|
||||
check_error(filename, shader_id, GL_COMPILE_STATUS);
|
||||
|
||||
// check compile error
|
||||
return shader_id;
|
||||
}
|
||||
|
||||
Shader::Shader(const char* vertexPath, const char* fragmentPath) {
|
||||
unsigned int vertex = load_shader(vertexPath, GL_VERTEX_SHADER);
|
||||
unsigned int fragment = load_shader(fragmentPath, GL_FRAGMENT_SHADER);
|
||||
|
||||
ID = glCreateProgram();
|
||||
glAttachShader(ID, vertex);
|
||||
glAttachShader(ID, fragment);
|
||||
glLinkProgram(ID);
|
||||
|
||||
check_error("link", ID, GL_LINK_STATUS);
|
||||
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
}
|
||||
|
||||
void Shader::use() const {
|
||||
glUseProgram(ID);
|
||||
}
|
||||
|
||||
void Shader::cleanup() {
|
||||
glDeleteProgram(ID);
|
||||
}
|
||||
|
||||
void Shader::setBool(const std::string &name, bool value) const {
|
||||
auto uniform = glGetUniformLocation(ID, name.c_str());
|
||||
glUniform1i(uniform, (int)value);
|
||||
}
|
||||
|
||||
void Shader::setInt(const std::string &name, int value) const {
|
||||
auto uniform = glGetUniformLocation(ID, name.c_str());
|
||||
glUniform1i(uniform, value);
|
||||
}
|
||||
|
||||
void Shader::setFloat(const std::string &name, float value) const {
|
||||
auto uniform = glGetUniformLocation(ID, name.c_str());
|
||||
glUniform1f(uniform, value);
|
||||
}
|
||||
|
||||
void Shader::setVec4(const std::string &name, float v1, float v2, float v3, float v4) const {
|
||||
auto uniform = glGetUniformLocation(ID, name.c_str());
|
||||
glUniform4f(uniform, v1, v2, v3, v4);
|
||||
}
|
||||
|
||||
void Shader::setVec4(const std::string &name, const glm::vec4& value) const
|
||||
{
|
||||
auto uniform = glGetUniformLocation(ID, name.c_str());
|
||||
glUniform4fv(uniform, 1, &value[0]);
|
||||
}
|
||||
|
||||
void Shader::setVec3(const std::string &name, const glm::vec3& value) const
|
||||
{
|
||||
auto uniform = glGetUniformLocation(ID, name.c_str());
|
||||
glUniform3fv(uniform, 1, &value[0]);
|
||||
}
|
||||
|
||||
void Shader::setVec3(const std::string &name, float v1, float v2, float v3) const {
|
||||
auto uniform = glGetUniformLocation(ID, name.c_str());
|
||||
glUniform3f(uniform, v1, v2, v3);
|
||||
}
|
||||
|
||||
void Shader::setMat4(const std::string &name, const glm::mat4& mat) const {
|
||||
unsigned int loc = glGetUniformLocation(ID, name.c_str());
|
||||
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(mat));
|
||||
}
|
||||
112
13-multiple-lights/src/shader.hpp
Normal file
112
13-multiple-lights/src/shader.hpp
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
#pragma once
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <string>
|
||||
#include <climits>
|
||||
#include <glm/glm.hpp>
|
||||
#include <vector>
|
||||
#include <format>
|
||||
#include "dbc.hpp"
|
||||
|
||||
struct Material {
|
||||
glm::vec3 ambient{0.1f,0.1f,0.1f};
|
||||
float shininess{32.0f};
|
||||
unsigned int diffuseMap = 0;
|
||||
unsigned int specularMap = 0;
|
||||
};
|
||||
|
||||
struct Light {
|
||||
glm::vec3 position{0.0f, 0.0f, 0.0f};
|
||||
glm::vec3 direction{0.0f, 0.0f, 0.0f};
|
||||
glm::vec3 ambient{0.0f, 0.0f, 0.0f};
|
||||
glm::vec3 diffuse{0.0f, 0.0f, 0.0f};
|
||||
glm::vec3 specular{0.0f, 0.0f, 0.0f};
|
||||
float constant=0.0f;
|
||||
float linear=0.0f;
|
||||
float quadratic=0.0f;
|
||||
float cutOff=0.0f;
|
||||
float outerCutOff=0.0f;
|
||||
|
||||
void adjust(float amount) {
|
||||
ambient += amount;
|
||||
diffuse += amount;
|
||||
specular += amount;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#define NR_POINT_LIGHTS 4
|
||||
|
||||
struct Lighting {
|
||||
Light directional;
|
||||
std::vector<Light> positioned;
|
||||
Light spot;
|
||||
};
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int ID = UINT_MAX;
|
||||
|
||||
Shader(const char* vertexPath, const char* fragmentPath);
|
||||
unsigned int load_shader(const std::string& filename, GLenum shader_type);
|
||||
void use() const;
|
||||
void setBool(const std::string &name, bool value) const;
|
||||
void setInt(const std::string &name, int value) const;
|
||||
void setFloat(const std::string &name, float value) const;
|
||||
void setVec4(const std::string &name, float v1, float v2, float v3, float v4) const;
|
||||
void setVec4(const std::string &name, const glm::vec4& value) const;
|
||||
void setVec3(const std::string &name, float v1, float v2, float v3) const;
|
||||
void setVec3(const std::string &name, const glm::vec3& value) const;
|
||||
void setMat4(const std::string &name, const glm::mat4& what) const;
|
||||
void cleanup();
|
||||
|
||||
void applyMaterial(const Material& material) {
|
||||
setVec3("material.ambient", material.ambient);
|
||||
setFloat("material.shininess", material.shininess);
|
||||
}
|
||||
|
||||
void applyLighting(const Lighting& lighting) {
|
||||
applyDirLight(lighting.directional);
|
||||
setInt("pointLightCount", lighting.positioned.size());
|
||||
|
||||
for(size_t i = 0; i < lighting.positioned.size(); i++) {
|
||||
applyPointLight(lighting.positioned[i], i);
|
||||
}
|
||||
|
||||
applySpotLight(lighting.spot);
|
||||
}
|
||||
|
||||
void applyDirLight(const Light& light) {
|
||||
setVec3("dirLight.direction", light.direction);
|
||||
setVec3("dirLight.ambient", light.ambient);
|
||||
setVec3("dirLight.diffuse", light.diffuse);
|
||||
setVec3("dirLight.specular", light.specular);
|
||||
}
|
||||
|
||||
void applyPointLight(const Light& light, size_t index) {
|
||||
dbc::check(index < NR_POINT_LIGHTS, "too many positioned lights");
|
||||
|
||||
setVec3(std::format("pointLights[{}].position", index), light.position);
|
||||
setVec3(std::format("pointLights[{}].ambient", index), light.ambient);
|
||||
setVec3(std::format("pointLights[{}].diffuse", index), light.diffuse);
|
||||
setVec3(std::format("pointLights[{}].specular", index), light.specular);
|
||||
|
||||
setFloat(std::format("pointLights[{}].constant", index), light.constant);
|
||||
setFloat(std::format("pointLights[{}].linear", index), light.linear);
|
||||
setFloat(std::format("pointLights[{}].quadratic", index), light.quadratic);
|
||||
}
|
||||
|
||||
void applySpotLight(const Light& light) {
|
||||
setVec3("spotLight.position", light.position);
|
||||
setVec3("spotLight.direction", light.direction);
|
||||
setVec3("spotLight.ambient", light.ambient);
|
||||
setVec3("spotLight.diffuse", light.diffuse);
|
||||
setVec3("spotLight.specular", light.specular);
|
||||
setFloat("spotLight.constant", light.constant);
|
||||
setFloat("spotLight.linear", light.linear);
|
||||
setFloat("spotLight.quadratic", light.quadratic);
|
||||
setFloat("spotLight.cutOff", glm::cos(glm::radians(light.cutOff)));
|
||||
setFloat("spotLight.outerCutOff", glm::cos(glm::radians(light.outerCutOff)));
|
||||
}
|
||||
};
|
||||
2
13-multiple-lights/src/stb_image.cpp
Normal file
2
13-multiple-lights/src/stb_image.cpp
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
7988
13-multiple-lights/src/stb_image.h
Normal file
7988
13-multiple-lights/src/stb_image.h
Normal file
File diff suppressed because it is too large
Load diff
10
13-multiple-lights/tests/bad_shader.vs
Normal file
10
13-multiple-lights/tests/bad_shader.vs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos; // the position variable has attribute position 0
|
||||
|
||||
out vec4 vertexColor; // specify a color output to the fragment shader
|
||||
|
||||
void main()
|
||||
// syntax error here
|
||||
gl_Position = vec4(aPos, 1.0); // see how we directly give a vec3 to vec4's constructor
|
||||
vertexColor = vec4(0.5, 0.0, 0.0, 1.0); // set the output variable to a dark-red color
|
||||
}
|
||||
38
13-multiple-lights/tests/main.cpp
Normal file
38
13-multiple-lights/tests/main.cpp
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#include <fuc2/run.hpp>
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "dbc.hpp"
|
||||
|
||||
TEST_SET(shader_tests);
|
||||
|
||||
using namespace fuc2;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
||||
if (window == NULL)
|
||||
{
|
||||
dbc::log("Failed to create GLFW window");
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
// glad: load all OpenGL function pointers
|
||||
// ---------------------------------------
|
||||
if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||
{
|
||||
dbc::log("Failed to initialize GLAD");
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::vector<fuc2::Set> tests{
|
||||
shader_tests::TESTS
|
||||
};
|
||||
|
||||
return run_tests(tests, argc, argv);
|
||||
}
|
||||
4
13-multiple-lights/tests/meson.build
Normal file
4
13-multiple-lights/tests/meson.build
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
fuc2_tests = files(
|
||||
'main.cpp',
|
||||
'shader_tests.cpp',
|
||||
)
|
||||
33
13-multiple-lights/tests/shader_tests.cpp
Normal file
33
13-multiple-lights/tests/shader_tests.cpp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#include <deque>
|
||||
#include <string>
|
||||
#include <fuc2/testing.hpp>
|
||||
#include "shader.hpp"
|
||||
|
||||
using namespace fuc2;
|
||||
|
||||
namespace shader_tests {
|
||||
void test_load_shaders() {
|
||||
Shader shader("shaders/3.3.shader.vs", "shaders/3.3.shader.fs");
|
||||
|
||||
shader.use();
|
||||
|
||||
CHECK(shader.ID != UINT_MAX, "Shader not initialized");
|
||||
}
|
||||
|
||||
void test_compile_fail() {
|
||||
auto runner = [&]() {
|
||||
Shader shader("tests/bad_shader.vs", "shaders/3.3.shader.fs");
|
||||
};
|
||||
|
||||
BLOWS_UP(runner, "compile fail should blow up");
|
||||
}
|
||||
|
||||
fuc2::Set TESTS{
|
||||
.name="shaders",
|
||||
.options={ .fail_fast=false },
|
||||
.tests={
|
||||
TEST(test_load_shaders),
|
||||
TEST(test_compile_fail),
|
||||
}
|
||||
};
|
||||
}
|
||||
9
13-multiple-lights/wraps/assimp.wrap
Normal file
9
13-multiple-lights/wraps/assimp.wrap
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
[wrap-git]
|
||||
directory=assimp-5.4.3
|
||||
url=https://github.com/assimp/assimp.git
|
||||
revision=v5.4.3
|
||||
depth=1
|
||||
method=cmake
|
||||
|
||||
[provide]
|
||||
assimp = assimp_dep
|
||||
13
13-multiple-lights/wraps/box2d.wrap
Normal file
13
13-multiple-lights/wraps/box2d.wrap
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
[wrap-file]
|
||||
directory = box2d-2.4.1
|
||||
source_url = https://github.com/erincatto/box2d/archive/v2.4.1/box2d-2.4.1.tar.gz
|
||||
source_filename = box2d-2.4.1.tar.gz
|
||||
source_hash = d6b4650ff897ee1ead27cf77a5933ea197cbeef6705638dd181adc2e816b23c2
|
||||
patch_filename = box2d_2.4.1-4_patch.zip
|
||||
patch_url = https://wrapdb.mesonbuild.com/v2/box2d_2.4.1-4/get_patch
|
||||
patch_hash = f5235178029fa0a8deaf8dddbccfd70739b289cffbeec5c98fc91209683c4802
|
||||
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/box2d_2.4.1-4/box2d-2.4.1.tar.gz
|
||||
wrapdb_version = 2.4.1-4
|
||||
|
||||
[provide]
|
||||
box2d = box2d_dep
|
||||
13
13-multiple-lights/wraps/fmt.wrap
Normal file
13
13-multiple-lights/wraps/fmt.wrap
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
[wrap-file]
|
||||
directory = fmt-11.0.2
|
||||
source_url = https://github.com/fmtlib/fmt/archive/11.0.2.tar.gz
|
||||
source_filename = fmt-11.0.2.tar.gz
|
||||
source_hash = 6cb1e6d37bdcb756dbbe59be438790db409cdb4868c66e888d5df9f13f7c027f
|
||||
patch_filename = fmt_11.0.2-1_patch.zip
|
||||
patch_url = https://wrapdb.mesonbuild.com/v2/fmt_11.0.2-1/get_patch
|
||||
patch_hash = 90c9e3b8e8f29713d40ca949f6f93ad115d78d7fb921064112bc6179e6427c5e
|
||||
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/fmt_11.0.2-1/fmt-11.0.2.tar.gz
|
||||
wrapdb_version = 11.0.2-1
|
||||
|
||||
[provide]
|
||||
fmt = fmt_dep
|
||||
9
13-multiple-lights/wraps/fuc2.wrap
Normal file
9
13-multiple-lights/wraps/fuc2.wrap
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
[wrap-git]
|
||||
directory=fuc2-0.2.0
|
||||
url=https://lcthw.dev/cpp/fuc2.git
|
||||
revision=HEAD
|
||||
depth=1
|
||||
method=meson
|
||||
|
||||
[provide]
|
||||
fuc2 = fuc2_dep
|
||||
13
13-multiple-lights/wraps/glfw.wrap
Normal file
13
13-multiple-lights/wraps/glfw.wrap
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
[wrap-file]
|
||||
directory = glfw-3.4
|
||||
source_url = https://github.com/glfw/glfw/archive/refs/tags/3.4.tar.gz
|
||||
source_filename = glfw-3.4.tar.gz
|
||||
source_hash = c038d34200234d071fae9345bc455e4a8f2f544ab60150765d7704e08f3dac01
|
||||
patch_filename = glfw_3.4-1_patch.zip
|
||||
patch_url = https://wrapdb.mesonbuild.com/v2/glfw_3.4-1/get_patch
|
||||
patch_hash = 58a6a6cdb28195d7f7e6f5de85dff7044d378e49b46bf1d4a9b04c97ed93e6b0
|
||||
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/glfw_3.4-1/glfw-3.4.tar.gz
|
||||
wrapdb_version = 3.4-1
|
||||
|
||||
[provide]
|
||||
glfw3 = glfw_dep
|
||||
13
13-multiple-lights/wraps/glm.wrap
Normal file
13
13-multiple-lights/wraps/glm.wrap
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
[wrap-file]
|
||||
directory = glm-1.0.1
|
||||
source_url = https://github.com/g-truc/glm/archive/refs/tags/1.0.1.tar.gz
|
||||
source_filename = glm-1.0.1.tar.gz
|
||||
source_hash = 9f3174561fd26904b23f0db5e560971cbf9b3cbda0b280f04d5c379d03bf234c
|
||||
patch_filename = glm_1.0.1-1_patch.zip
|
||||
patch_url = https://wrapdb.mesonbuild.com/v2/glm_1.0.1-1/get_patch
|
||||
patch_hash = 25679275e26bc4c36bb617d1b4a52197039402af828d2a4bf67b3c0260a5df6a
|
||||
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/glm_1.0.1-1/glm-1.0.1.tar.gz
|
||||
wrapdb_version = 1.0.1-1
|
||||
|
||||
[provide]
|
||||
glm = glm_dep
|
||||
Loading…
Add table
Add a link
Reference in a new issue