-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7009ed1
commit 9462437
Showing
8 changed files
with
66 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#include "Render.hpp" | ||
|
||
struct MainContext | ||
{ | ||
static MainContext Main; | ||
|
||
ResourceManager R; | ||
Renderer Render; | ||
|
||
void setup(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,9 @@ | ||
#include "Render.hpp" | ||
#include <format> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <string> | ||
|
||
unsigned int VAO; | ||
|
||
Render::Render(Shader shader) | ||
: shader(std::move(shader)) | ||
Renderer::Renderer() | ||
{ | ||
// VAO | ||
glGenVertexArrays(1, &VAO); | ||
glBindVertexArray(VAO); | ||
|
||
// VBO | ||
unsigned int VBO; | ||
glGenBuffers(1, &VBO); | ||
glBindBuffer(GL_ARRAY_BUFFER, VBO); | ||
|
||
// EBO | ||
unsigned int EBO; | ||
glGenBuffers(1, &EBO); | ||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); | ||
|
||
// Vertex Attribute | ||
// 位置属性 | ||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); | ||
glEnableVertexAttribArray(0); | ||
// 颜色属性 | ||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); | ||
glEnableVertexAttribArray(1); | ||
|
||
// Wireframe | ||
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); | ||
} | ||
|
||
void Render::render() | ||
void Renderer::newFrame() | ||
{ | ||
shader.use(); | ||
|
||
glBindVertexArray(VAO); | ||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); | ||
glBindVertexArray(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,34 @@ | ||
#include "Shader.hpp" | ||
|
||
Shader::Shader(ResourceStream vShader, ResourceStream fShader) | ||
{ | ||
// 1. retrieve the vertex/fragment source code from filePath | ||
std::string vertexCode; | ||
std::string fragmentCode; | ||
std::stringstream vShaderStream, fShaderStream; | ||
|
||
// read file's buffer contents into streams | ||
vShaderStream << vShader->rdbuf(); | ||
fShaderStream << fShader->rdbuf(); | ||
// convert stream into string | ||
vertexCode = vShaderStream.str(); | ||
fragmentCode = fShaderStream.str(); | ||
|
||
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); | ||
// fragment Shader | ||
fragment = glCreateShader(GL_FRAGMENT_SHADER); | ||
glShaderSource(fragment, 1, &fShaderCode, NULL); | ||
glCompileShader(fragment); | ||
// shader Program | ||
ID = glCreateProgram(); | ||
glAttachShader(ID, vertex); | ||
glAttachShader(ID, fragment); | ||
glLinkProgram(ID); | ||
// delete the shaders as they're linked into our program now and no longer necessary | ||
glDeleteShader(vertex); | ||
glDeleteShader(fragment); | ||
} | ||
#include <glad/glad.h> | ||
|
||
Shader::~Shader() | ||
{ | ||
glDeleteProgram(ID); | ||
glDeleteProgram(ProgramID); | ||
} | ||
|
||
Shader::Shader(Shader&& _Right) | ||
: ID(_Right.ID) | ||
: ProgramID(_Right.ProgramID) | ||
{ | ||
_Right.ID = 0; | ||
_Right.ProgramID = 0; | ||
} | ||
|
||
void Shader::use() const | ||
{ | ||
glUseProgram(ID); | ||
glUseProgram(ProgramID); | ||
} | ||
|
||
void Shader::setBool(const std::string_view name, bool value) const | ||
{ | ||
glUniform1i(glGetUniformLocation(ID, name.data()), (int)value); | ||
glUniform1i(glGetUniformLocation(ProgramID, name.data()), (int)value); | ||
} | ||
|
||
void Shader::setInt(const std::string_view name, int value) const | ||
{ | ||
glUniform1i(glGetUniformLocation(ID, name.data()), value); | ||
glUniform1i(glGetUniformLocation(ProgramID, name.data()), value); | ||
} | ||
|
||
void Shader::setFloat(const std::string_view name, float value) const | ||
{ | ||
glUniform1f(glGetUniformLocation(ID, name.data()), value); | ||
glUniform1f(glGetUniformLocation(ProgramID, name.data()), value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters