OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
WireframeTorus.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 "TorusModel.h"
25 #include "WireframeTorus.h"
26 
27 #include <GLES3/gl3.h>
28 #include <GLES3/gl3ext.h>
29 
30 #include <string>
31 
32 using namespace MaliSDK;
33 using std::string;
34 
35 WireframeTorus::WireframeTorus(float torusRadius, float circleRadius)
36 {
37  /* Initialize class fields. */
38  this->torusRadius = torusRadius;
39  this->circleRadius = circleRadius;
40  this->indicesBufferID = 0;
41 
42  /* Name of the file in which fragment shader's body is located. */
43  const string fragmentShaderPath = resourceDirectory + "Instanced_Tessellation_Wireframe_shader.frag";
44  /* Name of the file in which vertex shader's body is located. */
45  const string vertexShaderPath = resourceDirectory + "Instanced_Tessellation_Wireframe_shader.vert";
46 
47  /* Initialize shaders and program corresponding to the constructed torus object. */
48  setupGraphics(vertexShaderPath, fragmentShaderPath);
49 
50  /* Determine indices of the mesh. */
51  initializeBufferForIndices();
52 
53  /* Generate buffers and vertex arrays to store torus vertices and colors associated with them. */
54  initializeVertexAttribs();
55 
56  /* Set wireframe color to orange. */
57  setColor(1.0f, 0.3f, 0.0f, 1.0f);
58 }
59 
61 {
62  GL_CHECK(glDeleteBuffers(1, &indicesBufferID));
63 }
64 
66 {
67  /* Temporary array to store determined indices. */
68  unsigned int indices[indicesCount];
69 
70  TorusModel::calculateWireframeIndices(circlesCount, pointsPerCircleCount, indices);
71 
72  /* Create GL_ELEMENT_ARRAY_BUFFER and store indices in it. */
73  GL_CHECK(glGenBuffers(1, &indicesBufferID));
74  GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesBufferID));
75  GL_CHECK(glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesCount * sizeof(int), indices, GL_STATIC_DRAW));
76 }
77 
78 /* [Draw wireframe torus] */
79 void WireframeTorus::draw(float* rotationVector)
80 {
81  GLint rotationVectorLocation = GL_CHECK(glGetUniformLocation(programID, "rotationVector"));
82 
83  /* Set required elements to draw mesh torus. */
84  GL_CHECK(glUseProgram(programID));
85  GL_CHECK(glBindVertexArray(vaoID));
86  GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesBufferID));
87 
88  /* Pass Model-View matrix elements to the shader. */
89  GL_CHECK(glUniform3fv(rotationVectorLocation, 1, rotationVector));
90 
91  /* Draw lines described by previously determined indices. */
92  GL_CHECK(glDrawElements(GL_LINES, indicesCount, GL_UNSIGNED_INT, 0));
93 }
94 /* [Draw wireframe torus] */
95 
97 {
98  /* Get input attribute locations. */
99  GLint positionLocation = GL_CHECK(glGetAttribLocation(programID, "position"));
100 
101  /* ID of a buffer that stores vertices. */
102  GLuint vertexBufferID = 0;
103 
104  /* Temporary arrays to keep vertex and color data. */
105  float torusVertices[componentsCount];
106 
107  TorusModel::generateVertices(torusRadius, circleRadius, circlesCount, pointsPerCircleCount, torusVertices);
108 
109  /* Generate and bind vertex array object. */
110  GL_CHECK(glGenVertexArrays(1, &vaoID));
111  GL_CHECK(glBindVertexArray(vaoID));
112 
113  if (positionLocation != -1)
114  {
115  /* Generate and bind buffer object to store vertex data. */
116  GL_CHECK(glGenBuffers(1, &vertexBufferID));
117  GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID));
118 
119  /* Store torus vertices inside the generated buffer. */
120  GL_CHECK(glBufferData(GL_ARRAY_BUFFER, componentsCount * sizeof(float), torusVertices, GL_STATIC_DRAW));
121 
122  /* Set vertex attrib pointer to the beginning of the bound array buffer. */
123  GL_CHECK(glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, 0, NULL));
124  GL_CHECK(glEnableVertexAttribArray(positionLocation));
125  }
126  else
127  {
128  LOGE("Could not locate \"position\" input attribute in program [%d].", programID);
129  return false;
130  }
131 
132  return true;
133 }
GLint positionLocation
Definition: Native.cpp:99
bool initializeVertexAttribs(void)
Initialize vertex attribute arrays and buffer objects coresponding to them. Make sure that programID ...
static void calculateWireframeIndices(unsigned int circlesCount, unsigned int pointsPerCircleCount, unsigned int *indices)
Determines indices for glDrawElements() call for wireframed torus.
Definition: TorusModel.cpp:196
static void generateVertices(float torusRadius, float circleRadius, unsigned int circlesCount, unsigned int pointsPerCircleCount, float *vertices)
Generate vertices of the torus model.
Definition: TorusModel.cpp:234
GLuint vaoID
Definition: Native.cpp:98
void initializeBufferForIndices(void)
Determine indices needed for a single glDrawElements() call in GL_LINES mode.
GLsizei GLenum const void * indices
Definition: gl2ext.h:322
bool setupGraphics(int width, int height)
Definition: AntiAlias.cpp:71
GLfloat GLfloat f
Definition: gl2ext.h:2707
GLuint programID
Definition: AntiAlias.cpp:60
#define GL_CHECK(x)
Definition: AstcTextures.h:59
void draw(float *rotationVector)
Draw the torus model.
string resourceDirectory
Definition: AntiAlias.cpp:55
WireframeTorus(float torusRadius, float circleRadius)
Instantiates a representation of a solid torus, using user-provided radius and tube radius...
GLint rotationVectorLocation
Definition: Native.cpp:102
#define LOGE(...)
Definition: AstcTextures.h:30
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count