Started to apply the adhesive... A triangle is rotating on a invisible super elastic rubberband and the camera is attached with ethereal paperclips.

This commit is contained in:
Edward R. Gonzalez 2020-02-14 01:13:16 -05:00
parent 4829622988
commit 2b71f57284
23 changed files with 1342 additions and 77 deletions

10
3.3.shader.frag Normal file
View File

@ -0,0 +1,10 @@
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
void main()
{
FragColor = vec4(ourColor, 1.0f);
}

11
3.3.shader.vert Normal file
View File

@ -0,0 +1,11 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
out vec3 ourColor;
void main()
{
gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
}#pragma once

15
5.1.transform.frag Normal file
View File

@ -0,0 +1,15 @@
#version 330 core
out vec4 FragColor;
// in vec2 TexCoord;
// texture samplers
// uniform sampler2D texture1;
// uniform sampler2D texture2;
void main()
{
// linearly interpolate between both textures (80% container, 20% awesomeface)
//FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
}

16
5.1.transform.vert Normal file
View File

@ -0,0 +1,16 @@
#version 330 core
layout (location = 0) in vec3 aPos;
// layout (location = 1) in vec2 aTexCoord;
// out vec2 TexCoord;
uniform mat4 transform;
void main()
{
gl_Position = transform * vec4(aPos, 1.0);
// TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}

View File

@ -0,0 +1,15 @@
#version 330 core
out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2D texture1;
uniform sampler2D texture2;
void main()
{
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
}

View File

@ -0,0 +1,18 @@
#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 = vec2(aTexCoord.x, 1.0 - aTexCoord.y);
}

View File

@ -12,6 +12,18 @@ This merely removes the need to use operators I don't like and wraps them in eas
#pragma once
#include <algorithm>
#include <cstdarg>
#include <exception>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
// Macros
#define sfn \
@ -79,3 +91,12 @@ sfn Exit(ExitCode _code)
{
exit(int(_code));
}
// Error Stuff
sfn ErrorRuntime(const Ref(std::runtime_error) _error)
{
std::cout << "Runtime error occurred: " << _error.what() << std::endl;
return;
}

72
DGL.hpp
View File

