blob: 12e7c8f7857f8085a8a148b8c4715099d1e842aa [file] [log] [blame]
Brian Swetland2c7b08a2013-03-06 17:16:31 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
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#include <stdlib.h>
18#include <stdio.h>
19
20#include <EGL/egl.h>
21#include <GLES2/gl2.h>
22
23#include "util.h"
24
25static const char gVertexShader[] =
26 "attribute vec4 aPosition;\n"
27 "uniform mat4 uTransform;\n"
28 "varying vec4 vTexCoord;\n"
29 "void main() {\n"
30 " gl_Position = aPosition * uTransform;\n"
31 " vTexCoord = aPosition * vec4(1.0/16.0,-1.0/16.0,0.0,0.0);\n"
32 "}\n";
33
34static const char gFragmentShader[] =
35 "precision mediump float;\n"
36 "uniform sampler2D uTexture;\n"
37 "uniform float uAnim;\n"
38 "varying vec4 vTexCoord;\n"
39 "void main() {\n"
40 " vec2 tc = vec2(vTexCoord.x, uAnim + vTexCoord.y);\n"
41 " gl_FragColor = texture2D(uTexture, tc);\n"
42 "}\n";
43
44static GLuint pgm;
45static GLint aPosition, uTransform, uTexture, uAnim;
46
47static GLfloat vtx[2 * 3 * 2];
48static GLfloat mtx[16];
49
50//#define R (0xFF0000FF)
51#define R (0xFF000000)
52#define G (0xFF00FF00)
53uint32_t t32[] = {
54 R, R, R, R, R, R, R, G, G, R, R, R, R, R, R, R,
55 R, R, R, R, R, R, G, G, G, G, R, R, R, R, R, R,
56 R, R, R, R, R, G, G, G, G, G, G, R, R, R, R, R,
57 R, R, R, R, G, G, G, G, G, G, G, G, R, R, R, R,
58 R, R, R, G, G, G, G, G, G, G, G, G, G, R, R, R,
59 R, R, G, G, G, G, G, G, G, G, G, G, G, G, R, R,
60 R, R, G, G, G, G, G, G, G, G, G, G, G, G, R, R,
61 R, R, R, R, R, R, G, G, G, G, R, R, R, R, R, R,
62 R, R, R, R, R, R, G, G, G, G, R, R, R, R, R, R,
63 R, R, R, R, R, R, G, G, G, G, R, R, R, R, R, R,
64 R, R, R, R, R, R, G, G, G, G, R, R, R, R, R, R,
65 R, R, R, R, R, R, G, G, G, G, R, R, R, R, R, R,
66 R, R, R, R, R, R, G, G, G, G, R, R, R, R, R, R,
67 R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R,
68 R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R,
69 R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R,
70};
71#undef R
72#undef G
73
74int prepare(int w, int h) {
75 GLuint texid;
76
77 int left = w / 4;
78 int top = h / 4;
79 int right = (w / 4) * 3;
80 int bottom = (h / 4) * 3;
81
82 vtx[0] = left;
83 vtx[1] = top;
84 vtx[2] = left;
85 vtx[3] = bottom;
86 vtx[4] = right;
87 vtx[5] = bottom;
88
89 vtx[6] = right;
90 vtx[7] = bottom;
91 vtx[8] = right;
92 vtx[9] = top;
93 vtx[10] = left;
94 vtx[11] = top;
95
96 matrix_init_ortho(mtx, w, h);
97
98 pgm = load_program(gVertexShader, gFragmentShader);
99 if (!pgm)
100 return -1;
101
102 aPosition = glGetAttribLocation(pgm, "aPosition");
103 uTexture = glGetUniformLocation(pgm, "uTexture");
104 uTransform = glGetUniformLocation(pgm, "uTransform");
105 uAnim = glGetUniformLocation(pgm, "uAnim");
106
107 glViewport(0, 0, w, h);
108
109 glGenTextures(1, &texid);
110 glActiveTexture(GL_TEXTURE0);
111 glBindTexture(GL_TEXTURE_2D, texid);
112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
115 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
116 glEnable(GL_TEXTURE_2D);
117 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0,
118 GL_RGBA, GL_UNSIGNED_BYTE, t32);
119
120 return 0;
121}
122
123static float anim = 0.0;
124
125void render() {
126 anim += 0.1;
127 if (anim >= 16.0) anim = 0.0;
128
129 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
130 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
131 glUseProgram(pgm);
132 glUniform1i(uTexture, 0);
133 glUniform1f(uAnim, anim);
134 glUniformMatrix4fv(uTransform, 1, 0, mtx);
135 glVertexAttribPointer(aPosition, 2, GL_FLOAT, GL_FALSE, 0, vtx);
136 glEnableVertexAttribArray(aPosition);
137 glDrawArrays(GL_TRIANGLES, 0, 6);
138}
139
140int main(int argc, char **argv) {
141 EGLDisplay display;
142 EGLSurface surface;
Glenn Kasten6c2a68d2014-07-15 11:11:01 -0700143 int w, h, count = 0;
Brian Swetland2c7b08a2013-03-06 17:16:31 -0800144
145 if (argc > 1)
146 count = atoi(argv[1]);
147
148 if (egl_create(&display, &surface, &w, &h))
149 return -1;
150
151 if (prepare(w, h))
152 return -1;
153
154 for (;;) {
155 render();
156 eglSwapBuffers(display, surface);
157 if (count > 0)
158 if (--count == 0)
159 break;
160 }
161
162 egl_destroy(display, surface);
163 return 0;
164}