first commit
This commit is contained in:
123
src/Camera.cpp
Normal file
123
src/Camera.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
#include "Camera.hpp"
|
||||
|
||||
// Constructors
|
||||
|
||||
Camera::Camera(
|
||||
glm::vec3 position,
|
||||
float fov_deg,
|
||||
float fov_max,
|
||||
float move_speed,
|
||||
float mouse_sensitivity,
|
||||
float fov_min,
|
||||
float pitch_min,
|
||||
float pitch_max,
|
||||
glm::vec3 up,
|
||||
glm::vec3 world_up,
|
||||
float pitch_deg,
|
||||
float yaw_deg)
|
||||
: move_speed { move_speed }
|
||||
, mouse_sensitivity { mouse_sensitivity }
|
||||
, pitch_min { pitch_min }
|
||||
, pitch_max { pitch_max }
|
||||
, fov_min { fov_min }
|
||||
, fov_max { fov_max }
|
||||
, pos { position }
|
||||
, front {}
|
||||
, up { up }
|
||||
, right {}
|
||||
, world_up { world_up }
|
||||
, fov_deg { fov_deg }
|
||||
, pitch_deg { pitch_deg }
|
||||
, yaw_deg { yaw_deg } {
|
||||
|
||||
this->update_vectors();
|
||||
}
|
||||
|
||||
// public
|
||||
|
||||
float Camera::get_fov_rad() const {
|
||||
return glm::radians(this->fov_deg);
|
||||
}
|
||||
|
||||
glm::mat4 Camera::get_view_matrix() const {
|
||||
return glm::lookAt(this->pos, this->pos + this->front, this->up);
|
||||
}
|
||||
|
||||
void Camera::move_to_direction(
|
||||
const Camera::Direction direction,
|
||||
const float delta_time) {
|
||||
|
||||
const float speed { this->move_speed * delta_time };
|
||||
|
||||
switch (direction) {
|
||||
case Direction::FORWARD:
|
||||
this->pos += this->front * speed;
|
||||
break;
|
||||
|
||||
case Direction::BACKWARD:
|
||||
this->pos -= this->front * speed;
|
||||
break;
|
||||
|
||||
case Direction::LEFT:
|
||||
this->pos -= this->right * speed;
|
||||
break;
|
||||
|
||||
case Direction::RIGHT:
|
||||
this->pos += this->right * speed;
|
||||
break;
|
||||
|
||||
case Direction::UP:
|
||||
this->pos += this->up * speed;
|
||||
break;
|
||||
|
||||
case Direction::DOWN:
|
||||
this->pos -= this->up * speed;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Camera::process_mouse_move(
|
||||
float offset_x,
|
||||
float offset_y,
|
||||
const bool constrain_pitch) {
|
||||
|
||||
offset_x *= this->mouse_sensitivity;
|
||||
offset_y *= this->mouse_sensitivity;
|
||||
|
||||
this->yaw_deg += offset_x;
|
||||
this->pitch_deg -= offset_y;
|
||||
|
||||
if (constrain_pitch) {
|
||||
if (this->pitch_deg > this->pitch_max) {
|
||||
this->pitch_deg = this->pitch_max;
|
||||
} else if (this->pitch_deg < this->pitch_min) {
|
||||
this->pitch_deg = this->pitch_min;
|
||||
}
|
||||
}
|
||||
|
||||
this->update_vectors();
|
||||
}
|
||||
|
||||
void Camera::process_mouse_scroll(float offset_y) {
|
||||
this->fov_deg -= offset_y;
|
||||
if (this->fov_deg < this->fov_min) {
|
||||
this->fov_deg = this->fov_min;
|
||||
} else if (this->fov_deg > this->fov_max) {
|
||||
this->fov_deg = this->fov_max;
|
||||
}
|
||||
}
|
||||
|
||||
// private
|
||||
|
||||
void Camera::update_vectors() {
|
||||
this->front = glm::normalize(glm::vec3 {
|
||||
std::cos(glm::radians(this->yaw_deg)) * std::cos(glm::radians(this->pitch_deg)),
|
||||
std::sin(glm::radians(this->pitch_deg)),
|
||||
std::sin(glm::radians(this->yaw_deg)) * std::cos(glm::radians(this->pitch_deg)) });
|
||||
|
||||
this->right = glm::normalize(glm::cross(this->front, this->world_up));
|
||||
this->up = glm::normalize(glm::cross(this->right, this->front));
|
||||
}
|
||||
86
src/Shader.cpp
Normal file
86
src/Shader.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#include <print>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include "glad/glad.h"
|
||||
|
||||
#include "Shader.hpp"
|
||||
#include "quit.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
Shader::Shader(std::filesystem::path& vertex_path, std::filesystem::path& fragment_path)
|
||||
: id{0} {
|
||||
|
||||
std::string vertex_code { read_entire_file(vertex_path) };
|
||||
std::string fragment_code { read_entire_file(fragment_path) };
|
||||
if (vertex_code == "" || fragment_code == "") {
|
||||
quit(1);
|
||||
}
|
||||
|
||||
const char* vertex_code_c_str { vertex_code.c_str() };
|
||||
const char* fragment_code_c_str { fragment_code.c_str() };
|
||||
|
||||
unsigned int vertex_shader { glCreateShader(GL_VERTEX_SHADER) };
|
||||
if (vertex_shader == 0) {
|
||||
std::println(stderr, "Failed to create vertex shader.");
|
||||
quit(1);
|
||||
}
|
||||
|
||||
glShaderSource(vertex_shader, 1, &vertex_code_c_str, nullptr);
|
||||
glCompileShader(vertex_shader);
|
||||
check_shader_compile_error(vertex_shader);
|
||||
|
||||
unsigned int fragment_shader { glCreateShader(GL_FRAGMENT_SHADER) };
|
||||
if (fragment_shader == 0) {
|
||||
std::println(stderr, "Failed to create fragment shader.");
|
||||
quit(1);
|
||||
}
|
||||
|
||||
glShaderSource(fragment_shader, 1, &fragment_code_c_str, nullptr);
|
||||
glCompileShader(fragment_shader);
|
||||
check_shader_compile_error(fragment_shader);
|
||||
|
||||
this->id = glCreateProgram();
|
||||
if (this->id == 0) {
|
||||
std::println(stderr, "Failed to create shader program");
|
||||
quit(1);
|
||||
}
|
||||
|
||||
glAttachShader(this->id, vertex_shader);
|
||||
glAttachShader(this->id, fragment_shader);
|
||||
|
||||
glLinkProgram(this->id);
|
||||
check_shader_program_link_error(this->id);
|
||||
|
||||
glDeleteShader(vertex_shader);
|
||||
glDeleteShader(fragment_shader);
|
||||
}
|
||||
|
||||
unsigned int Shader::get_id() const {
|
||||
return this->id;
|
||||
}
|
||||
|
||||
void Shader::use() const {
|
||||
glUseProgram(this->id);
|
||||
}
|
||||
|
||||
void Shader::set_mat4(const std::string_view& name, const glm::mat4& value) {
|
||||
const int uniform { glGetUniformLocation(this->id, name.data()) };
|
||||
if (uniform == -1) {
|
||||
std::println(stderr, "Could not find uniform '{}'.", name);
|
||||
quit(1);
|
||||
}
|
||||
glUniformMatrix4fv(uniform, 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
void Shader::set_vec4(const std::string_view& name, const glm::vec4& value) {
|
||||
const int uniform { glGetUniformLocation(this->id, name.data()) };
|
||||
if (uniform == -1) {
|
||||
std::println(stderr, "Could not find uniform '{}'.", name);
|
||||
quit(1);
|
||||
}
|
||||
glUniform4fv(uniform, 1, glm::value_ptr(value));
|
||||
}
|
||||
104
src/Shape.cpp
Normal file
104
src/Shape.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
#include <memory>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
|
||||
#include "glad/glad.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "Shape.hpp"
|
||||
|
||||
|
||||
// Shape class
|
||||
|
||||
// Constructors
|
||||
|
||||
Shape::Shape(
|
||||
std::shared_ptr<Shader> shader,
|
||||
ShapeInfo& shapeInfo,
|
||||
glm::vec3 pos
|
||||
)
|
||||
: rotation { 0.0f, 0.0f, 0.0f }
|
||||
, vertices { shapeInfo.vertices }
|
||||
, indices { shapeInfo.indices }
|
||||
, pos { pos }
|
||||
, vao { 0 }
|
||||
, vbo { 0 }
|
||||
, ebo { 0 }
|
||||
, shader {shader} {
|
||||
|
||||
int orig_vao { 0 };
|
||||
int orig_vbo { 0 };
|
||||
int orig_ebo { 0 };
|
||||
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &orig_vao);
|
||||
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &orig_vbo);
|
||||
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &orig_ebo);
|
||||
|
||||
glGenVertexArrays(1, &this->vao);
|
||||
glBindVertexArray(this->vao);
|
||||
|
||||
glGenBuffers(1, &this->vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(float), this->vertices.data(), GL_STATIC_DRAW);
|
||||
|
||||
unsigned int attrib_index { 0 };
|
||||
int elem_count { 3 };
|
||||
GLenum data_type { GL_FLOAT };
|
||||
bool normalize { false };
|
||||
std::size_t stride { 3 * sizeof(float) };
|
||||
void* first_value { (void*) 0 };
|
||||
glVertexAttribPointer(attrib_index, elem_count, data_type, normalize, stride, first_value);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glGenBuffers(1, &this->ebo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(unsigned int), this->indices.data(), GL_STATIC_DRAW);
|
||||
|
||||
glBindVertexArray(orig_vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, orig_vbo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, orig_ebo);
|
||||
}
|
||||
|
||||
// private
|
||||
|
||||
void Shape::draw() const {
|
||||
int orig_vao { 0 };
|
||||
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &orig_vao);
|
||||
|
||||
glm::mat4 model { 1.0f };
|
||||
model = glm::translate(model, this->pos);
|
||||
for (int i { 0 }; i < this->rotation.length(); i++) {
|
||||
const float rot_amount { this->rotation[i] };
|
||||
if (rot_amount != 0.0f) {
|
||||
glm::vec3 rot_axis { 0.0f };
|
||||
rot_axis[i] = 1.0f;
|
||||
model = glm::rotate(model, glm::radians(rot_amount), rot_axis);
|
||||
}
|
||||
}
|
||||
this->shader->set_mat4("model", model);
|
||||
|
||||
glBindVertexArray(this->vao);
|
||||
glDrawElements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, nullptr);
|
||||
|
||||
glBindVertexArray(orig_vao);
|
||||
}
|
||||
|
||||
|
||||
// Premade shapes
|
||||
|
||||
std::map<std::string, ShapeInfo> shapes {
|
||||
{
|
||||
"triangle",
|
||||
{
|
||||
.vertices = {
|
||||
0.0f, 0.5f, 0.0f, // top
|
||||
-0.5f, -0.5f, 0.0f, // right
|
||||
0.5f, -0.5f, 0.0f, // left
|
||||
},
|
||||
.indices = {
|
||||
0, 1, 2
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
1833
src/glad.c
Normal file
1833
src/glad.c
Normal file
File diff suppressed because it is too large
Load Diff
63
src/headers/Camera.hpp
Normal file
63
src/headers/Camera.hpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Camera {
|
||||
public:
|
||||
enum class Direction {
|
||||
FORWARD,
|
||||
BACKWARD,
|
||||
LEFT,
|
||||
RIGHT,
|
||||
UP,
|
||||
DOWN
|
||||
};
|
||||
|
||||
float move_speed;
|
||||
float mouse_sensitivity;
|
||||
float pitch_min;
|
||||
float pitch_max;
|
||||
float fov_min;
|
||||
float fov_max;
|
||||
|
||||
Camera(
|
||||
glm::vec3 position,
|
||||
float fov_deg,
|
||||
float fov_max,
|
||||
float move_speed,
|
||||
float mouse_sensitivity,
|
||||
float fov_min = 0.0f,
|
||||
float pitch_min = -89.0f,
|
||||
float pitch_max = 89.0f,
|
||||
glm::vec3 up = { 0.0f, 1.0f, 0.0f },
|
||||
glm::vec3 world_up = { 0.0f, 1.0f, 0.0f },
|
||||
float pitch_deg = 0.0f,
|
||||
float yaw_deg = -90.0f);
|
||||
|
||||
float get_fov_rad() const;
|
||||
|
||||
glm::mat4 get_view_matrix() const;
|
||||
|
||||
void move_to_direction(
|
||||
const Camera::Direction direction,
|
||||
const float delta_time);
|
||||
|
||||
void process_mouse_move(
|
||||
float offset_x,
|
||||
float offset_y,
|
||||
const bool constrain_pitch = true);
|
||||
|
||||
void process_mouse_scroll(float offset_y);
|
||||
|
||||
private:
|
||||
glm::vec3 pos;
|
||||
glm::vec3 front;
|
||||
glm::vec3 up;
|
||||
glm::vec3 right;
|
||||
glm::vec3 world_up;
|
||||
float fov_deg;
|
||||
float pitch_deg;
|
||||
float yaw_deg;
|
||||
|
||||
void update_vectors();
|
||||
};
|
||||
311
src/headers/KHR/khrplatform.h
Normal file
311
src/headers/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_ */
|
||||
20
src/headers/Shader.hpp
Normal file
20
src/headers/Shader.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
#include <filesystem>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Shader {
|
||||
public:
|
||||
Shader(std::filesystem::path& vertex_path, std::filesystem::path& fragment_path);
|
||||
|
||||
unsigned int get_id() const;
|
||||
|
||||
void use() const;
|
||||
void set_mat4(const std::string_view& name, const glm::mat4& value);
|
||||
void set_vec4(const std::string_view& name, const glm::vec4& value);
|
||||
|
||||
private:
|
||||
unsigned int id;
|
||||
};
|
||||
36
src/headers/Shape.hpp
Normal file
36
src/headers/Shape.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "Shader.hpp"
|
||||
|
||||
struct ShapeInfo {
|
||||
std::vector<float> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
};
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
glm::vec3 rotation;
|
||||
|
||||
Shape(
|
||||
std::shared_ptr<Shader> shader,
|
||||
ShapeInfo& shapeInfo,
|
||||
glm::vec3 pos = { 0.0f, 0.0f, 0.0f }
|
||||
);
|
||||
|
||||
void draw() const;
|
||||
|
||||
private:
|
||||
std::vector<float> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
glm::vec3 pos;
|
||||
unsigned int vao;
|
||||
unsigned int vbo;
|
||||
unsigned int ebo;
|
||||
|
||||
std::shared_ptr<Shader> shader;
|
||||
};
|
||||
3694
src/headers/glad/glad.h
Normal file
3694
src/headers/glad/glad.h
Normal file
File diff suppressed because it is too large
Load Diff
3
src/headers/quit.hpp
Normal file
3
src/headers/quit.hpp
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
void quit(int code);
|
||||
12
src/headers/settings.hpp
Normal file
12
src/headers/settings.hpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
struct Settings {
|
||||
std::filesystem::path vertex_shader_path;
|
||||
std::filesystem::path fragment_shader_path;
|
||||
|
||||
void init();
|
||||
std::string str() const;
|
||||
};
|
||||
7988
src/headers/stb_image.h
Normal file
7988
src/headers/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
6
src/headers/util.hpp
Normal file
6
src/headers/util.hpp
Normal file
@@ -0,0 +1,6 @@
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
std::string read_entire_file(std::filesystem::path& path);
|
||||
void check_shader_compile_error(const unsigned int shader_id);
|
||||
void check_shader_program_link_error(const unsigned int program);
|
||||
213
src/main.cpp
Normal file
213
src/main.cpp
Normal file
@@ -0,0 +1,213 @@
|
||||
#include <map>
|
||||
#include <print>
|
||||
#include <string>
|
||||
|
||||
#include "glad/glad.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "Camera.hpp"
|
||||
#include "Shader.hpp"
|
||||
#include "Shape.hpp"
|
||||
#include "quit.hpp"
|
||||
#include "settings.hpp"
|
||||
|
||||
extern std::map<std::string, ShapeInfo> shapes;
|
||||
|
||||
int window_width { 800 };
|
||||
int window_height { 600 };
|
||||
|
||||
static struct {
|
||||
float last_x;
|
||||
float last_y;
|
||||
} mouse {
|
||||
.last_x = window_width / 2.0f,
|
||||
.last_y = window_height / 2.0f
|
||||
};
|
||||
|
||||
Settings settings {};
|
||||
|
||||
float delta {};
|
||||
|
||||
Camera camera {
|
||||
glm::vec3 { 0.0f, 0.0f, 1.0f },
|
||||
70.0f,
|
||||
70.0f,
|
||||
1.0f,
|
||||
0.1f
|
||||
};
|
||||
|
||||
void print_glfw_error() {
|
||||
const char* desc {};
|
||||
const int err { glfwGetError(&desc) };
|
||||
std::println(stderr, "GLFW Error. code: {}, desc: {}", err, desc);
|
||||
}
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
||||
(void) window;
|
||||
window_width = width;
|
||||
window_height = height;
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
|
||||
(void) window;
|
||||
|
||||
static bool first_mouse_input { true };
|
||||
|
||||
if (first_mouse_input) {
|
||||
mouse.last_x = xpos;
|
||||
mouse.last_y = ypos;
|
||||
first_mouse_input = false;
|
||||
}
|
||||
|
||||
float offset_x { static_cast<float>(xpos) - mouse.last_x };
|
||||
float offset_y { static_cast<float>(ypos) - mouse.last_y };
|
||||
mouse.last_x = xpos;
|
||||
mouse.last_y = ypos;
|
||||
|
||||
camera.process_mouse_move(offset_x, offset_y);
|
||||
}
|
||||
|
||||
void mouse_scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
|
||||
(void) window;
|
||||
(void) xoffset;
|
||||
|
||||
camera.process_mouse_scroll(yoffset);
|
||||
}
|
||||
|
||||
void process_keyboard_input(GLFWwindow* window) {
|
||||
// Window
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
return;
|
||||
}
|
||||
|
||||
// Camera
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
||||
camera.move_to_direction(Camera::Direction::FORWARD, delta);
|
||||
} else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
||||
camera.move_to_direction(Camera::Direction::BACKWARD, delta);
|
||||
}
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
||||
camera.move_to_direction(Camera::Direction::LEFT, delta);
|
||||
} else if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
||||
camera.move_to_direction(Camera::Direction::RIGHT, delta);
|
||||
}
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
|
||||
camera.move_to_direction(Camera::Direction::UP, delta);
|
||||
} else if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) {
|
||||
camera.move_to_direction(Camera::Direction::DOWN, delta);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
settings.init();
|
||||
std::println(
|
||||
"-----------------------------------------\n"
|
||||
"Settings:\n\n"
|
||||
"{}\n"
|
||||
"-----------------------------------------\n",
|
||||
settings.str());
|
||||
|
||||
if (glfwInit() != GLFW_TRUE) {
|
||||
print_glfw_error();
|
||||
quit(1);
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
|
||||
|
||||
GLFWwindow* window { glfwCreateWindow(
|
||||
window_width,
|
||||
window_height,
|
||||
"LuaGl",
|
||||
nullptr,
|
||||
nullptr) };
|
||||
if (window == nullptr) {
|
||||
print_glfw_error();
|
||||
quit(1);
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, mouse_scroll_callback);
|
||||
|
||||
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
|
||||
std::println(stderr, "Failed to init GLAD.");
|
||||
quit(1);
|
||||
}
|
||||
|
||||
glViewport(0, 0, window_width, window_height);
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
auto shader { std::make_shared<Shader>(settings.vertex_shader_path, settings.fragment_shader_path) };
|
||||
shader->use();
|
||||
|
||||
std::vector<Shape> created_shapes {
|
||||
{ shader, shapes.at("triangle"), { 0.0f, 0.0f, 0.0f } },
|
||||
{ shader, shapes.at("triangle"), { 0.5f, 0.0f, -0.5f } },
|
||||
{ shader, shapes.at("triangle"), { -0.5f, 0.0f, -0.5f } },
|
||||
{ shader, shapes.at("triangle"), { 0.0f, 0.0f, -1.0f } }
|
||||
};
|
||||
|
||||
created_shapes.at(1).rotation.y = 90.0f;
|
||||
created_shapes.at(2).rotation.y = 90.0f;
|
||||
|
||||
std::array<glm::vec4, 4> colors {
|
||||
glm::vec4 { 1.0f, 0.0f, 0.0f, 0.0f },
|
||||
{ 0.0f, 1.0f, 0.0f, 0.0f },
|
||||
{ 0.0f, 0.0f, 1.0f, 0.0f },
|
||||
{ 1.0f, 1.0f, 0.0f, 0.0f },
|
||||
};
|
||||
|
||||
long unsigned int frame { 0 };
|
||||
float last_frame_time { 0 };
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
const float current_time { static_cast<float>(glfwGetTime()) };
|
||||
delta = current_time - last_frame_time;
|
||||
last_frame_time = current_time;
|
||||
|
||||
std::println("Frame: {}", frame);
|
||||
frame++;
|
||||
|
||||
process_keyboard_input(window);
|
||||
|
||||
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glm::mat4 view { camera.get_view_matrix() };
|
||||
shader->set_mat4("view", view);
|
||||
|
||||
float aspect_ratio { static_cast<float>(window_width) / window_height };
|
||||
constexpr float near_plane { 0.1f };
|
||||
constexpr float far_plane { 100.0f };
|
||||
const glm::mat4 projection {
|
||||
glm::perspective(
|
||||
camera.get_fov_rad(),
|
||||
aspect_ratio,
|
||||
near_plane,
|
||||
far_plane)
|
||||
};
|
||||
shader->set_mat4("projection", projection);
|
||||
|
||||
for (int i { 0 }; i < created_shapes.size(); i++) {
|
||||
shader->set_vec4("thecolor", colors[i]);
|
||||
created_shapes[i].draw();
|
||||
}
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
quit(0);
|
||||
}
|
||||
8
src/quit.cpp
Normal file
8
src/quit.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <cstdlib>
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
void quit(int code) {
|
||||
glfwTerminate();
|
||||
std::exit(code);
|
||||
}
|
||||
19
src/settings.cpp
Normal file
19
src/settings.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <format>
|
||||
|
||||
#include "settings.hpp"
|
||||
|
||||
void Settings::init() {
|
||||
this->vertex_shader_path = std::filesystem::canonical(VERTEX_SHADER_PATH);
|
||||
this->fragment_shader_path = std::filesystem::canonical(FRAGMENT_SHADER_PATH);
|
||||
}
|
||||
|
||||
std::string Settings::str() const {
|
||||
return std::format(
|
||||
"vertex_shader_path : {}\n"
|
||||
"fragment_shader_path: {}",
|
||||
this->vertex_shader_path.c_str(),
|
||||
this->fragment_shader_path.c_str()
|
||||
);
|
||||
}
|
||||
10
src/shaders/shader.frag
Normal file
10
src/shaders/shader.frag
Normal file
@@ -0,0 +1,10 @@
|
||||
#version 460 core
|
||||
|
||||
out vec4 frag_color;
|
||||
|
||||
uniform vec4 thecolor;
|
||||
|
||||
void main() {
|
||||
// frag_color = vec4(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
frag_color = thecolor;
|
||||
}
|
||||
11
src/shaders/shader.vert
Normal file
11
src/shaders/shader.vert
Normal file
@@ -0,0 +1,11 @@
|
||||
#version 460 core
|
||||
|
||||
layout (location = 0) in vec3 a_pos;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main() {
|
||||
gl_Position = projection * view * model * vec4(a_pos, 1.0f);
|
||||
}
|
||||
47
src/util.cpp
Normal file
47
src/util.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <print>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
|
||||
#include "glad/glad.h"
|
||||
|
||||
#include "util.hpp"
|
||||
#include "quit.hpp"
|
||||
|
||||
std::string read_entire_file(std::filesystem::path& path) {
|
||||
std::ifstream file_stream;
|
||||
file_stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
|
||||
try {
|
||||
file_stream.open(path);
|
||||
std::stringstream string_stream;
|
||||
string_stream << file_stream.rdbuf();
|
||||
return string_stream.str();
|
||||
} catch (std::ifstream::failure& e) {
|
||||
std::println(stderr, "Failed to read file '{}'. {}", path.c_str(), e.what());
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
void check_shader_compile_error(const unsigned int shader_id) {
|
||||
int success {};
|
||||
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
char info_log[512];
|
||||
glGetShaderInfoLog(shader_id, 512, nullptr, info_log);
|
||||
std::println(stderr, "Shader compilation failed: {}", info_log);
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void check_shader_program_link_error(const unsigned int program) {
|
||||
int success {};
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &success);
|
||||
if (!success) {
|
||||
char info_log[512];
|
||||
glGetProgramInfoLog(program, 512, nullptr, info_log);
|
||||
std::println(stderr, "Linking shader program failed: {}", info_log);
|
||||
quit(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user