2022-10-28 01:44:23 -04:00
|
|
|
/*
|
|
|
|
* Created by Brett Terpstra 6920201 on 26/10/22.
|
|
|
|
* Copyright (c) 2022 Brett Terpstra. All Rights Reserved.
|
|
|
|
* stores / handles GL related stuff like implementing functions and common classes
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef STEP_3_GL_H
|
|
|
|
#define STEP_3_GL_H
|
|
|
|
|
|
|
|
#include <engine/util/std.h>
|
|
|
|
#include <engine/types.h>
|
|
|
|
#include <engine/image/image.h>
|
2022-10-31 00:51:51 -04:00
|
|
|
#include <config.h>
|
|
|
|
#include <graphics/gl/shader.h>
|
2022-11-13 13:03:48 -05:00
|
|
|
|
2022-10-31 00:51:51 -04:00
|
|
|
#ifndef USE_GLFW
|
|
|
|
#include <GLES3/gl32.h>
|
|
|
|
#include <GLES3/gl3.h>
|
|
|
|
#include <GL/gl.h>
|
|
|
|
#include "graphics/gl/glext.h"
|
2022-11-13 13:03:48 -05:00
|
|
|
|
|
|
|
extern PFNGLCREATEVERTEXARRAYSPROC glCreateVertexArrays;
|
|
|
|
extern PFNGLCREATEBUFFERSPROC glCreateBuffers;
|
|
|
|
extern PFNGLNAMEDBUFFERDATAPROC glNamedBufferData;
|
|
|
|
extern PFNGLNAMEDBUFFERSUBDATAPROC glNamedBufferSubData;
|
|
|
|
|
|
|
|
extern PFNGLENABLEVERTEXARRAYATTRIBPROC glEnableVertexArrayAttrib;
|
|
|
|
extern PFNGLVERTEXARRAYATTRIBBINDINGPROC glVertexArrayAttribBinding;
|
|
|
|
extern PFNGLVERTEXARRAYATTRIBFORMATPROC glVertexArrayAttribFormat;
|
2022-10-31 00:51:51 -04:00
|
|
|
#else
|
2022-11-13 13:03:48 -05:00
|
|
|
|
2022-10-31 00:51:51 -04:00
|
|
|
#include <graphics/gl/glad/gl.h>
|
2022-11-13 13:03:48 -05:00
|
|
|
|
2022-10-31 00:51:51 -04:00
|
|
|
#endif
|
2022-10-28 01:44:23 -04:00
|
|
|
|
2022-11-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.5f, 0.5f,
|
|
|
|
-0.5f, 0.5f, 0.5f,
|
|
|
|
-0.5f, 0.5f, -0.5f,
|
|
|
|
};
|
|
|
|
std::vector<float> cubeUVs = {
|
|
|
|
0.0f, 0.0f,
|
|
|
|
1.0f, 0.0f,
|
|
|
|
1.0f, 1.0f,
|
|
|
|
1.0f, 1.0f,
|
|
|
|
0.0f, 1.0f,
|
|
|
|
0.0f, 0.0f,
|
|
|
|
|
|
|
|
0.0f, 0.0f,
|
|
|
|
1.0f, 0.0f,
|
|
|
|
1.0f, 1.0f,
|
|
|
|
1.0f, 1.0f,
|
|
|
|
0.0f, 1.0f,
|
|
|
|
0.0f, 0.0f,
|
|
|
|
|
|
|
|
1.0f, 0.0f,
|
|
|
|
1.0f, 1.0f,
|
|
|
|
0.0f, 1.0f,
|
|
|
|
0.0f, 1.0f,
|
|
|
|
0.0f, 0.0f,
|
|
|
|
1.0f, 0.0f,
|
|
|
|
|
|
|
|
1.0f, 0.0f,
|
|
|
|
1.0f, 1.0f,
|
|
|
|
0.0f, 1.0f,
|
|
|
|
0.0f, 1.0f,
|
|
|
|
0.0f, 0.0f,
|
|
|
|
1.0f, 0.0f,
|
|
|
|
|
|
|
|
0.0f, 1.0f,
|
|
|
|
1.0f, 1.0f,
|
|
|
|
1.0f, 0.0f,
|
|
|
|
1.0f, 0.0f,
|
|
|
|
0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f,
|
|
|
|
|
|
|
|
0.0f, 1.0f,
|
|
|
|
1.0f, 1.0f,
|
|
|
|
1.0f, 0.0f,
|
|
|
|
1.0f, 0.0f,
|
|
|
|
0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f
|
|
|
|
};
|
|
|
|
static cubeVertexBuilder getCubeExtends(float xRadius, float yRadius, float zRadius) {
|
|
|
|
cubeVertexBuilder builder {};
|
2022-11-13 14:13:14 -05:00
|
|
|
// Can we use the transformation matrix? Yes.
|
|
|
|
// Are we going to? No.
|
|
|
|
// Why? No good reason. Perhaps a TODO:?
|
2022-11-13 13:03:48 -05:00
|
|
|
builder.cubeVerticesRaw = {
|
2022-11-13 14:13:14 -05:00
|
|
|
-xRadius, -yRadius, -zRadius,
|
|
|
|
xRadius, -yRadius, -zRadius,
|
|
|
|
xRadius, yRadius, -zRadius,
|
|
|
|
xRadius, yRadius, -zRadius,
|
|
|
|
-xRadius, yRadius, -zRadius,
|
|
|
|
-xRadius, -yRadius, -zRadius,
|
2022-11-13 13:03:48 -05:00
|
|
|
|
2022-11-13 14:13:14 -05:00
|
|
|
-xRadius, -yRadius, zRadius,
|
|
|
|
xRadius, -yRadius, zRadius,
|
|
|
|
xRadius, yRadius, zRadius,
|
|
|
|
xRadius, yRadius, zRadius,
|
|
|
|
-xRadius, yRadius, zRadius,
|
|
|
|
-xRadius, -yRadius, zRadius,
|
2022-11-13 13:03:48 -05:00
|
|
|
|
2022-11-13 14:13:14 -05:00
|
|
|
-xRadius, yRadius, zRadius,
|
|
|
|
-xRadius, yRadius, -zRadius,
|
|
|
|
-xRadius, -yRadius, -zRadius,
|
|
|
|
-xRadius, -yRadius, -zRadius,
|
|
|
|
-xRadius, -yRadius, zRadius,
|
|
|
|
-xRadius, yRadius, zRadius,
|
2022-11-13 13:03:48 -05:00
|
|
|
|
2022-11-13 14:13:14 -05:00
|
|
|
xRadius, yRadius, zRadius,
|
|
|
|
xRadius, yRadius, -zRadius,
|
|
|
|
xRadius, -yRadius, -zRadius,
|
|
|
|
xRadius, -yRadius, -zRadius,
|
|
|
|
xRadius, -yRadius, zRadius,
|
|
|
|
xRadius, yRadius, zRadius,
|
2022-11-13 13:03:48 -05:00
|
|
|
|
2022-11-13 14:13:14 -05:00
|
|
|
-xRadius, -yRadius, -zRadius,
|
|
|
|
xRadius, -yRadius, -zRadius,
|
|
|
|
xRadius, -yRadius, zRadius,
|
|
|
|
xRadius, -yRadius, zRadius,
|
|
|
|
-xRadius, -yRadius, zRadius,
|
|
|
|
-xRadius, -yRadius, -zRadius,
|
2022-11-13 13:03:48 -05:00
|
|
|
|
2022-11-13 14:13:14 -05:00
|
|
|
-xRadius, yRadius, -zRadius,
|
|
|
|
xRadius, yRadius, -zRadius,
|
|
|
|
xRadius, yRadius, zRadius,
|
|
|
|
xRadius, yRadius, zRadius,
|
|
|
|
-xRadius, yRadius, zRadius,
|
|
|
|
-xRadius, yRadius, -zRadius,
|
2022-11-13 13:03:48 -05:00
|
|
|
};
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-10-28 01:44:23 -04:00
|
|
|
// since we are doing everything with raytracing
|
|
|
|
// the purpose of these utility classes are purely for debug
|
|
|
|
// such as drawing bounding boxes around a BVH
|
|
|
|
class Texture {
|
|
|
|
private:
|
|
|
|
Texture(const Texture& that); // Disable Copy Constructor
|
|
|
|
Texture& operator=(const Texture& that); // Disable Copy Assignment
|
|
|
|
protected:
|
|
|
|
unsigned int textureID;
|
|
|
|
int width, height, channels;
|
|
|
|
unsigned char* loadTexture(const std::string& path);
|
|
|
|
|
|
|
|
Raytracing::Image* _image = nullptr;
|
|
|
|
unsigned char* data;
|
|
|
|
|
|
|
|
public:
|
2022-11-13 13:03:48 -05:00
|
|
|
Texture(Texture&&) noexcept = delete; // Disable move constructor.
|
|
|
|
Texture& operator=(Texture&&) noexcept = delete; // Disable Move Assignment
|
2022-10-28 01:44:23 -04:00
|
|
|
Texture();
|
|
|
|
explicit Texture(const std::string& path);
|
|
|
|
explicit Texture(Raytracing::Image* image);
|
|
|
|
~Texture();
|
|
|
|
void updateImage();
|
|
|
|
void bind() const;
|
|
|
|
void unbind();
|
|
|
|
void enableGlTextures(int textureCount);
|
|
|
|
};
|
|
|
|
|
|
|
|
class VAO {
|
|
|
|
private:
|
|
|
|
unsigned int VaoID, instanceVBO;
|
|
|
|
std::vector<unsigned int> VBOs;
|
|
|
|
int drawCount = -1, currentTransforms = -1;
|
|
|
|
// vertex data
|
|
|
|
unsigned int storeData(int attrNumber, int coordSize, int stride, long offset, int length, const float* data);
|
|
|
|
// element data (indices)
|
2022-11-13 13:03:48 -05:00
|
|
|
unsigned int storeData(int length, const unsigned int* data);
|
2022-10-28 01:44:23 -04:00
|
|
|
// instance data
|
|
|
|
unsigned int createInstanceVBO(int count, int bytePerInstance);
|
|
|
|
// used much in the same way that store data sets an attribute where the data is expected
|
|
|
|
// except this sets based on the master instance vbo, telling the GPU where to use the data and when.
|
|
|
|
void addInstancedAttribute(int attribute, int dataSize, int dataLengthBytes, int offset) const;
|
|
|
|
// disable bad constructors
|
|
|
|
// we can't just make copies of GPU objects like we can on the CPU. It's stupidly expensive.
|
|
|
|
VAO() = default;
|
|
|
|
VAO(const VAO& that); // Disable Copy Constructor
|
|
|
|
VAO& operator=(const VAO& that); // Disable Copy Assignment
|
|
|
|
public:
|
2022-11-13 13:03:48 -05:00
|
|
|
VAO(VAO&&) noexcept = delete; // Disable move constructor.
|
|
|
|
VAO& operator=(VAO&&) noexcept = delete; // Disable Move Assignment
|
2022-10-28 01:44:23 -04:00
|
|
|
|
2022-11-13 13:03:48 -05:00
|
|
|
explicit VAO(const std::vector<Raytracing::Triangle>& triangles);
|
2022-10-28 01:44:23 -04:00
|
|
|
VAO(const std::vector<float>& verts, const std::vector<float>& uvs, const std::vector<unsigned int>& indices);
|
2022-11-13 03:00:30 -05:00
|
|
|
VAO(const std::vector<float>& verts, const std::vector<float>& uvs);
|
2022-10-28 01:44:23 -04:00
|
|
|
|
|
|
|
void bind() const;
|
|
|
|
void unbind();
|
|
|
|
// draws as if it where a fullscreen quad (literally used for that)
|
|
|
|
void draw() const;
|
|
|
|
// draw as if it's a box that we need to bulk draw.
|
2022-10-31 00:51:51 -04:00
|
|
|
void draw(Raytracing::Shader& shader, const std::vector<Raytracing::Vec4>& positions);
|
2022-10-28 01:44:23 -04:00
|
|
|
~VAO();
|
|
|
|
};
|
|
|
|
|
|
|
|
// should be called by the window class to get the function pointers.
|
|
|
|
void assignGLFunctionPointers();
|
|
|
|
|
|
|
|
#endif //STEP_3_GL_H
|