OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Torus.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2012-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include "Common.h"
22 #include "Matrix.h"
23 #include "Shader.h"
24 #include "Torus.h"
25 
26 #include <cmath>
27 #include <cstdlib>
28 #include <cstdio>
29 
30 using namespace MaliSDK;
31 using std::string;
32 
34 
36 {
37  if (resourceDirectory.empty())
38  {
39  LOGE("Resource Directory has not been set\n");
40  }
41 }
42 
44 {
45  GL_CHECK(glDeleteProgram(programID));
46  GL_CHECK(glDeleteVertexArrays(1, &vaoID));
47 }
48 
49 void Torus::setColor(float red, float green, float blue, float alpha)
50 {
51  GLint colorLocation = GL_CHECK(glGetUniformLocation(programID, "color"));
52 
53  float color[] = {red, green, blue, alpha};
54 
55  GL_CHECK(glUseProgram(programID));
56 
57  if (colorLocation != -1)
58  {
59  GL_CHECK(glUniform4fv(colorLocation, 1, color));
60  }
61  else
62  {
63  LOGE("Could not locate \"color\" uniform in program [%d]", programID);
64  }
65 }
66 
68 {
69  GLint projectionMatrixLocation = GL_CHECK(glGetUniformLocation(programID, "projectionMatrix"));
70 
71  GL_CHECK(glUseProgram(programID));
72  GL_CHECK(glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, projectionMatrix->getAsArray()));
73 }
74 
75 void Torus::setupGraphics(const string vertexShaderPath, const string fragmentShaderPath)
76 {
79 
80  Shader::processShader(&vertexShaderID, vertexShaderPath.c_str(), GL_VERTEX_SHADER);
81  Shader::processShader(&fragmentShaderID, fragmentShaderPath.c_str(), GL_FRAGMENT_SHADER);
82 
83  programID = GL_CHECK(glCreateProgram());
84 
85  GL_CHECK(glAttachShader(programID, vertexShaderID));
86  GL_CHECK(glAttachShader(programID, fragmentShaderID));
87 
88  GL_CHECK(glLinkProgram(programID));
89 
90  float scalingFactor = 0.7f;
91  float cameraTranslation = -2.5f;
92 
93  Matrix cameraMatrix = Matrix::createTranslation(0.0f, 0.0f, cameraTranslation);
94  Matrix scaleMatrix = Matrix::createScaling(scalingFactor, scalingFactor, scalingFactor);
95 
96  GLint scaleMatrixLocation = GL_CHECK(glGetUniformLocation(programID, "scaleMatrix"));
97  GLint cameraMatrixLocation = GL_CHECK(glGetUniformLocation(programID, "cameraMatrix"));
98 
99  GL_CHECK(glUseProgram(programID));
100 
101  GL_CHECK(glUniformMatrix4fv(scaleMatrixLocation, 1, GL_FALSE, scaleMatrix.getAsArray()));
102  GL_CHECK(glUniformMatrix4fv(cameraMatrixLocation, 1, GL_FALSE, cameraMatrix.getAsArray()));
103 }
104 
105 void Torus::setResourceDirectory(string requiredResourceDirectory)
106 {
107  resourceDirectory = requiredResourceDirectory;
108 }
virtual ~Torus(void)
Frees allocated memory.
Definition: Torus.cpp:43
float projectionMatrix[16]
Definition: Native.cpp:156
void setProjectionMatrix(MaliSDK::Matrix *projectionMatrix)
Pass the correctly defined projection matrix to the program related to the torus model.
Definition: Torus.cpp:67
Functions for manipulating matrices.
Definition: Matrix.h:31
GLuint fragmentShaderID
GLuint colorLocation
Definition: Native.cpp:199
static Matrix createTranslation(float x, float y, float z)
Create and return a translation matrix.
Definition: Matrix.cpp:414
static Matrix createScaling(float x, float y, float z)
Create and return a scaling matrix.
Definition: Matrix.cpp:403
float * getAsArray(void)
Get the matrix elements as a column major order array.
Definition: Matrix.cpp:78
Torus(void)
Protected constructor used to do intialization general to all torus objects.
Definition: Torus.cpp:35
float scalingFactor
Definition: Native.cpp:138
GLuint vaoID
Definition: Native.cpp:98
void setColor(float red, float green, float blue, float alpha)
Sets the uniform color of the drawn torus.
Definition: Torus.cpp:49
static void setResourceDirectory(std::string requiredResourceDirectory)
Set the resource directory for all tori.
Definition: Torus.cpp:105
GLfloat GLfloat f
Definition: gl2ext.h:2707
GLuint programID
Definition: AntiAlias.cpp:60
#define GL_CHECK(x)
Definition: AstcTextures.h:59
string resourceDirectory
Definition: AntiAlias.cpp:55
GLuint vertexShaderID
#define LOGE(...)
Definition: AstcTextures.h:30
static void processShader(GLuint *shader, const char *filename, GLint shaderType)
Create shader, load in source, compile, and dump debug as necessary.
Definition: Shader.cpp:29
void setupGraphics(const std::string vertexShaderPath, const std::string fragmentShaderPath)
Initialize constant OpenGL components such as program, shaders and constant matrices.
Definition: Torus.cpp:75
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
static std::string resourceDirectory
Definition: Torus.h:45