summaryrefslogtreecommitdiffstats
path: root/fiz/naloga/gl/helmholtz.c
blob: 7d8171b99cbe03dda97a807ebdf2167cf6b73499 (plain) (blame)
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
#include <stdlib.h>
#include <stdio.h>
#include <complex.h>
#include <unistd.h>
#include <GL/glxew.h>
#include <GLFW/glfw3.h>
extern char _binary_vertex_glsl_start[];
extern char _binary_vertex_glsl_end[];
extern char _binary_fragment_glsl_start[];
extern char _binary_fragment_glsl_end[];
void error_callback (int error, const char * description) {
	fprintf(stderr, "error_callback: %s, error == %d\n", description, error);
}
void key_callback (GLFWwindow * okno, int tipka, int koda, int dejanje, int modifikatorji) {
	fprintf(stderr, "tipka %d, koda %d, dejanje %d, modifikatorji %d, okno %p\n",
			tipka, koda, dejanje, modifikatorji, (void *) okno);
}
int main (void) {
	GLint vertex_len = _binary_vertex_glsl_end - _binary_vertex_glsl_start;
	GLint fragment_len = _binary_fragment_glsl_end - _binary_fragment_glsl_start;
	if (!glfwInit()) {
		fprintf(stderr, "glfwInit failed\n");
		return 1;
	}
	glfwSetErrorCallback(error_callback);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
	// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // forward compat is ogl 3.0+
	GLFWwindow * okno = glfwCreateWindow(800, 600, "okno", NULL, NULL);
	if (!okno) {
		fprintf(stderr, "glfwCreateWindow failed\n");
		glfwTerminate();
		return 2;
	}
	glfwMakeContextCurrent(okno);
	GLenum err = glewInit();
	if (err != GLEW_OK) {
		fprintf(stderr, "glewInit failed: %s\n", glewGetErrorString(err));
		return 3;
	}
	glfwSwapInterval(1); // vsync
	glfwSetKeyCallback(okno, key_callback);
	GLuint program = glCreateProgram();
#define DO_SHADER(whi, WHI) \
	GLuint whi##_shader = glCreateShader(GL_##WHI##_SHADER); \
	glShaderSource(whi##_shader, 1, (const GLchar **) &_binary_##whi##_glsl_start, &whi##_len); \
	glCompileShader(whi##_shader); \
	GLint whi##_success = 0; \
	glGetShaderiv(whi##_shader, GL_COMPILE_STATUS, &whi##_success); \
	if (whi##_success == GL_FALSE) { \
		GLint logSize = 0; \
		glGetShaderiv(whi##_shader, GL_INFO_LOG_LENGTH, &logSize); \
		char infoLog[logSize]; \
		glGetShaderInfoLog(whi##_shader, logSize, &logSize, infoLog); \
		fprintf(stderr, "prevod shaderja ni uspel. dnevnik:\n%s\n", infoLog); \
		glDeleteShader(whi##_shader); \
		return 4; \
	} \
	glAttachShader(program, whi##_shader);
	DO_SHADER(vertex, VERTEX);
	DO_SHADER(fragment, FRAGMENT);
	glLinkProgram(program);
	GLint isLinked = 0;
	glGetProgramiv(program, GL_LINK_STATUS, &isLinked);
	if (isLinked == GL_FALSE) {
		GLint logSize = 0;
		glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logSize);
		char infoLog[logSize];
		glGetProgramInfoLog(program, logSize, &logSize, infoLog);
		fprintf(stderr, "linkanje programa ni uspelo. dnevnik:\n%s\n", infoLog);
		glDeleteShader(vertex_shader);
		glDeleteShader(fragment_shader);
		glDeleteProgram(program);
		return 5;
	}
	glDetachShader(program, vertex_shader); // TODO: ali lahko glDeleteShader po linku?
	glDetachShader(program, fragment_shader);
	while (!glfwWindowShouldClose(okno)) {
		int w, h;
		glfwGetFramebufferSize(okno, &w, &h);
		glViewport(0, 0, w, h);
		
		glfwSwapBuffers(okno);
		glfwWaitEvents(); // glfwPollEvents pa returna takoj
	}
	glfwDestroyWindow(okno);
	glfwTerminate();
	return 0;
}