@ -1,19 +1,18 @@
#pragma once
// C++ STL
#include <iostream>
#include <stdexcept>
#include <vector>
// GL
//#include <gl/GLU.h>
#include <glew.h>
#include <glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "DGL_FundamentalTypes.hpp"
#include "DGL_MiscTypes.hpp"
#include "DGL_Enum.hpp"
#include "DGL_Shader.hpp"
#include "DGL_Buffers.hpp"
#include "DGL_Space.hpp"
// Non-Standard C++
#include "Cpp_Alias.hpp"
@ -45,18 +44,6 @@ namespace GL
WindowRefList Windows;
// Error Stuff
sfn ErrorRuntime(Ref(std::runtime_error) _error)
{
cout << "Runtime error occurred: " << _error.what() << endl;
return;
}
// Constants
sfn constexpr NotShared () -> ptr<Window > { return NULL; }
@ -64,15 +51,13 @@ namespace GL
// Forward Declares
sfn SwapBuffers(const ptr<Window> _window) -> void;
// Functionality
sfn SwapBuffers(const ptr<Window> _window) -> void
{
glfwSwapBuffers(_window);
return;
}
sfn CanClose(const ptr<Window> _theWindow)
{
return glfwWindowShouldClose(_theWindow);
@ -124,6 +109,16 @@ namespace GL
return;
}
sfn DrawArrays(EPrimitives _primitive, gInt _startingIndex, gInt _numToRender)
{
glDrawArrays(GLenum(_primitive), _startingIndex, _numToRender); // Starting from vertex 0; 3 vertices total -> 1 triangle.
}
sfn DrawElements(EPrimitives _primitive, gSize _numElements, EDataType _dataType, DataPtr _offfsetAddressFromFirstIndex)
{
glDrawElements(GLenum(_primitive), _numElements, GLenum(_dataType), _offfsetAddressFromFirstIndex);
}
sfn GetTime() -> TimeValDec
{
return glfwGetTime();
@ -146,7 +141,7 @@ namespace GL
throw std::runtime_error("Failed to initialize GLFW");
}
}
catch (std::runtime_error _error)
catch (const std::runtime_error _error)
{
ErrorRuntime(_error);
@ -177,7 +172,7 @@ namespace GL
cout << "OpenGL Version: " << glGetString(GL_VERSION) << endl;
}
catch (std::runtime_error _error)
catch (const std::runtime_error _error)
{
ErrorRuntime(_error);
@ -197,7 +192,7 @@ namespace GL
/* Loop until the user closes the window */
while (not CanClose(_window))
{
ClearBuffer();
ClearBuffer(EFrameBuffer::Color);
SwapBuffers(_window);
@ -217,7 +212,14 @@ namespace GL
if (deltaSinceClear > _interval)
{
ClearBuffer();
ClearBuffer(EFrameBuffer::Color, EFrameBuffer::Depth);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.25f, 0.25f, 0.25f, 1.0f);
//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
_renderProcedure();
@ -254,7 +256,7 @@ namespace GL
throw std::runtime_error( Dref(ErrorMsg) );
}
}
catch (std::runtime_error _error)
catch (const std::runtime_error _error)
{
ErrorRuntime(_error);
@ -264,6 +266,18 @@ namespace GL
return;
}
sfn SetPolygonMode(EFace _desiredFaces, EMode _desiredMode)
{
glPolygonMode(GLenum(_desiredFaces), GLenum(_desiredMode));
}
sfn SwapBuffers(const ptr<Window> _window) -> void
{
glfwSwapBuffers(_window);
return;
}
sfn TerminateGLFW()
{
glfwTerminate();

View File

@ -15,35 +15,46 @@
namespace GL
{
sfn ClearBuffer()
{
glClear(GL_COLOR_BUFFER_BIT);
return;
}
sfn BindBuffer(EBufferTarget _targetType, ID<Buffer> _buffer)
sfn BindBuffer(const EBufferTarget _targetType, const ID<Buffer> _buffer)
{
glBindBuffer(GLenum(_targetType), _buffer);
return;
}
sfn BindVertexArray(ptr<gUInt> _referenceToTrackArray)
sfn BindVertexArray(const gUInt _referenceToTrackArray)
{
glBindVertexArray(Dref(_referenceToTrackArray));
glBindVertexArray(_referenceToTrackArray);
return;
}
template<typename TypeOfData>
sfn BufferData(ptr<TypeOfData> _data, EBufferTarget _targetType, EBufferUsage _usageType)
sfn BufferData(ptr<TypeOfData> _data, const EBufferTarget _targetType, const EBufferUsage _usageType)
{
glBufferData(GLenum(_targetType), sizeof(TypeOfData), _data, GLenum(_usageType));
return;
}
template<typename... Type, typename = EFrameBuffer>
sfn ClearBuffer(const Type... _buffersToClear)
{
glClear((gBitfield(_buffersToClear) | ...));
return;
}
sfn DisableVertexAttributeArray(const gInt _vertexAttributeArrayIndex)
{
glDisableVertexAttribArray(_vertexAttributeArrayIndex);
}
sfn EnableVertexAttributeArray(const gInt _vertexAttributeArrayIndex)
{
glEnableVertexAttribArray(_vertexAttributeArrayIndex);
}
template<typename VertType>
sfn FormatVertexAttributes
(

View File

@ -46,6 +46,12 @@ namespace GL
StaticRead = GL_STATIC_READ
};
enum class ECompareMode
{
RefToTexture = GL_COMPARE_REF_TO_TEXTURE,
None = GL_NONE
};
enum class EDataType
{
Byte = GL_BYTE ,
@ -60,19 +66,56 @@ namespace GL
Double = GL_DOUBLE
};
enum class EShaderType
enum class EFace
{
Vertex = GL_VERTEX_SHADER ,
Fragment = GL_FRAGMENT_SHADER
Front = GL_FRONT,
Back = GL_BACK,
Front_and_Back = GL_FRONT_AND_BACK
};
enum class EFrameBuffer
{
Accumulation = GL_ACCUM_BUFFER_BIT ,
Color = GL_COLOR_BUFFER_BIT ,
Depth = GL_DEPTH_BUFFER_BIT ,
Stencil = GL_STENCIL_BUFFER_BIT
};
enum class ELODBias
{
Max = GL_MAX_TEXTURE_LOD_BIAS
};
enum class EMaxFilter
{
Nearest = GL_NEAREST,
Linear = GL_LINEAR
};
enum class EMinFilter
{
Nearest = GL_NEAREST ,
Linear = GL_LINEAR ,
NearestToNearest = GL_NEAREST_MIPMAP_NEAREST,
LinearToNearest = GL_NEAREST_MIPMAP_LINEAR ,
NearestToLinear = GL_NEAREST_MIPMAP_LINEAR ,
LinearToLinear = GL_LINEAR_MIPMAP_LINEAR
};
enum class EMode
{
Point = GL_POINT,
Line = GL_LINE ,
Fill = GL_FILL
};
enum class EPrimitives
{
Points = GL_POINTS,
Points = GL_POINTS,
Lines = GL_LINES ,
LineStrip = GL_LINE_STRIP,
LineLoop = GL_LINE_LOOP ,
Lines = GL_LINES ,
LineStrip = GL_LINE_STRIP,
LineLoop = GL_LINE_LOOP ,
Triangles = GL_TRIANGLES ,
TriangleStrip = GL_TRIANGLE_STRIP,
@ -81,6 +124,119 @@ namespace GL
Quads = GL_QUADS ,
QuadsStrip = GL_QUAD_STRIP,
Patches = GL_PATCHES
Patches = GL_PATCHES,
Polygon = GL_POLYGON
};
enum class EShaderInfo
{
Type = GL_SHADER_TYPE ,
DeleteStatus = GL_DELETE_STATUS ,
CompileStatus = GL_COMPILE_STATUS ,
InfoLogLength = GL_INFO_LOG_LENGTH ,
ShaderSourceLength = GL_SHADER_SOURCE_LENGTH
};
enum class EShaderProgramInfo
{
DeleteStatus = GL_DELETE_STATUS ,
LinkStatus = GL_LINK_STATUS ,
ValidateStatus = GL_VALIDATE_STATUS ,
InfoLogLength = GL_INFO_LOG_LENGTH ,
AttachedShaders = GL_ATTACHED_SHADERS ,
ActiveAttributes = GL_ACTIVE_ATTRIBUTES ,
ActiveAttributesMaxLength = GL_ACTIVE_ATTRIBUTE_MAX_LENGTH ,
ActiveUniforms = GL_ACTIVE_UNIFORMS ,
TransformFeedbackActive = GL_TRANSFORM_FEEDBACK_BUFFER_MODE ,
TransformFeedbackVaryings = GL_TRANSFORM_FEEDBACK_VARYING ,
TransformFeedbackVaryingsMax = GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH,
ShaderVerticiesMax = GL_GEOMETRY_VERTICES_OUT ,
GeometryInputType = GL_GEOMETRY_INPUT_TYPE ,
GeometryOutputType = GL_GEOMETRY_OUTPUT_TYPE
};
enum class EShaderType
{
Vertex = GL_VERTEX_SHADER ,
Fragment = GL_FRAGMENT_SHADER
};
enum class EStencilMode
{
DepthComponent = GL_DEPTH_COMPONENT,
StencilIndex = GL_STENCIL_INDEX
};
enum class ESWIZZLE
{
Red = GL_RED ,
Green = GL_GREEN,
Blue = GL_BLUE ,
Alpha = GL_ALPHA,
Zero = GL_ZERO ,
One = GL_ONE
};
enum class ETexCompareFuncs
{
LessOrEqual = GL_LEQUAL ,
GreaterOrEqual = GL_GEQUAL ,
Less = GL_LESS ,
Greater = GL_GREATER ,
Equal = GL_EQUAL ,
NotEqual = GL_NOTEQUAL,
Always = GL_ALWAYS ,
Never = GL_NEVER
};
enum class ETextureMode
{
DepthStencil = GL_DEPTH_STENCIL_TEXTURE_MODE,
MipmapLowest = GL_TEXTURE_BASE_LEVEL ,
ComparisonOperator = GL_TEXTURE_COMPARE_FUNC ,
Comparision = GL_TEXTURE_COMPARE_MODE ,
LevelOfDetailBias = GL_TEXTURE_LOD_BIAS ,
MinimizingFilter = GL_TEXTURE_MIN_FILTER ,
MagnificationFilter = GL_TEXTURE_MAG_FILTER ,
MinimumLOD = GL_TEXTURE_MIN_LOD ,
MaximumLOD = GL_TEXTURE_MAX_LOD ,
MipmapMax = GL_TEXTURE_MAX_LEVEL ,
Swizzle_R = GL_TEXTURE_SWIZZLE_R ,
Swizzle_G = GL_TEXTURE_SWIZZLE_G ,
Swizzle_B = GL_TEXTURE_SWIZZLE_B ,
Swizzle_A = GL_TEXTURE_SWIZZLE_A ,
Swizzle_RGBA = GL_TEXTURE_SWIZZLE_RGBA ,
Wrap_S = GL_TEXTURE_WRAP_S ,
Wrap_T = GL_TEXTURE_WRAP_T ,
Wrap_R = GL_TEXTURE_WRAP_R
};
enum class ETextureType
{
_1D = GL_TEXTURE_1D,
_2D = GL_TEXTURE_2D,
_3D = GL_TEXTURE_3D,
Rectangle = GL_TEXTURE_RECTANGLE,
Buffer = GL_TEXTURE_BUFFER ,
CubeMap = GL_TEXTURE_CUBE_MAP ,
CubeMapArray = GL_TEXTURE_CUBE_MAP_ARRAY,
Array1D = GL_TEXTURE_1D_ARRAY,
Array2D = GL_TEXTURE_2D_ARRAY,
Multisample = GL_TEXTURE_2D_MULTISAMPLE ,
Multisample2D = GL_TEXTURE_2D_MULTISAMPLE_ARRAY
};
enum class EWrap
{
ClampToEdge = GL_CLAMP_TO_EDGE ,
ClampToBorder = GL_CLAMP_TO_BORDER ,
MirroredRepeat = GL_MIRRORED_REPEAT ,
Repeat = GL_REPEAT ,
MirrorClampToEdge = GL_MIRROR_CLAMP_TO_EDGE
};
}

View File

@ -9,9 +9,10 @@ namespace GL
{
// Fundamental Types
using gChar = GLchar ;
using gFloat = GLfloat;
using gInt = GLint ;
using gUInt = GLuint ;
using gSize = GLsizei;
using gChar = GLchar ;
using gBitfield = GLbitfield;
using gFloat = GLfloat ;
using gInt = GLint ;
using gUInt = GLuint ;
using gSize = GLsizei ;
}

View File

@ -2,6 +2,8 @@
// OpenGL
#include <glew.h>
#include <glm/detail/type_vec3.hpp>
// Duck Tape
#include "DGL_FundamentalTypes.hpp"
@ -13,17 +15,28 @@
namespace GL
{
// Made up types.
namespace Generic
{
template<typename Type>
using Vector3 = glm::tvec3<Type>;
}
using DataPtr = ptr<GLvoid>;
template<typename ReferenceType>
using ID = gUInt;
// ID Reference Types
class Buffer ;
class Shader ;
class Buffer;
class Matrix;
class Shader;
class ShaderProgram;
class VertexBuffer;
class ElementBuffer;
using DataPtr = ptr<GLvoid>;
using VertexBuffer = gUInt ;
using Matrix4x4 = glm::mat4;
using Vector3 = glm::vec3;
using Vector4 = glm::vec4;
}

14
DGL_Primitive.hpp Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include "DGL_Space.hpp"
namespace GL
{
struct Primitive
{
};
}

View File

@ -1,12 +1,10 @@
#pragma once
// C++
#include <cstdarg>
//DGL
#include "DGL_FundamentalTypes.hpp"
#include "DGL_MiscTypes.hpp"
#include "DGL_Enum.hpp"
#include "DGL_Space.hpp"
@ -36,8 +34,48 @@ namespace GL
"}";
// Default Shaders
ID<ShaderProgram> RawShader ,
SimpleShader ,
SimpleShader_Transformed ;
ID<CoordSpace> ScreenSpaceVarID;
// Forward Declarations
sfn GetShaderInfo(ID<Shader> _shader , gSize _logLength , ptr<gSize> _infoLengthRef , RawString<gChar> _infoLogRef) -> void;
sfn QueryShader (ID<Shader> _shaderToQuery, EShaderInfo _shaderInfoDesired, ptr<gInt > _requestedInfoObject ) -> void;
sfn MakeShader(Ref(ID<Shader>) _shaderIDHolder, EShaderType _typeOfShader, uInt64 _numberOfStringElements, ptr<RawString<const gChar>> _sourceCode, ptr<const gInt> _lengthsOfStrings) -> void;
sfn MakeShaderProgram(Ref(ID<ShaderProgram>) _shaderProgramIDHolder, const ID<Shader> _vertexShader, const ID<Shader> _fragShader) -> void;
// Functions
sfn ActiveUniforms(ID<Shader> _shaderToQueryForUniforms) -> GLint
{
GLint uniforms;
glGetProgramiv(_shaderToQueryForUniforms, GL_ACTIVE_UNIFORMS, &uniforms);
for (int i = 0; i < uniforms; i++)
{
int name_len = -1, num = -1;
GLenum type = GL_ZERO;
char name[100];
glGetActiveUniform(_shaderToQueryForUniforms, GLuint(i), sizeof(name) - 1, &name_len, &num, &type, name);
name[name_len] = 0;
}
return uniforms;
}
sfn AttachShader(ID<ShaderProgram> _shaderProgram, ID<Shader> _shaderToAttach)
{
glAttachShader(_shaderProgram, _shaderToAttach);
@ -52,10 +90,34 @@ namespace GL
return;
}
sfn CompileShader(ID<Shader> _shaderToCompile)
sfn CompileShader(const ID<Shader> _shaderToCompile)
{
glCompileShader(_shaderToCompile);
gInt Result = gInt(EBool::False);
gSize InfoLogLength;
QueryShader(_shaderToCompile, EShaderInfo::CompileStatus, Address(Result));
if (!Result)
{
QueryShader(_shaderToCompile, EShaderInfo::InfoLogLength, Address(InfoLogLength));
if (InfoLogLength > 0)
{
std::vector<char> ErrorMessage(InfoLogLength + 1);
GetShaderInfo(_shaderToCompile, InfoLogLength, nullptr, Address(ErrorMessage.at(0)));
throw std::runtime_error(Address(ErrorMessage.at(0)));
}
else
{
throw std::runtime_error("Shader compilation failed and did not get a proper info log.");
}
}
return;
}
@ -76,6 +138,30 @@ namespace GL
return;
}
sfn DetachShader(ID<ShaderProgram> _shaderProgram, ID<Shader> _shaderToDetach)
{
glDetachShader(_shaderProgram, _shaderToDetach);
return;
}
sfn GetShaderInfo(ID<Shader> _shader, gSize _logLength, ptr<gSize> _infoLengthRef, RawString<gChar> _infoLogRef) -> void
{
glGetShaderInfoLog(_shader, _logLength, _infoLengthRef, _infoLogRef);
return;
}
sfn GetShaderProgramInfo(ID<ShaderProgram> _shaderProgram, gSize _logLength, ptr<gSize> _infoLengthRef, RawString<gChar> _infoLogRef) -> void
{
glGetProgramInfoLog(_shaderProgram, _logLength, _infoLengthRef, _infoLogRef);
}
sfn GetUniformVariable(const ID<ShaderProgram> _programID, RawString<const char> _nameOfVariable) -> ID<Matrix>
{
return glGetUniformLocation(_programID, _nameOfVariable);
}
sfn LinkProgramShader(ID<ShaderProgram> _shaderProgramToLink)
{
glLinkProgram(_shaderProgramToLink);
@ -83,15 +169,127 @@ namespace GL
return;
}
sfn UseProgramShader(ID<ShaderProgram> _shaderProgramToUse)
sfn QueryShader(ID<Shader> _shaderToQuery, EShaderInfo _shaderInfoDesired, ptr<gInt> _requestedInfoObject) -> void
{
glUseProgram(_shaderProgramToUse);
return;
glGetShaderiv(_shaderToQuery, GLenum(_shaderInfoDesired), _requestedInfoObject);
}
sfn QueryShaderProgram(ID<ShaderProgram> _shaderToQuery, EShaderProgramInfo _shaderProgramInfoDesired, ptr<gInt> _requestedInfoObject) -> void
{
glGetProgramiv(_shaderToQuery, GLenum(_shaderProgramInfoDesired), _requestedInfoObject);
}
// Raw Tape
sfn LoadShaders(RawString<const char> _vertexShaderFilePath, RawString<const char> _fragmentShaderFilePath) -> ID<ShaderProgram>
{
using std::cout ;
using std::endl ;
using std::ifstream ;
using std::ios ;
using std::string ;
using std::stringstream;
using std::vector ;
string vertexShaderCode ;
string fragmentShaderCode ;
ifstream vertexShaderFileStream ;
ifstream fragmentShaderFileStream;
vertexShaderFileStream .open(_vertexShaderFilePath);
fragmentShaderFileStream.open(_fragmentShaderFilePath);
try
{
if (vertexShaderFileStream.is_open() and fragmentShaderFileStream.is_open())
{
stringstream vertSourceStrStream;
stringstream fragSourceStrStream;
vertSourceStrStream << vertexShaderFileStream .rdbuf();
fragSourceStrStream << fragmentShaderFileStream.rdbuf();
vertexShaderFileStream .close();
fragmentShaderFileStream.close();
vertexShaderCode = vertSourceStrStream.str();
fragmentShaderCode = fragSourceStrStream.str();
}
else
{
throw std::runtime_error("Impossible to open% s.Are you in the right directory ? Don't forget to read the FAQ !");
}
RawString<const char> vertexSourcePtr = vertexShaderCode .c_str();
RawString<const char> fragmentSourcePtr = fragmentShaderCode.c_str();
cout << "Compiling shader: " << _vertexShaderFilePath << endl;
ID<Shader> vertexShader = CreateShader(EShaderType::Vertex);
BindShaderSource(vertexShader, 1, Address(vertexSourcePtr), NULL);
CompileShader (vertexShader );
cout << "Compiling shader: " << _fragmentShaderFilePath << endl;
ID<Shader> fragmentShader = CreateShader(EShaderType::Fragment);
BindShaderSource(fragmentShader, 1, Address(fragmentSourcePtr), NULL);
CompileShader (fragmentShader );
cout << "Making Shader Program and Linking..." << endl;
ID<ShaderProgram> generatedProgram;
MakeShaderProgram(generatedProgram, vertexShader, fragmentShader);
DeleteShader(vertexShader );
DeleteShader(fragmentShader);
return generatedProgram;
}
catch (const std::runtime_error _error)
{
ErrorRuntime(_error);
Exit(ExitCode::Failed);
}
}
sfn LoadRawShader()
{
ID<Shader> VertexShader ;
ID<Shader> FragmentShader;
MakeShader(VertexShader , EShaderType::Vertex , 1, Address(GL::RawVertextShaderSource ), NULL);
MakeShader(FragmentShader, EShaderType::Fragment, 1, Address(GL::RawFragmentShaderSource), NULL);
GL::MakeShaderProgram(RawShader, VertexShader, FragmentShader);
GL::DeleteShader(VertexShader );
GL::DeleteShader(FragmentShader);
}
sfn LoadSimpleShader()
{
SimpleShader = LoadShaders("SimpleVertexShader.vert", "SimpleFragmentShader.frag");
}
sfn LoadSimpleShader_Transformed()
{
SimpleShader_Transformed = LoadShaders("SimpleTransform.vert", "SingleColor.frag");
ScreenSpaceVarID = GL::GetUniformVariable(GL::SimpleShader_Transformed, "modelViewProjection");
}
sfn LoadDefaultShaders()
{
LoadRawShader ();
LoadSimpleShader ();
LoadSimpleShader_Transformed();
}
sfn MakeShader
(
@ -101,6 +299,7 @@ namespace GL
ptr<RawString<const gChar>> _sourceCode ,
ptr< const gInt > _lengthsOfStrings
)
-> void
{
_shaderIDHolder = CreateShader(_typeOfShader);
@ -109,18 +308,54 @@ namespace GL
CompileShader(_shaderIDHolder);
}
sfn MakeShaderProgram(Ref(ID<ShaderProgram>) _shaderProgramIDHolder, ID<Shader> _shadersToAttach...)
sfn MakeShaderProgram(Ref(ID<ShaderProgram>) _shaderProgramIDHolder, const ID<Shader> _vertexShader, const ID<Shader> _fragShader) -> void
{
_shaderProgramIDHolder = CreateShaderProgram();
va_list args;
va_start(args, _shadersToAttach);
AttachShader(_shaderProgramIDHolder, va_arg(args, ID<Shader>));
va_end(args);
AttachShader(_shaderProgramIDHolder, _vertexShader);
AttachShader(_shaderProgramIDHolder, _fragShader );
LinkProgramShader(_shaderProgramIDHolder);
gInt Result = false;
QueryShaderProgram(_shaderProgramIDHolder, EShaderProgramInfo::LinkStatus, Address(Result));
if (!Result)
{
gInt infoLogLength;
QueryShaderProgram(_shaderProgramIDHolder, EShaderProgramInfo::InfoLogLength, Address(infoLogLength));
if (infoLogLength > 0)
{
std::vector<char> ErrorMessage(infoLogLength + 1);
GetShaderProgramInfo(_shaderProgramIDHolder, infoLogLength, NULL, Address(ErrorMessage.at(0)));
throw std::runtime_error(Address(ErrorMessage.at(0)));
}
else
{
throw std::runtime_error("ShaderProgram compilation failed and did not get a proper info log.");
}
}
DetachShader(_shaderProgramIDHolder, _vertexShader);
DetachShader(_shaderProgramIDHolder, _fragShader );
return;
}
sfn SetUniformVariable_MatrixVariableArray(const ID<Matrix> _matrixID, const gSize _numMatricies, const EBool _shouldTransposeValues, ptr<const float> _dataPtr)
{
glUniformMatrix4fv(_matrixID, _numMatricies, GLenum(_shouldTransposeValues), _dataPtr);
}
sfn UseProgramShader(ID<ShaderProgram> _shaderProgramToUse)
{
glUseProgram(_shaderProgramToUse);
return;
}
}

