Bump to OpenGL 2.0 & Shader setup.

This commit is contained in:
2024-01-28 13:34:06 -05:00
parent c782eff296
commit 15555a2493
13 changed files with 151 additions and 37 deletions

View File

@@ -67,7 +67,7 @@ CPMAddPackage(
)
CPMAddPackage(
NAME glad
URL https://git.redacted.cc/Redacted/glad/archive/v1.1.zip
URL https://git.redacted.cc/Redacted/glad/archive/v2.0.zip
)
add_library(Re3D SHARED ${SOURCES})
# Why god???

View File

@@ -0,0 +1,5 @@
#version 110
void main() {
gl_Position = vec4(0.0, 0.0, 0.0, 0.0);
}

View File

@@ -6,5 +6,6 @@
namespace Collision {
VertexArray calculateBoundingBox(VertexArray* vArray, const LinearAlgebra::Vector3& angle);
VertexArray calculateAxisAlignedBoundingBox(VertexArray vArray);
bool insideAABoundingBox(const LinearAlgebra::Vector3& position, const VertexArray& boundingBox);
}

View File

@@ -2,6 +2,7 @@
#include <types/vertex.h>
#include <types/texture.h>
#include <types/shader.h>
#include <types/entity/entity.h>
#include <J3ML/LinearAlgebra/Vector4.h>
@@ -48,4 +49,5 @@ public:
std::string name;
std::vector<VertexArray> geometryList;
std::vector<Texture> textureList;
std::vector<Shader> shaderList;
};

View File

