28 #include <GLES3/gl3.h>
29 #include <GLES3/gl3ext.h>
34 using namespace MaliSDK;
40 this->torusRadius = torusRadius;
41 this->circleRadius = circleRadius;
44 const string fragmentShaderPath =
resourceDirectory +
"Instanced_Tessellation_Instanced_shader.frag";
47 const string vertexShaderPath =
resourceDirectory +
"Instanced_Tessellation_Instanced_shader.vert";
53 initializeControlUniformBuffers();
55 initializeVertexAttribs();
58 setColor(0.0
f, 0.7
f, 0.0
f, 1.0
f);
66 GLuint buffersArray[] = {controlIndicesBufferID, controlVerticesBufferID, patchIndicesBufferID, patchVertexBufferID};
67 GLint buffersArraySize =
sizeof(buffersArray) /
sizeof(buffersArray[0]);
70 GL_CHECK(glDeleteBuffers(buffersArraySize, &controlIndicesBufferID));
82 GL_CHECK(glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, patchIndicesBufferID));
84 if (rotationVectorLocation != -1)
87 GL_CHECK(glUniform3fv(rotationVectorLocation, 1, rotationVector));
91 LOGE(
"Could not locate \"rotationVector\" uniform in program [%d]",
programID);
95 GL_CHECK(glDrawElementsInstanced(GL_TRIANGLES, patchTriangleIndicesCount, GL_UNSIGNED_INT, 0, patchInstancesCount));
101 float torusVertices [componentsCount];
102 unsigned int controlPointsIndices[controlPointsIndicesCount];
111 if (controlIndicesBlockIndex != GL_INVALID_INDEX)
114 GL_CHECK(glGenBuffers (1, &controlIndicesBufferID ));
115 GL_CHECK(glBindBuffer (GL_UNIFORM_BUFFER, controlIndicesBufferID ));
116 GL_CHECK(glBufferData (GL_UNIFORM_BUFFER,
sizeof(controlPointsIndices), controlPointsIndices, GL_STATIC_DRAW));
118 GL_CHECK(glBindBufferBase (GL_UNIFORM_BUFFER, 0, controlIndicesBufferID ));
122 LOGE(
"Could not locate \"ControlPointsIndices\" uniform block in program [%d].",
programID);
127 if (controlVerticesBlockIndex != GL_INVALID_INDEX)
130 GL_CHECK(glGenBuffers (1, &controlVerticesBufferID ));
131 GL_CHECK(glBindBuffer (GL_UNIFORM_BUFFER, controlVerticesBufferID ));
132 GL_CHECK(glBufferData (GL_UNIFORM_BUFFER, componentsCount *
sizeof(
float), torusVertices, GL_STATIC_DRAW));
134 GL_CHECK(glBindBufferBase (GL_UNIFORM_BUFFER, 1, controlVerticesBufferID ));
138 LOGE(
"Could not locate \"ControlPointsVertices\" uniform block in program [%d].",
programID);
149 float patchVertices [patchComponentsCount];
150 unsigned int patchTriangleIndices[patchTriangleIndicesCount];
159 if (positionLocation != -1)
162 GL_CHECK(glGenBuffers(1, &patchVertexBufferID));
163 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, patchVertexBufferID ));
165 GL_CHECK(glBufferData(GL_ARRAY_BUFFER, patchComponentsCount *
sizeof(
float), patchVertices, GL_STATIC_DRAW));
167 GL_CHECK(glVertexAttribPointer (positionLocation, 2, GL_FLOAT, GL_FALSE, 0, NULL));
168 GL_CHECK(glEnableVertexAttribArray(positionLocation ));
172 LOGE(
"Could not locate \"patchUVposition\" input attribute in program [%d].",
programID);
177 GL_CHECK(glGenBuffers(1, &patchIndicesBufferID ));
178 GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, patchIndicesBufferID ));
180 GL_CHECK(glBufferData(GL_ELEMENT_ARRAY_BUFFER, patchTriangleIndicesCount *
sizeof(
unsigned int), patchTriangleIndices, GL_STATIC_DRAW));
188 const float lightAngle =
M_PI / 6.0f;
190 const float lightColor[] = {1.0f, 1.0f, 1.0f};
192 const float lightDirection[] = {cosf(lightAngle), sinf(lightAngle), 2.0f};
194 const float ambientIntensity = 0.2f;
197 GLint lightColorLocation =
GL_CHECK(glGetUniformLocation(
programID,
"light.lightColor"));
198 GLint lightDirectionLocation =
GL_CHECK(glGetUniformLocation(
programID,
"light.lightDirection"));
199 GLint ambientIntensityLocation =
GL_CHECK(glGetUniformLocation(
programID,
"light.ambientIntensity"));
204 if (lightColorLocation != -1)
206 GL_CHECK(glUniform3fv(lightColorLocation, 1, lightColor));
210 LOGE(
"Could not find \"light.lightColor\" uniform in program [%d]",
programID);
213 if (lightDirectionLocation != -1)
215 GL_CHECK(glUniform3fv(lightDirectionLocation, 1, lightDirection));
219 LOGE(
"Could not find \"light.lightDirection\" uniform in program [%d]",
programID);
222 if (ambientIntensityLocation != -1)
224 GL_CHECK(glUniform1f(ambientIntensityLocation, ambientIntensity));
228 LOGE(
"Could not find \"light.ambientIntensity\" uniform in program [%d]",
programID);
static void calculateControlPointsIndices(unsigned int patchDimension, unsigned int patchInstancesCount, unsigned int controlPointsIndicesCount, unsigned int *controlPointsIndices)
Determines an array of indices defining a mesh of control points for instanced torus patches...
static void generateBezierVertices(float torusRadius, float circleRadius, float *vertices)
Generate torus vertices applying distortions to some of them.
void draw(float *rotationVector)
Draws instanced solid torus.
bool setupGraphics(int width, int height)
~InstancedSolidTorus(void)
Frees allocated memory.
#define M_PI
The value of pi.
GLint rotationVectorLocation
static void calculatePatchData(unsigned int patchDensity, float *patchVertices, unsigned int *patchTriangleIndices)
Determines patch data for an instanced torus model.
bool initializeVertexAttribs(void)
Initialize vertex attribute arrays and buffer objects coresponding to them. Make sure that programID ...
InstancedSolidTorus(float torusRadius, float circleRadius)
Instantiates a representation of a solid torus, using user-provided radius and tube radius...
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
bool initializeControlUniformBuffers(void)
Initializes control mesh data and stores it in appropriate uniform buffers.
void setLightParameters(void)
Sets directionl light parameters, such as light direction, its color and ambient intensity and passes...