116
DGL_Space.hpp Normal file
View File

@ -0,0 +1,116 @@
#pragma once
#include "DGL_FundamentalTypes.hpp"
#include "DGL_MiscTypes.hpp"
#include "Cpp_Alias.hpp"
namespace GL
{
using CoordSpace = Matrix4x4;
using Projection = Matrix4x4;
// Function
template<typename Type>
sfn CreateLookAtView(const Generic::Vector3<Type> _viewPosition, const Generic::Vector3<Type> _lookAtPosition, const Generic::Vector3<Type> _upDirection) -> Matrix4x4
{
return glm::lookAt(_viewPosition, _lookAtPosition, _upDirection);
}
sfn CreateOrthographic(gFloat _leftScreenCoord, gFloat _rightScreenCoord, gFloat _bottomScreenCoord, gFloat _topScreenCoord, gFloat _nearPlane, gFloat _farPlane)
{
return glm::ortho(_leftScreenCoord, _rightScreenCoord, _bottomScreenCoord, _topScreenCoord, _nearPlane, _farPlane);
}
template<typename FloatType>
sfn CreatePerspective(const FloatType _fieldOfView, const FloatType _aspectRatio, const FloatType _nearPlane, const FloatType _farPlane)
{
return glm::perspective(_fieldOfView, _aspectRatio, _nearPlane, _farPlane);
}
sfn Rotate(const Matrix4x4 _matrix, gFloat _rotationAngleAmount, Vector3 _axis) -> Matrix4x4
{
return glm::rotate(_matrix, _rotationAngleAmount, _axis);
}
template<typename Type>
sfn ToRadians(const Ref(Type) _degrees) -> Type
{
return glm::radians(_degrees);
}
struct ClippingPlanes
{
gFloat Near, Far;
ClippingPlanes(gFloat _near, gFloat _far) : Near(_near), Far(_far) {};
};
namespace DefaultSpace
{
gFloat
AspectRatio = 16.0f / 10.0f,
FieldOfView = 90.0f ,
NearClippingPlane = 0.1f ,
FarClippingPlane = 100.0f ;
Vector3 CameraPosition(-3, 0, 0),
LookAtPosition( 0, 0, 0),
UpDirection ( 0, 1, 0) ;
gFloat ScreenWidth = 720.0f, ScreenHeight = 450.0f;
}
struct Camera
{
gFloat AspectRatio, FieldOfView;
ClippingPlanes ClipSpace;
CoordSpace ViewPort;
Projection Orthographic, Perspective;
Vector3 Position, LookAtPosition, UpDirection;
Camera
(
gFloat _aspectRatio ,
gFloat _fieldOfView ,
ClippingPlanes _clippingPlanes,
Vector3 _position ,
Vector3 _lookAtPosition,
Vector3 _upDirection
) :
AspectRatio (_aspectRatio ),
FieldOfView (_fieldOfView ),
ClipSpace (_clippingPlanes),
Position (_position ),
LookAtPosition(_lookAtPosition),
UpDirection (_upDirection )
{
ViewPort = CreateLookAtView(Position, LookAtPosition, UpDirection);
Orthographic = CreateOrthographic(0.0f, DefaultSpace::ScreenWidth, 0.0f, DefaultSpace::ScreenHeight, ClipSpace.Near, ClipSpace.Far);
Perspective = CreatePerspective<gFloat>(ToRadians(FieldOfView), AspectRatio, ClipSpace.Near, ClipSpace.Far);
}
};
namespace DefaultSpace
{
Camera WorldCamera(AspectRatio, FieldOfView, ClippingPlanes(NearClippingPlane, FarClippingPlane), CameraPosition, LookAtPosition, UpDirection);
CoordSpace WorldSpace(Matrix4x4(1.0f));
CoordSpace Screenspace = WorldCamera.Perspective * WorldCamera.ViewPort * WorldSpace;
}
}

