Initial Commit
This commit is contained in:
77
CMakeLists.txt
Normal file
77
CMakeLists.txt
Normal file
@@ -0,0 +1,77 @@
|
||||
cmake_minimum_required(VERSION 3.27)
|
||||
project(LearnOpenGL
|
||||
VERSION 1.0
|
||||
LANGUAGES CXX
|
||||
)
|
||||
|
||||
if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
|
||||
message(FATAL_ERROR "In-source builds are not allowed!")
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
if (WIN32)
|
||||
set(CMAKE_CXX_FLAGS "-municode")
|
||||
endif()
|
||||
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
|
||||
# Enable Package Manager
|
||||
include(cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME J3ML
|
||||
URL https://git.redacted.cc/josh/j3ml/archive/Prerelease-18.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME GLAD
|
||||
URL https://git.redacted.cc/Redacted/glad/archive/v2.1.zip
|
||||
)
|
||||
|
||||
#https://github.com/assimp/assimp/blob/master/LICENSE
|
||||
CPMAddPackage(
|
||||
NAME ASSIMP
|
||||
URL https://github.com/assimp/assimp/archive/refs/tags/v5.3.1.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME ReWindow
|
||||
URL https://git.redacted.cc/Redacted/ReWindow/archive/vA0.2.18.zip
|
||||
)
|
||||
|
||||
find_package(OpenGL REQUIRED)
|
||||
|
||||
|
||||
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp")
|
||||
file(GLOB_RECURSE SOURCES "src/*.c" "src/*.cpp")
|
||||
|
||||
# TODO: Wrangle target_include_directories
|
||||
include_directories(${PROJECT_SOURCE_DIR}/include)
|
||||
|
||||
|
||||
add_library(LearnOpenGL SHARED ${SOURCES}
|
||||
include/LearnOpenGL/LearnOpenGL.h
|
||||
src/LearnOpenGL/LearnOpenGL.cpp
|
||||
)
|
||||
|
||||
target_include_directories(LearnOpenGL PUBLIC ${J3ML_SOURCE_DIR}/include)
|
||||
target_include_directories(LearnOpenGL PUBLIC ${glad_SOURCE_DIR}/include)
|
||||
target_include_directories(LearnOpenGL PUBLIC ${ASSIMP_SOURCE_DIR}/include)
|
||||
|
||||
|
||||
target_link_libraries(LearnOpenGL PUBLIC J3ML)
|
||||
target_link_libraries(LearnOpenGL PUBLIC GL)
|
||||
target_link_libraries(LearnOpenGL PUBLIC glad)
|
||||
target_link_libraries(LearnOpenGL PUBLIC assimp)
|
||||
|
||||
|
||||
|
||||
set_target_properties(LearnOpenGL PROPERTIES LINKER_LANGUAGE CXX)
|
||||
|
||||
add_executable(LearnOpenGLDemo main.cpp)
|
||||
|
||||
target_include_directories(LearnOpenGLDemo PRIVATE ${ReWindow_SOURCE_DIR}/include)
|
||||
|
||||
target_link_libraries(LearnOpenGLDemo PRIVATE ReWindowLibrary)
|
||||
target_link_libraries(LearnOpenGLDemo PUBLIC LearnOpenGL)
|
24
README.md
Normal file
24
README.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# LearnOpenGL::
|
||||
A collection of OpenGL Utilities.
|
||||
|
||||
Several of these resources have been taken from https://learnopengl.com along with other resources (list below), plus a few of my own additions and refactoring.
|
||||
|
||||
|
||||
|
||||
### Classes
|
||||
* Mesh
|
||||
* Model
|
||||
* Shader
|
||||
* Texture2D
|
||||
|
||||
### Functions
|
||||
|
||||
###
|
||||
|
||||
## Notes
|
||||
|
||||
By the way, I'm the kind of asshole that rolls his own 3D Math library instead of using glm like a sane person.
|
||||
|
||||
WTF Why?
|
||||
1. It's my project
|
||||
2. Feel free to add conditional glm support
|
24
cmake/CPM.cmake
Normal file
24
cmake/CPM.cmake
Normal file
@@ -0,0 +1,24 @@
|
||||
# SPDX-License-Identifier: MIT
|
||||
#
|
||||
# SPDX-FileCopyrightText: Copyright (c) 2019-2023 Lars Melchior and contributors
|
||||
|
||||
set(CPM_DOWNLOAD_VERSION 0.38.7)
|
||||
set(CPM_HASH_SUM "83e5eb71b2bbb8b1f2ad38f1950287a057624e385c238f6087f94cdfc44af9c5")
|
||||
|
||||
if(CPM_SOURCE_CACHE)
|
||||
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
|
||||
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
|
||||
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
|
||||
else()
|
||||
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
|
||||
endif()
|
||||
|
||||
# Expand relative path. This is important if the provided path contains a tilde (~)
|
||||
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)
|
||||
|
||||
file(DOWNLOAD
|
||||
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
|
||||
${CPM_DOWNLOAD_LOCATION} EXPECTED_HASH SHA256=${CPM_HASH_SUM}
|
||||
)
|
||||
|
||||
include(${CPM_DOWNLOAD_LOCATION})
|
8
include/LearnOpenGL/LearnOpenGL.h
Normal file
8
include/LearnOpenGL/LearnOpenGL.h
Normal file
@@ -0,0 +1,8 @@
|
||||
//
|
||||
// Created by dawsh on 2/22/24.
|
||||
//
|
||||
|
||||
#ifndef LEARNOPENGL_LEARNOPENGL_H
|
||||
#define LEARNOPENGL_LEARNOPENGL_H
|
||||
|
||||
#endif //LEARNOPENGL_LEARNOPENGL_H
|
148
include/LearnOpenGL/Mesh.h
Normal file
148
include/LearnOpenGL/Mesh.h
Normal file
@@ -0,0 +1,148 @@
|
||||
/// @file Mesh.h
|
||||
/// @description
|
||||
/// @author LearnOpenGL - Updated Josh O'Leary
|
||||
/// @lastedit 2024-02-22
|
||||
|
||||
// Original Source:
|
||||
// https://learnopengl.com/code_viewer_gh.php?code=includes/learnopengl/mesh.h
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
//#include <glad/glad.h>
|
||||
#include <glad/glad.h>
|
||||
|
||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
||||
#include <J3ML/LinearAlgebra/Vector2.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "Shader.h"
|
||||
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
using J3ML::LinearAlgebra::Vector3;
|
||||
using J3ML::u32;
|
||||
|
||||
constexpr int MAX_BONE_INFLUENCE = 4;
|
||||
|
||||
namespace LearnOpenGL
|
||||
{
|
||||
|
||||
struct Vertex
|
||||
{
|
||||
Vector3 Position;
|
||||
Vector3 Normal;
|
||||
Vector2 TexCoords;
|
||||
Vector3 Tangent;
|
||||
Vector3 BiTangent;
|
||||
int BoneIDs[MAX_BONE_INFLUENCE];
|
||||
float BoneWeights[MAX_BONE_INFLUENCE];
|
||||
};
|
||||
|
||||
// Different from Texture2D in purpose
|
||||
|
||||
class Mesh {
|
||||
public:
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<uint> indices;
|
||||
std::vector<Texture2D> textures;
|
||||
uint VAO;
|
||||
|
||||
Mesh(std::vector<Vertex> verts, std::vector<uint> indices, std::vector<Texture2D> textures)
|
||||
{
|
||||
this->vertices = verts;
|
||||
this->indices = indices;
|
||||
this->textures = textures;
|
||||
|
||||
setupMesh();
|
||||
}
|
||||
|
||||
void Draw(Shader &shader)
|
||||
{
|
||||
uint diffuse = 1;
|
||||
uint specular = 1;
|
||||
uint normal = 1;
|
||||
uint height = 1;
|
||||
for (uint i = 0; i < textures.size(); i++)
|
||||
{
|
||||
// activate proper texture unit before binding
|
||||
glActiveTexture(GL_TEXTURE0 + i);
|
||||
// retrieve texture number (the N in diffuse_textureN)
|
||||
std::string number;
|
||||
std::string name = textures[i].Type;
|
||||
if (name == "texture_diffuse")
|
||||
number = std::to_string(diffuse++);
|
||||
else if (name == "texture_specular")
|
||||
number = std::to_string(specular++);
|
||||
else if (name == "texture_normal")
|
||||
number = std::to_string(normal++);
|
||||
else if (name == "texture_height")
|
||||
number = std::to_string(height++);
|
||||
|
||||
// now set the sampler to the correct texture unit
|
||||
glUniform1i(glGetUniformLocation(shader.ID, (name + number).c_str()), i);
|
||||
// and finally bind the texture
|
||||
glBindTexture(GL_TEXTURE_2D, textures[i].ID);
|
||||
}
|
||||
|
||||
// draw mesh
|
||||
|
||||
//glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, static_cast<uint>(indices.size()), GL_UNSIGNED_INT, 0);
|
||||
//glBindVertexArray(0);
|
||||
|
||||
// always good practice to set everything back to defaults once configured.
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
}
|
||||
private:
|
||||
// render data
|
||||
unsigned int VBO, EBO;
|
||||
|
||||
// initializes all the buffer objects/arrays
|
||||
void setupMesh()
|
||||
{
|
||||
|
||||
//glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glGenBuffers(1, &EBO);
|
||||
|
||||
//glBindVertexArray(VAO);
|
||||
// load data into vertex buffers
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
|
||||
// A great thing about structs is that their memory layout is sequential for all member items
|
||||
// The effect is that we can simply pass a pointer to the struct and it translates perfectly to a glm::vec3/vec2 array
|
||||
// which again translates to 3/2 floats which translates to a byte array
|
||||
// Except we use J3ML instead of glm, but the concept applies nonetheless
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(uint), &indices[0], GL_STATIC_DRAW);
|
||||
|
||||
// set the vertex attribute pointers
|
||||
// vertex positions
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
|
||||
// vertex normals
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
|
||||
// vertex texture coords
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
|
||||
// vertex tangent
|
||||
glEnableVertexAttribArray(3);
|
||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Tangent));
|
||||
// vertex bitangent
|
||||
glEnableVertexAttribArray(4);
|
||||
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, BiTangent));
|
||||
// ids
|
||||
glEnableVertexAttribArray(5);
|
||||
glVertexAttribPointer(5, 4, GL_INT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, BoneIDs));
|
||||
|
||||
// weights
|
||||
glEnableVertexAttribArray(6);
|
||||
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, BoneWeights));
|
||||
//glBindVertexArray(0);
|
||||
}
|
||||
};
|
||||
}
|
54
include/LearnOpenGL/Model.h
Normal file
54
include/LearnOpenGL/Model.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/// @file Model.h
|
||||
/// @description
|
||||
/// @author learnopengl.com - Updated by Josh
|
||||
/// @lastedit 2024-02-22
|
||||
/// @license Unlicense - Public Domain
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdlib>
|
||||
#include <glad/glad.h>
|
||||
#include <assimp/Importer.hpp>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <LearnOpenGL/Mesh.h>
|
||||
#include <LearnOpenGL/Shader.h>
|
||||
|
||||
namespace LearnOpenGL
|
||||
{
|
||||
uint TextureFromFile(const char *path, const std::string& directory, bool gamma = false);
|
||||
|
||||
|
||||
class Model
|
||||
{
|
||||
public:
|
||||
// model data
|
||||
std::vector<LearnOpenGL::Texture2D> textures_loaded; // Stores all the textures loaded so far, optimization to make sure textures aren't loaded more than once
|
||||
std::vector<LearnOpenGL::Mesh> meshes;
|
||||
std::string directory;
|
||||
bool gammaCorrection;
|
||||
|
||||
// Expects a filepath to a 3D model
|
||||
Model(const std::string& path, bool gamma = false) : gammaCorrection(gamma)
|
||||
{
|
||||
loadModel(path);
|
||||
}
|
||||
|
||||
// Draws the model, and this all its meshes
|
||||
void Draw(Shader& shader)
|
||||
{
|
||||
for(auto& mesh : meshes)
|
||||
mesh.Draw(shader);
|
||||
}
|
||||
protected:
|
||||
private:
|
||||
// loads a model with supported ASSIMP extensions from file and stores the resulting meshes in the meshes vector
|
||||
void loadModel(const std::string& path)
|
||||
{
|
||||
// read file via ASSIMP
|
||||
Assimp::Importer importer;
|
||||
}
|
||||
};
|
||||
}
|
58
include/LearnOpenGL/Shader.h
Normal file
58
include/LearnOpenGL/Shader.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
// LearnOpenGL::Shader
|
||||
// OpenGL Shader Class Wrapper
|
||||
// Updated by dawsh
|
||||
// https://learnopengl.com/code_viewer_gh.php?code=includes/learnopengl/shader.h
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <glad/glad.h>
|
||||
#include <J3ML/LinearAlgebra/Vector2.h>
|
||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
||||
#include <J3ML/LinearAlgebra/Vector4.h>
|
||||
#include <J3ML/LinearAlgebra/Matrix2x2.h>
|
||||
#include <J3ML/LinearAlgebra/Matrix3x3.h>
|
||||
#include <J3ML/LinearAlgebra/Matrix4x4.h>
|
||||
|
||||
namespace LearnOpenGL {
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
using J3ML::LinearAlgebra::Vector3;
|
||||
using J3ML::LinearAlgebra::Vector4;
|
||||
using J3ML::LinearAlgebra::Matrix2x2;
|
||||
using J3ML::LinearAlgebra::Matrix3x3;
|
||||
using J3ML::LinearAlgebra::Matrix4x4;
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int ID{};
|
||||
|
||||
Shader();
|
||||
Shader(std::filesystem::path vertexProgramPath, std::filesystem::path fragmentProgramPath);
|
||||
Shader(std::string vertexProgramSrc, std::string fragmentProgramSrc);
|
||||
|
||||
void use();
|
||||
// Utility uniform functions
|
||||
void setBool(const std::string& name, bool value) const;
|
||||
void setInt(const std::string& name, int value) const;
|
||||
void setFloat(const std::string& name, float value) const;
|
||||
void setVec2(const std::string& name, const Vector2& value) const;
|
||||
void setVec2(const std::string& name, float x, float y) const;
|
||||
void setVec3(const std::string& name, const Vector3& value) const;
|
||||
void setVec3(const std::string& name, float x, float y, float z) const;
|
||||
void setVec4(const std::string& name, const Vector4& value) const;
|
||||
void setVec4(const std::string& name, float x, float y, float z, float w) const;
|
||||
void setMat2(const std::string& name, const Matrix2x2 &mat) const;
|
||||
void setMat3(const std::string& name, const Matrix3x3 &mat) const;
|
||||
void setMat4(const std::string& name, const Matrix4x4 &mat) const;
|
||||
|
||||
GLint getAttribute(const std::string& name) const;
|
||||
GLint getUniform(const std::string& name) const;
|
||||
|
||||
private:
|
||||
void checkCompileErrors(GLuint shader, std::string type);
|
||||
};
|
||||
}
|
28
include/LearnOpenGL/Texture2D.h
Normal file
28
include/LearnOpenGL/Texture2D.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace LearnOpenGL {
|
||||
// Texture2D is able to store and configure a texture in OpenGL
|
||||
// It also hosts utility functions for easy management.
|
||||
class Texture2D {
|
||||
public:
|
||||
unsigned int ID; // ID of the texture object
|
||||
unsigned int Width, Height;
|
||||
unsigned int InternalFormat;
|
||||
unsigned int ImageFormat;
|
||||
unsigned int WrapS;
|
||||
unsigned int WrapT;
|
||||
unsigned int Filter_Min;
|
||||
unsigned int Filter_Max;
|
||||
|
||||
std::string Type;
|
||||
std::string Path;
|
||||
|
||||
Texture2D();
|
||||
|
||||
void Generate(unsigned int width, unsigned int height, unsigned char *data);
|
||||
|
||||
void Bind() const;
|
||||
};
|
||||
}
|
29
main.cpp
Normal file
29
main.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <LearnOpenGL/Texture2D.h>
|
||||
#include <LearnOpenGL/Shader.h>
|
||||
#include <LearnOpenGL/Mesh.h>
|
||||
#include <LearnOpenGL/Model.h>
|
||||
#include <rewindow/types/window.h>
|
||||
|
||||
class LearnOpenGLDemoWindow : public ReWindow::RWindow
|
||||
{
|
||||
public:
|
||||
LearnOpenGLDemoWindow() : ReWindow::RWindow("LearnOpenGL Window", 1000, 600, RenderingAPI::OPENGL)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::cout << "OpenGL Utility Classes" << std::endl;
|
||||
|
||||
auto window = new LearnOpenGLDemoWindow();
|
||||
window->Open();
|
||||
gladLoadGL();
|
||||
|
||||
LearnOpenGL::Texture2D testTexture;
|
||||
|
||||
return 0;
|
||||
}
|
1
src/LearnOpenGL/LearnOpenGL.cpp
Normal file
1
src/LearnOpenGL/LearnOpenGL.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include <LearnOpenGL/LearnOpenGL.h>
|
205
src/LearnOpenGL/Shader.cpp
Normal file
205
src/LearnOpenGL/Shader.cpp
Normal file
@@ -0,0 +1,205 @@
|
||||
#include <LearnOpenGL/Shader.h>
|
||||
|
||||
namespace LearnOpenGL
|
||||
{
|
||||
|
||||
Shader::Shader(std::filesystem::path vertexProgramPath, std::filesystem::path fragmentProgramPath) {
|
||||
std::string vertexCode;
|
||||
std::string fragmentCode;
|
||||
std::string geometryCode;
|
||||
std::ifstream vShaderFile;
|
||||
std::ifstream fShaderFile;
|
||||
std::ifstream gShaderFile;
|
||||
|
||||
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
|
||||
try { // Open Files
|
||||
vShaderFile.open(vertexProgramPath);
|
||||
fShaderFile.open(fragmentProgramPath);
|
||||
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 (false)
|
||||
{
|
||||
gShaderFile.open("");
|
||||
std::stringstream gShaderStream;
|
||||
gShaderStream << gShaderFile.rdbuf();
|
||||
gShaderFile.close();
|
||||
geometryCode = gShaderStream.str();
|
||||
}
|
||||
} catch (std::ifstream::failure& e) {
|
||||
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " << e.what() << 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 (false)
|
||||
{
|
||||
//const char * gShaderCode = geometryCode.c_str();
|
||||
//geometry = glCreateShader(GL_GEOMETRY_SHADER);
|
||||
//glShaderSource(geometry, 1, &gShaderCode, NULL);
|
||||
//glCompileShader(geometry);
|
||||
//checkCompileErrors(geometry, "GEOMETRY");
|
||||
}
|
||||
// shader Program
|
||||
ID = glCreateProgram();
|
||||
glAttachShader(ID, vertex);
|
||||
glAttachShader(ID, fragment);
|
||||
if (false) // geometryPath != nullptr)
|
||||
glAttachShader(ID, geometry);
|
||||
glLinkProgram(ID);
|
||||
checkCompileErrors(ID, "PROGRAM");
|
||||
// delete the shaders as they're linked into our program now and are no longer necessary
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
if (false) //geometryPath != nullptr)
|
||||
glDeleteShader(geometry);
|
||||
}
|
||||
|
||||
Shader::Shader(std::string vertexProgramSrc, std::string fragmentProgramSrc) {
|
||||
|
||||
std::string geometryCode;
|
||||
|
||||
const char* vShaderCode = vertexProgramSrc.c_str();
|
||||
const char* fShaderCode = fragmentProgramSrc.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 (false)
|
||||
{
|
||||
//const char * gShaderCode = geometryCode.c_str();
|
||||
//geometry = glCreateShader(GL_GEOMETRY_SHADER);
|
||||
//glShaderSource(geometry, 1, &gShaderCode, NULL);
|
||||
//glCompileShader(geometry);
|
||||
//checkCompileErrors(geometry, "GEOMETRY");
|
||||
}
|
||||
// shader Program
|
||||
ID = glCreateProgram();
|
||||
glAttachShader(ID, vertex);
|
||||
glAttachShader(ID, fragment);
|
||||
if (false) // geometryPath != nullptr)
|
||||
glAttachShader(ID, geometry);
|
||||
glLinkProgram(ID);
|
||||
checkCompileErrors(ID, "PROGRAM");
|
||||
// delete the shaders as they're linked into our program now and are no longer necessary
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
if (false) //geometryPath != nullptr)
|
||||
glDeleteShader(geometry);
|
||||
}
|
||||
|
||||
void Shader::use() {
|
||||
glUseProgram(ID);
|
||||
}
|
||||
|
||||
GLint Shader::getAttribute(const std::string& name) const
|
||||
{
|
||||
return glGetAttribLocation(ID, name.c_str());
|
||||
}
|
||||
|
||||
GLint Shader::getUniform(const std::string &name) const {
|
||||
return glGetUniformLocation(ID, name.c_str());
|
||||
}
|
||||
|
||||
void Shader::setBool(const std::string &name, bool value) const {
|
||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
|
||||
}
|
||||
|
||||
void Shader::setInt(const std::string &name, int value) const {
|
||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
|
||||
void Shader::setFloat(const std::string &name, float value) const {
|
||||
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
|
||||
void Shader::setVec2(const std::string &name, const Vector2 &value) const {
|
||||
glUniform2f(glGetUniformLocation(ID, name.c_str()), value.x, value.y);
|
||||
}
|
||||
|
||||
void Shader::setVec2(const std::string &name, float x, float y) const {
|
||||
glUniform2f(glGetUniformLocation(ID, name.c_str()), x, y);
|
||||
}
|
||||
|
||||
void Shader::setVec3(const std::string &name, const Vector3 &value) const {
|
||||
glUniform3f(glGetUniformLocation(ID, name.c_str()), value.x, value.y, value.z);
|
||||
}
|
||||
|
||||
void Shader::setVec3(const std::string &name, float x, float y, float z) const {
|
||||
glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z);
|
||||
}
|
||||
|
||||
void Shader::setVec4(const std::string &name, const Vector4 &value) const {
|
||||
glUniform4f(glGetUniformLocation(ID, name.c_str()), value.x, value.y, value.z, value.w);
|
||||
}
|
||||
|
||||
void Shader::setVec4(const std::string &name, float x, float y, float z, float w) const {
|
||||
glUniform4f(glGetUniformLocation(ID, name.c_str()), x, y, z, w);
|
||||
}
|
||||
|
||||
void Shader::setMat2(const std::string &name, const Matrix2x2 &mat) const {
|
||||
//glUniformMatrix2fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat.At(0, 0));
|
||||
}
|
||||
|
||||
void Shader::setMat3(const std::string &name, const Matrix3x3 &mat) const {
|
||||
//glUniformMatrix3fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat.At(0, 0));
|
||||
}
|
||||
|
||||
void Shader::setMat4(const std::string &name, const Matrix4x4 &mat) const {
|
||||
//glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat.At(0, 0));
|
||||
}
|
||||
|
||||
void Shader::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 << 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 << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Shader::Shader() {}
|
||||
}
|
36
src/LearnOpenGL/Texture2D.cpp
Normal file
36
src/LearnOpenGL/Texture2D.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <LearnOpenGL/Texture2D.h>
|
||||
#include <glad/glad.h>
|
||||
#include <GL/glut.h>
|
||||
|
||||
namespace LearnOpenGL
|
||||
{
|
||||
|
||||
|
||||
|
||||
Texture2D::Texture2D()
|
||||
: Width(0), Height(0), InternalFormat(GL_RGB), ImageFormat(GL_RGB), WrapS(GL_REPEAT), WrapT(GL_REPEAT),
|
||||
Filter_Min(GL_LINEAR), Filter_Max(GL_LINEAR) {
|
||||
glGenTextures(1, &this->ID);
|
||||
}
|
||||
|
||||
void Texture2D::Generate(unsigned int width, unsigned int height, unsigned char *data) {
|
||||
this->Width = width;
|
||||
this->Height = height;
|
||||
glBindTexture(GL_TEXTURE_2D, this->ID);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, this->InternalFormat, width, height, 0, this->ImageFormat, GL_UNSIGNED_BYTE,
|
||||
data);
|
||||
// set TextureHandle wrap and filter modes
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, this->WrapS);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, this->WrapT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, this->Filter_Min);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, this->Filter_Max);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void Texture2D::Bind() const
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D, this->ID);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user