summaryrefslogtreecommitdiffstats
path: root/fiz/naloga/gl/helmholtz.c
diff options
context:
space:
mode:
Diffstat (limited to 'fiz/naloga/gl/helmholtz.c')
-rw-r--r--fiz/naloga/gl/helmholtz.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/fiz/naloga/gl/helmholtz.c b/fiz/naloga/gl/helmholtz.c
new file mode 100644
index 0000000..7d8171b
--- /dev/null
+++ b/fiz/naloga/gl/helmholtz.c
@@ -0,0 +1,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;
+}