basic triangle

This commit is contained in:
leca 2025-01-18 03:10:32 +03:00
parent a80395b964
commit b012641dc9
4 changed files with 120 additions and 8 deletions

View File

@ -1,2 +1,2 @@
#!/usr/bin/env bash
cd build && cmake .. && make && ./TimeCoil
cd build && cmake -DCMAKE_C_FLAGS="-g" .. && make && ./TimeCoil

6
shaders/shader.frag Normal file
View File

@ -0,0 +1,6 @@
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}

6
shaders/shader.vert Normal file
View File

@ -0,0 +1,6 @@
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}

View File

@ -1,6 +1,8 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <cstring>
void framebufferSizeCallback(GLFWwindow *window, int width, int height) {
glViewport(10, 10, width-10, height-10);
@ -16,6 +18,78 @@ void renderGraphics(GLFWwindow *window) {
}
void gracefulExit(int code) {
glfwTerminate();
exit(code);
}
std::string readFile(std::string path) {
std::ifstream file(path);
if (!file.is_open())
std::cerr << "File " << path << " was not found." << std::endl;
std::string content = "";
std::string buffer = "";
while(getline(file, buffer)) {
content += buffer + "\n";
}
content += '\0';
std::cout << content << std::endl;
file.close();
return content;
}
unsigned int prepareShaders() {
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
std::string vertexShaderSourceString = readFile("../shaders/shader.vert");
std::string fragmentShaderSourceString = readFile("../shaders/shader.frag");
const char *vertexShaderSource = vertexShaderSourceString.c_str();
const char *fragmentShaderSource = fragmentShaderSourceString.c_str();
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
int success;
char log[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertexShader, 512, NULL, log);
std::cerr << "Could not compile vertex shader: " << log << std::endl;
gracefulExit(-1);
}
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragmentShader, 512, NULL, log);
std::cerr << "Could not compile fragment shader: " << log << std::endl;
gracefulExit(-1);
}
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if(!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, log);
std::cerr << "Could not link program: " << log << std::endl;
gracefulExit(-1);
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return shaderProgram;
}
int main () {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@ -28,8 +102,7 @@ int main () {
GLFWwindow *window = glfwCreateWindow(800, 600, "TimeCoil", NULL, NULL);
if (window == NULL) {
std::cerr << "Failed to create GLFW window." << std::endl;
glfwTerminate();
return -1;
gracefulExit(-1);
}
glfwMakeContextCurrent(window);
@ -40,16 +113,43 @@ int main () {
return -1;
}
unsigned int shaderProgram = prepareShaders();
float vertices[] = {
-0.5, 0.0, 0.0,
0.5, 0.5, 0.0,
0.5, -0.5, 0.0
};
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
while(!glfwWindowShouldClose(window)) {
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
gracefulExit(0);
}