1 | /* |
2 | Copyright (c) 2013, Broadcom Europe Ltd |
3 | Copyright (c) 2013, Tim Gover |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met: |
8 | * Redistributions of source code must retain the above copyright |
9 | notice, this list of conditions and the following disclaimer. |
10 | * Redistributions in binary form must reproduce the above copyright |
11 | notice, this list of conditions and the following disclaimer in the |
12 | documentation and/or other materials provided with the distribution. |
13 | * Neither the name of the copyright holder nor the |
14 | names of its contributors may be used to endorse or promote products |
15 | derived from this software without specific prior written permission. |
16 | |
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
21 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | #include <GLES/gl.h> |
30 | #include <GLES/glext.h> |
31 | #include <EGL/egl.h> |
32 | #include <EGL/eglext.h> |
33 | #include "RaspiTexUtil.h" |
34 | |
35 | /* Vertex co-ordinates: |
36 | * |
37 | * v0----v1 |
38 | * | | |
39 | * | | |
40 | * | | |
41 | * v3----v2 |
42 | */ |
43 | |
44 | static const GLfloat vertices[] = |
45 | { |
46 | #define V0 -0.8, 0.8, 0.8, |
47 | #define V1 0.8, 0.8, 0.8, |
48 | #define V2 0.8, -0.8, 0.8, |
49 | #define V3 -0.8, -0.8, 0.8, |
50 | V0 V3 V2 V2 V1 V0 |
51 | }; |
52 | |
53 | /* Texture co-ordinates: |
54 | * |
55 | * (0,0) b--c |
56 | * | | |
57 | * a--d |
58 | * |
59 | * b,a,d d,c,b |
60 | */ |
61 | static const GLfloat tex_coords[] = |
62 | { |
63 | 0, 0, 0, 1, 1, 1, |
64 | 1, 1, 1, 0, 0, 0 |
65 | }; |
66 | |
67 | static GLfloat angle; |
68 | static uint32_t anim_step; |
69 | |
70 | static int square_init(RASPITEX_STATE *state) |
71 | { |
72 | int rc = raspitexutil_gl_init_1_0(state); |
73 | |
74 | if (rc != 0) |
75 | goto end; |
76 | |
77 | angle = 0.0f; |
78 | anim_step = 0; |
79 | |
80 | glClearColor(0, 0, 0, 0); |
81 | glClearDepthf(1); |
82 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
83 | glLoadIdentity(); |
84 | |
85 | end: |
86 | return rc; |
87 | } |
88 | |
89 | static int square_update_model(RASPITEX_STATE *state) |
90 | { |
91 | int frames_per_rev = 30 * 15; |
92 | (void) state; |
93 | angle = (anim_step * 360) / (GLfloat) frames_per_rev; |
94 | anim_step = (anim_step + 1) % frames_per_rev; |
95 | |
96 | return 0; |
97 | } |
98 | |
99 | static int square_redraw(RASPITEX_STATE *state) |
100 | { |
101 | /* Bind the OES texture which is used to render the camera preview */ |
102 | GLCHK(glBindTexture(GL_TEXTURE_EXTERNAL_OES, state->texture)); |
103 | glLoadIdentity(); |
104 | glRotatef(angle, 0.0, 0.0, 1.0); |
105 | glEnableClientState(GL_VERTEX_ARRAY); |
106 | glVertexPointer(3, GL_FLOAT, 0, vertices); |
107 | glDisableClientState(GL_COLOR_ARRAY); |
108 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
109 | glTexCoordPointer(2, GL_FLOAT, 0, tex_coords); |
110 | GLCHK(glDrawArrays(GL_TRIANGLES, 0, vcos_countof(tex_coords) / 2)); |
111 | return 0; |
112 | } |
113 | |
114 | int square_open(RASPITEX_STATE *state) |
115 | { |
116 | state->ops.gl_init = square_init; |
117 | state->ops.update_model = square_update_model; |
118 | state->ops.redraw = square_redraw; |
119 | state->ops.update_texture = raspitexutil_update_texture; |
120 | return 0; |
121 | } |
122 | |