1
DGL_Variable.hpp Normal file
View File

@ -0,0 +1 @@
#pragma once

183
Execution.cpp Normal file
View File

@ -0,0 +1,183 @@
// Cpp STL
#include <chrono>
#include <thread>
#include <memory>
// Project
#include "DGL.hpp"
#include "TriangleRaw.hpp"
#include "Cpp_Alias.hpp"
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
namespace Execution
{
// C++ STL
using std::thread;
template<typename Type>
using UPtr = std::unique_ptr<Type>;
template<typename Type, typename... ParamTypes>
sfn MakeUPtr(rRef(ParamTypes)... _params) -> UPtr<Type>
{
return std::make_unique<Type>(_params...);
}
// GL
using GL::TimeValDec;
using GL::Window ;
using GL::Matrix ;
using GL::Matrix4x4 ;
using GL::Vector3 ;
using GL::gFloat ;
bool Exist = true;
TimeValDec CycleStart, CycleEnd, DeltaTime, InputPollingInterval = 1.0f / 240.0f, RenderInterval = 1.0f / 60.0f;
UPtr<thread> BasicLoop, BasicTimedLoop;
// Testing
// Foward Declares
sfn RenderProcedure () -> void;
sfn PrepareRenderObjects() -> void;
sfn CreateWindow_BasicLoop()
{
GL::InitalizeGLFW();
deduce windowObj = GL::CreateWindow(720, 450, "Assignment 1: RawLoop", GL::WindowedMode(), GL::NotShared());
GL::SetCurrentContext(windowObj);
GL::RunBasicWindowLoop(windowObj);
}
sfn CreateWindow_TimedRender()
{
GL::InitalizeGLFW();
deduce windowObj = GL::CreateWindow(720, 540, "Assignment 1: Timed Render", GL::WindowedMode(), GL::NotShared());
GL::SetCurrentContext(windowObj);
GL::InitalizeGLEW();
PrepareRenderObjects();
GL::RunBasicWindowLoop_Timed(windowObj, 1.0 / 60.0, Address(RenderProcedure));
}
sfn PrepareRenderObjects() -> void
{
RAW_SetupBuffers();
RAW_BindAndBufferDataToIDs();
GL::LoadDefaultShaders();
GL::FormatVertexAttributes<Vertex3>(VertexAttributeIndex, EDataType::Float, ZeroOffset(), Vertex3::ValueCount(), EBool::True);
GL::BindBuffer(EBufferTarget::VertexAttributes, 0); // Dunno. Prob unbinding...
GL::BindVertexArray(0);
}
sfn Cycler(FnPtr<void> _inputProcedure, FnPtr<void> _renderProcedure)
{
while (Exist)
{
CycleStart = GL::GetTime();
if (DeltaTime >= InputPollingInterval)
{
GL::PollEvents();
_inputProcedure();
}
if (DeltaTime >= RenderInterval)
{
_renderProcedure();
}
CycleEnd = GL::GetTime();
DeltaTime = DeltaTime + CycleEnd - CycleStart;
}
return;
}
sfn InputProcedure()
{
}
sfn RenderProcedure() -> void
{
GL::EnableVertexAttributeArray(VertexAttributeIndex);
GL::SetPolygonMode(GL::EFace::Front_and_Back, GL::EMode::Fill);
GL::DefaultSpace::WorldSpace = GL::Rotate(GL::DefaultSpace::WorldSpace, 0.0035f, Vector3(0.0f, 0.5f, 0.0f));
GL::DefaultSpace::Screenspace = GL::DefaultSpace::WorldCamera.Perspective * GL::DefaultSpace::WorldCamera.ViewPort * GL::DefaultSpace::WorldSpace;
GL::UseProgramShader(GL::SimpleShader_Transformed);
GL::SetUniformVariable_MatrixVariableArray(GL::ScreenSpaceVarID, 1, GL::EBool::False, Address(GL::DefaultSpace::Screenspace[0][0]));
GL::BindVertexArray(VertexArrayObj);
GL::DrawArrays(GL::EPrimitives::Triangles, 0, TriangleRaw::VertexCount());
GL::DisableVertexAttributeArray(VertexAttributeIndex);
}
sfn PrepWorkspace()
{
}
// Runtime Execution: Loop must be specified here to use.
sfn Execute() -> ExitCode
{
/*PrepWorkspace();*/
//Cycler(Address(InputProcedure), Address(RenderProcedure));
CreateWindow_TimedRender();
GL::TerminateGLFW();
return ExitCode::Success;
}
}
int main(void)
{
return int(Execution::Execute());
}

