Add missing credit for Esperanto translation
[neverball.git] / share / glsl.c
blobf64093c0c9df7dda23d75ada22bfe41c4562ffae
1 /*
2 * Copyright (C) 2013 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
15 #include <stdlib.h>
16 #include <stdarg.h>
17 #include <string.h>
18 #include <stdio.h>
20 #include "glext.h"
21 #include "glsl.h"
22 #include "log.h"
24 /*----------------------------------------------------------------------------*/
26 #if ENABLE_OPENGLES
28 /* OpenGL ES support in Neverball is targeted toward OpenGL ES version 1.1. */
29 /* This version of ES has no support for programmable shading. */
31 GLboolean glsl_create(glsl *G, int vertc, const char *const *vertv,
32 int fragc, const char *const *fragv)
34 return GL_FALSE;
37 void glsl_delete(glsl *G) { }
39 void glsl_uniform1f(glsl *G, const char *name, GLfloat a) { }
40 void glsl_uniform2f(glsl *G, const char *name, GLfloat a, GLfloat b) { }
41 void glsl_uniform3f(glsl *G, const char *name, GLfloat a, GLfloat b, GLfloat c) { }
42 void glsl_uniform4f(glsl *G, const char *name, GLfloat a, GLfloat b, GLfloat c, GLfloat d) { }
44 #else
46 /*----------------------------------------------------------------------------*/
48 static int check_shader_log(GLuint shader)
50 char *p = 0;
51 GLint s = 0;
52 GLint n = 0;
54 /* Check the shader compile status. If failed, print the log. */
56 glGetShaderiv_(shader, GL_COMPILE_STATUS, &s);
57 glGetShaderiv_(shader, GL_INFO_LOG_LENGTH, &n);
59 if (s == 0)
61 if ((p = (char *) calloc(n + 1, 1)))
63 glGetShaderInfoLog_(shader, n, NULL, p);
65 log_printf("OpenGL Shader Error:\n%s", p);
66 free(p);
68 return 0;
70 return 1;
73 static int check_program_log(GLuint program)
75 char *p = 0;
76 GLint s = 0;
77 GLint n = 0;
79 /* Check the program link status. If failed, print the log. */
81 glGetProgramiv_(program, GL_LINK_STATUS, &s);
82 glGetProgramiv_(program, GL_INFO_LOG_LENGTH, &n);
84 if (s == 0)
86 if ((p = (char *) calloc(n + 1, 1)))
88 glGetProgramInfoLog_(program, n, NULL, p);
90 log_printf("OpenGL Program Error:\n%s", p);
91 free(p);
93 return 0;
95 return 1;
98 /*----------------------------------------------------------------------------*/
100 static GLuint glsl_init_shader(GLenum type, int strc, const char *const *strv)
102 /* Compile a new shader with the given source. */
104 GLuint shader = glCreateShader_(type);
106 glShaderSource_ (shader, strc, strv, NULL);
107 glCompileShader_(shader);
109 /* If the shader is valid, return it. Else, delete it. */
111 if (check_shader_log(shader))
112 return shader;
113 else
114 glDeleteShader_(shader);
116 return 0;
119 static GLuint glsl_init_program(GLuint shader_vert,
120 GLuint shader_frag)
122 /* Link a new program object. */
124 GLuint program = glCreateProgram_();
126 glAttachShader_(program, shader_vert);
127 glAttachShader_(program, shader_frag);
129 glLinkProgram_(program);
131 /* If the program is valid, return it. Else, delete it. */
133 if (check_program_log(program))
134 return program;
135 else
136 glDeleteProgram_(program);
138 return 0;
141 /*----------------------------------------------------------------------------*/
143 GLboolean glsl_create(glsl *G, int vertc, const char *const *vertv,
144 int fragc, const char *const *fragv)
146 if (gli.shader_objects == 0) return GL_FALSE;
148 /* Compile the shaders. */
150 G->vert_shader = glsl_init_shader(GL_VERTEX_SHADER, vertc, vertv);
151 G->frag_shader = glsl_init_shader(GL_FRAGMENT_SHADER, fragc, fragv);
153 /* Link the program. */
155 if (G->vert_shader && G->frag_shader)
157 G->program = glsl_init_program(G->vert_shader, G->frag_shader);
158 return GL_TRUE;
160 return GL_FALSE;
163 void glsl_delete(glsl *G)
165 if (gli.shader_objects == 0) return;
167 /* Delete the program and shaders. */
169 if (G->program) glDeleteProgram_(G->program);
170 if (G->frag_shader) glDeleteShader_ (G->frag_shader);
171 if (G->vert_shader) glDeleteShader_ (G->vert_shader);
173 memset(G, 0, sizeof (glsl));
176 void glsl_uniform1f(glsl *G, const char *name, GLfloat a)
178 if (gli.shader_objects)
179 glUniform1f_(glGetUniformLocation_(G->program, name), a);
182 void glsl_uniform2f(glsl *G, const char *name, GLfloat a, GLfloat b)
184 if (gli.shader_objects)
185 glUniform2f_(glGetUniformLocation_(G->program, name), a, b);
188 void glsl_uniform3f(glsl *G, const char *name, GLfloat a, GLfloat b, GLfloat c)
190 if (gli.shader_objects)
191 glUniform3f_(glGetUniformLocation_(G->program, name), a, b, c);
194 void glsl_uniform4f(glsl *G, const char *name, GLfloat a, GLfloat b, GLfloat c, GLfloat d)
196 if (gli.shader_objects)
197 glUniform4f_(glGetUniformLocation_(G->program, name), a, b, c, d);
200 #endif