Initial
This commit is contained in:
311
include/KHR/khrplatform.h
Normal file
311
include/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_ */
|
||||
105
include/camera.hpp
Normal file
105
include/camera.hpp
Normal file
@@ -0,0 +1,105 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <stb_image.h>
|
||||
|
||||
#include <shader.hpp>
|
||||
#include <object.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using glm::vec2;
|
||||
using glm::vec3;
|
||||
using glm::mat4;
|
||||
|
||||
const float PI = 3.14159f;
|
||||
|
||||
enum Direction
|
||||
{
|
||||
FORWARD,
|
||||
RIGHT,
|
||||
BACKWARD,
|
||||
LEFT
|
||||
};
|
||||
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
vec3 position;
|
||||
vec3 cameraFront;
|
||||
vec3 cameraUp;
|
||||
vec3 direction;
|
||||
vec3 angles;
|
||||
|
||||
Camera()
|
||||
{
|
||||
position = vec3(0, 0, 10);
|
||||
cameraFront = vec3(0, 0, -1);
|
||||
cameraUp = vec3(0, 1, 0);
|
||||
angles = vec3(0, -PI / 2, 0);
|
||||
|
||||
updateDirection();
|
||||
}
|
||||
|
||||
mat4 viewMatrix()
|
||||
{
|
||||
return glm::lookAt(position, position + direction, vec3(0, 1, 0));
|
||||
}
|
||||
|
||||
void setAngles(vec3 angles)
|
||||
{
|
||||
angles = angles;
|
||||
updateDirection();
|
||||
}
|
||||
|
||||
void updateAngles(float deltaX, float deltaY)
|
||||
{
|
||||
angles.x += deltaX;
|
||||
angles.y += deltaY;
|
||||
|
||||
if (angles.x > (PI / 2) - 0.1f) angles.x = (PI / 2) - 0.1f;
|
||||
if (angles.x < -(PI / 2) - 0.1f) angles.x = -((PI / 2) - 0.1f);
|
||||
|
||||
updateDirection();
|
||||
}
|
||||
|
||||
void updateDirection()
|
||||
{
|
||||
direction.x = cos(angles.y) * cos(angles.x);
|
||||
direction.y = sin(angles.x);
|
||||
direction.z = sin(angles.y) * cos(angles.x);
|
||||
direction = glm::normalize(direction);
|
||||
}
|
||||
|
||||
void setPosition(vec3 p)
|
||||
{
|
||||
position = p;
|
||||
}
|
||||
|
||||
void movePosition(vec3 delta)
|
||||
{
|
||||
position += delta;
|
||||
}
|
||||
|
||||
void movePosition(Direction d, float speed)
|
||||
{
|
||||
switch (d)
|
||||
{
|
||||
case static_cast<int>(FORWARD):
|
||||
movePosition(speed * direction);
|
||||
break;
|
||||
case static_cast<int>(BACKWARD):
|
||||
movePosition(-speed * direction);
|
||||
break;
|
||||
case static_cast<int>(RIGHT):
|
||||
movePosition(speed * glm::normalize(glm::cross(direction, cameraUp)));
|
||||
break;
|
||||
case static_cast<int>(LEFT):
|
||||
movePosition(-speed * glm::normalize(glm::cross(direction, cameraUp)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
2129
include/glad/glad.h
Normal file
2129
include/glad/glad.h
Normal file
File diff suppressed because it is too large
Load Diff
41
include/line.hpp
Normal file
41
include/line.hpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef LINE_H
|
||||
#define LINE_H
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include <shader.hpp>
|
||||
#include <texture.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
using std::vector;
|
||||
using glm::vec3;
|
||||
using glm::mat4;
|
||||
|
||||
class Line {
|
||||
public:
|
||||
vec3 position;
|
||||
vec3 rotation;
|
||||
vec3 scale;
|
||||
|
||||
vector<float> vertices;
|
||||
vector<unsigned int> indices;
|
||||
|
||||
unsigned int VAO;
|
||||
unsigned int VBO;
|
||||
unsigned int EBO;
|
||||
|
||||
Line(const vec3 position, const vec3 rotation, const vec3 scale, vector<float> v, vector<unsigned int> i);
|
||||
|
||||
void generateImage();
|
||||
void draw(Shader s);
|
||||
mat4 translationMatrix();
|
||||
mat4 rotationMatrix();
|
||||
mat4 scaleMatrix();
|
||||
mat4 modelMatrix();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
432
include/object.hpp
Normal file
432
include/object.hpp
Normal file
@@ -0,0 +1,432 @@
|
||||
#ifndef OBJECT_H
|
||||
#define OBJECT_H
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include <shader.hpp>
|
||||
#include <texture.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
using std::vector;
|
||||
using glm::vec3;
|
||||
using glm::mat4;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
vec3 position;
|
||||
vec3 color;
|
||||
vec3 normal;
|
||||
} Vertex;
|
||||
|
||||
class Object
|
||||
{
|
||||
public:
|
||||
const char* path;
|
||||
|
||||
vec3 position;
|
||||
vec3 rotation;
|
||||
vec3 scale;
|
||||
|
||||
vector<float> vertexData;
|
||||
|
||||
vector<Vertex> vertices;
|
||||
vector<vec3> normals;
|
||||
vector<unsigned int> indices;
|
||||
vector<unsigned int> normalIndices;
|
||||
|
||||
unsigned int VAO;
|
||||
unsigned int VBO;
|
||||
unsigned int EBO;
|
||||
|
||||
Texture texture1;
|
||||
|
||||
Object()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
Object(const vec3 p, const vec3 r, const vec3 s, const char* objFilePath)
|
||||
{
|
||||
this->position = p;
|
||||
this->rotation = r;
|
||||
this->scale = s;
|
||||
this->path = objFilePath;
|
||||
|
||||
|
||||
|
||||
std::string obj;
|
||||
std::ifstream objFile;
|
||||
std::stringstream objStringStream;
|
||||
|
||||
objFile.exceptions(std::ifstream::failbit | std:: ifstream::badbit);
|
||||
|
||||
try
|
||||
{
|
||||
objFile.open(objFilePath);
|
||||
|
||||
objStringStream << objFile.rdbuf();
|
||||
objFile.close();
|
||||
|
||||
obj = objStringStream.str();
|
||||
}
|
||||
catch (std::ifstream::failure e)
|
||||
{
|
||||
std::cout << "Error reading obj file.";
|
||||
}
|
||||
|
||||
std::string line;
|
||||
while (std::getline(objStringStream, line))
|
||||
{
|
||||
//std::cout << "\n\nLINE: " << line << std::endl;
|
||||
std::istringstream lineStream(line);
|
||||
std::string lineType;
|
||||
lineStream >> lineType;
|
||||
//std::cout << lineType << std::endl;
|
||||
|
||||
//std::cout << "Reading line of type " << lineType << std::endl;
|
||||
|
||||
if (lineType == "v")
|
||||
{
|
||||
Vertex vertex;
|
||||
std::string token;
|
||||
float vector[3];
|
||||
for (int i = 0 ; i < 6; i++)
|
||||
{
|
||||
//std::cout << "I VALUE: " << i << std::endl;
|
||||
float value = 1;
|
||||
if (!(lineStream >> token))
|
||||
{
|
||||
vector[i % 3] = 1;
|
||||
//std::cout << "Adding 1 to vertexData because nothing else." << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
vector[i % 3] = std::stof(token);
|
||||
//std::cout << "Adding " << token << " to vertexData." << std::endl;
|
||||
}
|
||||
|
||||
if (i == 2)
|
||||
{
|
||||
vertex.position = vec3(vector[0], vector[1], vector[2]);
|
||||
//std::cout << " BEGIN POSITION" << std::endl <<vector[0] << " ";
|
||||
//std::cout << vector[1] << " ";
|
||||
//std::cout << vector[2] << " " << std::endl;
|
||||
}
|
||||
else if (i == 5)
|
||||
{
|
||||
vertex.color = vec3(vector[0], vector[1], vector[2]);
|
||||
//std::cout << " BEGIN COLOR" << std::endl <<vector[0] << " ";
|
||||
//std::cout << vector[1] << " ";
|
||||
//std::cout << vector[2] << " " << std::endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
vertices.push_back(vertex);
|
||||
}
|
||||
else if (lineType == "vn")
|
||||
{
|
||||
vec3 normal;
|
||||
std::string token;
|
||||
float vector[3];
|
||||
for (int i = 0 ; i < 3; i++)
|
||||
{
|
||||
//std::cout << "I VALUE: " << i << std::endl;
|
||||
float value = 1;
|
||||
if (!(lineStream >> token))
|
||||
{
|
||||
vector[i % 3] = 1;
|
||||
//std::cout << "Adding 1 to vertexData because nothing else." << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
vector[i % 3] = std::stof(token);
|
||||
//std::cout << "Adding " << token << " to vertexData." << std::endl;
|
||||
}
|
||||
|
||||
if (i == 2)
|
||||
{
|
||||
normal = vec3(vector[0], vector[1], vector[2]);
|
||||
//std::cout << " BEGIN POSITION" << std::endl <<vector[0] << " ";
|
||||
//std::cout << vector[1] << " ";
|
||||
//std::cout << vector[2] << " " << std::endl;
|
||||
}
|
||||
}
|
||||
normals.push_back(normal);
|
||||
}
|
||||
else if (lineType == "f")
|
||||
{
|
||||
std::string token;
|
||||
std::vector<unsigned int> tokenIndices;
|
||||
std::vector<unsigned int> tokenTexCoords;
|
||||
std::vector<unsigned int> tokenNormals;
|
||||
for (int i = 0; lineStream >> token; i++)
|
||||
{
|
||||
std::istringstream tokenStream(token);
|
||||
std::string element;
|
||||
int index, texCoord, normal;
|
||||
for (int i = 0; std::getline(tokenStream, element, '/'); i++)
|
||||
{
|
||||
//std::cout << "element is " << element << std::endl;
|
||||
switch (i)
|
||||
{
|
||||
// Vertex index
|
||||
case 0:
|
||||
index = std::stoi(token);
|
||||
tokenIndices.push_back(index);
|
||||
break;
|
||||
|
||||
// Vertex texture coordinates
|
||||
case 1:
|
||||
texCoord = std::stoi(token);
|
||||
tokenTexCoords.push_back(texCoord);
|
||||
break;
|
||||
|
||||
// Vertex normal
|
||||
case 2:
|
||||
if (normals.size() == 0) break;
|
||||
normal = std::stoi(token);
|
||||
tokenNormals.push_back(normal);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Iterate through each index (X/0/0) in the f line
|
||||
for (int i = 1; i < tokenIndices.size() - 1; i++)
|
||||
{
|
||||
//std::cout << "token index " << i << std::endl;
|
||||
// Create triangle (0, i, i + 1)
|
||||
for (int which = 0; which < 3; which++)
|
||||
{
|
||||
int index;
|
||||
switch (which)
|
||||
{
|
||||
case 0:
|
||||
index = 0;
|
||||
break;
|
||||
case 1:
|
||||
index = i;
|
||||
break;
|
||||
case 2:
|
||||
index = i + 1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (index >= 0)
|
||||
{
|
||||
//std::cout << "pushing " <<tokenIndices[index] - 1 << " to indices" << std::endl;
|
||||
indices.push_back(tokenIndices[index] - 1);
|
||||
if (tokenNormals.size() > index) normalIndices.push_back(tokenNormals[index] - 1);
|
||||
//if (tokenNormals.size() == tokenIndices.size())
|
||||
//{
|
||||
// normals.push_back(tokenNormals[index - 1]);
|
||||
//}
|
||||
//std::cout << "Pushed " << index - 1 << " to indices" << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Negative index
|
||||
indices.push_back((vertices.size()) + tokenIndices[index]);
|
||||
if (tokenNormals.size() > index) normalIndices.push_back((normals.size()) + tokenNormals[index]);
|
||||
//if (tokenNormals.size() == tokenIndices.size())
|
||||
//{
|
||||
// normals.push_back((vertexData.size() / 6) + tokenNormals[index]);
|
||||
//}
|
||||
//std::cout << "Pushed " << vertexData.size() + index << " to indices" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << path << ": Indices: " << indices.size() << "; Normals: " << normalIndices.size() << std::endl;
|
||||
|
||||
/*for (vec3 normal : normals)
|
||||
{
|
||||
std::cout << normal.x << " " << normal.y << " " << normal.z << std::endl;
|
||||
}
|
||||
*/std::cout << std::endl;
|
||||
|
||||
// Loop through indices and insert normals to vertices
|
||||
for (int i = 0; i < indices.size(); i++)
|
||||
{
|
||||
if (indices.size() != normalIndices.size())
|
||||
{
|
||||
std::cout << "WARNING: Inequal number of vertex indices and normal indices. Skipping normals." << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
vertices[indices[i]].normal = normals[normalIndices[i]];
|
||||
}
|
||||
|
||||
// Generate vertexData
|
||||
for (int i = 0; i < vertices.size(); i++)
|
||||
{
|
||||
Vertex vertex = vertices[i];
|
||||
|
||||
vertexData.push_back(vertex.position.x);
|
||||
vertexData.push_back(vertex.position.y);
|
||||
vertexData.push_back(vertex.position.z);
|
||||
|
||||
vertexData.push_back(vertex.color.x);
|
||||
vertexData.push_back(vertex.color.y);
|
||||
vertexData.push_back(vertex.color.z);
|
||||
|
||||
vertexData.push_back(vertex.normal.x);
|
||||
vertexData.push_back(vertex.normal.y);
|
||||
vertexData.push_back(vertex.normal.z);
|
||||
|
||||
//std::cout << vertex.position.x << " ";
|
||||
//std::cout << vertex.position.y << " ";
|
||||
//std::cout << vertex.position.z << "\t\t\t";
|
||||
|
||||
//std::cout << vertex.color.x << " ";
|
||||
//std::cout << vertex.color.y << " ";
|
||||
//std::cout << vertex.color.z << std::endl;
|
||||
}
|
||||
|
||||
//std::cout << "Length of vertices: " << vertices.size() << std::endl;
|
||||
//std::cout << "Path of this one " << path << std::endl << std::endl;
|
||||
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertexData.size() * sizeof(float), &vertexData[0], GL_STATIC_DRAW);
|
||||
|
||||
glGenBuffers(1, &EBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void*)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
}
|
||||
|
||||
/*
|
||||
Object(const vec3 p, const vec3 r, const vec3 s, vector<float> v, vector<unsigned int> i, Texture t1 = nullptr)
|
||||
{
|
||||
this->position = p;
|
||||
this->rotation = r;
|
||||
this->scale = s;
|
||||
this->vertexData = v;
|
||||
this->indices = i;
|
||||
this->texture1 = t1;
|
||||
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertexData.size() * sizeof(float), &vertexData[0], GL_STATIC_DRAW);
|
||||
|
||||
glGenBuffers(1, &EBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void*)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
}
|
||||
*/
|
||||
void draw(Shader s)
|
||||
{
|
||||
/*mat4 mod = rotationMatrix();
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
std::cout << mod[i][j] << "\t";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}*/
|
||||
s.setMat4("modelMatrix", &modelMatrix()[0][0]);
|
||||
s.setMat4("normalMatrix", &normalMatrix()[0][0]);
|
||||
|
||||
texture1.activate(0);
|
||||
|
||||
s.use();
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
|
||||
|
||||
s.setInt("texture1", 0);
|
||||
s.setVec3("lightColor", 1.0, 1.0, 1.0);
|
||||
s.setVec3("objectColor", 0.8, 0.4, 0.2);
|
||||
s.setVec3("lightPos", sin(glfwGetTime() / 3.0f) * 10, 8.0, cos(glfwGetTime() / 3.0f) * 10);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
mat4 translationMatrix()
|
||||
{
|
||||
return mat4(
|
||||
1, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
position.x, position.y, position.z, 1
|
||||
);
|
||||
}
|
||||
|
||||
mat4 rotationMatrix()
|
||||
{
|
||||
mat4 x(
|
||||
1, 0, 0, 0,
|
||||
0, cos(rotation.x), sin(rotation.x), 0,
|
||||
0, -sin(rotation.x), cos(rotation.x), 0,
|
||||
0, 0, 0, 1
|
||||
);
|
||||
mat4 y(
|
||||
cos(rotation.y), 0, -sin(rotation.y), 0,
|
||||
0, 1, 0, 0,
|
||||
sin(rotation.y), 0, cos(rotation.y), 0,
|
||||
0, 0, 0, 1
|
||||
);
|
||||
mat4 z(
|
||||
cos(rotation.z), sin(rotation.z), 0, 0,
|
||||
-sin(rotation.z), cos(rotation.z), 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1
|
||||
);
|
||||
|
||||
return z * y * x;
|
||||
}
|
||||
|
||||
mat4 scaleMatrix()
|
||||
{
|
||||
return mat4(
|
||||
scale.x, 0, 0, 0,
|
||||
0, scale.y, 0, 0,
|
||||
0, 0, scale.z, 0,
|
||||
0, 0, 0, 1
|
||||
);
|
||||
}
|
||||
|
||||
mat4 modelMatrix()
|
||||
{
|
||||
return translationMatrix() * rotationMatrix() * scaleMatrix();
|
||||
}
|
||||
|
||||
mat4 normalMatrix()
|
||||
{
|
||||
return glm::transpose(glm::inverse(modelMatrix()));
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
137
include/shader.hpp
Normal file
137
include/shader.hpp
Normal file
@@ -0,0 +1,137 @@
|
||||
#ifndef SHADER_H
|
||||
#define SHADER_H
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
using glm::vec3;
|
||||
using glm::mat4;
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int ID;
|
||||
|
||||
Shader()
|
||||
{
|
||||
ID = -1;
|
||||
}
|
||||
|
||||
Shader(const char* vertexPath, const char* fragmentPath)
|
||||
{
|
||||
std::string vertexCode;
|
||||
std::string fragmentCode;
|
||||
std::ifstream vShaderFile;
|
||||
std::ifstream 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" << std::endl;
|
||||
}
|
||||
|
||||
const char* vShaderCode = vertexCode.c_str();
|
||||
const char* fShaderCode = fragmentCode.c_str();
|
||||
|
||||
unsigned int vertex, fragment;
|
||||
int success;
|
||||
char infoLog[512];
|
||||
|
||||
vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
||||
glCompileShader(vertex);
|
||||
|
||||
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragment, 1, &fShaderCode, NULL);
|
||||
glCompileShader(fragment);
|
||||
|
||||
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
ID = glCreateProgram();
|
||||
glAttachShader(ID, vertex);
|
||||
glAttachShader(ID, fragment);
|
||||
glLinkProgram(ID);
|
||||
|
||||
glGetProgramiv(ID, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(ID, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
|
||||
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
}
|
||||
}
|
||||
|
||||
void use()
|
||||
{
|
||||
glUseProgram(ID);
|
||||
}
|
||||
|
||||
void setBool(const std::string &name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
|
||||
}
|
||||
|
||||
void setInt(const std::string &name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
|
||||
void setFloat(const std::string &name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
|
||||
void setVec3(const std::string &name, float x, float y, float z) const
|
||||
{
|
||||
glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z);
|
||||
}
|
||||
|
||||
void setVec3(const std::string &name, vec3 vector) const
|
||||
{
|
||||
glUniform3f(glGetUniformLocation(ID, name.c_str()), vector.x, vector.y, vector.z);
|
||||
}
|
||||
|
||||
void setMat4(const std::string &name, float* matrix) const
|
||||
{
|
||||
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, matrix);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
7988
include/stb_image.h
Normal file
7988
include/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
52
include/texture.hpp
Normal file
52
include/texture.hpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#ifndef TEXTURE_H
|
||||
#define TEXTURE_H
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <stb_image.h>>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Texture
|
||||
{
|
||||
public:
|
||||
unsigned int ID;
|
||||
|
||||
Texture()
|
||||
{
|
||||
ID = -1;
|
||||
}
|
||||
|
||||
Texture(const char* path)
|
||||
{
|
||||
glGenTextures(1, &ID);
|
||||
glBindTexture(GL_TEXTURE_2D, ID);
|
||||
|
||||
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);
|
||||
|
||||
int width, height, nrChannels;
|
||||
unsigned char *data = stbi_load(path, &width, &height, &nrChannels, 0);
|
||||
if (data)
|
||||
{
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Failed to load texture" << std::endl;
|
||||
}
|
||||
stbi_image_free(data);
|
||||
}
|
||||
|
||||
void activate(int id)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0 + id);
|
||||
glBindTexture(GL_TEXTURE_2D, ID);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user