Commit | Line | Data |
---|---|---|
d42e7319 JB |
1 | /* |
2 | * Copyright (c) 2012 Carsten Munk <carsten.munk@gmail.com> | |
3 | * | |
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 | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
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. | |
15 | * | |
16 | */ | |
17 | ||
18 | #include <EGL/egl.h> | |
19 | #include <GLES2/gl2.h> | |
20 | #include <assert.h> | |
21 | #include <stdio.h> | |
22 | #include <math.h> | |
23 | #include <stddef.h> | |
24 | ||
25 | const char vertex_src [] = | |
26 | " \ | |
27 | attribute vec4 position; \ | |
28 | varying mediump vec2 pos; \ | |
29 | uniform vec4 offset; \ | |
30 | \ | |
31 | void main() \ | |
32 | { \ | |
33 | gl_Position = position + offset; \ | |
34 | pos = position.xy; \ | |
35 | } \ | |
36 | "; | |
37 | ||
38 | ||
39 | const char fragment_src [] = | |
40 | " \ | |
41 | varying mediump vec2 pos; \ | |
42 | uniform mediump float phase; \ | |
43 | \ | |
44 | void main() \ | |
45 | { \ | |
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 ); \ | |
49 | } \ | |
50 | "; | |
51 | ||
52 | GLuint load_shader(const char *shader_source, GLenum type) | |
53 | { | |
54 | GLuint shader = glCreateShader(type); | |
55 | ||
56 | glShaderSource(shader, 1, &shader_source, NULL); | |
57 | glCompileShader(shader); | |
58 | ||
59 | return shader; | |
60 | } | |
61 | ||
62 | ||
63 | GLfloat norm_x = 0.0; | |
64 | GLfloat norm_y = 0.0; | |
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; | |
69 | ||
70 | GLint phase_loc; | |
71 | GLint offset_loc; | |
72 | GLint position_loc; | |
73 | ||
74 | const float vertexArray[] = { | |
75 | 0.0, 1.0, 0.0, | |
76 | -1., 0.0, 0.0, | |
77 | 0.0, -1.0, 0.0, | |
78 | 1., 0.0, 0.0, | |
79 | 0.0, 1., 0.0 | |
80 | }; | |
81 | ||
82 | ||
83 | int main(int argc, char **argv) | |
84 | { | |
85 | EGLDisplay display; | |
86 | EGLConfig ecfg; | |
87 | EGLint num_config; | |
88 | EGLint attr[] = { // some attributes to set up our egl-interface | |
89 | EGL_BUFFER_SIZE, 32, | |
90 | EGL_RENDERABLE_TYPE, | |
91 | EGL_OPENGL_ES2_BIT, | |
92 | EGL_NONE | |
93 | }; | |
94 | EGLSurface surface; | |
95 | EGLint ctxattr[] = { | |
96 | EGL_CONTEXT_CLIENT_VERSION, 2, | |
97 | EGL_NONE | |
98 | }; | |
99 | EGLContext context; | |
100 | ||
101 | EGLBoolean rv; | |
102 | ||
103 | display = eglGetDisplay(NULL); | |
104 | assert(eglGetError() == EGL_SUCCESS); | |
105 | assert(display != EGL_NO_DISPLAY); | |
106 | ||
107 | rv = eglInitialize(display, 0, 0); | |
108 | assert(eglGetError() == EGL_SUCCESS); | |
109 | assert(rv == EGL_TRUE); | |
110 | ||
111 | eglChooseConfig((EGLDisplay) display, attr, &ecfg, 1, &num_config); | |
112 | assert(eglGetError() == EGL_SUCCESS); | |
113 | assert(rv == EGL_TRUE); | |
114 | ||
115 | surface = eglCreateWindowSurface((EGLDisplay) display, ecfg, (EGLNativeWindowType)NULL, NULL); | |
116 | assert(eglGetError() == EGL_SUCCESS); | |
117 | assert(surface != EGL_NO_SURFACE); | |
118 | ||
119 | context = eglCreateContext((EGLDisplay) display, ecfg, EGL_NO_CONTEXT, ctxattr); | |
120 | assert(eglGetError() == EGL_SUCCESS); | |
121 | assert(context != EGL_NO_CONTEXT); | |
122 | ||
123 | assert(eglMakeCurrent((EGLDisplay) display, surface, surface, context) == EGL_TRUE); | |
124 | ||
125 | const char *version = (const char *)glGetString(GL_VERSION); | |
126 | assert(version); | |
127 | printf("%s\n",version); | |
128 | ||
129 | ||
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 | |
132 | ||
133 | GLuint shaderProgram = glCreateProgram (); // create program object | |
134 | glAttachShader ( shaderProgram, vertexShader ); // and attach both... | |
135 | glAttachShader ( shaderProgram, fragmentShader ); // ... shaders to it | |
136 | ||
137 | glLinkProgram ( shaderProgram ); // link the program | |
138 | glUseProgram ( shaderProgram ); // and select it for usage | |
139 | ||
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 ) { | |
145 | return 1; | |
146 | } | |
147 | ||
148 | //glViewport ( 0 , 0 , 800, 600); // commented out so it uses the initial window dimensions | |
149 | glClearColor ( 1. , 1. , 1. , 1.); // background color | |
150 | float phase = 0; | |
151 | int i; | |
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 | |
156 | ||
157 | glUniform4f ( offset_loc , offset_x , offset_y , 0.0 , 0.0 ); | |
158 | ||
159 | glVertexAttribPointer ( position_loc, 3, GL_FLOAT, GL_FALSE, 0, vertexArray ); | |
160 | glEnableVertexAttribArray ( position_loc ); | |
161 | glDrawArrays ( GL_TRIANGLE_STRIP, 0, 5 ); | |
162 | ||
163 | eglSwapBuffers ( (EGLDisplay) display, surface ); // get the rendered buffer to the screen | |
164 | } | |
165 | ||
166 | printf("stop\n"); | |
167 | ||
168 | #if 0 | |
169 | (*egldestroycontext)((EGLDisplay) display, context); | |
170 | printf("destroyed context\n"); | |
171 | ||
172 | (*egldestroysurface)((EGLDisplay) display, surface); | |
173 | printf("destroyed surface\n"); | |
174 | (*eglterminate)((EGLDisplay) display); | |
175 | printf("terminated\n"); | |
176 | android_dlclose(baz); | |
177 | #endif | |
178 | } | |
179 | ||
180 | // vim:ts=4:sw=4:noexpandtab |