@@ -5,10 +5,11 @@
#include <sstream>
#include <iostream>
#include <engine/engine.h>
//A "scripted move" is a vector of points and angles that function as keyframes.
//A "track" is a vector of points and angles that function as keyframes.
//A moby will follow *and interpolate* between those points.
//TODO Refactor into cpp.
class ScriptedMove {
class Track {
public:
std::vector<LinearAlgebra::Vector3> positions;
std::vector<LinearAlgebra::Vector3> angles;

View File

@@ -1,7 +1,7 @@
#pragma once
#include <types/entity/entity.h>
#include <types/animation/scriptedMove.h>
#include <types/animation/track.h>
//Movable Object.
class Moby : public Entity {
@@ -14,12 +14,12 @@ public:
float hVelocity;
float vVelocity;
LinearAlgebra::Vector3 upVector = {0.0f,1.0f,0.0f};
ScriptedMove sMove;
Track track;
virtual void hMove(LinearAlgebra::Vector3 a, float vel);
void vMove(float vel);
LinearAlgebra::Vector3 simulateHMove(LinearAlgebra::Vector3 a, float speed);
LinearAlgebra::Vector3 simulateVMove(LinearAlgebra::Vector3 position, float speed);
void doSMove();
void sMove();
LinearAlgebra::Vector3 fAngle(); // forward angle
LinearAlgebra::Vector3 bAngle(); // back angle
LinearAlgebra::Vector3 lAngle(); // left angle

19
include/types/shader.h Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include <glad/glad.h>
#include <cstdlib>
#include <string>
class Shader {
public:
std::string name;
//The shader program
GLuint id = NULL;
//The individual shaders
GLuint vertex = NULL;
GLuint fragment = NULL;
void load(std::string filePath, GLenum type);
void link();
};

View File

@@ -44,7 +44,8 @@ void Engine::initGL()
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
glClearDepth(1.0f);
//glEnable(GL_BLEND);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
//glEnable(GL_LIGHT0);

View File

@@ -15,7 +15,7 @@ void Render::pre_render() {
auto* camera = new Camera();
camera->SetPos({0.0f, -2.0f, -5.0f});
camera->angle.y = 0.0f;
camera->sMove.load("assets/scriptedMove/default.smov");
camera->track.load("assets/scriptedMove/default.smov");
camera->SetParent(engine->world);
auto *player = new Player();
@@ -28,6 +28,12 @@ void Render::pre_render() {
skybox->angle = {0,0,0};
skybox->SetParent(engine->world);
engine->world->setAmbientLight(0.6, 0.6, 0.6, 1);
Shader test;
test.name = "test";
test.load(engine->workingDir + "/assets/shaders/default.glsl", GL_VERTEX_SHADER);
test.link();
engine->world->shaderList.push_back(test);
}
//std::cout << engine->frameCount << std::endl;
engine->window->pollEvents();

View File

@@ -32,10 +32,6 @@ void Camera::modeBinds() {
if (engine->window->keyDown(SCANCODE::THREE))
cameraMode = CameraMode::SCRIPTED_MOVE;
//if (cameraMode == CameraMode::SCRIPTED_MOVE) {
//doScriptedMovement();
//}
}
void Camera::freeCam() {
if (engine->window->keyDown(SCANCODE::S)) {
@@ -92,8 +88,8 @@ void Camera::pre_render() {
freeCam();
if (cameraMode == CameraMode::THIRD_PERSON)
thirdPerson();
//if(cameraMode == CameraMode::SCRIPTED_MOVE)
//0doScriptedMovement();
if (cameraMode == CameraMode::SCRIPTED_MOVE)
sMove();
}
void Camera::render() {
// Preferrably: Camera would return a coordinate system that GameEngine

View File

@@ -136,6 +136,12 @@ VertexArray* Entity::getGeometry() {
void Entity::render() {
bool cameraInside = false;
//Run the example vertex shader.
for (auto& shader : engine->world->shaderList)
if (shader.name == "test")
glUseProgram(shader.id);
glUseProgram(0);
//Eventually use a more accurate collider for this.
Camera* camera;
for (auto& e : engine->world->GetChildren())

View File

@@ -52,41 +52,42 @@ LinearAlgebra::Vector3 Moby::lAngle()
}
//broken
void Moby::doSMove() {
//If the movement has a set starting position, Then teleport there.
if (sMove.positions.empty())
void Moby::sMove() {
if (track.positions.empty())
return;
if (sMove.index == -1 && !sMove.start.operator==({0, 0, 0})) {
this->position = sMove.start;
sMove.index++;
//If the movement has a set starting position, Then teleport there.
if (track.index == -1 && !track.start.operator==({0, 0, 0})) {
position = track.start;
track.index++;
}
//If the movement does not have a starting position
if (sMove.index == -1 && sMove.start.operator==({0,0,0})) {
sMove.index++;
if (track.index == -1 && track.start.operator==({0,0,0})) {
track.index++;
}
sMove.lastDistance = LinearAlgebra::Vector3::Distance(position, sMove.positions[sMove.index]);
track.lastDistance = LinearAlgebra::Vector3::Distance(position, track.positions[track.index]);
//We've reached the end of the movement script.
if (sMove.index + 1 > sMove.positions.size()) {
if (sMove.loop)
if (track.index + 1 > track.positions.size()) {
if (track.loop)
return;
sMove.reset();
track.reset();
}
//LinearAlgebra::Vector3 a = VectorMath::calcAngle(this->position, scriptedMove.positions[scriptedMove.index]).movementAngle();
//move(a,scriptedMove.velocity);
angle.x -= (angle.x - sMove.angles[sMove.index].x)*(sMove.velocity)*engine->frameDelta;
angle.y -= (angle.y -sMove.angles[sMove.index].y)*(sMove.velocity)*engine->frameDelta;
angle.z -= (angle.z - sMove.angles[sMove.index].z)*engine->frameDelta;
LinearAlgebra::Vector3 a = LinearAlgebra::Vector3::Direction(VectorMath::calcAngle(this->position, track.positions[track.index]));
hMove(a,track.velocity);
angle.x += (angle.x - track.angles[track.index].x)*(track.velocity)*engine->frameDelta;
angle.y += (angle.y -track.angles[track.index].y)*(track.velocity)*engine->frameDelta;
angle.z += (angle.z - track.angles[track.index].z)*engine->frameDelta;
//If the next movement would make us go passed it.
//if (scriptedMove.lastDistance < VectorMath::distance(simulateMove(a,scriptedMove.velocity),scriptedMove.positions[scriptedMove.index])) {
//position = scriptedMove.positions[scriptedMove.index];
//this->angle = scriptedMove.angles[scriptedMove.index];
//scriptedMove.index++;
//return;
//}
if (track.lastDistance < simulateHMove(a, track.velocity).Distance(track.positions[track.index])) {
position = track.positions[track.index];
angle = track.angles[track.index];
track.index++;
return;
}
}

76
src/types/shader.cpp Normal file
View File

@@ -0,0 +1,76 @@
#include <types/shader.h>
#include <iostream>
#include <fstream>
#include <sstream>
void compilationError(GLuint shader) {
GLint success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar info[512];
glGetShaderInfoLog(shader, sizeof(info), nullptr, info);
std::cerr << "Shader compilation error: " << info << std::endl;
}
}
void linkingError(GLuint shader) {
GLint success;
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success) {
GLchar info[512];
glGetProgramInfoLog(shader, sizeof(info), nullptr, info);
std::cerr << "ShaderProgram linking " << info << std::endl;
}
}
void Shader::load(std::string filePath, GLenum type) {
std::ifstream file(filePath);
if (!file.is_open()) {
std::cerr << "Couldn't Load Shader: " << filePath << std::endl;
exit(0);
}
if (type != GL_VERTEX_SHADER && type != GL_FRAGMENT_SHADER) {
std::cerr << "Invalid Shader Type: " << type << std::endl;
exit(0);
}
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
std::string shaderSource = buffer.str();
const GLchar* source = shaderSource.c_str();
if (type == GL_VERTEX_SHADER) {
vertex = glCreateShader(type);
glShaderSource(vertex, 1, &source, NULL);
glCompileShader(vertex);
compilationError(vertex);
}
if (type == GL_FRAGMENT_SHADER) {
fragment = glCreateShader(type);
glShaderSource(fragment, 1, &source, NULL);
glCompileShader(fragment);
compilationError(fragment);
}
}
void Shader::link() {
if (id != NULL || vertex == NULL && fragment == NULL) {
std::cerr << "Error Linking Shader." << std::endl;
exit(0);
}
id = glCreateProgram();
if (vertex != NULL)
glAttachShader(id, vertex);
if (fragment != NULL)
glAttachShader(id, fragment);
glLinkProgram(id);
linkingError(id);
}