Skip to content

Instantly share code, notes, and snippets.

@smoku
Created August 28, 2014 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smoku/04d3a42174d81532fbb8 to your computer and use it in GitHub Desktop.
Save smoku/04d3a42174d81532fbb8 to your computer and use it in GitHub Desktop.
/*===============================================================================
Copyright (c) 2012-2014 Qualcomm Connected Experiences, Inc. All Rights Reserved.
Vuforia is a trademark of QUALCOMM Incorporated, registered in the United States
and other countries. Trademarks of QUALCOMM Incorporated are used with permission.
===============================================================================*/
#include "CylinderModel.h"
#include <math.h>
CylinderModel::CylinderModel()
{
prepareData();
}
void*
CylinderModel::ptrVertices()
{
return &cylinderVertices[0];
}
void*
CylinderModel::ptrIndices()
{
return &cylinderIndices[0];
}
void*
CylinderModel::ptrTexCoords()
{
return &cylinderTexCoords[0];
}
void*
CylinderModel::ptrNormals()
{
return &cylinderNormals[0];
}
int
CylinderModel::nbIndices()
{
return sizeof(cylinderIndices) / sizeof(unsigned short);
}
void
CylinderModel::prepareData() {
double deltaTex = 1.0 / (double) CYLINDER_NB_SIDES;
// vertices index for the bottom and top vertex
const int ix_vertex_center_bottom = 2 * CYLINDER_NB_SIDES;
const int ix_vertex_center_top = ix_vertex_center_bottom + 1;
for (int i = 0; i < CYLINDER_NB_SIDES; i++)
{
double angle = 2 * M_PI * i / CYLINDER_NB_SIDES;
// bottom circle
cylinderVertices[(i * 3) + 0] = cos(angle); // x
cylinderVertices[(i * 3) + 1] = sin(angle); // y
cylinderVertices[(i * 3) + 2] = 0; // z
// top circle
cylinderVertices[(i + CYLINDER_NB_SIDES) * 3 + 0] = cylinderVertices[i * 3 + 0]; // x
cylinderVertices[(i + CYLINDER_NB_SIDES) * 3 + 1] = cylinderVertices[i * 3 + 1]; // y
cylinderVertices[(i + CYLINDER_NB_SIDES) * 3 + 2] = 1; // z
// texture coordinates
cylinderTexCoords[(i * 2) + 0] = i * deltaTex;
cylinderTexCoords[(i * 2) + 1] = 1;
cylinderTexCoords[((i + CYLINDER_NB_SIDES) * 2) + 0] = i * deltaTex;
cylinderTexCoords[((i + CYLINDER_NB_SIDES) * 2) + 1] = 0;
// normals
cylinderNormals[(i * 3) + 0] = cylinderVertices[(i * 3) + 1];
cylinderNormals[(i * 3) + 1] = - (cylinderVertices[(i * 3) + 0]);
cylinderNormals[(i * 3) + 2] = 0; // z
// top circle
cylinderNormals[(i + CYLINDER_NB_SIDES) * 3 + 0] = cylinderVertices[i * 3 + 1];
cylinderNormals[(i + CYLINDER_NB_SIDES) * 3 + 1] = - (cylinderVertices[i * 3 + 0]);
cylinderNormals[(i + CYLINDER_NB_SIDES) * 3 + 2] = 0; // z
// indices
// triangles are b0 b1 t1 and t1 t0 b0 (bn: vertex #n on the bottom circle, tn: vertex #n on the to circle)
// i1 is the index of the next vertex - we wrap if we reach the end of the circle
int i1 = i + 1;
if (i1 == CYLINDER_NB_SIDES) {
i1 = 0;
}
cylinderIndices[(i * 12) + 0] = i ;
cylinderIndices[(i * 12) + 1] = i1 ;
cylinderIndices[(i * 12) + 2] = (i1 + CYLINDER_NB_SIDES) ;
cylinderIndices[(i * 12) + 3] = (i1 + CYLINDER_NB_SIDES) ;
cylinderIndices[(i * 12) + 4] = (i + CYLINDER_NB_SIDES);
cylinderIndices[(i * 12) + 5] = i;
// bottom circle
cylinderIndices[(i * 12) + 6] = i1 ;
cylinderIndices[(i * 12) + 7] = i ;
cylinderIndices[(i * 12) + 8] = ix_vertex_center_bottom ;
// top circle
cylinderIndices[(i * 12) + 9] = (i + CYLINDER_NB_SIDES) ;
cylinderIndices[(i * 12) + 10] = (i1 + CYLINDER_NB_SIDES) ;
cylinderIndices[(i * 12) + 11] = ix_vertex_center_top ;
}
// we are adding 2 extra vertices: the center of each circle
cylinderVertices[(3 * ix_vertex_center_bottom) + 0] = 0; // x
cylinderVertices[(3 * ix_vertex_center_bottom) + 1] = 0; // y
cylinderVertices[(3 * ix_vertex_center_bottom) + 2] = 0; // z
cylinderVertices[(3 * ix_vertex_center_top) + 0] = 0; // x
cylinderVertices[(3 * ix_vertex_center_top) + 1] = 0; // y
cylinderVertices[(3 * ix_vertex_center_top) + 2] = 1; // z
cylinderTexCoords[(ix_vertex_center_bottom * 2) + 0] = 0.5;
cylinderTexCoords[(ix_vertex_center_bottom * 2) + 1] = 0.5;
cylinderTexCoords[(ix_vertex_center_top * 2) + 0] = 0.5;
cylinderTexCoords[(ix_vertex_center_top * 2) + 1] = 0.5;
cylinderNormals[(3 * ix_vertex_center_bottom) + 0] = 0;
cylinderNormals[(3 * ix_vertex_center_bottom) + 1] = 0;
cylinderNormals[(3 * ix_vertex_center_bottom) + 2] = -1; // z
cylinderNormals[(3 * ix_vertex_center_top) + 0] = 0;
cylinderNormals[(3 * ix_vertex_center_top) + 1] = 0;
cylinderNormals[(3 * ix_vertex_center_top) + 2] = 1; // z
}
/*===============================================================================
Copyright (c) 2012-2014 Qualcomm Connected Experiences, Inc. All Rights Reserved.
Vuforia is a trademark of QUALCOMM Incorporated, registered in the United States
and other countries. Trademarks of QUALCOMM Incorporated are used with permission.
===============================================================================*/
#ifndef CYLINDER_MODEL_H_
#define CYLINDER_MODEL_H_
// number of sides used to build the cylinder
#define CYLINDER_NB_SIDES 64
// 2 series of CYLINDER_NB_SIDES vertex, plus
// one for the bottom circle, one for the top circle
#define CYLINDER_NUM_VERTEX ((CYLINDER_NB_SIDES * 2) + 2)
#ifdef __cplusplus
extern "C" {
#endif
class CylinderModel {
public:
CylinderModel();
void* ptrVertices();
void* ptrIndices();
void* ptrTexCoords();
void* ptrNormals();
int nbIndices();
private:
void prepareData();
float cylinderVertices[CYLINDER_NUM_VERTEX * 3];
// 4 triangles per side, so 12 indices per side
unsigned short cylinderIndices[CYLINDER_NB_SIDES * 12];
float cylinderTexCoords[CYLINDER_NUM_VERTEX * 2];
float cylinderNormals[CYLINDER_NUM_VERTEX * 3];
};
#ifdef __cplusplus
}
#endif
#endif // CYLINDER_MODEL_H_
if (PLAYING == currentStatus || PAUSED == currentStatus || REACHED_END == currentStatus || STOPPED == currentStatus) {
aspectRatio = (float)[videoPlayerHelper getVideoHeight] / (float)[videoPlayerHelper getVideoWidth];
// Convert trackable pose to matrix for use with OpenGL
QCAR::Matrix44F modelViewMatrixVideo = QCAR::Tool::convertPose2GLMatrix(trackablePose);
QCAR::Matrix44F modelViewProjectionVideo;
ShaderUtils::scalePoseMatrix(kCylinderScaleX,
kCylinderScaleY,
kCylinderScaleZ,
&modelViewMatrixVideo.data[0]);
ShaderUtils::multiplyMatrix(&qUtils.projectionMatrix.data[0],
&modelViewMatrixVideo.data[0] ,
&modelViewProjectionVideo.data[0]);
if (frameTextureID != 0) {
glUseProgram(shaderProgramID);
// glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, quadVertices);
// glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, quadNormals);
// glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, texCoords);
glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)cylinderModel->ptrVertices());
glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)cylinderModel->ptrNormals());
glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)cylinderModel->ptrTexCoords());
glEnableVertexAttribArray(vertexHandle);
glEnableVertexAttribArray(normalHandle);
glEnableVertexAttribArray(textureCoordHandle);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, frameTextureID);
glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (GLfloat*)&modelViewProjectionVideo.data[0]);
glUniform1i(texSampler2DHandle, 0 /*GL_TEXTURE0*/);
glUniform1f(alphaHandle, 1.0f);
glDrawElements(GL_TRIANGLES, cylinderModel->nbIndices(), GL_UNSIGNED_SHORT, (const GLvoid*)cylinderModel->ptrIndices());
// glDrawElements(GL_TRIANGLES, NUM_QUAD_INDEX, GL_UNSIGNED_SHORT, quadIndices);
glDisableVertexAttribArray(vertexHandle);
glDisableVertexAttribArray(normalHandle);
glDisableVertexAttribArray(textureCoordHandle);
glUseProgram(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment