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.
24 /*----------------------------------------------------------------------------*/
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
)
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
) { }
46 /*----------------------------------------------------------------------------*/
48 static int check_shader_log(GLuint shader
)
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
);
61 if ((p
= (char *) calloc(n
+ 1, 1)))
63 glGetShaderInfoLog_(shader
, n
, NULL
, p
);
65 log_printf("OpenGL Shader Error:\n%s", p
);
73 static int check_program_log(GLuint program
)
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
);
86 if ((p
= (char *) calloc(n
+ 1, 1)))
88 glGetProgramInfoLog_(program
, n
, NULL
, p
);
90 log_printf("OpenGL Program Error:\n%s", p
);
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
))
114 glDeleteShader_(shader
);
119 static GLuint
glsl_init_program(GLuint shader_vert
,
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
))
136 glDeleteProgram_(program
);
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
);
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
);