mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 18:48:36 +02:00
~ | First triangle
This commit is contained in:
30
srcs/RT.cpp
30
srcs/RT.cpp
@ -15,18 +15,42 @@
|
||||
int main(void)
|
||||
{
|
||||
Window window;
|
||||
|
||||
Shader shader("shaders/vertex.glsl", "shaders/frag.glsl");
|
||||
|
||||
|
||||
shader.attach();
|
||||
|
||||
|
||||
RT::Vec2f vertices[6] = {
|
||||
{ -0.5f, -1.0f }, { 1.0f, -1.0f }, { -1.0f, 1.0f },
|
||||
{ 1.0f, -1.0f }, { 1.0f, 1.0f }, { -1.0f, 1.0f }
|
||||
};
|
||||
|
||||
unsigned int VAO, VBO;
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
|
||||
// Bind the VAO
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
// Bind the VBO and upload the vertex data to it
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
// Set vertex attributes
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// Unbind the VBO and VAO
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
while (!window.shouldClose())
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glUseProgram(shader.getProgram());
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
|
||||
window.display();
|
||||
window.pollEvents();
|
||||
|
@ -37,23 +37,10 @@ char* load_file(char const* path)
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void Shader::checkCompileErrors(GLuint shader)
|
||||
Shader::Shader(std::string vertexPath, std::string fragmentPath)
|
||||
{
|
||||
GLint success;
|
||||
GLchar infoLog[512];
|
||||
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(shader, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
Shader::Shader(char *vertexPath, char *fragmentPath)
|
||||
{
|
||||
const char *vertexCode = load_file(vertexPath);
|
||||
const char *fragmentCode = load_file(fragmentPath);
|
||||
const char *vertexCode = load_file(vertexPath.c_str());
|
||||
const char *fragmentCode = load_file(fragmentPath.c_str());
|
||||
|
||||
_vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||
|
||||
@ -70,15 +57,6 @@ Shader::Shader(char *vertexPath, char *fragmentPath)
|
||||
checkCompileErrors(_fragment);
|
||||
}
|
||||
|
||||
void Shader::attach(void)
|
||||
{
|
||||
_program = glCreateProgram();
|
||||
|
||||
glAttachShader(_program, _vertex);
|
||||
glAttachShader(_program, _fragment);
|
||||
glLinkProgram(_program);
|
||||
}
|
||||
|
||||
Shader::Shader(Shader const &src)
|
||||
{
|
||||
*this = src;
|
||||
@ -102,6 +80,28 @@ Shader::~Shader(void)
|
||||
glDeleteProgram(_program);
|
||||
}
|
||||
|
||||
void Shader::attach(void)
|
||||
{
|
||||
_program = glCreateProgram();
|
||||
|
||||
glAttachShader(_program, _vertex);
|
||||
glAttachShader(_program, _fragment);
|
||||
glLinkProgram(_program);
|
||||
}
|
||||
|
||||
void Shader::checkCompileErrors(GLuint shader)
|
||||
{
|
||||
GLint success;
|
||||
GLchar infoLog[512];
|
||||
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(shader, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
GLuint Shader::getProgram(void) const
|
||||
{
|
||||
return (_program);
|
||||
|
Reference in New Issue
Block a user