2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
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.
20 #include <EGL/eglext.h>
21 #include <GLES2/gl2.h>
22 #include <GLES2/gl2ext.h>
23 #include <mv_common.h>
24 #include <mv_private.h>
26 #include "mv_util_matrix.h"
27 #include "mv_util_render_2d.h"
28 #include "mv_util_render_3d.h"
30 static int s_texid_dummy = 0;
32 static shader_obj_t s_sobj;
33 static float s_matPrj[16];
34 static GLint s_loc_mtx_mv;
35 static GLint s_loc_mtx_pmv;
36 static GLint s_loc_mtx_nrm;
37 static GLint s_loc_color;
38 static GLint s_loc_alpha;
39 static GLint s_loc_lightpos;
41 static GLfloat s_nrm[] = {
42 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
45 static GLfloat s_uv[] = {
46 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
49 static char s_strVS[] = " \n\
51 attribute vec4 a_Vertex; \n\
52 attribute vec3 a_Normal; \n\
53 attribute vec2 a_TexCoord; \n\
54 uniform mat4 u_PMVMatrix; \n\
55 uniform mat4 u_MVMatrix; \n\
56 uniform mat3 u_ModelViewIT; \n\
57 varying vec3 v_diffuse; \n\
58 varying vec3 v_specular; \n\
59 varying vec2 v_texcoord; \n\
60 const float shiness = 16.0; \n\
61 uniform vec3 u_LightPos; \n\
62 const vec3 LightCol = vec3(1.0, 1.0, 1.0); \n\
64 void DirectionalLight(vec3 normal, vec3 eyePos) \n\
66 vec3 lightDir = normalize(u_LightPos); \n\
67 vec3 halfV = normalize(u_LightPos - eyePos); \n\
68 float dVP = max(dot(normal, lightDir), 0.0); \n\
69 float dHV = max(dot(normal, halfV), 0.0); \n\
73 pf = pow(dHV, shiness); \n\
75 v_diffuse += dVP * LightCol; \n\
76 v_specular+= pf * LightCol * 0.5; \n\
81 gl_Position = u_PMVMatrix * a_Vertex; \n\
82 vec3 normal = normalize(u_ModelViewIT * a_Normal); \n\
83 vec3 eyePos = vec3(u_MVMatrix * a_Vertex); \n\
85 v_diffuse = vec3(0.5); \n\
86 v_specular = vec3(0.0); \n\
87 DirectionalLight(normal, eyePos); \n\
89 v_diffuse = clamp(v_diffuse, 0.0, 1.0); \n\
90 v_texcoord = a_TexCoord; \n\
93 static char s_strFS[] = " \n\
94 precision mediump float; \n\
96 uniform vec3 u_color; \n\
97 uniform float u_alpha; \n\
98 varying vec3 v_diffuse; \n\
99 varying vec3 v_specular; \n\
100 varying vec2 v_texcoord; \n\
101 uniform sampler2D u_sampler; \n\
106 color = vec3(texture2D(u_sampler, v_texcoord)); \n\
107 color *= (u_color * v_diffuse); \n\
108 gl_FragColor = vec4(color, u_alpha); \n\
111 static void compute_invmat3x3(float *matMVI3x3, float *matMV)
115 matrix_copy(matMVI4x4, matMV);
116 matrix_invert(matMVI4x4);
117 matrix_transpose(matMVI4x4);
118 matMVI3x3[0] = matMVI4x4[0];
119 matMVI3x3[1] = matMVI4x4[1];
120 matMVI3x3[2] = matMVI4x4[2];
121 matMVI3x3[3] = matMVI4x4[4];
122 matMVI3x3[4] = matMVI4x4[5];
123 matMVI3x3[5] = matMVI4x4[6];
124 matMVI3x3[6] = matMVI4x4[8];
125 matMVI3x3[7] = matMVI4x4[9];
126 matMVI3x3[8] = matMVI4x4[10];
129 int init_cube(float aspect)
131 generate_shader(&s_sobj, s_strVS, s_strFS);
132 s_loc_mtx_mv = glGetUniformLocation(s_sobj.program, "u_MVMatrix");
133 s_loc_mtx_pmv = glGetUniformLocation(s_sobj.program, "u_PMVMatrix");
134 s_loc_mtx_nrm = glGetUniformLocation(s_sobj.program, "u_ModelViewIT");
135 s_loc_color = glGetUniformLocation(s_sobj.program, "u_color");
136 s_loc_alpha = glGetUniformLocation(s_sobj.program, "u_alpha");
137 s_loc_lightpos = glGetUniformLocation(s_sobj.program, "u_LightPos");
139 matrix_proj_perspective(s_matPrj, 72.0f, aspect, 1.f, 10000.f);
141 unsigned char imgbuf[] = { 255, 255, 255, 255 };
142 s_texid_dummy = create_2d_texture(imgbuf, 1, 1);
147 int draw_line(float *mtxGlobal, float *p0, float *p1, float *color)
149 float matMV[16], matPMV[16], matMVI3x3[9];
150 GLfloat floor_vtx[9];
152 for (int i = 0; i < 3; i++) {
153 floor_vtx[0 + i] = p0[i];
154 floor_vtx[3 + i] = p1[i];
157 glEnable(GL_DEPTH_TEST);
158 glDisable(GL_CULL_FACE);
160 glUseProgram(s_sobj.program);
162 glEnableVertexAttribArray(s_sobj.loc_vtx);
163 glEnableVertexAttribArray(s_sobj.loc_uv);
164 glDisableVertexAttribArray(s_sobj.loc_nrm);
165 glVertexAttribPointer(s_sobj.loc_vtx, 3, GL_FLOAT, GL_FALSE, 0, floor_vtx);
166 glVertexAttribPointer(s_sobj.loc_uv, 2, GL_FLOAT, GL_FALSE, 0, s_uv);
167 glVertexAttrib4fv(s_sobj.loc_nrm, s_nrm);
169 matrix_identity(matMV);
170 compute_invmat3x3(matMVI3x3, matMV);
172 matrix_mult(matMV, mtxGlobal, matMV);
173 matrix_mult(matPMV, s_matPrj, matMV);
175 glUniformMatrix4fv(s_loc_mtx_mv, 1, GL_FALSE, matMV);
176 glUniformMatrix4fv(s_loc_mtx_pmv, 1, GL_FALSE, matPMV);
177 glUniformMatrix3fv(s_loc_mtx_nrm, 1, GL_FALSE, matMVI3x3);
178 glUniform3f(s_loc_lightpos, 1.0f, 1.0f, 1.0f);
179 glUniform3f(s_loc_color, color[0], color[1], color[2]);
180 glUniform1f(s_loc_alpha, color[3]);
184 glBindTexture(GL_TEXTURE_2D, s_texid_dummy);
185 glDrawArrays(GL_LINES, 0, 2);
192 int draw_point_arrays(float *mtxGlobal, float *vtx, float *uv, int num, int texid, float *color)
194 float matMV[16], matPMV[16], matMVI3x3[9];
196 glEnable(GL_DEPTH_TEST);
197 glDisable(GL_CULL_FACE);
199 glUseProgram(s_sobj.program);
201 glEnableVertexAttribArray(s_sobj.loc_vtx);
202 glEnableVertexAttribArray(s_sobj.loc_uv);
203 glDisableVertexAttribArray(s_sobj.loc_nrm);
204 glVertexAttribPointer(s_sobj.loc_vtx, 3, GL_FLOAT, GL_FALSE, 0, vtx);
205 glVertexAttribPointer(s_sobj.loc_uv, 2, GL_FLOAT, GL_FALSE, 0, uv);
206 glVertexAttrib4fv(s_sobj.loc_nrm, s_nrm);
208 matrix_identity(matMV);
209 compute_invmat3x3(matMVI3x3, matMV);
211 matrix_mult(matMV, mtxGlobal, matMV);
212 matrix_mult(matPMV, s_matPrj, matMV);
214 glUniformMatrix4fv(s_loc_mtx_mv, 1, GL_FALSE, matMV);
215 glUniformMatrix4fv(s_loc_mtx_pmv, 1, GL_FALSE, matPMV);
216 glUniformMatrix3fv(s_loc_mtx_nrm, 1, GL_FALSE, matMVI3x3);
217 glUniform3f(s_loc_lightpos, 1.0f, 1.0f, 1.0f);
218 glUniform3f(s_loc_color, color[0], color[1], color[2]);
219 glUniform1f(s_loc_alpha, color[3]);
224 glBindTexture(GL_TEXTURE_2D, texid);
225 glDrawArrays(GL_POINTS, 0, num);
232 int create_mesh(mesh_obj_t *mesh, int num_tile_w, int num_tile_h)
234 int num_vtx_u = num_tile_w + 1;
235 int num_vtx_v = num_tile_h + 1;
236 int num_vtx = num_vtx_u * num_vtx_v;
238 mesh->vtx_array = (float *) malloc(num_vtx * 3 * sizeof(float));
239 mesh->uv_array = (float *) malloc(num_vtx * 2 * sizeof(float));
242 glGenBuffers(3, vbos);
243 mesh->vbo_vtx = vbos[0];
244 mesh->vbo_uv = vbos[1];
245 mesh->vbo_idx = vbos[2];
247 int num_tri = num_tile_w * num_tile_h * 2;
248 int num_idx = num_tri * 3;
249 int idx_buf_size = num_idx * sizeof(unsigned short);
250 unsigned short *idx_array = (unsigned short *) malloc(idx_buf_size);
252 for (int tile_y = 0; tile_y < num_tile_h; tile_y++) {
253 for (int tile_x = 0; tile_x < num_tile_w; tile_x++) {
254 int idx = tile_y * num_tile_w + tile_x;
256 idx_array[6 * idx + 0] = (tile_y) *num_vtx_u + (tile_x);
257 idx_array[6 * idx + 1] = (tile_y + 1) * num_vtx_u + (tile_x);
258 idx_array[6 * idx + 2] = (tile_y) *num_vtx_u + (tile_x + 1);
259 idx_array[6 * idx + 3] = (tile_y) *num_vtx_u + (tile_x + 1);
260 idx_array[6 * idx + 4] = (tile_y + 1) * num_vtx_u + (tile_x);
261 idx_array[6 * idx + 5] = (tile_y + 1) * num_vtx_u + (tile_x + 1);
264 mesh->idx_array = idx_array;
265 mesh->num_tile_w = num_tile_w;
266 mesh->num_tile_h = num_tile_h;
267 mesh->num_idx = num_idx;
269 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->vbo_idx);
270 glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx_buf_size, idx_array, GL_STATIC_DRAW);
272 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);