Shader类,Texture2D类,ResourceManager类都是从learnopengl中复制过来的。主要是这些代码的确写的很好,基本没有修改的必要,我直接复制了。( ̄_, ̄ )
Shader类
#ifndef SHADER_H
#define SHADER_H
#include <string>
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
class Shader
{
public:
unsigned int ID;
Shader();
Shader& Use();
void Compile(const char* vertexSource, const char* fragmentSource, const char* geometrySource = nullptr);
void SetFloat(const char* name, float value, bool useShader = false);
void SetInteger(const char* name, int value, bool useShader = false);
void SetVector2f(const char* name, float x, float y, bool useShader = false);
void SetVector2f(const char* name, const glm::vec2& value, bool useShader = false);
void SetVector3f(const char* name, float x, float y, float z, bool useShader = false);
void SetVector3f(const char* name, const glm::vec3& value, bool useShader = false);
void SetVector4f(const char* name, float x, float y, float z, float w, bool useShader = false);
void SetVector4f(const char* name, const glm::vec4& value, bool useShader = false);
void SetMatrix4(const char* name, const glm::mat4& matrix, bool useShader = false);
private:
void checkCompileErrors(unsigned int object, std::string type);
};
#endif
#include "shader.h"
#include <iostream>
inline Shader::Shader() { }
Shader& Shader::Use()
{
glUseProgram(this->ID);
return *this;
}
void Shader::Compile(const char* vertexSource, const char* fragmentSource, const char* geometrySource)
{
unsigned int sVertex, sFragment, gShader;
sVertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(sVertex, 1, &vertexSource, NULL);
glCompileShader(sVertex);
checkCompileErrors(sVertex, "VERTEX");
sFragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(sFragment, 1, &fragmentSource, NULL);
glCompileShader(sFragment);
checkCompileErrors(sFragment, "FRAGMENT");
if (geometrySource != nullptr)
{
gShader = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(gShader, 1, &geometrySource, NULL);
glCompileShader(gShader);
checkCompileErrors(gShader, "GEOMETRY");
}
this->ID = glCreateProgram();
glAttachShader(this->ID, sVertex);
glAttachShader(this->ID, sFragment);
if (geometrySource != nullptr)
glAttachShader(this->ID, gShader);
glLinkProgram(this->ID);
checkCompileErrors(this->ID, "PROGRAM");
glDeleteShader(sVertex);
glDeleteShader(sFragment);
if (geometrySource != nullptr)
glDeleteShader(gShader);
}
void Shader::SetFloat(const char* name, float value, bool useShader)
{
if (useShader)
this->Use();
glUniform1f(glGetUniformLocation(this->ID, name), value);
}
void Shader::SetInteger(const char* name, int value, bool useShader)
{
if (useShader)
this->Use();
glUniform1i(glGetUniformLocation(this->ID, name), value);
}
void Shader::SetVector2f(const char* name, float x, float y, bool useShader)
{
if (useShader)
this->Use();
glUniform2f(glGetUniformLocation(this->ID, name), x, y);
}
void Shader::SetVector2f(const char* name, const glm::vec2& value, bool useShader)
{
if (useShader)
this->Use();
glUniform2f(glGetUniformLocation(this->ID, name), value.x, value.y);
}
void Shader::SetVector3f(const char* name, float x, float y, float z, bool useShader)
{
if (useShader)
this->Use();
glUniform3f(glGetUniformLocation(this->ID, name), x, y, z);
}
void Shader::SetVector3f(const char* name, const glm::vec3& value, bool useShader)
{
if (useShader)
this->Use();
glUniform3f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z);
}
void Shader::SetVector4f(const char* name, float x, float y, float z, float w, bool useShader)
{
if (useShader)
this->Use();
glUniform4f(glGetUniformLocation(this->ID, name), x, y, z, w);
}
void Shader::SetVector4f(const char* name, const glm::vec4& value, bool useShader)
{
if (useShader)
this->Use();
glUniform4f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z, value.w);
}
void Shader::SetMatrix4(const char* name, const glm::mat4& matrix, bool useShader)
{
if (useShader)
this->Use();
glUniformMatrix4fv(glGetUniformLocation(this->ID, name), 1, false, glm::value_ptr(matrix));
}
void Shader::checkCompileErrors(unsigned int object, std::string type)
{
int success;
char infoLog[1024];
if (type != "PROGRAM")
{
glGetShaderiv(object, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(object, 1024, NULL, infoLog);
std::cout << "| ERROR::SHADER: Compile-time error: Type: " << type << "\n"
<< infoLog << "\n -- --------------------------------------------------- -- "
<< std::endl;
}
}
else
{
glGetProgramiv(object, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(object, 1024, NULL, infoLog);
std::cout << "| ERROR::Shader: Link-time error: Type: " << type << "\n"
<< infoLog << "\n -- --------------------------------------------------- -- "
<< std::endl;
}
}
}
Texture类
#ifndef TEXTURE_H
#define TEXTURE_H
#include <glad/glad.h>
class Texture2D
{
public:
unsigned int ID;
unsigned int Width, Height;
unsigned int Internal_Format;
unsigned int Image_Format;
unsigned int Wrap_S;
unsigned int Wrap_T;
unsigned int Filter_Min;
unsigned int Filter_Max;
Texture2D();
void Generate(unsigned int width, unsigned int height, unsigned char* data);
void Bind() const;
};
#endif
#include <iostream>
#include "Texture.h"
Texture2D::Texture2D()
: Width(0), Height(0), Internal_Format(GL_RGB), Image_Format(GL_RGB), Wrap_S(GL_REPEAT), Wrap_T(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->Internal_Format, width, height, 0, this->Image_Format, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, this->Wrap_S);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, this->Wrap_T);
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);
}
ResourceManager类
#ifndef RESOURCE_MANAGER_H
#define RESOURCE_MANAGER_H
#include <map>
#include <string>
#include <glad/glad.h>
#include "Texture.h"
#include "Shader.h"
class ResourceManager
{
public:
static std::map<std::string, Shader> Shaders;
static std::map<std::string, Texture2D> Textures;
static Shader LoadShader(const char* vShaderFile, const char* fShaderFile, const char* gShaderFile, std::string name);
static Shader GetShader(std::string name);
static Texture2D LoadTexture(const char* file, bool alpha, std::string name);
static Texture2D GetTexture(std::string name);
static void Clear();
private:
ResourceManager() { }
static Shader loadShaderFromFile(const char* vShaderFile, const char* fShaderFile, const char* gShaderFile = nullptr);
static Texture2D loadTextureFromFile(const char* file, bool alpha);
};
#endif
#include "ResourceManager.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include "stb_image.h"
std::map<std::string, Texture2D> ResourceManager::Textures;
std::map<std::string, Shader> ResourceManager::Shaders;
Shader ResourceManager::LoadShader(const char* vShaderFile, const char* fShaderFile, const char* gShaderFile, std::string name)
{
Shaders[name] = loadShaderFromFile(vShaderFile, fShaderFile, gShaderFile);
return Shaders[name];
}
Shader ResourceManager::GetShader(std::string name)
{
return Shaders[name];
}
Texture2D ResourceManager::LoadTexture(const char* file, bool alpha, std::string name)
{
Textures[name] = loadTextureFromFile(file, alpha);
return Textures[name];
}
Texture2D ResourceManager::GetTexture(std::string name)
{
return Textures[name];
}
void ResourceManager::Clear()
{
for (auto iter : Shaders)
glDeleteProgram(iter.second.ID);
for (auto iter : Textures)
glDeleteTextures(1, &iter.second.ID);
}
Shader ResourceManager::loadShaderFromFile(const char* vShaderFile, const char* fShaderFile, const char* gShaderFile)
{
std::string vertexCode;
std::string fragmentCode;
std::string geometryCode;
try
{
std::ifstream vertexShaderFile(vShaderFile);
std::ifstream fragmentShaderFile(fShaderFile);
std::stringstream vShaderStream, fShaderStream;
vShaderStream << vertexShaderFile.rdbuf();
fShaderStream << fragmentShaderFile.rdbuf();
vertexShaderFile.close();
fragmentShaderFile.close();
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
if (gShaderFile != nullptr)
{
std::ifstream geometryShaderFile(gShaderFile);
std::stringstream gShaderStream;
gShaderStream << geometryShaderFile.rdbuf();
geometryShaderFile.close();
geometryCode = gShaderStream.str();
}
}
catch (std::exception e)
{
std::cout << "ERROR::SHADER: Failed to read shader files" << std::endl;
}
const char* vShaderCode = vertexCode.c_str();
const char* fShaderCode = fragmentCode.c_str();
const char* gShaderCode = geometryCode.c_str();
Shader shader;
shader.Compile(vShaderCode, fShaderCode, gShaderFile != nullptr ? gShaderCode : nullptr);
return shader;
}
Texture2D ResourceManager::loadTextureFromFile(const char* file, bool alpha)
{
Texture2D texture;
if (alpha)
{
texture.Internal_Format = GL_RGBA;
texture.Image_Format = GL_RGBA;
}
int width, height, nrChannels;
unsigned char* data = stbi_load(file, &width, &height, &nrChannels, 0);
texture.Generate(width, height, data);
stbi_image_free(data);
return texture;
}
|