3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.tizenopensource.org/license
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
20 #include "CameraTools.h"
24 using namespace Tizen::Graphics::Opengl;
35 Renderer::SetTextureData(int width, int height, void* data)
37 glBindTexture (GL_TEXTURE_2D, __textureColorId);
38 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, width >> 1, height >> 1, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, ((unsigned char*) data) + width * height);
39 glBindTexture (GL_TEXTURE_2D, __textureId);
40 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
44 Renderer::SetModelViewMatrix(int i, const Tizen::Graphics::FloatMatrix4* mat)
47 memcpy(__modelView[i], mat->matrix, 16 * sizeof(float));
49 float matrix[16] = {0.f};
50 multiplyMatrix(leftTransform, __modelView[i], matrix);
51 multiplyMatrix(matrix, rightTransform, __modelView[i]);
55 Renderer::SetCameraAngle(float angle)
57 __cameraAngle = angle;
61 Renderer::Init(int width, int height)
64 "attribute vec4 a_Position; \n"
65 "attribute vec2 a_TexCoord; \n"
66 "attribute vec3 a_Colour; \n"
68 "uniform mat4 u_Mvp; \n"
70 "varying vec2 v_TexCoord; \n"
71 "varying vec3 v_Colour; \n"
75 " gl_Position = u_Mvp * a_Position; \n"
76 " v_TexCoord = a_TexCoord; \n"
77 " v_Colour = a_Colour; \n"
81 "precision mediump float; \n"
82 "uniform sampler2D s_Texture; \n"
83 "uniform sampler2D s_TextureColor; \n"
84 "varying vec2 v_TexCoord; \n"
85 "varying vec3 v_Colour; \n"
89 " float y = texture2D(s_Texture, v_TexCoord).r + v_Colour.r; \n"
90 " float v = texture2D(s_TextureColor, vec2(v_TexCoord.x, v_TexCoord.y) * 0.5).r + v_Colour.b - 0.5;\n"
91 " float u = texture2D(s_TextureColor, vec2(v_TexCoord.x, v_TexCoord.y + 1.0) * 0.5).a + v_Colour.g - 0.5;\n"
92 " float r = y + 1.772 * u; \n"
93 " float g = y - 0.34414 * u - 0.71414 * v; \n"
94 " float b = y + 1.402 * v; \n"
95 " gl_FragColor = vec4(r, g, b, 1.0); \n"
98 // Load the shaders and get a linked program object
99 __programObject = loadProgram ((char *)vShaderStr, (char *)fShaderStr);
101 // Get the attribute locations
102 __positionLoc = glGetAttribLocation (__programObject, "a_Position");
103 __texCoordLoc = glGetAttribLocation (__programObject, "a_TexCoord");
104 __colorLoc = glGetAttribLocation (__programObject, "a_Colour");
106 // Get the sampler location
107 __samplerLoc = glGetUniformLocation (__programObject, "s_Texture");
108 __samplerColorLoc = glGetUniformLocation (__programObject, "s_TextureColor");
109 __mvpPLoc = glGetUniformLocation (__programObject, "u_Mvp");
112 __textureId = createSimpleTexture2D (GL_LUMINANCE, width, height);
113 __textureColorId = createSimpleTexture2D (GL_LUMINANCE_ALPHA, width >> 1, height >> 1);
115 __mvp2[0] = 1; __mvp2[1] = 0; __mvp2[2] = 0; __mvp2[3] = 0;
116 __mvp2[4] = 0; __mvp2[5] = 1; __mvp2[6] = 0; __mvp2[7] = 0;
117 __mvp2[8] = 0; __mvp2[9] = 0; __mvp2[10] = 1; __mvp2[11] = 0;
118 __mvp2[12] = 0; __mvp2[13] = 0; __mvp2[14] = 0; __mvp2[15] = 1;
120 for(int i = 0; i < CUBE_COUNT; ++i)
122 memset(__modelView[i], 0, 16 * sizeof(float));
125 float u[] = {0.f, 0.25f, 0.f};
126 float v[] = {0.f, 0.f, 0.25f};
127 for(int j = 0; j < 3; ++j)
129 for(int i = 0; i < 36; ++i)
131 colorList[j][i * 3] = aColours[i * 3];
132 colorList[j][i * 3 + 1] = aColours[i * 3 + 1] + u[j];
133 colorList[j][i * 3 + 2] = aColours[i * 3 + 2] + v[j];
143 float glScale = (float)GLtools::getHeight() / GLtools::getWidth();
145 float glScale = (float)GLtools::getWidth() / GLtools::getHeight();
147 float cScale = (float)CameraTools::GetWidth() / CameraTools::GetHeight();
149 float scaleWidth = 1.0f;
150 float scaleHeight = 1.0f;
155 if (cScale < glScale)
157 scaleHeight = cScale / glScale;
161 scaleWidth = cScale / glScale;
165 float widthShift = (1.f - scaleWidth) / 2.f;
166 float heightShift = (1.f - scaleHeight) / 2.f ;
169 GLfloat vVertices[] = {
171 1.0f , 1.0f , 0.0f, // Position 3
173 -1.0f , 1.0f , 0.0f, // Position 0
175 0.0f , heightShift , // TexCoord 0
176 0.f , 0.0f , 0.0f, // Color
179 -1.0f , 1.0f , 0.0f, // Position 0
181 -1.0f , -1.0f , 0.0f, // Position 1
183 0.0f , scaleHeight + heightShift, // TexCoord 1
184 0.0f , 0.f , 0.0f, // Color
187 -1.0f , -1.0f , 0.0f, // Position 1
189 1.0f , -1.0f, 0.0f, // Position 2
191 scaleWidth + widthShift, scaleHeight + heightShift, // TexCoord 2
192 0.0f , 0.0f , 0.0f, // Color
195 1.0f , -1.0f, 0.0f, // Position 2
197 1.0f , 1.0f , 0.0f, // Position 3
199 scaleWidth + widthShift, heightShift , // TexCoord 3
200 0.0f , 0.0f , 0.0f // Color
205 GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
207 perspectiveMatrix (__cameraAngle, (double)GLtools::getWidth()/(double)GLtools::getHeight(), 0.01, 100.0, __perspectiveMatrix);
209 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
211 glUseProgram (__programObject);
213 glVertexAttribPointer (__positionLoc, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), vVertices);
214 glVertexAttribPointer (__texCoordLoc, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), &vVertices[3]);
215 glVertexAttribPointer (__colorLoc, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), &vVertices[5]);
217 glEnableVertexAttribArray (__positionLoc);
218 glEnableVertexAttribArray (__texCoordLoc);
219 glEnableVertexAttribArray (__colorLoc);
221 glUniformMatrix4fv (__mvpPLoc, 1, GL_FALSE, __mvp2); // GL_CHECK
223 glActiveTexture (GL_TEXTURE1);
224 glBindTexture (GL_TEXTURE_2D, __textureColorId);
225 glUniform1i (__samplerColorLoc, 1);
227 glActiveTexture (GL_TEXTURE0);
228 glBindTexture (GL_TEXTURE_2D, __textureId);
229 glUniform1i (__samplerLoc, 0);
231 glDisable (GL_DEPTH_TEST);
232 glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
234 glVertexAttribPointer (__positionLoc, 3, GL_FLOAT, GL_FALSE, 0, aVertices);
235 glVertexAttribPointer (__texCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, aTexCoords);
237 glEnable (GL_CULL_FACE);
238 glEnable (GL_DEPTH_TEST);
240 glActiveTexture (GL_TEXTURE1);
241 glBindTexture (GL_TEXTURE_2D, 0);
242 glActiveTexture (GL_TEXTURE0);
243 glBindTexture (GL_TEXTURE_2D, 0);
246 for(int i = 0; i < CUBE_COUNT; ++i)
248 multiplyMatrix(__perspectiveMatrix, __modelView[i], mvp);
249 glUniformMatrix4fv (__mvpPLoc, 1, GL_FALSE, mvp); // GL_CHECK
250 glVertexAttribPointer (__colorLoc, 3, GL_FLOAT, GL_FALSE, 0, colorList[i % 3]);
251 glDrawArrays (GL_TRIANGLES, 0, 36);
256 Renderer::loadShader(GLenum type, const char *shaderSrc)
261 shader = glCreateShader (type);
268 // Load the shader source
269 glShaderSource (shader, 1, &shaderSrc, NULL);
271 // Compile the shader
272 glCompileShader (shader);
274 // Check the compile status
275 glGetShaderiv (shader, GL_COMPILE_STATUS, &compiled);
281 glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLen);
285 char* infoLog = (char*)malloc (sizeof(char) * infoLen);
287 glGetShaderInfoLog (shader, infoLen, NULL, infoLog);
288 AppSecureLogExceptionTag ("QrCodeRecognizer", "Error compiling shader:\n%s\n", infoLog);
293 glDeleteShader (shader);
302 Renderer::loadProgram(const char *vertShaderSrc, const char *fragShaderSrc)
305 GLuint fragmentShader;
306 GLuint programObject;
309 // Load the vertex/fragment shaders
310 vertexShader = loadShader (GL_VERTEX_SHADER, vertShaderSrc);
311 if (vertexShader == 0)
316 fragmentShader = loadShader (GL_FRAGMENT_SHADER, fragShaderSrc);
317 if (fragmentShader == 0)
319 glDeleteShader(vertexShader);
323 // Create the program object
324 programObject = glCreateProgram ();
326 if (programObject == 0)
331 glAttachShader (programObject, vertexShader);
332 glAttachShader (programObject, fragmentShader);
335 glLinkProgram (programObject);
337 // Check the link status
338 glGetProgramiv (programObject, GL_LINK_STATUS, &linked);
344 glGetProgramiv (programObject, GL_INFO_LOG_LENGTH, &infoLen);
348 char* infoLog = (char*)malloc (sizeof(char) * infoLen);
350 glGetProgramInfoLog (programObject, infoLen, NULL, infoLog);
351 AppSecureLogExceptionTag("ImageRecognizer", "Error linking program: %s", infoLog);
356 glDeleteProgram (programObject);
360 // Free up no longer needed shader resources
361 glDeleteShader (vertexShader);
362 glDeleteShader (fragmentShader);
364 return programObject;
368 Renderer::createSimpleTexture2D(GLenum type, int width, int height)
370 // Create a simple 2x2 texture image with four different colors
371 // Texture object handle
374 GLubyte* pixels = new GLubyte[width * height * 4];
376 // Generate a texture object
377 glGenTextures (1, &textureId);
379 // Bind the texture object
380 glBindTexture (GL_TEXTURE_2D, textureId);
383 glTexImage2D (GL_TEXTURE_2D, 0, type, width, height, 0, type, GL_UNSIGNED_BYTE, pixels);
385 // Set the filtering mode
386 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
387 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
395 Renderer::rotateMatrix(double angle, double x, double y, double z, float *R)
397 double radians, c, s, c1, u[3], length;
400 radians = (angle * M_PI) / 180.0;
405 c1 = 1.0 - cos(radians);
407 length = sqrt(x * x + y * y + z * z);
413 for (i = 0; i < 16; i++)
420 for (i = 0; i < 3; i++)
422 R[i * 4 + (i + 1) % 3] = float(u[(i + 2) % 3] * s);
423 R[i * 4 + (i + 2) % 3] = float(-u[(i + 1) % 3] * s);
426 for (i = 0; i < 3; i++)
428 for (j = 0; j < 3; j++)
430 R[i * 4 + j] += float(c1 * u[i] * u[j] + (i == j ? c : 0.0));
436 Renderer::perspectiveMatrix(double fovy, double aspect, double znear, double zfar, float *P)
441 f = 1.0/tan(fovy * 0.5);
443 for (i = 0; i < 16; i++)
449 P[0] = float(f / aspect);
453 P[5] = float(f * aspect);
456 P[10] = float((znear + zfar) / (znear - zfar));
458 P[14] = float((2.0 * znear * zfar) / (znear - zfar));
463 Renderer::multiplyMatrix(const float *A, const float *B, float *C)
468 for (i = 0; i < 4; i++)
470 for (j = 0; j < 4; j++)
472 aTmp[j * 4 + i] = 0.0f;
474 for (k = 0; k < 4; k++)
476 aTmp[j * 4 + i] += A[k * 4 + i] * B[j * 4 + k];
481 for (i = 0; i < 16; i++)