153 lines
4.2 KiB
C++
153 lines
4.2 KiB
C++
#include <cstdio>
|
|
#include <iostream>
|
|
#include "vendor/glad/glad.h"
|
|
#define GLFW_INCLUDE_NONE
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
|
unsigned int create_shader(unsigned int type, const char **source)
|
|
{
|
|
unsigned int shader = glCreateShader(type);
|
|
if (!shader) {
|
|
printf("err: create_shader: glCreateShader returned 0\n");
|
|
return 0;
|
|
}
|
|
glShaderSource(shader, 1, source, NULL);
|
|
glCompileShader(shader);
|
|
|
|
int success;
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
|
if (!success) {
|
|
char log[512];
|
|
glGetShaderInfoLog(shader, 512, NULL, log);
|
|
printf("err: create_shader: compilation failed, log: %s\n", log);
|
|
return 0;
|
|
}
|
|
|
|
return shader;
|
|
}
|
|
|
|
unsigned int create_program(const char *vertex_shader_source, const char *fragment_shader_source)
|
|
{
|
|
unsigned int program = glCreateProgram();
|
|
if (!program) {
|
|
printf("err: create_program: glCreateProgram returned 0\n");
|
|
return 0;
|
|
}
|
|
|
|
unsigned int vertex_shader = create_shader(GL_VERTEX_SHADER, &vertex_shader_source);
|
|
if (!vertex_shader) {
|
|
printf("err: create_program: create_shader failed returned 0 (vertex_shader)\n");
|
|
return 0;
|
|
}
|
|
unsigned int fragment_shader = create_shader(GL_FRAGMENT_SHADER, &fragment_shader_source);
|
|
if (!fragment_shader) {
|
|
printf("err: create_program: create_shader failed returned 0 (fragment_shader)\n");
|
|
return 0;
|
|
}
|
|
|
|
glAttachShader(program, vertex_shader);
|
|
glAttachShader(program, fragment_shader);
|
|
glLinkProgram(program);
|
|
|
|
int success;
|
|
glGetProgramiv(program, GL_LINK_STATUS, &success);
|
|
if (!success) {
|
|
char log[512];
|
|
glGetProgramInfoLog(program, 512, NULL, log);
|
|
printf("err: create_program: link failed, log: %s\n", log);
|
|
return 0;
|
|
}
|
|
|
|
return program;
|
|
}
|
|
|
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
|
|
{
|
|
glViewport(0, 0, width, height);
|
|
}
|
|
|
|
int main()
|
|
{
|
|
if (!glfwInit()) {
|
|
return 1;
|
|
}
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
GLFWwindow *window = glfwCreateWindow(800, 600, "gaming", NULL, NULL);
|
|
if (!window) {
|
|
return -1;
|
|
}
|
|
|
|
glfwMakeContextCurrent(window);
|
|
gladLoadGL();
|
|
glfwSwapInterval(1);
|
|
|
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
|
|
|
|
|
float verts[] = {
|
|
0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
|
|
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
|
0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
|
|
};
|
|
|
|
static const char *vertex_shader_source = ""
|
|
"#version 330 core\n"
|
|
"layout (location = 0) in vec2 aPos;\n"
|
|
"layout (location = 1) in vec3 aColor;\n"
|
|
"out vec3 fragColor;\n"
|
|
"void main()\n"
|
|
"{\n"
|
|
" gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);\n"
|
|
" fragColor = aColor;\n"
|
|
"}\0";
|
|
|
|
static const char *fragment_shader_source = ""
|
|
"#version 330 core\n"
|
|
"in vec3 fragColor;\n"
|
|
"out vec4 FragColor;\n"
|
|
"void main()\n"
|
|
"{\n"
|
|
" FragColor = vec4(fragColor, 1.0f);\n"
|
|
"}\0";
|
|
|
|
unsigned int program = create_program(vertex_shader_source, fragment_shader_source);
|
|
|
|
unsigned int vertex_array;
|
|
glGenVertexArrays(1, &vertex_array);
|
|
glBindVertexArray(vertex_array);
|
|
|
|
unsigned int buffer = 0;
|
|
glGenBuffers(1, &buffer);
|
|
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), &verts, GL_STATIC_DRAW);
|
|
|
|
// 2D position
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*)0);
|
|
glEnableVertexAttribArray(0);
|
|
|
|
// RGB color
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*)(2 * sizeof(float)));
|
|
glEnableVertexAttribArray(1);
|
|
|
|
while (!glfwWindowShouldClose(window)) {
|
|
glClearColor(0.5, 0.5, 0.5, 1.0);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glUseProgram(program);
|
|
glBindVertexArray(vertex_array);
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
|
|
glfwSwapBuffers(window);
|
|
glfwPollEvents();
|
|
}
|
|
|
|
glfwDestroyWindow(window);
|
|
glfwTerminate();
|
|
|
|
return 0;
|
|
}
|