]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/grgl2shader.cpp
first pass at OpenGL ES 2 support
[taylor/freespace2.git] / src / graphics / grgl2shader.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 #include "SDL_opengles2.h"
10
11 #include "pstypes.h"
12 #include "gropengl.h"
13 #include "gropenglinternal.h"
14 #include "grgl2.h"
15
16
17 GLuint basicTexture = 0;
18
19
20 static const char vert_src[] =
21         "uniform mat4 vOrtho;\n"
22         "attribute vec4 vPosition;\n"
23         "attribute vec4 vColor;\n"
24         "attribute vec2 vTexCoord;\n"
25         "varying vec4 colorVar;\n"
26         "varying vec2 texCoordVar;\n"
27         "void main()\n"
28         "{\n"
29         "       gl_Position = vOrtho * vPosition;\n"
30         "       colorVar = vColor;\n"
31         "       texCoordVar = vTexCoord;\n"
32         "}\n";
33
34 static const char frag_src[] =
35         "precision mediump float;\n"
36         "uniform sampler2D texture;\n"
37         "varying vec4 colorVar;\n"
38         "varying vec2 texCoordVar;\n"
39         "void main()\n"
40         "{\n"
41         "       gl_FragColor = colorVar * texture2D(texture, texCoordVar);\n"
42         "}\n";
43
44
45 static GLuint opengl2_create_shader(const char *src, GLenum type)
46 {
47         GLuint shader;
48         GLint compiled;
49
50         shader = glCreateShader(type);
51
52         if ( !shader ) {
53                 return 0;
54         }
55
56         glShaderSource(shader, 1, &src, NULL);
57
58         glCompileShader(shader);
59
60         glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
61
62         if ( !compiled ) {
63                 GLint len = 0;
64
65                 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
66
67                 if (len > 1) {
68                         char *log = (char *) malloc(sizeof(char) * len);
69
70                         glGetShaderInfoLog(shader, len, NULL, log);
71                         nprintf(("OpenGL", "Error compiling shader:\n%s\n", log));
72
73                         free(log);
74                 }
75
76                 glDeleteShader(shader);
77
78                 return 0;
79         }
80
81         return shader;
82 }
83 /*
84 static GLuint opengl2_create_program(GLuint vert, GLuint frag)
85 {
86         GLuint program;
87         GLint linked;
88
89         program = glCreateProgram();
90
91         if ( !program ) {
92                 return 0;
93         }
94
95         glAttachShader(program, vert);
96         glAttachShader(program, frag);
97
98         glLinkProgram(program);
99
100         glGetProgramiv(program, GL_LINK_STATUS, &linked);
101
102         if ( !linked ) {
103                 GLint len = 0;
104
105                 glGetProgramiv(basicTexture, GL_INFO_LOG_LENGTH, &len);
106
107                 if (len > 1) {
108                         char *log = (char *) malloc(sizeof(char) * len);
109
110                         glGetProgramInfoLog(basicTexture, len, NULL, log);
111                         nprintf(("OpenGL", "Error linking program:\n%s\n", log));
112
113                         free(log);
114                 }
115
116                 glDeleteProgram(basicTexture);
117
118                 return 0;
119         }
120
121         return program;
122 }
123 */
124 int opengl2_shader_init()
125 {
126         GLint linked;
127
128         GLuint vert_shader = opengl2_create_shader(vert_src, GL_VERTEX_SHADER);
129         GLuint frag_shader = opengl2_create_shader(frag_src, GL_FRAGMENT_SHADER);
130
131         basicTexture = glCreateProgram();
132
133         if ( !basicTexture ) {
134                 return 0;
135         }
136
137         glAttachShader(basicTexture, vert_shader);
138         glAttachShader(basicTexture, frag_shader);
139
140         glBindAttribLocation(basicTexture, 1, "vPosition");
141         glBindAttribLocation(basicTexture, 2, "vColor");
142         glBindAttribLocation(basicTexture, 3, "vTexCoord");
143
144         glLinkProgram(basicTexture);
145
146         glGetProgramiv(basicTexture, GL_LINK_STATUS, &linked);
147
148         if ( !linked ) {
149                 GLint len = 0;
150
151                 glGetProgramiv(basicTexture, GL_INFO_LOG_LENGTH, &len);
152
153                 if (len > 1) {
154                         char *log = (char *) malloc(sizeof(char) * len);
155
156                         glGetProgramInfoLog(basicTexture, len, NULL, log);
157                         nprintf(("OpenGL", "Error linking program:\n%s\n", log));
158
159                         free(log);
160                 }
161
162                 glDeleteProgram(basicTexture);
163
164                 return 0;
165         }
166
167         glUseProgram(basicTexture);
168
169         return 1;
170 }
171
172 void opengl2_shader_cleanup()
173 {
174         if (basicTexture) {
175                 glDeleteProgram(basicTexture);
176                 basicTexture = 0;
177         }
178 }