aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 4b8aa32a791001e9d0c0bf9c31086f05a1019dc3 (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
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include "fill_vert.h"
#include "visuals_frag.h"

int main(int argc, char** argv) {
	// initialize window
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
	GLFWwindow* window = glfwCreateWindow(800, 600, "test", NULL, NULL);
	glfwMakeContextCurrent(window);
	glewInit();

	// initialize triangle
	const float vertices[] = {
		0, 1, 0,
		1, -1, 0,
		-1, -1, 0,
	};
	GLuint VBO, VAO;
	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glGenVertexArrays(1, &VAO);
	glBindVertexArray(VAO);
	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(VAO);

	// initialize shaders
	GLuint vert = glCreateShader(GL_VERTEX_SHADER);
	glShaderBinary(1, &vert, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, fill_vert, fill_vert_size);
	glSpecializeShaderARB(vert, "main", 0, NULL, NULL);
	GLuint frag = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderBinary(1, &frag, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, visuals_frag, visuals_frag_size);
	glSpecializeShaderARB(frag, "main", 0, NULL, NULL);
	GLuint shader = glCreateProgram();
	glAttachShader(shader, vert);
	glAttachShader(shader, frag);
	glLinkProgram(shader);
	glUseProgram(shader); // <- used the program before getting uniform name
	glDeleteShader(vert);
	glDeleteShader(frag);

	// print driver info
	printf("vendor:        %s\n", glGetString(GL_VENDOR));
	printf("renderer:      %s\n", glGetString(GL_RENDERER));
	printf("version:       %s\n", glGetString(GL_VERSION));
	printf("glsl version:  %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
	printf("------------------------------\n");

	// list all ACTIVE uniforms in shader:
	GLint count;
	GLchar name[80];
	GLsizei length;
	glGetProgramiv(shader, GL_ACTIVE_UNIFORMS, &count);
	for (unsigned i = 0; i < count; i++) {
		glGetActiveUniformName(shader, i, 80, &length, name);
		printf("[%u] = \"%.*s\"\n", i, length, name);
	}

	// directly get uniform location
	GLint uniform_location = glGetUniformLocation(shader, "test");
	printf("`test` uniform location = %d\n", uniform_location);

	return 0;
}