2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
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
9 #include "SDL_opengles2.h"
13 #include "gropenglinternal.h"
17 GLuint basicTexture = 0;
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"
29 " gl_Position = vOrtho * vPosition;\n"
30 " colorVar = vColor;\n"
31 " texCoordVar = vTexCoord;\n"
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"
41 " gl_FragColor = colorVar * texture2D(texture, texCoordVar);\n"
45 static GLuint opengl2_create_shader(const char *src, GLenum type)
50 shader = glCreateShader(type);
56 glShaderSource(shader, 1, &src, NULL);
58 glCompileShader(shader);
60 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
65 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
68 char *log = (char *) malloc(sizeof(char) * len);
70 glGetShaderInfoLog(shader, len, NULL, log);
71 nprintf(("OpenGL", "Error compiling shader:\n%s\n", log));
76 glDeleteShader(shader);
84 static GLuint opengl2_create_program(GLuint vert, GLuint frag)
89 program = glCreateProgram();
95 glAttachShader(program, vert);
96 glAttachShader(program, frag);
98 glLinkProgram(program);
100 glGetProgramiv(program, GL_LINK_STATUS, &linked);
105 glGetProgramiv(basicTexture, GL_INFO_LOG_LENGTH, &len);
108 char *log = (char *) malloc(sizeof(char) * len);
110 glGetProgramInfoLog(basicTexture, len, NULL, log);
111 nprintf(("OpenGL", "Error linking program:\n%s\n", log));
116 glDeleteProgram(basicTexture);
124 int opengl2_shader_init()
128 GLuint vert_shader = opengl2_create_shader(vert_src, GL_VERTEX_SHADER);
129 GLuint frag_shader = opengl2_create_shader(frag_src, GL_FRAGMENT_SHADER);
131 basicTexture = glCreateProgram();
133 if ( !basicTexture ) {
137 glAttachShader(basicTexture, vert_shader);
138 glAttachShader(basicTexture, frag_shader);
140 glBindAttribLocation(basicTexture, 1, "vPosition");
141 glBindAttribLocation(basicTexture, 2, "vColor");
142 glBindAttribLocation(basicTexture, 3, "vTexCoord");
144 glLinkProgram(basicTexture);
146 glGetProgramiv(basicTexture, GL_LINK_STATUS, &linked);
151 glGetProgramiv(basicTexture, GL_INFO_LOG_LENGTH, &len);
154 char *log = (char *) malloc(sizeof(char) * len);
156 glGetProgramInfoLog(basicTexture, len, NULL, log);
157 nprintf(("OpenGL", "Error linking program:\n%s\n", log));
162 glDeleteProgram(basicTexture);
167 glUseProgram(basicTexture);
172 void opengl2_shader_cleanup()
175 glDeleteProgram(basicTexture);