Setup for day 18.
This commit is contained in:
parent
032f10ef44
commit
993f148710
87 changed files with 14992 additions and 0 deletions
311
18-depth-testing/src/KHR/khrplatform.h
Normal file
311
18-depth-testing/src/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_ */
|
||||
70
18-depth-testing/src/camera.cpp
Normal file
70
18-depth-testing/src/camera.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#include "camera.hpp"
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
glm::mat4 Camera::look_at() {
|
||||
return glm::lookAt(position, position + front, up);
|
||||
}
|
||||
|
||||
void Camera::forward() {
|
||||
dirty = true;
|
||||
position += speed * front;
|
||||
}
|
||||
|
||||
void Camera::back() {
|
||||
dirty = true;
|
||||
position -= speed * front;
|
||||
}
|
||||
|
||||
void Camera::left() {
|
||||
dirty = true;
|
||||
position -= glm::normalize(glm::cross(front, up)) * speed;
|
||||
}
|
||||
|
||||
void Camera::right() {
|
||||
dirty = true;
|
||||
position += glm::normalize(glm::cross(front, up)) * speed;
|
||||
}
|
||||
|
||||
void Camera::update(float deltaTime) {
|
||||
speed = movement_speed * deltaTime;
|
||||
}
|
||||
|
||||
void Camera::mouse_move(double xpos, double ypos) {
|
||||
dirty = true;
|
||||
|
||||
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) {
|
||||
dirty = true;
|
||||
fov -= (float)yoffset;
|
||||
|
||||
if(fov < 1.0f) fov = 1.0f;
|
||||
if(fov > 90.0f) fov = 90.0f;
|
||||
}
|
||||
28
18-depth-testing/src/camera.hpp
Normal file
28
18-depth-testing/src/camera.hpp
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
struct Camera {
|
||||
glm::vec3 position{0.0f, 0.0f, 3.0f};
|
||||
glm::vec3 front{0.0f, 0.0f, -1.0f};
|
||||
glm::vec3 up{0.0f, 1.0f, 0.0f};
|
||||
glm::vec3 direction{0.0f, 0.0f, 0.0f};
|
||||
float movement_speed = 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;
|
||||
bool dirty = true;
|
||||
|
||||
glm::mat4 look_at();
|
||||
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);
|
||||
};
|
||||
105
18-depth-testing/src/components.hpp
Normal file
105
18-depth-testing/src/components.hpp
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "json.hpp"
|
||||
|
||||
const unsigned int SCR_WIDTH = 800;
|
||||
const unsigned int SCR_HEIGHT = 600;
|
||||
|
||||
#define MAX_LIGHTS 4
|
||||
|
||||
namespace components {
|
||||
template <typename T> struct NameOf;
|
||||
|
||||
struct Material {
|
||||
glm::vec3 ambient{0.1f,0.1f,0.1f};
|
||||
float shininess{32.0f};
|
||||
unsigned int diffuseMap = 0;
|
||||
unsigned int specularMap = 0;
|
||||
};
|
||||
|
||||
ENROLL_COMPONENT(Material, ambient, shininess, diffuseMap, specularMap);
|
||||
|
||||
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 cut_off=0.0f;
|
||||
float outer_cut_off=0.0f;
|
||||
|
||||
void adjust(float amount) {
|
||||
ambient += amount;
|
||||
diffuse += amount;
|
||||
specular += amount;
|
||||
}
|
||||
};
|
||||
|
||||
ENROLL_COMPONENT(Light,
|
||||
position, direction,
|
||||
ambient, diffuse, specular,
|
||||
constant, linear, quadratic,
|
||||
cut_off, outer_cut_off);
|
||||
|
||||
struct Lighting {
|
||||
std::vector<Light> directional;
|
||||
std::vector<Light> positioned;
|
||||
std::vector<Light> spot;
|
||||
Light camera;
|
||||
};
|
||||
|
||||
ENROLL_COMPONENT(Lighting, directional, positioned, spot, camera);
|
||||
|
||||
struct Camera {
|
||||
glm::vec3 position{0.0f, 0.0f, 3.0f};
|
||||
glm::vec3 front{0.0f, 0.0f, -1.0f};
|
||||
glm::vec3 up{0.0f, 1.0f, 0.0f};
|
||||
glm::vec3 direction{0.0f, 0.0f, 0.0f};
|
||||
float movement_speed = 20.0f;
|
||||
};
|
||||
|
||||
ENROLL_COMPONENT(Camera, position, front, up, direction, movement_speed);
|
||||
|
||||
struct Model {
|
||||
std::string directory;
|
||||
std::string model_path;
|
||||
};
|
||||
|
||||
ENROLL_COMPONENT(Model, directory, model_path);
|
||||
|
||||
using Position = glm::vec3;
|
||||
|
||||
struct Thing {
|
||||
std::string model;
|
||||
Position position;
|
||||
std::string material;
|
||||
};
|
||||
|
||||
ENROLL_COMPONENT(Thing, model, position, material);
|
||||
|
||||
struct Shader {
|
||||
std::string vertex_path;
|
||||
std::string frag_path;
|
||||
};
|
||||
|
||||
ENROLL_COMPONENT(Shader, vertex_path, frag_path);
|
||||
|
||||
struct Scene {
|
||||
components::Shader shader;
|
||||
components::Shader light_shader;
|
||||
std::map<std::string, Material> materials;
|
||||
std::map<std::string, Model> models;
|
||||
std::vector<Thing> things;
|
||||
Camera camera;
|
||||
Lighting light;
|
||||
};
|
||||
|
||||
ENROLL_COMPONENT(Scene, shader, light_shader, materials, models, things, camera, light);
|
||||
}
|
||||
47
18-depth-testing/src/dbc.cpp
Normal file
47
18-depth-testing/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
18-depth-testing/src/dbc.hpp
Normal file
46
18-depth-testing/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());
|
||||
}
|
||||
1140
18-depth-testing/src/glad/glad.cpp
Normal file
1140
18-depth-testing/src/glad/glad.cpp
Normal file
File diff suppressed because it is too large
Load diff
2129
18-depth-testing/src/glad/glad.h
Normal file
2129
18-depth-testing/src/glad/glad.h
Normal file
File diff suppressed because it is too large
Load diff
59
18-depth-testing/src/json.hpp
Normal file
59
18-depth-testing/src/json.hpp
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#pragma once
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <nlohmann/json_fwd.hpp>
|
||||
#include <optional>
|
||||
#include <glm/glm.hpp>
|
||||
#include <vector>
|
||||
|
||||
#define ENROLL_COMPONENT(COMPONENT, ...) \
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(COMPONENT, __VA_ARGS__); \
|
||||
template <> struct NameOf<COMPONENT> { \
|
||||
static constexpr const char *name = #COMPONENT; \
|
||||
};
|
||||
|
||||
// partial specialization (full specialization works too)
|
||||
namespace nlohmann {
|
||||
|
||||
template <>
|
||||
struct adl_serializer<glm::vec3> {
|
||||
static void to_json(json& j, const glm::vec3& opt) {
|
||||
|
||||
}
|
||||
|
||||
static void from_json(const json& j, glm::vec3& opt) {
|
||||
opt = glm::vec3(j[0], j[1], j[2]);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct adl_serializer<std::optional<T>> {
|
||||
static void to_json(json& j, const std::optional<T>& opt) {
|
||||
if (opt == std::nullopt) {
|
||||
j = nullptr;
|
||||
} else {
|
||||
j = *opt; // this will call adl_serializer<T>::to_json which will
|
||||
// find the free function to_json in T's namespace!
|
||||
}
|
||||
}
|
||||
|
||||
static void from_json(const json& j, std::optional<T>& opt) {
|
||||
if (j.is_null() || j == false) {
|
||||
opt = std::nullopt;
|
||||
} else {
|
||||
opt = std::make_optional<T>(j.template get<T>());
|
||||
// same as above, but with adl_serializer<T>::from_json
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct adl_serializer<std::chrono::milliseconds> {
|
||||
static void to_json(json& j, const std::chrono::milliseconds& opt) {
|
||||
j = opt.count();
|
||||
}
|
||||
|
||||
static void from_json(const json& j, std::chrono::milliseconds& opt) {
|
||||
opt = std::chrono::milliseconds{int(j)};
|
||||
}
|
||||
};
|
||||
}
|
||||
126
18-depth-testing/src/main.cpp
Normal file
126
18-depth-testing/src/main.cpp
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
#define _USE_MATH_DEFINES
|
||||
#include <stb_image.h>
|
||||
#include "scene.hpp"
|
||||
#include "utils.hpp"
|
||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
||||
|
||||
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) {
|
||||
Scene* scene = static_cast<Scene*>(glfwGetWindowUserPointer(window));
|
||||
scene->camera.mouse_move(xpos, ypos);
|
||||
}
|
||||
|
||||
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) {
|
||||
Scene* scene = static_cast<Scene*>(glfwGetWindowUserPointer(window));
|
||||
scene->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 process_input(GLFWwindow *window, Scene& scene) {
|
||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
||||
scene.camera.forward();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
||||
scene.camera.back();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
||||
scene.camera.left();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
||||
scene.camera.right();
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) {
|
||||
scene.spawn("popcorn", scene.camera.position);
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
|
||||
for(auto& light : scene.light.directional) {
|
||||
light.adjust(-0.01f);
|
||||
}
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
|
||||
for(auto& light : scene.light.directional) {
|
||||
light.adjust(0.01f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Scene setup() {
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
components::Scene config = utils::load_scene_config("config.json");
|
||||
Scene scene(config);
|
||||
|
||||
return scene;
|
||||
}
|
||||
|
||||
void update(GLFWwindow* window, Scene& scene) {
|
||||
glfwPollEvents();
|
||||
process_input(window, scene);
|
||||
scene.update();
|
||||
}
|
||||
|
||||
void render(GLFWwindow* window, Scene& scene) {
|
||||
scene.render(window);
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
|
||||
void quit(GLFWwindow* window, Scene& scene) {
|
||||
scene.cleanup();
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
int main() {
|
||||
init_glfw();
|
||||
|
||||
auto window = create_window();
|
||||
auto scene = setup();
|
||||
|
||||
glfwSetWindowUserPointer(window, &scene);
|
||||
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
update(window, scene);
|
||||
render(window, scene);
|
||||
}
|
||||
|
||||
quit(window, scene);
|
||||
|
||||
return 0;
|
||||
}
|
||||
48
18-depth-testing/src/mesh.cpp
Normal file
48
18-depth-testing/src/mesh.cpp
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#include "mesh.hpp"
|
||||
#include "dbc.hpp"
|
||||
|
||||
void Mesh::draw(Shader &shader) {
|
||||
// CAN WE DO THIS LESS OFTEN OUTSIDE DRAWING?
|
||||
apply_textures(shader);
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
|
||||
glBindVertexArray(0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
}
|
||||
|
||||
void Mesh::apply_textures(const Shader& shader) {
|
||||
for(unsigned int i = 0; i < textures.size(); i++) {
|
||||
Texture& texture = textures[i];
|
||||
|
||||
texture.uniform_id = glGetUniformLocation(shader.ID, texture.target.c_str());
|
||||
glActiveTexture(GL_TEXTURE0 + i);
|
||||
|
||||
// move this to setup_mesh
|
||||
glUniform1i(texture.uniform_id, i);
|
||||
glBindTexture(GL_TEXTURE_2D, texture.id);
|
||||
}
|
||||
}
|
||||
|
||||
void Mesh::setup_mesh() {
|
||||
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);
|
||||
|
||||
// this matches the locations in the vertex shader
|
||||
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, tex_coords));
|
||||
}
|
||||
72
18-depth-testing/src/mesh.hpp
Normal file
72
18-depth-testing/src/mesh.hpp
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#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 tex_coords;
|
||||
};
|
||||
|
||||
struct TextureCounts {
|
||||
unsigned int diffuseNr = 1;
|
||||
unsigned int specularNr = 1;
|
||||
unsigned int normalNr = 1;
|
||||
unsigned int heightNr = 1;
|
||||
};
|
||||
|
||||
struct Texture {
|
||||
unsigned int id;
|
||||
std::string type;
|
||||
std::string path;
|
||||
std::string target;
|
||||
int uniform_id=-1;
|
||||
|
||||
Texture(unsigned int id, const std::string& type, const std::string& path, TextureCounts& count) :
|
||||
id(id), type(type)
|
||||
{
|
||||
unsigned int number = 0;
|
||||
|
||||
if(type == "texture_diffuse") {
|
||||
number = count.diffuseNr++;
|
||||
} else if(type == "texture_specular") {
|
||||
number = count.specularNr++;
|
||||
} else if(type == "texture_normal") {
|
||||
number = count.normalNr++;
|
||||
} else if(type == "texture_height") {
|
||||
number = count.heightNr++;
|
||||
} else {
|
||||
dbc::sentinel($F("Invalid texture type={} for file={}", type, path));
|
||||
}
|
||||
|
||||
target = std::format("{}{}", type, number);
|
||||
}
|
||||
};
|
||||
|
||||
struct Mesh {
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
std::vector<Texture> textures;
|
||||
unsigned int VAO = 0;
|
||||
unsigned int VBO = 0;
|
||||
unsigned int EBO = 0;
|
||||
|
||||
Mesh(std::vector<Vertex>& vertices, std::vector<unsigned int>& indices, std::vector<Texture>& textures) :
|
||||
vertices(vertices),
|
||||
indices(indices),
|
||||
textures(textures)
|
||||
{
|
||||
setup_mesh();
|
||||
}
|
||||
|
||||
void draw(Shader &shader);
|
||||
void setup_mesh();
|
||||
void apply_textures(const Shader& shader);
|
||||
};
|
||||
211
18-depth-testing/src/model.cpp
Normal file
211
18-depth-testing/src/model.cpp
Normal file
|
|
@ -0,0 +1,211 @@
|
|||
#include "model.hpp"
|
||||
#include "dbc.hpp"
|
||||
#include <print>
|
||||
|
||||
unsigned int texture_from_file(const std::string& directory, const std::string& path);
|
||||
unsigned int texture_from_internal(const aiScene *scene, size_t index);
|
||||
|
||||
unsigned int image_to_texture(unsigned char *data, int width, int height, int nrComponents) {
|
||||
unsigned int textureID;
|
||||
glGenTextures(1, &textureID);
|
||||
|
||||
GLenum format = GL_RGB;
|
||||
|
||||
switch(nrComponents) {
|
||||
case 1:
|
||||
format = GL_RED;
|
||||
break;
|
||||
case 3:
|
||||
format = GL_RGB;
|
||||
break;
|
||||
case 4:
|
||||
format = GL_RGBA;
|
||||
break;
|
||||
default:
|
||||
dbc::sentinel($F("Impossible nrComponents={} when loading image", nrComponents));
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
return textureID;
|
||||
}
|
||||
|
||||
unsigned int texture_from_internal(const aiScene *scene, size_t index) {
|
||||
const aiTexture *aiTex = scene->mTextures[index];
|
||||
unsigned int textureID = 0;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
int nrComponents = 0;
|
||||
unsigned char *data = nullptr;
|
||||
const unsigned char* dataBytes = (const unsigned char*)aiTex->pcData;
|
||||
|
||||
if(aiTex->mHeight == 0) {
|
||||
// compressed texture (PNG or JPG)
|
||||
size_t dataSize = aiTex->mWidth;
|
||||
data = stbi_load_from_memory(dataBytes, dataSize, &width, &height, &nrComponents, 0);
|
||||
textureID = image_to_texture(data, width, height, nrComponents);
|
||||
stbi_image_free(data);
|
||||
} else {
|
||||
data = (unsigned char*)aiTex->pcData;
|
||||
width = aiTex->mWidth;
|
||||
height = aiTex->mHeight;
|
||||
nrComponents = 4; // either BGRA8888 or RGBA8888
|
||||
textureID = image_to_texture(data, width, height, nrComponents);
|
||||
}
|
||||
|
||||
return textureID;
|
||||
}
|
||||
|
||||
unsigned int texture_from_file(const std::string& directory, const std::string& path)
|
||||
{
|
||||
std::string filename = directory + "/" + path;
|
||||
|
||||
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));
|
||||
|
||||
unsigned int textureID = image_to_texture(data, width, height, nrComponents);
|
||||
|
||||
stbi_image_free(data);
|
||||
|
||||
return textureID;
|
||||
}
|
||||
|
||||
void Model::draw(Shader &shader) {
|
||||
for(auto& mesh : meshes) {
|
||||
mesh.draw(shader);
|
||||
}
|
||||
}
|
||||
|
||||
void Model::load_model() {
|
||||
std::string path = directory + "/" + model_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");
|
||||
|
||||
process_node(scene, scene->mRootNode);
|
||||
}
|
||||
|
||||
void Model::process_node(const aiScene *scene, aiNode *node) {
|
||||
for(unsigned int i = 0; i < node->mNumMeshes; i++) {
|
||||
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
|
||||
meshes.push_back(process_mesh(scene, mesh));
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < node->mNumChildren; i++) {
|
||||
process_node(scene, node->mChildren[i]);
|
||||
}
|
||||
}
|
||||
|
||||
Mesh Model::process_mesh(const aiScene *scene, aiMesh *mesh) {
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
std::vector<Texture> textures;
|
||||
|
||||
for(unsigned int i = 0; i < mesh->mNumVertices; i++) {
|
||||
glm::vec3 position{
|
||||
mesh->mVertices[i].x,
|
||||
mesh->mVertices[i].y,
|
||||
mesh->mVertices[i].z};
|
||||
|
||||
glm::vec3 normal{};
|
||||
glm::vec2 tex_coords{0.0f, 0.0f};
|
||||
|
||||
if(mesh->HasNormals()) {
|
||||
normal = glm::vec3(
|
||||
mesh->mNormals[i].x,
|
||||
mesh->mNormals[i].y,
|
||||
mesh->mNormals[i].z);
|
||||
}
|
||||
|
||||
if(mesh->mTextureCoords[0]) {
|
||||
tex_coords = glm::vec2(
|
||||
mesh->mTextureCoords[0][i].x,
|
||||
mesh->mTextureCoords[0][i].y
|
||||
);
|
||||
}
|
||||
|
||||
vertices.emplace_back(position, normal, tex_coords);
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < mesh->mNumFaces; i++) {
|
||||
aiFace& face = mesh->mFaces[i];
|
||||
|
||||
for(unsigned int j = 0; j < face.mNumIndices; j++) {
|
||||
indices.emplace_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
|
||||
load_material_textures(scene, textures, material, aiTextureType_DIFFUSE, "texture_diffuse");
|
||||
|
||||
// 2. specular maps
|
||||
load_material_textures(scene, textures, material, aiTextureType_SPECULAR, "texture_specular");
|
||||
|
||||
// 3. normal maps
|
||||
load_material_textures(scene, textures, material, aiTextureType_HEIGHT, "texture_normal");
|
||||
|
||||
// 4. height maps
|
||||
load_material_textures(scene, textures, material, aiTextureType_AMBIENT, "texture_height");
|
||||
|
||||
return Mesh(vertices, indices, textures);
|
||||
}
|
||||
|
||||
void Model::load_material_textures(const aiScene *scene, std::vector<Texture>& textures, aiMaterial *mat, aiTextureType type, std::string type_name)
|
||||
{
|
||||
for(unsigned int i = 0; i < mat->GetTextureCount(type); i++) {
|
||||
aiString str;
|
||||
|
||||
mat->GetTexture(type, i, &str);
|
||||
std::string tx_path{str.C_Str()};
|
||||
dbc::check(!tx_path.empty(), "Texture has empty path, should be impossible?");
|
||||
|
||||
if(textures_loaded.contains(tx_path)) {
|
||||
textures.push_back(textures_loaded.at(tx_path));
|
||||
} else {
|
||||
unsigned int tx_id = 0;
|
||||
|
||||
// if it's a *# style path then it's internal
|
||||
if(tx_path[0] == '*') {
|
||||
// get the texture from the internal version
|
||||
tx_id = texture_from_internal(scene, std::stoi(tx_path.substr(1)));
|
||||
} else {
|
||||
// else get it from a file
|
||||
tx_id = texture_from_file(directory, tx_path);
|
||||
}
|
||||
|
||||
// dubious code here, but seems to be right
|
||||
auto& result = textures.emplace_back(tx_id, type_name, tx_path, texture_counts);
|
||||
|
||||
textures_loaded.try_emplace(tx_path, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
42
18-depth-testing/src/model.hpp
Normal file
42
18-depth-testing/src/model.hpp
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#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>
|
||||
|
||||
struct Model {
|
||||
std::string directory;
|
||||
std::string model_path;
|
||||
std::map<std::string, Texture> textures_loaded;
|
||||
std::vector<Mesh> meshes;
|
||||
TextureCounts texture_counts;
|
||||
bool gammaCorrection;
|
||||
|
||||
Model(const std::string& directory, const std::string& model_path) :
|
||||
directory(directory), model_path(model_path)
|
||||
{
|
||||
load_model();
|
||||
}
|
||||
|
||||
void draw(Shader &shader);
|
||||
|
||||
void load_model();
|
||||
void process_node(const aiScene *scene, aiNode *node);
|
||||
Mesh process_mesh(const aiScene *scene, aiMesh *mesh);
|
||||
|
||||
void load_material_textures(const aiScene *scene, std::vector<Texture>& textures, aiMaterial *mat, aiTextureType type, std::string typeName);
|
||||
};
|
||||
71
18-depth-testing/src/scene.cpp
Normal file
71
18-depth-testing/src/scene.cpp
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
#include "scene.hpp"
|
||||
#include "dbc.hpp"
|
||||
#include <math.h>
|
||||
|
||||
void Scene::update() {
|
||||
float currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
|
||||
camera.update(deltaTime);
|
||||
}
|
||||
|
||||
void Scene::draw_model(Model& scene_model, Material& material, glm::mat4& projection, glm::mat4& view, glm::vec3& position)
|
||||
{
|
||||
shader.apply_material(material);
|
||||
|
||||
float time = glfwGetTime();
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, position);
|
||||
model = glm::rotate(model, glm::radians(time * 10.0f), glm::vec3(1.0f, 0.3f, 0.5f));
|
||||
|
||||
shader.setMat4("model", model);
|
||||
|
||||
scene_model.draw(shader);
|
||||
}
|
||||
|
||||
void Scene::render(GLFWwindow* window) {
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
shader.use();
|
||||
|
||||
// time is used for fake 3d rotation
|
||||
float time = glfwGetTime();
|
||||
|
||||
glm::mat4 view = camera.look_at();
|
||||
glm::mat4 projection = glm::mat4(1.0f);
|
||||
|
||||
// fov, aspect, near plane, far plane
|
||||
projection = glm::perspective(glm::radians(camera.fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||
|
||||
shader.use();
|
||||
shader.setMat4("view", view);
|
||||
shader.setMat4("projection", projection);
|
||||
shader.setVec3("viewPos", camera.position);
|
||||
|
||||
// for now just detect the camera moved and do spotlight update
|
||||
if(camera.dirty) {
|
||||
light.camera.position = camera.position;
|
||||
light.camera.direction = camera.front;
|
||||
// just update the camera's light
|
||||
shader.apply_spot_light(light.camera, 0);
|
||||
camera.dirty = false;
|
||||
}
|
||||
|
||||
// BUG: no connection between models and positions
|
||||
for(auto& thing : things) {
|
||||
draw_model(thing.model, thing.material, projection, view, thing.position);
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::cleanup() {
|
||||
shader.cleanup();
|
||||
}
|
||||
|
||||
void Scene::spawn(const std::string& name, components::Position& position) {
|
||||
things.emplace_back(
|
||||
models.at(name),
|
||||
position,
|
||||
materials.at(name));
|
||||
}
|
||||
67
18-depth-testing/src/scene.hpp
Normal file
67
18-depth-testing/src/scene.hpp
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "shader.hpp"
|
||||
#include "model.hpp"
|
||||
#include "shader.hpp"
|
||||
#include "model.hpp"
|
||||
#include "camera.hpp"
|
||||
#include "components.hpp"
|
||||
#include <vector>
|
||||
|
||||
struct Thing {
|
||||
Model& model;
|
||||
components::Position position;
|
||||
Material& material;
|
||||
};
|
||||
|
||||
struct Scene {
|
||||
Shader shader;
|
||||
Shader light_shader;
|
||||
std::map<std::string, components::Material> materials;
|
||||
Camera camera;
|
||||
components::Lighting light;
|
||||
std::map<std::string, Model> models;
|
||||
std::vector<Thing> things;
|
||||
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
|
||||
Scene(components::Scene& config):
|
||||
shader{config.shader.vertex_path, config.shader.frag_path},
|
||||
light_shader{config.light_shader.vertex_path, config.shader.frag_path},
|
||||
materials{config.materials},
|
||||
camera{
|
||||
.position=config.camera.position,
|
||||
.front=config.camera.front,
|
||||
.up=config.camera.up,
|
||||
.direction=config.camera.direction,
|
||||
.movement_speed=config.camera.movement_speed,
|
||||
},
|
||||
light{config.light}
|
||||
{
|
||||
for(auto& [name, model] : config.models) {
|
||||
models.try_emplace(name, model.directory, model.model_path);
|
||||
}
|
||||
|
||||
for(auto& thing : config.things) {
|
||||
things.emplace_back(
|
||||
models.at(thing.model),
|
||||
thing.position,
|
||||
materials.at(thing.material));
|
||||
}
|
||||
|
||||
shader.use();
|
||||
shader.apply_lighting(light);
|
||||
}
|
||||
|
||||
void update();
|
||||
void draw_model(Model& scene_model, Material& material, glm::mat4& projection, glm::mat4& view, glm::vec3& position);
|
||||
void render(GLFWwindow* window);
|
||||
void cleanup();
|
||||
void spawn(const std::string& name, components::Position& position);
|
||||
};
|
||||
196
18-depth-testing/src/shader.cpp
Normal file
196
18-depth-testing/src/shader.cpp
Normal file
|
|
@ -0,0 +1,196 @@
|
|||
#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 std::string& vertexPath, const std::string& 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));
|
||||
}
|
||||
|
||||
void Shader::apply_material(const Material& material) {
|
||||
setVec3("material.ambient", material.ambient);
|
||||
setFloat("material.shininess", material.shininess);
|
||||
}
|
||||
|
||||
void Shader::apply_lighting(const Lighting& lighting) {
|
||||
setInt("pointLightCount", lighting.positioned.size());
|
||||
setInt("dirLightCount", lighting.directional.size());
|
||||
|
||||
// need +1 for the camera spot light
|
||||
setInt("spotLightCount", lighting.spot.size() + 1);
|
||||
|
||||
for(size_t i = 0; i < lighting.directional.size(); i++) {
|
||||
apply_dir_light(lighting.directional[i], i);
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < lighting.positioned.size(); i++) {
|
||||
apply_point_light(lighting.positioned[i], i);
|
||||
}
|
||||
|
||||
// set the first spotlight to the camera light
|
||||
apply_spot_light(lighting.camera, 0);
|
||||
|
||||
for(size_t i = 0; i < lighting.spot.size(); i++) {
|
||||
// need to be off by one because the camera is a spot light
|
||||
apply_spot_light(lighting.spot[i], i+1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Shader::apply_dir_light(const Light& light, size_t index) {
|
||||
dbc::check(index < MAX_LIGHTS, "too many directional lights");
|
||||
|
||||
// nasty, this needs to go
|
||||
setVec3(std::format("dirLights[{}].direction", index), light.direction);
|
||||
setVec3(std::format("dirLights[{}].ambient", index), light.ambient);
|
||||
setVec3(std::format("dirLights[{}].diffuse", index), light.diffuse);
|
||||
setVec3(std::format("dirLights[{}].specular", index), light.specular);
|
||||
}
|
||||
|
||||
void Shader::apply_point_light(const Light& light, size_t index) {
|
||||
dbc::check(index < MAX_LIGHTS, "too many positioned lights");
|
||||
|
||||
// disgusting, crafting a string on every render?
|
||||
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 Shader::apply_spot_light(const Light& light, size_t index) {
|
||||
dbc::check(index < MAX_LIGHTS, "too many spot lights");
|
||||
|
||||
// find a way to not make strings all the time
|
||||
setVec3(std::format("spotLights[{}].position", index), light.position);
|
||||
setVec3(std::format("spotLights[{}].direction", index), light.direction);
|
||||
setVec3(std::format("spotLights[{}].ambient", index), light.ambient);
|
||||
setVec3(std::format("spotLights[{}].diffuse", index), light.diffuse);
|
||||
setVec3(std::format("spotLights[{}].specular", index), light.specular);
|
||||
setFloat(std::format("spotLights[{}].constant", index), light.constant);
|
||||
setFloat(std::format("spotLights[{}].linear", index), light.linear);
|
||||
setFloat(std::format("spotLights[{}].quadratic", index), light.quadratic);
|
||||
setFloat(std::format("spotLights[{}].cut_off", index), glm::cos(glm::radians(light.cut_off)));
|
||||
setFloat(std::format("spotLights[{}].outer_cut_off", index), glm::cos(glm::radians(light.outer_cut_off)));
|
||||
}
|
||||
37
18-depth-testing/src/shader.hpp
Normal file
37
18-depth-testing/src/shader.hpp
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#pragma once
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <string>
|
||||
#include <climits>
|
||||
#include <glm/glm.hpp>
|
||||
#include <vector>
|
||||
#include <format>
|
||||
#include "dbc.hpp"
|
||||
#include "components.hpp"
|
||||
|
||||
using components::Material, components::Lighting, components::Light;
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int ID = UINT_MAX;
|
||||
|
||||
Shader(const std::string& vertexPath, const std::string& 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 apply_material(const Material& material);
|
||||
void apply_lighting(const Lighting& lighting);
|
||||
void apply_dir_light(const Light& light, size_t index);
|
||||
void apply_point_light(const Light& light, size_t index);
|
||||
void apply_spot_light(const Light& light, size_t index);
|
||||
};
|
||||
2
18-depth-testing/src/stb_image.cpp
Normal file
2
18-depth-testing/src/stb_image.cpp
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
7988
18-depth-testing/src/stb_image.h
Normal file
7988
18-depth-testing/src/stb_image.h
Normal file
File diff suppressed because it is too large
Load diff
11
18-depth-testing/src/utils.cpp
Normal file
11
18-depth-testing/src/utils.cpp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include <fstream>
|
||||
#include "json.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace utils {
|
||||
components::Scene load_scene_config(const std::string& config_file) {
|
||||
std::ifstream f{"config.json"};
|
||||
auto j = json::parse(f);
|
||||
return j["scene"].template get<components::Scene>();
|
||||
}
|
||||
}
|
||||
9
18-depth-testing/src/utils.hpp
Normal file
9
18-depth-testing/src/utils.hpp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include "components.hpp"
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
namespace utils {
|
||||
components::Scene load_scene_config(const std::string& config_file);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue