Compare commits
32 Commits
91cbfcef04
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 692fbf8196 | |||
| a75360a8ef | |||
| b693f6809b | |||
| 6554e0dc74 | |||
| 0f9f72af4e | |||
| 6d6bac8ca1 | |||
| 78f53fdd54 | |||
| 78ba333164 | |||
| 6e69a543f0 | |||
| 8350b85aa1 | |||
| cfc47df080 | |||
| b734e8270d | |||
| 452e9bdc92 | |||
| 16d9959ace | |||
| e0a695c7b1 | |||
| c639afe08b | |||
| 8885b40709 | |||
| 8f7ca3ae38 | |||
| b06cf47480 | |||
| 335c79e23e | |||
| 987961cfe4 | |||
| 1409eb78ae | |||
| 753c6cead6 | |||
| d233bbe720 | |||
| c085fdbf5a | |||
| 15cc550bf9 | |||
| 8cb5f8c773 | |||
| d79f49535e | |||
| fc06658659 | |||
| 8d28b81b89 | |||
| b045b1e2d3 | |||
| d1403cd681 |
13
.gitea/workflows/nix-check.yaml
Normal file
13
.gitea/workflows/nix-check.yaml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
name: Verify build
|
||||||
|
run-name: ${{ gitea.actor }} is building
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
verify_build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: cachix/install-nix-action@v30
|
||||||
|
with:
|
||||||
|
nix_path: nixpkgs=channel:nixos-24.11
|
||||||
|
- run: nix-shell shell.nix --run make
|
||||||
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
.direnv
|
||||||
|
*.o
|
||||||
|
ogl
|
||||||
311
headers/KHR/khrplatform.h
Normal file
311
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_ */
|
||||||
29
headers/camera.hpp
Normal file
29
headers/camera.hpp
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#ifndef CAMERA_H
|
||||||
|
#define CAMERA_H
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
class Camera {
|
||||||
|
public:
|
||||||
|
Camera(glm::vec3 pos, float yaw, float pitch, float fov, float aspect);
|
||||||
|
~Camera();
|
||||||
|
void setPos(glm::vec3 pos);
|
||||||
|
void addPos(glm::vec3 pos);
|
||||||
|
void addFPos(glm::vec3 pos);
|
||||||
|
void setRotate(float yaw, float pitch);
|
||||||
|
void addRotate(float yaw, float pitch);
|
||||||
|
glm::vec3 getPos();
|
||||||
|
glm::mat4 getView();
|
||||||
|
glm::mat4 getProjection();
|
||||||
|
float aspect;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void update();
|
||||||
|
glm::mat4 projection;
|
||||||
|
glm::vec3 up;
|
||||||
|
glm::vec3 front;
|
||||||
|
glm::vec3 pos;
|
||||||
|
float yaw;
|
||||||
|
float pitch;
|
||||||
|
float fov;
|
||||||
|
};
|
||||||
|
#endif
|
||||||
2129
headers/glad/glad.h
Normal file
2129
headers/glad/glad.h
Normal file
File diff suppressed because it is too large
Load Diff
31
headers/mesh.hpp
Normal file
31
headers/mesh.hpp
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#ifndef MESH_H
|
||||||
|
#define MESH_H
|
||||||
|
#include "camera.hpp"
|
||||||
|
#include "shader.hpp"
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/quaternion.hpp>
|
||||||
|
|
||||||
|
class Mesh {
|
||||||
|
public:
|
||||||
|
Mesh(Shader *s, unsigned int VAO, int count);
|
||||||
|
Mesh(Shader *s, std::string path);
|
||||||
|
~Mesh();
|
||||||
|
void draw(Camera *c);
|
||||||
|
void setPosition(glm::vec3 p);
|
||||||
|
void setScale(glm::vec3 s);
|
||||||
|
void setRotation(glm::quat r);
|
||||||
|
void setOwnedBuffers(bool o);
|
||||||
|
void setOwnedShader(bool o);
|
||||||
|
Shader *getShader();
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool buffersOwned;
|
||||||
|
bool shaderOwned;
|
||||||
|
int count;
|
||||||
|
unsigned int VAO;
|
||||||
|
Shader *shader;
|
||||||
|
glm::vec3 position; /*0,0,0*/
|
||||||
|
glm::vec3 scale; /*1,1,1*/
|
||||||
|
glm::quat rotation; /*1,0,0,0*/
|
||||||
|
};
|
||||||
|
#endif
|
||||||
4
headers/obj.hpp
Normal file
4
headers/obj.hpp
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
unsigned int loadOBJ(const char *path, std::vector<float> *out_vertices);
|
||||||
25
headers/shader.hpp
Normal file
25
headers/shader.hpp
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef SHADER_H
|
||||||
|
#define SHADER_H
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Shader {
|
||||||
|
public:
|
||||||
|
Shader(const char *vertexPath, const char *fragmentPath);
|
||||||
|
~Shader();
|
||||||
|
void use();
|
||||||
|
void setInt(const std::string &name, int value);
|
||||||
|
void setFloat(const std::string &name, float value);
|
||||||
|
void setVec3(const std::string &name, glm::vec3 value);
|
||||||
|
void setMat4(const std::string &name, glm::mat4 value);
|
||||||
|
|
||||||
|
private:
|
||||||
|
unsigned int ID;
|
||||||
|
unsigned int shaderCMPL(GLenum type, const char *src);
|
||||||
|
void shaderERR(unsigned int shader);
|
||||||
|
unsigned int shaderProgramCMPL(const char *vertexSource,
|
||||||
|
const char *fragmentSource);
|
||||||
|
void shaderProgramERR(unsigned int shaderProgram);
|
||||||
|
};
|
||||||
|
#endif
|
||||||
167
headers/shapes.h
Normal file
167
headers/shapes.h
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
float tri_vertices[] = {
|
||||||
|
-0.5f, -0.5f, 0.0f, /**/
|
||||||
|
0.5f, -0.5f, 0.0f, /**/
|
||||||
|
0.0f, 0.5f, 0.0f /**/
|
||||||
|
};
|
||||||
|
|
||||||
|
float tri_rgb_vertices[] = {
|
||||||
|
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, /**/
|
||||||
|
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, /**/
|
||||||
|
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f /**/
|
||||||
|
};
|
||||||
|
|
||||||
|
float sqr_vertices[] = {
|
||||||
|
0.5f, 0.5f, 0.0f, /*top R*/
|
||||||
|
0.5f, -0.5f, 0.0f, /*bottom R*/
|
||||||
|
-0.5f, -0.5f, 0.0f, /*bottom L*/
|
||||||
|
-0.5f, 0.5f, 0.0f /*top R*/
|
||||||
|
};
|
||||||
|
|
||||||
|
unsigned int sqr_indices[] = {
|
||||||
|
0, 1, 3, /*tl triangle*/
|
||||||
|
1, 2, 3 /*br triangle*/
|
||||||
|
};
|
||||||
|
|
||||||
|
float sqr_tex_vertices[] = {
|
||||||
|
/*positions*colors*texture coords*/
|
||||||
|
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, /*top right*/
|
||||||
|
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, /*bottom right*/
|
||||||
|
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, /*bottom left*/
|
||||||
|
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f /*top left*/
|
||||||
|
};
|
||||||
|
|
||||||
|
float cube_vertices[] = {
|
||||||
|
/*positions*texture cords*/
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, /**/
|
||||||
|
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, /**/
|
||||||
|
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, /**/
|
||||||
|
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, /**/
|
||||||
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, /**/
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, /**/
|
||||||
|
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, /**/
|
||||||
|
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, /**/
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, /**/
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, /**/
|
||||||
|
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, /**/
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, /**/
|
||||||
|
|
||||||
|
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, /**/
|
||||||
|
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, /**/
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, /**/
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, /**/
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, /**/
|
||||||
|
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, /**/
|
||||||
|
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, /**/
|
||||||
|
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, /**/
|
||||||
|
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, /**/
|
||||||
|
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, /**/
|
||||||
|
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, /**/
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, /**/
|
||||||
|
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, /**/
|
||||||
|
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, /**/
|
||||||
|
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, /**/
|
||||||
|
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, /**/
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, /**/
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, /**/
|
||||||
|
|
||||||
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, /**/
|
||||||
|
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, /**/
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, /**/
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, /**/
|
||||||
|
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, /**/
|
||||||
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f /**/
|
||||||
|
};
|
||||||
|
|
||||||
|
float uvcube[] = {
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||||
|
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||||
|
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||||
|
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||||
|
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||||
|
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||||
|
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||||
|
|
||||||
|
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||||
|
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
|
||||||
|
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||||
|
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||||
|
|
||||||
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
|
||||||
|
};
|
||||||
|
|
||||||
|
const char *basicVertexShaderSource =
|
||||||
|
"#version 330 core\n"
|
||||||
|
"layout (location = 0) in vec3 aPos;\n"
|
||||||
|
"out vec3 color;\n"
|
||||||
|
"void main()\n"
|
||||||
|
"{\n"
|
||||||
|
" color = aPos;"
|
||||||
|
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
|
||||||
|
"}\0";
|
||||||
|
|
||||||
|
const char *basicFragmentShaderSource = "#version 330 core\n"
|
||||||
|
"out vec3 FragColor;\n"
|
||||||
|
"in vec3 color;"
|
||||||
|
"void main()\n"
|
||||||
|
"{\n"
|
||||||
|
" FragColor = (color/2+.5)*1.5;\n"
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
const char *rgbVertexShaderSource =
|
||||||
|
"#version 330 core\n"
|
||||||
|
"layout (location = 0) in vec3 aPos;\n"
|
||||||
|
"layout (location = 1) in vec3 aColor;\n"
|
||||||
|
"out vec3 color;\n"
|
||||||
|
"void main()\n"
|
||||||
|
"{\n"
|
||||||
|
" color = aColor;\n"
|
||||||
|
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
|
||||||
|
"}\0";
|
||||||
|
|
||||||
|
const char *rgbFragmentShaderSource = "#version 330 core\n"
|
||||||
|
"out vec4 FragColor;\n"
|
||||||
|
"in vec3 color;\n"
|
||||||
|
"void main()\n"
|
||||||
|
"{\n"
|
||||||
|
" FragColor = vec4(color, 1.0);\n"
|
||||||
|
"}\0";
|
||||||
|
|
||||||
|
const char *rgbPulseFragmentShaderSource =
|
||||||
|
"#version 330 core\n"
|
||||||
|
"out vec4 FragColor;\n"
|
||||||
|
"in vec3 color;\n"
|
||||||
|
"uniform float u_time;"
|
||||||
|
"void main()\n"
|
||||||
|
"{\n"
|
||||||
|
" FragColor = vec4(color,0.0)*(sin(u_time)/2.0+0.5);\n"
|
||||||
|
"}\0";
|
||||||
7988
headers/stb_image.h
Normal file
7988
headers/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
15
headers/texture.hpp
Normal file
15
headers/texture.hpp
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef TEXTURE_H
|
||||||
|
#define TEXTURE_H
|
||||||
|
#include <glad/glad.h>
|
||||||
|
|
||||||
|
class Texture {
|
||||||
|
public:
|
||||||
|
Texture(GLenum type, const char *path);
|
||||||
|
~Texture();
|
||||||
|
void bind(GLenum texture);
|
||||||
|
|
||||||
|
private:
|
||||||
|
unsigned int ID;
|
||||||
|
GLenum type;
|
||||||
|
};
|
||||||
|
#endif
|
||||||
34
makefile
Normal file
34
makefile
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
CC := gcc
|
||||||
|
CXX := g++
|
||||||
|
CFLAGS := -Wall -Wextra -I headers -ggdb
|
||||||
|
CXXFLAGS := -Wall -Wextra -I headers -l glfw -ggdb
|
||||||
|
|
||||||
|
# List all the source files
|
||||||
|
C_FILES := $(wildcard src/*.c)
|
||||||
|
CPP_FILES := $(wildcard src/*.cpp)
|
||||||
|
|
||||||
|
# Generate corresponding object file names
|
||||||
|
OBJ_FILES := $(C_FILES:.c=.o) $(CPP_FILES:.cpp=.o)
|
||||||
|
|
||||||
|
# Define the target executable
|
||||||
|
TARGET := ogl
|
||||||
|
|
||||||
|
# Default target
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
# Linking rule
|
||||||
|
$(TARGET): $(OBJ_FILES)
|
||||||
|
$(CXX) $(CXXFLAGS) $^ -o $@
|
||||||
|
|
||||||
|
# Compilation rule for C files
|
||||||
|
%.o: %.c
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
# Compilation rule for C++ files
|
||||||
|
%.o: %.cpp
|
||||||
|
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
# Clean rule
|
||||||
|
clean:
|
||||||
|
rm -f $(TARGET) $(OBJ_FILES)
|
||||||
|
|
||||||
15
shaders/MVPTexNoRGBVert.glsl
Normal file
15
shaders/MVPTexNoRGBVert.glsl
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec2 aTexCoord;
|
||||||
|
|
||||||
|
out vec2 TexCoord;
|
||||||
|
|
||||||
|
uniform mat4 model;
|
||||||
|
uniform mat4 view;
|
||||||
|
uniform mat4 projection;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||||
|
TexCoord = aTexCoord;
|
||||||
|
}
|
||||||
18
shaders/MVPTexVert.glsl
Normal file
18
shaders/MVPTexVert.glsl
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aColor;
|
||||||
|
layout (location = 2) in vec2 aTexCoord;
|
||||||
|
|
||||||
|
out vec3 ourColor;
|
||||||
|
out vec2 TexCoord;
|
||||||
|
|
||||||
|
uniform mat4 model;
|
||||||
|
uniform mat4 view;
|
||||||
|
uniform mat4 projection;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||||
|
ourColor = aColor;
|
||||||
|
TexCoord = aTexCoord;
|
||||||
|
}
|
||||||
12
shaders/TexFrag.glsl
Normal file
12
shaders/TexFrag.glsl
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec3 ourColor;
|
||||||
|
in vec2 TexCoord;
|
||||||
|
|
||||||
|
uniform sampler2D tex0;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = texture(tex0, TexCoord) * vec4(ourColor, 1.0);
|
||||||
|
}
|
||||||
12
shaders/TexNoRGBFrag.glsl
Normal file
12
shaders/TexNoRGBFrag.glsl
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec3 ourColor;
|
||||||
|
in vec2 TexCoord;
|
||||||
|
|
||||||
|
uniform sampler2D tex0;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = texture(tex0, TexCoord);
|
||||||
|
}
|
||||||
14
shaders/TexVert.glsl
Normal file
14
shaders/TexVert.glsl
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aColor;
|
||||||
|
layout (location = 2) in vec2 aTexCoord;
|
||||||
|
|
||||||
|
out vec3 ourColor;
|
||||||
|
out vec2 TexCoord;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos, 1.0);
|
||||||
|
ourColor = aColor;
|
||||||
|
TexCoord = aTexCoord;
|
||||||
|
}
|
||||||
50
shaders/lighting/Frag.glsl
Normal file
50
shaders/lighting/Frag.glsl
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#version 330 core
|
||||||
|
struct Material {
|
||||||
|
vec3 ambient;
|
||||||
|
vec3 diffuse;
|
||||||
|
vec3 specular;
|
||||||
|
float shininess;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct Light {
|
||||||
|
vec3 position;
|
||||||
|
vec3 ambient;
|
||||||
|
vec3 diffuse;
|
||||||
|
vec3 specular;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec3 FragPos;
|
||||||
|
in vec3 Normal;
|
||||||
|
|
||||||
|
uniform vec3 ourColor;
|
||||||
|
uniform vec3 viewPos;
|
||||||
|
uniform Material material;
|
||||||
|
uniform Light light;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
//misc
|
||||||
|
vec3 norm = normalize(Normal);
|
||||||
|
vec3 lightDir = normalize(light.position - FragPos);
|
||||||
|
|
||||||
|
// ambient
|
||||||
|
vec3 ambient = light.ambient * material.ambient;
|
||||||
|
|
||||||
|
// diffuse
|
||||||
|
float diff = max(dot(norm, lightDir), 0.0);
|
||||||
|
vec3 diffuse = light.diffuse * (diff * material.diffuse);
|
||||||
|
|
||||||
|
// specular
|
||||||
|
vec3 viewDir = normalize(viewPos - FragPos);
|
||||||
|
vec3 reflectDir = reflect(-lightDir, norm);
|
||||||
|
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||||
|
vec3 specular = light.specular * (spec * material.specular);
|
||||||
|
|
||||||
|
//out
|
||||||
|
vec3 result = ambient + diffuse + specular;
|
||||||
|
FragColor = vec4(result, 1.0);
|
||||||
|
}
|
||||||
9
shaders/lighting/LightFrag.glsl
Normal file
9
shaders/lighting/LightFrag.glsl
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
uniform vec3 ourColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = vec4(ourColor, 1.0);
|
||||||
|
}
|
||||||
18
shaders/lighting/Vert.glsl
Normal file
18
shaders/lighting/Vert.glsl
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aNormal;
|
||||||
|
|
||||||
|
out vec3 FragPos;
|
||||||
|
out vec3 Normal;
|
||||||
|
|
||||||
|
uniform mat4 model;
|
||||||
|
uniform mat4 view;
|
||||||
|
uniform mat4 projection;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||||
|
FragPos = vec3(model * vec4(aPos, 1.0));
|
||||||
|
Normal = aNormal;
|
||||||
|
//Normal = mat3(transpose(inverse(model))) * aNormal;//if non unifrom scale
|
||||||
|
}
|
||||||
8
shaders/rgbPulseFrag.glsl
Normal file
8
shaders/rgbPulseFrag.glsl
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
in vec3 color;
|
||||||
|
uniform float u_time;
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = vec4(color,0.0)*(sin(u_time)/2.0+0.5);
|
||||||
|
}
|
||||||
9
shaders/rgbPulseVert.glsl
Normal file
9
shaders/rgbPulseVert.glsl
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aColor;
|
||||||
|
out vec3 color;
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
color = aColor;
|
||||||
|
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||||
|
}
|
||||||
15
shell.nix
Normal file
15
shell.nix
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{pkgs ? import <nixpkgs> {}}:
|
||||||
|
with pkgs;
|
||||||
|
mkShell rec {
|
||||||
|
packages = [gdb clang-tools];
|
||||||
|
nativeBuildInputs = [
|
||||||
|
pkg-config
|
||||||
|
gcc
|
||||||
|
gnumake
|
||||||
|
];
|
||||||
|
buildInputs = [
|
||||||
|
glfw3
|
||||||
|
glm
|
||||||
|
];
|
||||||
|
LD_LIBRARY_PATH = lib.makeLibraryPath buildInputs;
|
||||||
|
}
|
||||||
59
src/camera.cpp
Normal file
59
src/camera.cpp
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
#include <camera.hpp>
|
||||||
|
#include <glm/ext/matrix_clip_space.hpp>
|
||||||
|
#include <glm/ext/matrix_transform.hpp>
|
||||||
|
|
||||||
|
Camera::Camera(glm::vec3 pos, float yaw, float pitch, float fov, float aspect)
|
||||||
|
: aspect(aspect), up(glm::vec3(0, 1, 0)), pos(pos), yaw(yaw), pitch(pitch),
|
||||||
|
fov(fov) {
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
Camera::~Camera() {}
|
||||||
|
|
||||||
|
void Camera::setRotate(float yaw, float pitch) {
|
||||||
|
this->yaw = yaw;
|
||||||
|
this->pitch = pitch;
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::addRotate(float yaw, float pitch) {
|
||||||
|
this->yaw += yaw;
|
||||||
|
this->pitch += pitch;
|
||||||
|
if (this->pitch > 89.0f)
|
||||||
|
this->pitch = 89.0f;
|
||||||
|
if (this->pitch < -89.0f)
|
||||||
|
this->pitch = -89.0f;
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::setPos(glm::vec3 pos) { this->pos = pos; }
|
||||||
|
|
||||||
|
void Camera::addPos(glm::vec3 pos) { this->pos += pos; }
|
||||||
|
|
||||||
|
void Camera::addFPos(glm::vec3 pos) {
|
||||||
|
glm::vec3 f = glm::normalize(glm::vec3(front.x, 0, front.z));
|
||||||
|
glm::vec3 r = glm::normalize(glm::cross(f, up));
|
||||||
|
this->pos += f * pos.x;
|
||||||
|
this->pos += up * pos.y;
|
||||||
|
this->pos += r * pos.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::update() {
|
||||||
|
glm::vec3 direction;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::mat4 Camera::getView() {
|
||||||
|
glm::mat4 view = glm::mat4(1.0f);
|
||||||
|
view = glm::lookAt(pos, pos + front, up);
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 Camera::getPos() { return pos; }
|
||||||
|
|
||||||
|
glm::mat4 Camera::getProjection() {
|
||||||
|
return glm::perspective(glm::radians(fov), aspect, 0.1f, 100.0f);
|
||||||
|
}
|
||||||
1140
src/glad.c
Normal file
1140
src/glad.c
Normal file
File diff suppressed because it is too large
Load Diff
160
src/main.cpp
Normal file
160
src/main.cpp
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
#include "camera.hpp"
|
||||||
|
#include "glad/glad.h"
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <cassert>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <mesh.hpp>
|
||||||
|
#include <shader.hpp>
|
||||||
|
#include <texture.hpp>
|
||||||
|
|
||||||
|
#define UNUSED(x) (void)(x)
|
||||||
|
|
||||||
|
glm::vec3 processInput(GLFWwindow *window);
|
||||||
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
||||||
|
void mouse_callback(GLFWwindow *window, double xpos, double ypos);
|
||||||
|
|
||||||
|
float deltaTime = 0;
|
||||||
|
|
||||||
|
GLFWwindow *init() {
|
||||||
|
glfwInit();
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
|
||||||
|
GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
||||||
|
assert(window && "Window Failed");
|
||||||
|
glfwMakeContextCurrent(window);
|
||||||
|
|
||||||
|
int is_ok = 0;
|
||||||
|
is_ok = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
||||||
|
assert(is_ok && "Failed to init glad");
|
||||||
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||||
|
|
||||||
|
glViewport(0, 0, 800, 600);
|
||||||
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||||
|
glfwSetCursorPosCallback(window, mouse_callback);
|
||||||
|
return window;
|
||||||
|
}
|
||||||
|
|
||||||
|
void showFPS(GLFWwindow *window, double delta) {
|
||||||
|
char title[22];
|
||||||
|
title[21] = '\0';
|
||||||
|
snprintf(title, 20, "OpenGl FPS: [%3.2f]", (1 / delta));
|
||||||
|
glfwSetWindowTitle(window, title);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
glm::vec3 lightPos = glm::vec3(1, 2.0f, 1);
|
||||||
|
glm::vec3 lightColor = glm::vec3(1);
|
||||||
|
|
||||||
|
GLFWwindow *window = init();
|
||||||
|
Camera *cam = new Camera(glm::vec3(0), 0, 0, 45, 800.0 / 600);
|
||||||
|
glfwSetWindowUserPointer(window, cam);
|
||||||
|
|
||||||
|
Shader *cubeShader = new Shader("./shaders/lighting/Vert.glsl",
|
||||||
|
"./shaders/lighting/Frag.glsl");
|
||||||
|
Shader *lightShader = new Shader("./shaders/lighting/Vert.glsl",
|
||||||
|
"./shaders/lighting/LightFrag.glsl");
|
||||||
|
|
||||||
|
Mesh *cube = new Mesh(cubeShader, "./cube.obj");
|
||||||
|
Mesh *light = new Mesh(lightShader, "./cube.obj");
|
||||||
|
cube->setOwnedShader(true);
|
||||||
|
light->setOwnedShader(true);
|
||||||
|
|
||||||
|
light->setPosition(lightPos);
|
||||||
|
light->setScale(glm::vec3(.3));
|
||||||
|
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
float lastFrame = 0;
|
||||||
|
while (!glfwWindowShouldClose(window)) {
|
||||||
|
float currentFrame = (float)glfwGetTime();
|
||||||
|
deltaTime = currentFrame - lastFrame;
|
||||||
|
lastFrame = currentFrame;
|
||||||
|
showFPS(window, deltaTime);
|
||||||
|
|
||||||
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
Shader *s = cube->getShader();
|
||||||
|
s->use();
|
||||||
|
s->setVec3("lightColor", lightColor);
|
||||||
|
s->setVec3("ourColor", glm::vec3(0.24725, 0.1995, 0.0745));
|
||||||
|
s->setVec3("lightPos", lightPos);
|
||||||
|
s->setVec3("viewPos", cam->getPos());
|
||||||
|
|
||||||
|
s->setVec3("light.ambient", glm::vec3(0.2));
|
||||||
|
s->setVec3("light.diffuse", glm::vec3(0.5));
|
||||||
|
s->setVec3("light.specular", glm::vec3(1));
|
||||||
|
s->setVec3("light.position", lightPos);
|
||||||
|
|
||||||
|
s->setVec3("material.ambient", glm::vec3(1.0, 0.5, 0.31));
|
||||||
|
s->setVec3("material.diffuse", glm::vec3(1.0, 0.5, 0.31));
|
||||||
|
s->setVec3("material.specular", glm::vec3(0.5));
|
||||||
|
s->setFloat("material.shininess", 32);
|
||||||
|
cube->draw(cam);
|
||||||
|
|
||||||
|
s = light->getShader();
|
||||||
|
s->use();
|
||||||
|
s->setVec3("ourColor", lightColor);
|
||||||
|
light->draw(cam);
|
||||||
|
|
||||||
|
glfwSwapBuffers(window);
|
||||||
|
glfwPollEvents();
|
||||||
|
glm::vec3 mov = processInput(window);
|
||||||
|
mov *= deltaTime;
|
||||||
|
cam->addFPos(mov);
|
||||||
|
}
|
||||||
|
|
||||||
|
delete cam;
|
||||||
|
delete cube;
|
||||||
|
delete light;
|
||||||
|
glfwTerminate();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 processInput(GLFWwindow *window) {
|
||||||
|
glm::vec3 mov = glm::vec3(0);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
|
glfwSetWindowShouldClose(window, true);
|
||||||
|
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||||
|
mov += glm::vec3(1, 0, 0);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||||
|
mov -= glm::vec3(1, 0, 0);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||||
|
mov += glm::vec3(0, 0, 1);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||||
|
mov -= glm::vec3(0, 0, 1);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
|
||||||
|
mov += glm::vec3(0, 1, 0);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
|
||||||
|
mov -= glm::vec3(0, 1, 0);
|
||||||
|
|
||||||
|
mov *= 5;
|
||||||
|
return mov;
|
||||||
|
}
|
||||||
|
|
||||||
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
||||||
|
UNUSED(window);
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
Camera *cam = (Camera *)glfwGetWindowUserPointer(window);
|
||||||
|
if (cam)
|
||||||
|
cam->aspect = (float)width / height;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouse_callback(GLFWwindow *window, double xpos, double ypos) {
|
||||||
|
static float xprev = 0.0, yprev = 0.0;
|
||||||
|
|
||||||
|
float xchange = xpos - xprev;
|
||||||
|
float ychange = yprev - ypos;
|
||||||
|
xprev = xpos;
|
||||||
|
yprev = ypos;
|
||||||
|
|
||||||
|
float sensitivity = 0.1f;
|
||||||
|
xchange *= sensitivity;
|
||||||
|
ychange *= sensitivity;
|
||||||
|
|
||||||
|
Camera *cam = (Camera *)glfwGetWindowUserPointer(window);
|
||||||
|
if (cam)
|
||||||
|
cam->addRotate(xchange, ychange);
|
||||||
|
}
|
||||||
78
src/mesh.cpp
Normal file
78
src/mesh.cpp
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
#include "camera.hpp"
|
||||||
|
#include "obj.hpp"
|
||||||
|
#include "shader.hpp"
|
||||||
|
#include <mesh.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
unsigned int meshInit(std::string path, std::vector<float> &data);
|
||||||
|
|
||||||
|
Mesh::Mesh(Shader *s, unsigned int VAO, int count)
|
||||||
|
: buffersOwned(false), shaderOwned(false), count(count), VAO(VAO),
|
||||||
|
shader(s), position(glm::vec3(0)), scale(glm::vec3(1)),
|
||||||
|
rotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f)) {}
|
||||||
|
|
||||||
|
Mesh::Mesh(Shader *s, std::string path)
|
||||||
|
: buffersOwned(true), shaderOwned(false), shader(s), position(glm::vec3(0)),
|
||||||
|
scale(glm::vec3(1)), rotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f)) {
|
||||||
|
std::vector<float> data;
|
||||||
|
VAO = meshInit(path, data);
|
||||||
|
count = data.size() / 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
Mesh::~Mesh() {
|
||||||
|
if (shaderOwned)
|
||||||
|
delete shader;
|
||||||
|
if (buffersOwned) {
|
||||||
|
glDeleteVertexArrays(1, &VAO);
|
||||||
|
// TODO vbo, ibo
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mesh::draw(Camera *c) {
|
||||||
|
glm::mat4 model = glm::mat4(1);
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
shader->use();
|
||||||
|
shader->setMat4("view", c->getView());
|
||||||
|
shader->setMat4("projection", c->getProjection());
|
||||||
|
model = glm::translate(model, position);
|
||||||
|
model = glm::scale(model, scale);
|
||||||
|
model = model * glm::mat4_cast(rotation);
|
||||||
|
shader->setMat4("model", model);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mesh::setPosition(glm::vec3 p) { position = p; }
|
||||||
|
|
||||||
|
void Mesh::setScale(glm::vec3 s) { scale = s; }
|
||||||
|
|
||||||
|
void Mesh::setRotation(glm::quat r) { rotation = r; }
|
||||||
|
|
||||||
|
void Mesh::setOwnedBuffers(bool o) { buffersOwned = o; }
|
||||||
|
|
||||||
|
void Mesh::setOwnedShader(bool o) { shaderOwned = o; }
|
||||||
|
|
||||||
|
Shader *Mesh::getShader() { return shader; }
|
||||||
|
|
||||||
|
unsigned int meshInit(std::string path, std::vector<float> &data) {
|
||||||
|
unsigned int VBO, VAO;
|
||||||
|
glGenVertexArrays(1, &VAO);
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
|
||||||
|
glGenBuffers(1, &VBO);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
|
||||||
|
loadOBJ(path.c_str(), &data);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), data.data(),
|
||||||
|
GL_STATIC_DRAW);
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
||||||
|
(void *)(3 * sizeof(float)));
|
||||||
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
||||||
|
(void *)(6 * sizeof(float)));
|
||||||
|
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glEnableVertexAttribArray(2); // not in use
|
||||||
|
glBindVertexArray(0);
|
||||||
|
return VAO;
|
||||||
|
}
|
||||||
66
src/obj.cpp
Normal file
66
src/obj.cpp
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
#include "obj.hpp"
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
/*this is ugly but works*/
|
||||||
|
unsigned int loadOBJ(const char *path, std::vector<float> *out_vertices) {
|
||||||
|
std::vector<glm::vec3> verts, norms;
|
||||||
|
std::vector<glm::vec2> uvs;
|
||||||
|
|
||||||
|
FILE *file = fopen(path, "r");
|
||||||
|
assert(file && "file not found");
|
||||||
|
|
||||||
|
int tmp = 0;
|
||||||
|
while (1) {
|
||||||
|
char lineHeader[128];
|
||||||
|
int res = fscanf(file, "%s", lineHeader);
|
||||||
|
if (res == EOF)
|
||||||
|
break;
|
||||||
|
if (strcmp(lineHeader, "v") == 0) {
|
||||||
|
glm::vec3 vert;
|
||||||
|
tmp = fscanf(file, "%f %f %f\n", &vert.x, &vert.y, &vert.z);
|
||||||
|
assert(tmp == 3 && "bad vert");
|
||||||
|
verts.push_back(vert);
|
||||||
|
} else if (strcmp(lineHeader, "vt") == 0) {
|
||||||
|
glm::vec2 uv;
|
||||||
|
tmp = fscanf(file, "%f %f\n", &uv.x, &uv.y);
|
||||||
|
assert(tmp == 2 && "bad uv");
|
||||||
|
uvs.push_back(uv);
|
||||||
|
} else if (strcmp(lineHeader, "vn") == 0) {
|
||||||
|
glm::vec3 norm;
|
||||||
|
tmp = fscanf(file, "%f %f %f\n", &norm.x, &norm.y, &norm.z);
|
||||||
|
assert(tmp == 3 && "bad norm");
|
||||||
|
norms.push_back(norm);
|
||||||
|
} else if (strcmp(lineHeader, "f") == 0) {
|
||||||
|
unsigned int vIndex[3], uvIndex[3], nIndex[3];
|
||||||
|
tmp = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vIndex[0],
|
||||||
|
&uvIndex[0], &nIndex[0], &vIndex[1], &uvIndex[1], &nIndex[1],
|
||||||
|
&vIndex[2], &uvIndex[2], &nIndex[2]);
|
||||||
|
assert(tmp == 9 && "bad face");
|
||||||
|
|
||||||
|
for (size_t i = 0; i < 3; ++i) {
|
||||||
|
size_t vi, ni, ui;
|
||||||
|
vi = (vIndex[i] - 1);
|
||||||
|
ni = (nIndex[i] - 1);
|
||||||
|
ui = (uvIndex[i] - 1);
|
||||||
|
assert(verts.size() > vi);
|
||||||
|
assert(norms.size() > ni);
|
||||||
|
assert(uvs.size() > ui);
|
||||||
|
auto vert = verts[vi];
|
||||||
|
auto norm = norms[ni];
|
||||||
|
auto uv = uvs[ui];
|
||||||
|
out_vertices->push_back(vert.x);
|
||||||
|
out_vertices->push_back(vert.y);
|
||||||
|
out_vertices->push_back(vert.z);
|
||||||
|
out_vertices->push_back(norm.x);
|
||||||
|
out_vertices->push_back(norm.y);
|
||||||
|
out_vertices->push_back(norm.z);
|
||||||
|
out_vertices->push_back(uv.x);
|
||||||
|
out_vertices->push_back(uv.y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return verts.size();
|
||||||
|
}
|
||||||
110
src/shader.cpp
Normal file
110
src/shader.cpp
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
#include <fstream>
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include <shader.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
/*pub*/
|
||||||
|
Shader::Shader(const char *vertexPath, const char *fragmentPath) {
|
||||||
|
std::string vertexCode, fragmentCode;
|
||||||
|
std::ifstream vShaderFile, fShaderFile;
|
||||||
|
|
||||||
|
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
try {
|
||||||
|
vShaderFile.open(vertexPath);
|
||||||
|
fShaderFile.open(fragmentPath);
|
||||||
|
std::stringstream vShaderStream, fShaderStream;
|
||||||
|
|
||||||
|
vShaderStream << vShaderFile.rdbuf();
|
||||||
|
fShaderStream << fShaderFile.rdbuf();
|
||||||
|
|
||||||
|
vShaderFile.close();
|
||||||
|
fShaderFile.close();
|
||||||
|
|
||||||
|
vertexCode = vShaderStream.str();
|
||||||
|
fragmentCode = fShaderStream.str();
|
||||||
|
} catch (std::ifstream::failure &e) {
|
||||||
|
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " << e.what()
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
this->ID = shaderProgramCMPL(vertexCode.c_str(), fragmentCode.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
Shader::~Shader() { glDeleteProgram(ID); }
|
||||||
|
|
||||||
|
void Shader::use() { glUseProgram(ID); }
|
||||||
|
|
||||||
|
void Shader::setInt(const std::string &name, int value) {
|
||||||
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shader::setFloat(const std::string &name, float value) {
|
||||||
|
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shader::setVec3(const std::string &name, glm::vec3 value) {
|
||||||
|
glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1,
|
||||||
|
glm::value_ptr(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shader::setMat4(const std::string &name, glm::mat4 value) {
|
||||||
|
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE,
|
||||||
|
glm::value_ptr(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*priv*/
|
||||||
|
unsigned int Shader::shaderCMPL(GLenum type, const char *src) {
|
||||||
|
unsigned int shader;
|
||||||
|
shader = glCreateShader(type);
|
||||||
|
glShaderSource(shader, 1, &src, NULL);
|
||||||
|
glCompileShader(shader);
|
||||||
|
return shader;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shader::shaderERR(unsigned int shader) {
|
||||||
|
int success;
|
||||||
|
char infoLog[512];
|
||||||
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||||
|
|
||||||
|
if (!success) {
|
||||||
|
glGetShaderInfoLog(shader, 512, NULL, infoLog);
|
||||||
|
std::cout << "ERROR::SHADER::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int Shader::shaderProgramCMPL(const char *vertexSource,
|
||||||
|
const char *fragmentSource) {
|
||||||
|
unsigned int vertexShader;
|
||||||
|
vertexShader = shaderCMPL(GL_VERTEX_SHADER, vertexSource);
|
||||||
|
shaderERR(vertexShader);
|
||||||
|
|
||||||
|
unsigned int fragmentShader;
|
||||||
|
fragmentShader = shaderCMPL(GL_FRAGMENT_SHADER, fragmentSource);
|
||||||
|
shaderERR(fragmentShader);
|
||||||
|
|
||||||
|
unsigned int shaderProgram;
|
||||||
|
shaderProgram = glCreateProgram();
|
||||||
|
|
||||||
|
glAttachShader(shaderProgram, vertexShader);
|
||||||
|
glAttachShader(shaderProgram, fragmentShader);
|
||||||
|
glLinkProgram(shaderProgram);
|
||||||
|
shaderProgramERR(shaderProgram);
|
||||||
|
glDeleteShader(vertexShader);
|
||||||
|
glDeleteShader(fragmentShader);
|
||||||
|
|
||||||
|
return shaderProgram;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shader::shaderProgramERR(unsigned int shaderProgram) {
|
||||||
|
int success;
|
||||||
|
char infoLog[512];
|
||||||
|
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
|
||||||
|
|
||||||
|
if (!success) {
|
||||||
|
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
|
||||||
|
std::cout << "ERROR::SHADER::PROGRAM::FAILED\n" << infoLog << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
2
src/stb_images.c
Normal file
2
src/stb_images.c
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#include "stb_image.h"
|
||||||
33
src/texture.cpp
Normal file
33
src/texture.cpp
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#include <cassert>
|
||||||
|
#include <stb_image.h>
|
||||||
|
#include <texture.hpp>
|
||||||
|
|
||||||
|
Texture::Texture(GLenum type, const char *path) : type(type) {
|
||||||
|
glGenTextures(1, &ID);
|
||||||
|
glBindTexture(type, ID);
|
||||||
|
|
||||||
|
glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||||
|
glTexParameteri(type, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||||
|
glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||||
|
|
||||||
|
int width, height, nrChannels;
|
||||||
|
unsigned char *data;
|
||||||
|
stbi_set_flip_vertically_on_load(true);
|
||||||
|
|
||||||
|
data = stbi_load(path, &width, &height, &nrChannels, 0);
|
||||||
|
assert(data && "Failed to load image");
|
||||||
|
|
||||||
|
glTexImage2D(type, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE,
|
||||||
|
data);
|
||||||
|
glGenerateMipmap(type);
|
||||||
|
|
||||||
|
stbi_image_free(data);
|
||||||
|
};
|
||||||
|
|
||||||
|
Texture::~Texture() {glDeleteTextures(1, &ID);}
|
||||||
|
|
||||||
|
void Texture::bind(GLenum texture) {
|
||||||
|
glActiveTexture(texture);
|
||||||
|
glBindTexture(type, ID);
|
||||||
|
}
|
||||||
BIN
texture/brick.jpg
Normal file
BIN
texture/brick.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 251 KiB |
BIN
texture/container.jpg
Normal file
BIN
texture/container.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 181 KiB |
Reference in New Issue
Block a user