summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 4dfe6b6ad4aa152de680ea4fb56be28ffdd706ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "shader.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>

extern unsigned char src_vertex_glsl[];
extern unsigned char src_fragment_glsl[];

static void framebuffer_size_callback(GLFWwindow *window, int w, int h);

int
main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
{
	if (glfwInit() == GLFW_FALSE) {
		fprintf(stderr, "glfwInit failed\n");
		return 1;
	}
	if (atexit(glfwTerminate)) {
		perror("main:atexit");
		glfwTerminate();
		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, "LearnOpenGL", NULL, NULL);
	if (window == NULL) {
		fprintf(stderr, "glfwCreateWindow failed\n");
		return 1;
	}
	glfwMakeContextCurrent(window);

	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
		fprintf(stderr, "gladLoadGLLoader failed\n");
		return 1;
	}

	glViewport(0, 0, 800, 600);
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

	static const float verts[] = {
		 0.5f,  0.5f, 0.0f,  1.0f, 0.0f, 1.0f, // top right
		 0.5f, -0.5f, 0.0f,  0.0f, 1.0f, 0.0f, // bottom right
		-0.5f, -0.5f, 0.0f,  0.0f, 0.0f, 1.0f, // bottom left
		-0.5f,  0.5f, 0.0f,  0.0f, 1.0f, 0.0f  // top left
	};
	static const GLuint indices[] = {
		0, 1, 3, // first triangle
		1, 2, 3  // second triangle
	};
	GLuint VBO;
	glGenBuffers(1, &VBO);

	GLuint EBO;
	glGenBuffers(1, &EBO);

	GLuint VAO;
	glGenVertexArrays(1, &VAO);
	glBindVertexArray(VAO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices) / 2, indices, GL_STATIC_DRAW);
	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);

	Shader shader;
	if (shader_init(&shader,
	                (const char *)src_vertex_glsl,
	                (const char *)src_fragment_glsl))
	{
		fprintf(stderr, "shader_init failed\n");
		return 1;
	}

	const GLint vertex_color_location =
	    shader_uniform_location(&shader, "ourColor");

	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	while (!glfwWindowShouldClose(window)) {
		shader_use(&shader);
		const int r = 0;//rand();
		glUniform4f(vertex_color_location,
		            !(r&1), !(r&2), !(r&4), !(r&8));
		glBindVertexArray(VAO);
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	return 0;
}

static void
framebuffer_size_callback([[maybe_unused]] GLFWwindow *window, int w, int h)
{
	glViewport(0, 0, w, h);
}