203
ShaderFromTut.hpp Normal file
View File

@ -0,0 +1,203 @@
#ifndef SHADER_H
#define SHADER_H
//#include <glad/glad.h>
#include <glew.h>
#include <glm/glm.hpp>
#include "DGL.hpp"
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
class Shader
{
public:
unsigned int ID;
// constructor generates the shader on the fly
// ------------------------------------------------------------------------
Shader(const char* vertexPath, const char* fragmentPath, const char* geometryPath = nullptr)
{
// 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::string geometryCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
std::ifstream gShaderFile;
// ensure ifstream objects can throw exceptions:
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
gShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
try
{
// open files
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
// read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers
vShaderFile.close();
fShaderFile.close();
// convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
// if geometry shader path is present, also load a geometry shader
if (geometryPath != nullptr)
{
gShaderFile.open(geometryPath);
std::stringstream gShaderStream;
gShaderStream << gShaderFile.rdbuf();
gShaderFile.close();
geometryCode = gShaderStream.str();
}
}
catch (std::ifstream::failure e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
}
const char* vShaderCode = vertexCode.c_str();
const char* fShaderCode = fragmentCode.c_str();
// 2. compile shaders
unsigned int vertex, fragment;
// vertex shader
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
checkCompileErrors(vertex, "VERTEX");
// fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
checkCompileErrors(fragment, "FRAGMENT");
// if geometry shader is given, compile geometry shader
unsigned int geometry;
if (geometryPath != nullptr)
{
const char* gShaderCode = geometryCode.c_str();
geometry = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(geometry, 1, &gShaderCode, NULL);
glCompileShader(geometry);
checkCompileErrors(geometry, "GEOMETRY");
}
GL::MakeShaderProgram(ID, vertex, fragment);
// shader Program
/* ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
if (geometryPath != nullptr)
glAttachShader(ID, geometry);
glLinkProgram(ID);*/
checkCompileErrors(ID, "PROGRAM");
// delete the shaders as they're linked into our program now and no longer necessery
glDeleteShader(vertex);
glDeleteShader(fragment);
if (geometryPath != nullptr)
glDeleteShader(geometry);
}
// activate the shader
// ------------------------------------------------------------------------
void use()
{
glUseProgram(ID);
}
// utility uniform functions
// ------------------------------------------------------------------------
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 setVec2(const std::string& name, const glm::vec2& value) const
{
glUniform2fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
void setVec2(const std::string& name, float x, float y) const
{
glUniform2f(glGetUniformLocation(ID, name.c_str()), x, y);
}
// ------------------------------------------------------------------------
void setVec3(const std::string& name, const glm::vec3& value) const
{
glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
void setVec3(const std::string& name, float x, float y, float z) const
{
glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z);
}
// ------------------------------------------------------------------------
void setVec4(const std::string& name, const glm::vec4& value) const
{
glUniform4fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
void setVec4(const std::string& name, float x, float y, float z, float w)
{
glUniform4f(glGetUniformLocation(ID, name.c_str()), x, y, z, w);
}
// ------------------------------------------------------------------------
void setMat2(const std::string& name, const glm::mat2& mat) const
{
glUniformMatrix2fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
// ------------------------------------------------------------------------
void setMat3(const std::string& name, const glm::mat3& mat) const
{
glUniformMatrix3fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
// ------------------------------------------------------------------------
void setMat4(const std::string& name, const glm::mat4& mat) const
{
deduce bullshit = glGetUniformLocation(ID, name.c_str());
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
private:
// utility function for checking shader compilation/linking errors.
// ------------------------------------------------------------------------
void checkCompileErrors(GLuint shader, std::string type)
{
GLint success;
GLchar infoLog[1024];
if (type != "PROGRAM")
{
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
else
{
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
}
};
#endif

12
SimpleFragmentShader.frag Normal file
View File

@ -0,0 +1,12 @@
#version 330 core
out vec3 color;
void main()
{
color = vec3(0.345, 0.345, 1);
}

15
SimpleTransform.vert Normal file
View File

@ -0,0 +1,15 @@
#version 330 core
layout (location = 0) in vec3 aPos;
// uniform mat4 model;
// uniform mat4 view;
// uniform mat4 projection;
uniform mat4 modelViewProjection;
void main()
{
//gl_Position = projection * view * model * vec4(aPos, 1.0);
gl_Position = modelViewProjection * vec4(aPos, 1.0);
}

13
SimpleVertexShader.vert Normal file
View File

@ -0,0 +1,13 @@
#version 330 core
layout (location = 0) in vec3 vertexPosition_modelSpace;
void main()
{
gl_Position.xyz = vertexPosition_modelSpace;
gl_Position.w = 1.0 ;
}

12
SingleColor.frag Normal file
View File

@ -0,0 +1,12 @@
#version 330 core
// Output data
out vec3 color;
void main()
{
// Output color = red
color = vec3(1,0,0);
}

160
TriangleRaw.hpp Normal file
View File

@ -0,0 +1,160 @@
#pragma once
#include "DGL.hpp"
#include "Cpp_Alias.hpp"
using GL::gFloat;
using GL::VertexBuffer;
using GL::EBufferTarget;
using GL::EBufferUsage;
using GL::Buffer;
using GL::ID;
using GL::gInt;
using GL::gSize;
// This will identify our vertex buffer
ID<Buffer> VertexBufferObj;
struct Vertex3
{
gFloat x, y, z;
static constexpr sfn ValueCount() -> gSize { return 3; }
};
struct TriangleRaw
{
Vertex3 a, b, c;
static constexpr sfn VertexCount() -> gSize { return 3; }
};
TriangleRaw EquilateralTriangleVerticies =
{
{ -1.0f, -1.0f, 0.0f },
{ 1.0f, -1.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f }
};
struct RectangleRaw
{
TriangleRaw first, second;
static constexpr sfn VertexCount() -> gSize { return 6; }
};
RectangleRaw SquareVerticies =
{
{
{ -0.5f, -0.5f, 0.0f },
{ 0.5f, -0.5f, 0.0f },
{ 0.0f, 0.5f, 0.0f }
},
{
{ 0.5f, -0.5f, 0.0f },
{ -0.5f, -0.5f, 0.0f },
{ -0.5f, 0.5f, 0.0f }
}
};
ID<VertexBuffer> VertexArrayObj;
struct RectangleCompressed
{
Vertex3 a, b, c, d;
static constexpr sfn VertexCount() -> gSize { return 4; }
};
struct TriIndex
{
gInt a, b, c;
};
struct RectangleIndices
{
TriIndex first, second;
};
RectangleCompressed rectCompress =
{
{ 0.5f, 0.5f, 0.0f },
{ 0.5f, -0.5f, 0.0f },
{ -0.5f, -0.5f, 0.0f },
{ -0.5f, 0.5f, 0.0f }
};
RectangleIndices rectIndices =
{
{ 0, 1, 3 },
{ 1, 2, 3 }
};
using GL::ElementBuffer;
ID<ElementBuffer> ElemBufferObj;
struct Vertex2
{
gFloat x, y;
};
struct TriTexCoords
{
Vertex2 a, b, c;
};
TriTexCoords textureCoords =
{
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 0.5f, 1.0f }
};
sfn RAW_SetupBuffers()
{
GL::GenerateVertexBuffers(Address(VertexArrayObj ), 1);
GL::GenerateBuffers (Address(VertexBufferObj), 1);
GL::GenerateBuffers (Address(ElemBufferObj ), 1);
}
sfn RAW_SetupTriangleBuffer()
{
GL::GenerateBuffers(Address(VertexBufferObj), 1);
}
sfn RAW_BindAndBufferDataToIDs()
{
GL::BindVertexArray(VertexArrayObj);
GL::BindBuffer(EBufferTarget::VertexAttributes, VertexBufferObj);
GL::BufferData<TriangleRaw>(Address(EquilateralTriangleVerticies), EBufferTarget::VertexAttributes, EBufferUsage::StaticDraw);
//GL::BufferData<RectangleCompressed>(Address(rectCompress), EBufferTarget::VertexAttributes, EBufferUsage::StaticDraw);
//GL::BindBuffer(EBufferTarget::VertexIndices, ElemBufferObj);
//GL::BufferData<RectangleIndices>(Address(rectIndices), EBufferTarget::VertexIndices, EBufferUsage::StaticDraw);
}
GL::gInt VertexAttributeIndex = 0; // See shader source: (layout = 0).
using GL::EBool ;
using GL::EDataType;
constexpr sfn ZeroOffset() -> ptr<void>
{
return 0;
}