2 * Copyright (c) 2012 Carsten Munk <carsten.munk@gmail.com>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <GLES2/gl2.h>
25 const char vertex_src
[] =
27 attribute vec4 position; \
28 varying mediump vec2 pos; \
29 uniform vec4 offset; \
33 gl_Position = position + offset; \
39 const char fragment_src
[] =
41 varying mediump vec2 pos; \
42 uniform mediump float phase; \
46 gl_FragColor = vec4( 1., 0.9, 0.7, 1.0 ) * \
47 cos( 30.*sqrt(pos.x*pos.x + 1.5*pos.y*pos.y) \
48 + atan(pos.y,pos.x) - phase ); \
52 GLuint
load_shader(const char *shader_source
, GLenum type
)
54 GLuint shader
= glCreateShader(type
);
56 glShaderSource(shader
, 1, &shader_source
, NULL
);
57 glCompileShader(shader
);
65 GLfloat offset_x
= 0.0;
66 GLfloat offset_y
= 0.0;
67 GLfloat p1_pos_x
= 0.0;
68 GLfloat p1_pos_y
= 0.0;
74 const float vertexArray
[] = {
83 int main(int argc
, char **argv
)
88 EGLint attr
[] = { // some attributes to set up our egl-interface
96 EGL_CONTEXT_CLIENT_VERSION
, 2,
103 display
= eglGetDisplay(NULL
);
104 assert(eglGetError() == EGL_SUCCESS
);
105 assert(display
!= EGL_NO_DISPLAY
);
107 rv
= eglInitialize(display
, 0, 0);
108 assert(eglGetError() == EGL_SUCCESS
);
109 assert(rv
== EGL_TRUE
);
111 eglChooseConfig((EGLDisplay
) display
, attr
, &ecfg
, 1, &num_config
);
112 assert(eglGetError() == EGL_SUCCESS
);
113 assert(rv
== EGL_TRUE
);
115 surface
= eglCreateWindowSurface((EGLDisplay
) display
, ecfg
, (EGLNativeWindowType
)NULL
, NULL
);
116 assert(eglGetError() == EGL_SUCCESS
);
117 assert(surface
!= EGL_NO_SURFACE
);
119 context
= eglCreateContext((EGLDisplay
) display
, ecfg
, EGL_NO_CONTEXT
, ctxattr
);
120 assert(eglGetError() == EGL_SUCCESS
);
121 assert(context
!= EGL_NO_CONTEXT
);
123 assert(eglMakeCurrent((EGLDisplay
) display
, surface
, surface
, context
) == EGL_TRUE
);
125 const char *version
= (const char *)glGetString(GL_VERSION
);
127 printf("%s\n",version
);
130 GLuint vertexShader
= load_shader ( vertex_src
, GL_VERTEX_SHADER
); // load vertex shader
131 GLuint fragmentShader
= load_shader ( fragment_src
, GL_FRAGMENT_SHADER
); // load fragment shader
133 GLuint shaderProgram
= glCreateProgram (); // create program object
134 glAttachShader ( shaderProgram
, vertexShader
); // and attach both...
135 glAttachShader ( shaderProgram
, fragmentShader
); // ... shaders to it
137 glLinkProgram ( shaderProgram
); // link the program
138 glUseProgram ( shaderProgram
); // and select it for usage
140 //// now get the locations (kind of handle) of the shaders variables
141 position_loc
= glGetAttribLocation ( shaderProgram
, "position" );
142 phase_loc
= glGetUniformLocation ( shaderProgram
, "phase" );
143 offset_loc
= glGetUniformLocation ( shaderProgram
, "offset" );
144 if ( position_loc
< 0 || phase_loc
< 0 || offset_loc
< 0 ) {
148 //glViewport ( 0 , 0 , 800, 600); // commented out so it uses the initial window dimensions
149 glClearColor ( 1. , 1. , 1. , 1.); // background color
152 for (i
=0; i
<3*60; ++i
) {
153 glClear(GL_COLOR_BUFFER_BIT
);
154 glUniform1f ( phase_loc
, phase
); // write the value of phase to the shaders phase
155 phase
= fmodf ( phase
+ 0.5f
, 2.f
* 3.141f
); // and update the local variable
157 glUniform4f ( offset_loc
, offset_x
, offset_y
, 0.0 , 0.0 );
159 glVertexAttribPointer ( position_loc
, 3, GL_FLOAT
, GL_FALSE
, 0, vertexArray
);
160 glEnableVertexAttribArray ( position_loc
);
161 glDrawArrays ( GL_TRIANGLE_STRIP
, 0, 5 );
163 eglSwapBuffers ( (EGLDisplay
) display
, surface
); // get the rendered buffer to the screen
169 (*egldestroycontext
)((EGLDisplay
) display
, context
);
170 printf("destroyed context\n");
172 (*egldestroysurface
)((EGLDisplay
) display
, surface
);
173 printf("destroyed surface\n");
174 (*eglterminate
)((EGLDisplay
) display
);
175 printf("terminated\n");
176 android_dlclose(baz
);
180 // vim:ts=4:sw=4:noexpandtab