joo o
- 📅 2023-10-16T17:44:59.021Z
- 👁️ 182 katselukertaa
- 🔓 Julkinen
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <signal.h>
#include "Colors.h"
#define ASSERT(x) \
if (!(x)) \
raise(SIGTRAP);
#define GLCall(x) \
GLClearError(); \
x; \
ASSERT(GLLogCall(#x, __FILE__, __LINE__));
static void GLClearError()
{
while (glGetError() != GL_NO_ERROR);
};
static bool GLLogCall(const char *function, const char *file, int line)
{
std::cout << STRING_BOLDRED <<glGetError();
while (GLenum error = glGetError())
{
std::cout << STRING_RED << "[OpenGL Error]" << STRING_BOLDRED << "(" << std::hex << error << ")" << STRING_RESET << "\n"
<< STRING_GREEN << function << ", " << STRING_YELLOW << file << ":" << std::dec << line << STRING_RESET << std::endl;
return false;
};
return true;
};
struct ShaderSources
{
std::string VertexSource;
std::string FragmentSource;
};
static ShaderSources ParseShader(const std::string &file)
{
enum class ShaderType
{
NONE = -1,
VERTEX = 0,
FRAGMENT = 1
};
std::ifstream stream(file);
std::string line;
std::stringstream ss[2];
ShaderType type = ShaderType::NONE;
while (getline(stream, line))
{
if (line.find("#shader") != std::string::npos)
{
if (line.find("vertex") != std::string::npos)
{
type = ShaderType::VERTEX;
}
else if (line.find("fragment") != std::string::npos)
{
type = ShaderType::FRAGMENT;
}
}
else
{
ss[(int)type] << line << "\n";
}
}
return {ss[0].str(), ss[1].str()};
};
static unsigned int CompileShader(unsigned int type, const std::string &source)
{
unsigned int id = glCreateShader(type);
const char *src = source.c_str(); // &source[0]
GLCall(glShaderSource(id, 1, &src, nullptr));
GLCall(glCompileShader(id));
int result;
GLCall(glGetShaderiv(id, GL_COMPILE_STATUS, &result));
if (result == GL_FALSE)
{
int length;
GLCall(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
char *message = (char *)alloca(length * sizeof(char));
GLCall(glGetShaderInfoLog(id, length, &length, message));
std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << std::endl;
std::cout << message << std::endl;
GLCall(glDeleteShader(id));
return 0;
}
return id;
}
static int CreateShader(const std::string &vertexShader, const std::string &fragmentShader)
{
GLCall(unsigned int program = glCreateProgram());
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
GLCall(glAttachShader(program, vs));
GLCall(glAttachShader(program, fs));
GLCall(glLinkProgram(program));
GLCall(glValidateProgram(program));
GLCall(glDeleteShader(vs)); // we dont need them anymore
GLCall(glDeleteShader(fs));
return program;
}
int main(void)
{
GLFWwindow *window;
/* Initialize the library */
if (!glfwInit())
{
std::cout << STRING_RED << "Error! Couldnt load glfw :D" << STRING_RESET << std::endl;
return -1;
};
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(480, 480, "aaaaa", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK)
{
std::cout << STRING_RED << "Error! Couldnt load glew :D or something else fucked up" << STRING_RESET << std::endl;
return -1;
};
GLCall(std::cout << glGetString(GL_VERSION) << "\n"
<< glGetString(GL_RENDERER) << std::endl);
float positions[] = {
-0.5f,
-0.5f,
0.5f,
-0.5f,
0.5f,
0.5f,
-0.5f,
0.5f,
};
unsigned int indicies[] = {
0, 1, 2,
2, 3, 0};
unsigned int vao;
GLCall(glGenVertexArrays(1,&vao));
GLCall(glBindVertexArray(vao));
unsigned int buffer;
GLCall(glGenBuffers(1, &buffer));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, buffer);)
GLCall(glBufferData(GL_ARRAY_BUFFER, 6 * 2 * sizeof(float), positions, GL_STATIC_DRAW));
GLCall(glEnableVertexAttribArray(0));
GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0));
// must be unsigned
unsigned int ibo;
GLCall(glGenBuffers(1, &ibo));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo));
GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indicies, GL_STATIC_DRAW));
GLCall(ShaderSources source = ParseShader("res/shaders/Basic.shader"));
std::cout << STRING_GREEN << "[VERTEX]" << std::endl << STRING_RESET
<< source.VertexSource << std::endl
<< STRING_GREEN << "[FRAGMENT]" << std::endl << STRING_RESET
<< source.FragmentSource << std::endl;
GLCall(unsigned int shader = CreateShader(source.VertexSource, source.FragmentSource));
GLCall(glUseProgram(shader));
GLCall(int location = glGetUniformLocation(shader, "u_Color"));
ASSERT(location != -1);
float i, o, u = 0.1f;
/* Loop until the user closes the window */
GLCall(glUseProgram(0));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
GLCall(glBindVertexArray(0));
while (!glfwWindowShouldClose(window))
{
GLCall(glClear(GL_COLOR_BUFFER_BIT));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo));
GLCall(glUseProgram(shader));
GLCall(glBindVertexArray(vao));
GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr));
GLCall(glfwSwapInterval(1)) // vsync
/* Swap front and back buffers */
GLCall(glUniform4f(location, i, o, u, 1.0f));
if (i > 1.0f)
i = 0.0f;
if (o > 1.0f)
o = 0.0f;
if (u > 1.0f)
u = 0.0f;
i += 0.01f;
o += 0.02f;
u += 0.03f;
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
// Change the color of the cube
}
GLCall(glDeleteProgram(shader));
glfwTerminate();
return 0;
}