summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 6cf2d5cfa4c303f7fe04e3e8ad4fe4db64559c01 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "shader.h"
#include "texture.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <cglm/cglm.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);

static float
radians(float x)
{
	return x * 0.01745329f;
}

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,  1.0f, 1.0f, // top right
		 0.5f, -0.5f, 0.0f,  0.0f, 1.0f, 0.0f,  1.0f, 0.0f, // bottom right
		-0.5f, -0.5f, 0.0f,  0.0f, 0.0f, 1.0f,  0.0f, 0.0f, // bottom left
		-0.5f,  0.5f, 0.0f,  0.0f, 1.0f, 0.0f,  0.0f, 1.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), indices, GL_STATIC_DRAW);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
	glEnableVertexAttribArray(2);

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

	Texture texture;
	if (texture_init(&texture, "res/wall.jpg")) {
		fprintf(stderr, "texture_init failed\n");
		return 1;
	}

	Texture texture1;
	if (texture_init(&texture1, "res/awesomeface.jpg")) {
		fprintf(stderr, "texture_init failed\n");
		return 1;
	}

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

	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, GL_LINEAR);

	while (!glfwWindowShouldClose(window)) {
		mat4 trans = GLM_MAT4_IDENTITY_INIT;
		glm_translate(trans, (vec3){0.0f, 0.5f, 0.2f});
		glm_rotate(trans, glfwGetTime(), (vec3){0.0f, 0.0f, 1.0f});
		glm_scale(trans, (vec3){0.5f, 0.5f, 0.5f});

		glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		shader_use(&shader);
		const int r = 0;//rand();
		glUniform4f(vertex_color_location,
		            !(r&1), !(r&2), !(r&4), !(r&8));
		glUniform1i(shader_uniform_location(&shader, "texture0"), 0);
		glUniform1i(shader_uniform_location(&shader, "texture1"), 1);
		glUniformMatrix4fv(shader_uniform_location(&shader,
		                                           "transform"),
		                   1, GL_FALSE, trans[0]);
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, texture.id);
		glActiveTexture(GL_TEXTURE1);
		glBindTexture(GL_TEXTURE_2D, texture1.id);

		glBindVertexArray(VAO);
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	texture_deinit(&texture1);
	texture_deinit(&texture);
	shader_deinit(&shader);
	return 0;
}

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