使用openGL图形库绘制,都需要通过openGL接口向图像显卡提交顶点数据,显卡根据提交的数据绘制出相应的图形。

openGL绘制方式有:直接模式,显示列表,顶点数组,顶点索引。

直接模式:最简单,最直接的模式,但是性能是最差的,因为每绘制一个基本图元都需要提交一次数据;

glBegin(GL_TRIANGLE_STRIP);

glColor3ub(255, 0, 0);

glVertex3f(-0.5f, 0.5f, 0.0f);

glColor3ub(0, 255, 0);

glVertex3f(-0.5f, -0.5f, 0.0f);

glColor3ub(0, 0, 255);

glVertex3f(0.5f, 0.5f, 0.0f);

glColor3ub(255, 0, 255);

glVertex3f(0.5f, -0.5f, 0.0f);

glEnd();

上面就是用直接模式绘制一个三角形带的所有openGL命令,如果要绘制无数个三角形,那么函数调用的开销是巨大的,而且这样的写法让顶点数据非常不容易扩展和修改,所以基本上这种模式不可能用在实际的用途中。

显示列表:直接模式在每次绘制的时候,都需要将顶点数组从cpu端重新发送到gpu端,如果每次数据都没有任何变化,这种重复的发送就显得没有意义而且低效。显示列表就是为了解决这个重复发送的性能问题,显示列表相当于把一组绘制命令存储在服务器端(gpu端),每次只需要发送一个调用命令,而不需要重复发送所有顶点数据,就可以执行已经预定好的绘制命令了。虽然显示列表解决了不用重发发送顶点数据的问题,但是缺点也是显而易见的,就是显示列表一旦定义好,就无法被修改,因此显示列表只适用于那些不会被修改的绘制命令。而且显示列表和直接模式依然具有相同的问题,就是函数调用开销和难以扩展和修改。只是相当于命令直接在gpu端执行,减少了从cpu发送gpu的过程而已。

定义一个显示列表:

glNewList (listName, GL_COMPILE);

glColor3f (1.0, 0.0, 0.0);

glBegin (GL_TRIANGLES);

glVertex2f (0.0, 0.0);

glVertex2f (1.0, 0.0);

glVertex2f (0.0, 1.0);

glEnd ();

glTranslatef (1.5, 0.0, 0.0);

glEndList ();

执行一个显示列表:

glCallList (listName);

删除一个显示列表:

glDeleteLists(listName, 1);

顶点数组:由于直接模式的局限性,openGL提供了另一种更加高效的绘制模式,顶点数组。

顶点数组顾名思义就是允许我们将我们的顶点数据放置到一个数组中,一次性提交给显卡进行绘制。这样只需要极少量的函数调用,而且数组全部聚合在一起,也更加容易修改和扩展。

// 先定义顶点位置,颜色数组,纹理数组的顶点相关数据:

GLfloat vertexes[] = {
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 512, 0.0f, 1.0f,
1024, 512, 0.0f, 1.0f,
1024, 0.0f, 0.0f, 1.0f
};
GLfloat colores[] = {
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f
};
GLfloat texCoordes[] = {
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f
};

// 0,1,2代表着色器的input的location,分别代表顶点位置,顶点颜色,顶点纹理坐标(这里使用的是可编程管线,如果使用固定管线,指定方式也是类似的)

// 为0,1,2指定相应的数据

glEnableVertexAttribArray(0); // 固定管线使用glEnableClientState(GL_VERTEX_ARRAY)
glEnableVertexAttribArray(1); // 固定管线使用glEnableClientState(GL_COLOR_ARRAY)
glEnableVertexAttribArray(2); // 固定管线使用glEnableClientState(GL_TEXTURE_COORD_ARRAY)
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, vertexes); // 固定管线使用glVertexPointer(4, GL_FLOAT, 0, vertexes);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, colores); // 固定管线使用glColorPointer(4, GL_FLOAT, 0, colores);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, texCoordes); // 固定管线使用glTexCoordPointer(2, GL_FLOAT, 0, texCoordes);

// 绘制一个四边形

glDrawArrays(GL_QUADS, 0, 4);

这里将位置,颜色,纹理分为三个数组分别存放,我们也可把所有数据放在一个数组中,称之为交错数组:

GLfloat data[] = {
0.0f, 0.0f, 0.0f, 1.0f,    1.0f, 1.0f, 1.0f, 1.0f,   0.0f, 1.0f,
0.0f, 512, 0.0f, 1.0f,    1.0f, 1.0f, 1.0f, 1.0f,   0.0f, 0.0f,
1024, 512, 0.0f, 1.0f,  1.0f, 1.0f, 1.0f, 1.0f,   1.0f, 0.0f,
1024, 0.0f, 0.0f, 1.0f,  1.0f, 1.0f, 1.0f, 1.0f,   1.0f, 1.0f
};

glEnableVertexAttribArray(0); 
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), vertexes); // 第四个参数指的是两个位置数据在数组中间距,第五个参数指的是第一个位置数据在数组中的起始位置
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), colores + 4); // 第四个参数指的是两个颜色数据在数组中间距,第五个参数指的是第一个颜色数据在数组中的起始位置
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), texCoordes + 8); // 第四个参数指的是两个纹理坐标数据在数组中间距,第五个参数指的是第一个纹理坐标数据在数组中的起始位置

// 绘制一个四边形

glDrawArrays(GL_QUADS, 0, 4);

除此之外,交错无数也可以直接使用:

  glInterleavedArrays (GLenum format, GLsizei stride, const GLvoid *pointer);

进行一次性指定,但是数组中数据的构成规则必须和format指定的规则一致。例如:glInterleavedArrays(GL_V2F,0,data); 表示数据组合规则是仅仅2个浮点数的位置数据

顶点索引:顶点数组已经可以为我们提供了方便指定绘制数据的方式,大幅提升了渲染的性能。但是依然还存在一个问题:如果我们需要绘制一个模型,这个模型由无数三角形片构成,大部分三角形都是连续拼接的没有空隙,所以每相邻的两个三角形可能会有拥有一个或者两个完全相同顶点(位置,颜色,纹理坐标都相同)。如果我们使用顶点数组的数据来构成这个模型,我们将为相邻的两个三角形片分别制定6个顶点数据,其中有可能会出现最多两对完全相同的顶点,而这些顶点其实是可以共享的,但却出现了冗余数据。在模型复杂的情况下,冗余数据也是巨大的。于是openGL为我们指定了一种更加灵活的方式,顶点索引数组,即我们只需要创建好必要的顶点数据,顶点数据在数组中的排列也不受图元绘制方式的限制,理论上可以随意排列。然后用索引去对应每一个顶点数据,绘制图元需要提交顶点数据的时候,直接指定顶点索引即可,因为顶点索引会一一映射到顶点数据,这样就消除了冗余的顶点数据,而且以更加灵活的方式进行渲染。

// 指定顶点数据,注意渲染顺序的迎风面是逆时针还是顺时针,这里绘制4个顶点,分别对应顶点数组中的前4个顶点

GLuint indexes = {0, 1, 2, 3};

// 用顶点索引进行绘制的绘制调用

glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, indexes);

VBO: 顶点数组加上顶点索引,似乎已经可以完美解决大部分问题,但是图形渲染对性能的追求是永无止境的。虽然使用顶点数组和顶点索引,我们可以一次性提交所有绘制数据,并只需要调用一次绘制命令。但是在数据很大的情况下,我们依然都要从cpu端向gpu端提交大量数据,如果这些数据又几乎不会发生改变,那么这种操作将是极大的性能浪费。

为了减少这种耗时但又无意义的工作,openGL为我们提供了VBO(顶点缓冲对象)来改善这种问题。

使用VBO,可以将我们的顶点数据存放在图像显卡的内存中,而不需要存放在cpu端的内存中,就不需要在每次绘制时,发送大量顶点数据到gpu端了。

// 生成VBO,并为VBO绑定顶点数据

size_t dataSize = sizeof(GLfloat) * vertexCount * 4; // 在图像显卡中需要分配的内存大小
GLuint vbos[1] = { 0 }; // VBO名字

glGenBuffers(1, vbos); // 生成一个可用的VBO名字
if (vbos[0] > 0) // 如果名字可用
{
vertexVBO = vbos[0];

glBindBuffer(GL_ARRAY_BUFFER, vertexVBO); // 绑定当前的VBO,GL_ARRAY_BUFFER是VBO使用的固定参数

glBufferData(GL_ARRAY_BUFFER, dataSize, vertexes, GL_STATIC_DRAW); // 将位置数据绑定到当前的VBO上,dataSize是需要的内存大小,vertexes是顶点的位置数据

// GL_STATIC_DRAW 是一个性能提示参数,这个参数指示了当前VBO的用途,该参数必须是GL_STREAM_DRAWGL_STATIC_DRAW, or GL_DYNAMIC_DRAW之一。openGL会根据该指示,尽可能将数据放置在性能最优的内存中,可能是显存,AGP内存,或者cpu内存中。

// GL_STATIC_DRAW:数据指定一次,并多次被用于绘制。

// GL_STREAM_DRAW:数据指定一次,最多几次用于绘制。

// GL_DYNAMIC_DRAW:数组多次指定,多次用于绘制。

delete[] vertexes;
m_vertexes = nullptr;

CHECK_GL_ERROR();
}

// 使用VBO进行绘制,和使用顶点数组类似

#define BUFFER_OFFSET(offset) ((GLvoid*)(NULL + offset)) // 数据在缓冲区中的偏移位置,和顶点数组指针位置效果类似

glBindBuffer(GL_ARRAY_BUFFER, vertexVBO); // 绑定位置VBO
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); // 指定位置数据
glBindBuffer(GL_ARRAY_BUFFER, colorVBO); // 绑定颜色VBO
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); // 指定颜色数据
glBindBuffer(GL_ARRAY_BUFFER, textureVBO); // 绑定纹理VBO
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); // 指定纹理数据
glBindBuffer(GL_ARRAY_BUFFER, 0);

glDrawArrays(GL_QUADS, 0, m_vertexCount); // 绘制

IBO: 索引缓冲对象,和VBO一样,只是存储的是索引数组。

glGenBuffers(1, &IBO);
if (sphereIBO > 0)
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); // 参数必须使用GL_ELEMENT_ARRAY_BUFFER
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * indexCount, indexes, GL_STATIC_DRAW);

CHECK_GL_ERROR();

delete[] indexes;
indexes = nullptr;
}

// 绘制

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glDrawElements(GL_QUADS, indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

VAO: 有了VBO和IBO,已经可以很好的解决问题了,但是使用VAO可以使我们的开发更加灵活。VAO其实就是可以绑定VBO和IBO的一个包装对象,我们把有关联的VBO和IBO一起绑定到一个VAO上,我们每次只需要使用VAO就可以进行绘制了。

// 生成VAO

glGenVertexArrays(1, &VAO); // 生成一个VAO
if (VAO > 0) // 如果VAO可用
{
glBindVertexArray(VAO); // 绑定到当前的VAO

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); // 绑定一个IBO到当前的VAO上

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);

glBindBuffer(GL_ARRAY_BUFFER, vertexVBO); // 绑定位置VBO到当前的VAO上
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); // 指定位置数据
glBindBuffer(GL_ARRAY_BUFFER, colorVBO); // 绑定颜色VBO到当前的VAO上
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); // 指定颜色数据
glBindBuffer(GL_ARRAY_BUFFER, textureVBO); // 绑定纹理VBO到当前的VAO上
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); // 指定纹理数据

CHECK_GL_ERROR_DEBUG();
}

// 绘制

glBindVertexArray(VAO);
glDrawElements(GL_QUADS, indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glBindVertexArray(0);

下面是基于cocos2d的完整的代码:

Sphere.h:

  1. #ifndef __SPHERE__
  2. #define __SPHERE__
  3.  
  4. #include "cocos2d.h"
  5.  
  6. USING_NS_CC;
  7.  
  8. class Sphere : public cocos2d::Node
  9. {
  10. public:
  11. static Sphere* create();
  12.  
  13. virtual ~Sphere();
  14.  
  15. bool init();
  16.  
  17. virtual void draw(Renderer *renderer, const Mat4& transform, uint32_t flags);
  18.  
  19. virtual void visit(Renderer *renderer, const Mat4& parentTransform, uint32_t parentFlags);
  20.  
  21. private:
  22. GLProgram* m_program;
  23. GLProgram* m_program_planview;
  24. GLuint m_textureName;
  25.  
  26. GLfloat* m_vertexes;
  27. GLfloat* m_colors;
  28. GLfloat* m_texcoordes;
  29. GLuint* m_indexes;
  30. unsigned int m_vertexCount;
  31. unsigned int m_indexCount;
  32.  
  33. float m_sphereLatitude; // 纬度
  34. float m_sphereLongitude; // 经度
  35. unsigned int m_sphereLatitudeCount;
  36. unsigned int m_sphereLongitudeCount;
  37. unsigned int m_sphereQuadCount;
  38.  
  39. float m_sphereRadius; // 半径
  40. float m_spherePerimeter; // 周长
  41. float m_planViewWidth;
  42. float m_planViewHeight;
  43.  
  44. Mat4 m_sphereTransform;
  45. float m_rotateXAngle;
  46. float m_rotateYAngle;
  47. float m_rotateZAngle;
  48. float m_sphereTranslateX;
  49. float m_sphereTranslateY;
  50.  
  51. float m_planViewTranslateX;
  52. float m_planViewTranslateY;
  53.  
  54. bool m_isContinue;
  55. float m_continueRotateY;
  56. float m_continueRotateX;
  57. float m_decreateDetal;
  58.  
  59. std::thread* m_thread_1;
  60. std::thread* m_thread_2;
  61. bool m_isThread1Done;
  62. bool m_isThread2Done;
  63.  
  64. std::vector<float> m_offsetDataList;
  65. std::vector<float> m_offsetDataListCopy;
  66.  
  67. GLuint m_sphereVAO;
  68. GLuint m_sphereIBO;
  69. GLuint m_sphereVertexVBO;
  70. GLuint m_sphereColorVBO;
  71. GLuint m_sphereTextureVBO;
  72.  
  73. bool initProgram();
  74. bool initVertexData();
  75.  
  76. void createQuaternion(float rotateX, float rotateY, float rotateZ, Quaternion& quat);
  77.  
  78. void generateVertexesSphere(float radius);
  79. void generateVertexesSphereNew(float radius);
  80. void generateTexture();
  81. void generateVAO();
  82. void generateIBO();
  83. void generateVBO();
  84.  
  85. void transformSphere(float touchOffsetX, float touchOffsetY);
  86.  
  87. void drawBg(const Mat4& transform);
  88. void drawSphere(const Mat4& transform, GLfloat* vertexes);
  89. void drawPlanView(const Mat4& transform, GLfloat* vertexes);
  90.  
  91. void update();
  92.  
  93. bool onTouchBegan(Touch* touch, Event* event);
  94. void onTouchMoved(Touch* touch, Event* event);
  95. void onTouchEnded(Touch* touch, Event* event);
  96.  
  97. void onThread1Proc();
  98. void onThread2Proc();
  99.  
  100. void noticeToTransform(float touchOffsetX, float touchOffsetY);
  101. };
  102.  
  103. #endif

Sphere.cpp:

  1. #include "SphereNew.h"
  2. #include "math.h"
  3. #include <mutex>
  4. #include <condition_variable>
  5. #include <chrono>
  6. #include <thread>
  7. #include <functional>
  8.  
  9. #define MPI 3.1415926f
  10. #define HMPI (3.1415926f / 2.0f)
  11. #define DMPI (3.1415926f * 2.0f)
  12. #define A_TO_R(angle) (3.1415926f / 180.0f * (float)(angle))
  13.  
  14. #define TEXTURE_WIDTH 1024
  15. #define TEXTURE_HEIGHT 512
  16.  
  17. #define BUFFER_OFFSET(offset) ((GLvoid*)(NULL + offset))
  18.  
  19. const GLchar* ccPositionTextureColor_v = " \
  20. attribute vec4 a_position; \n\
  21. attribute vec2 a_texCoord; \n\
  22. attribute vec4 a_color; \n\
  23. \n\
  24. #ifdef GL_ES \n\
  25. varying lowp vec4 v_fragmentColor; \n\
  26. varying mediump vec2 v_texCoord; \n\
  27. #else \n\
  28. varying vec4 v_fragmentColor; \n\
  29. varying vec2 v_texCoord; \n\
  30. #endif \n\
  31. \n\
  32. void main() \n\
  33. { \n\
  34. gl_Position = CC_MVPMatrix * a_position; \n\
  35. v_fragmentColor = a_color; \n\
  36. v_texCoord = a_texCoord; \n\
  37. } \n\
  38. ";
  39.  
  40. const GLchar* ccPositionTextureColorForSphere_v = " \
  41. uniform vec2 translate; \n\
  42. uniform float radius; \n\
  43. attribute vec4 a_position; \n\ attribute vec2 a_texCoord; \n\
  44. attribute vec4 a_color; \n\
  45. \n\
  46. #ifdef GL_ES \n\
  47. varying lowp vec4 v_fragmentColor; \n\
  48. varying mediump vec2 v_texCoord; \n\
  49. #else \n\
  50. varying vec4 v_fragmentColor; \n\
  51. varying vec2 v_texCoord; \n\
  52. #endif \n\
  53. const float pi = 3.1415926; \n\
  54. const float hpi = 1.5707963; \n\
  55. \n\
  56. void main() \n\
  57. { \n\
  58. vec4 _position = CC_MVMatrix * a_position; \n\
  59. float _angle1 = atan(_position.x, _position.z); \n\
  60. float _angle2 = atan(_position.z, _position.y); \n\
  61. float _xOffset = _angle1 * radius; \n\
  62. float _yOffset = _position.y * hpi; \n\
  63. _position.x = _xOffset + translate.x; \n\
  64. _position.y = _yOffset + translate.y; \n\
  65. _position.z = 0.0f; \n\
  66. gl_Position = CC_PMatrix * _position; \n\
  67. \n\
  68. v_fragmentColor = a_color; \n\
  69. v_texCoord = a_texCoord; \n\
  70. } \n\
  71. ";
  72.  
  73. const GLchar* ccPositionTextureColor_f = " \
  74. #ifdef GL_ES \n\
  75. precision lowp float; \n\
  76. #endif \n\
  77. \n\
  78. varying vec4 v_fragmentColor; \n\
  79. varying vec2 v_texCoord; \n\
  80. \n\
  81. void main() \n\
  82. { \n\
  83. vec4 color = v_fragmentColor * texture2D(CC_Texture0, v_texCoord); \n\
  84. color.a = 1.0; \n\
  85. gl_FragColor = color; \n\
  86. } \n\
  87. ";
  88.  
  89. static std::condition_variable cv_transform;
  90. static std::condition_variable cv_notice;
  91. static std::condition_variable cv_transform_sphere;
  92. static std::condition_variable cv_transform_planview;
  93. static std::mutex mx_transform;
  94. static std::mutex mx_transform_planview;
  95. static std::mutex mx_transform_sphere;
  96. static bool b_transform_sphere = false;
  97. static bool b_transform_planview = false;
  98.  
  99. static float _touchOffsetX = 0.0f;
  100. static float _touchOffsetY = 0.0f;
  101. static float _touchOffsetXCopy = 0.0f;
  102. static float _touchOffsetYCopy = 0.0f;
  103. static Mat4 _sphereTransformationCopy;
  104.  
  105. Sphere* Sphere::create()
  106. {
  107. Sphere *pRet = new(std::nothrow) Sphere();
  108.  
  109. if (pRet && pRet->init())
  110. {
  111. pRet->autorelease();
  112. return pRet;
  113. }
  114. else
  115. {
  116. delete pRet;
  117. pRet = nullptr;
  118. return nullptr;
  119. }
  120. }
  121.  
  122. bool Sphere::init()
  123. {
  124. if (!this->initProgram())
  125. return false;
  126.  
  127. if (!this->initVertexData())
  128. return false;
  129.  
  130. this->generateVertexesSphereNew(m_sphereRadius);
  131. this->generateTexture();
  132. this->generateVBO();
  133. this->generateIBO();
  134. this->generateVAO();
  135.  
  136. this->transformSphere(, );
  137.  
  138. auto eventListener = EventListenerTouchOneByOne::create();
  139. eventListener->setSwallowTouches(true);
  140. eventListener->onTouchBegan = CC_CALLBACK_2(Sphere::onTouchBegan, this);
  141. eventListener->onTouchMoved = CC_CALLBACK_2(Sphere::onTouchMoved, this);
  142. eventListener->onTouchEnded = CC_CALLBACK_2(Sphere::onTouchEnded, this);
  143. CCDirector::sharedDirector()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(eventListener, this);
  144.  
  145. /*
  146. m_isThread1Done = false;
  147. m_isThread2Done = false;
  148. m_thread_1 = new std::thread(std::bind(&Sphere::onThread1Proc, this));
  149. m_thread_2 = new std::thread(std::bind(&Sphere::onThread2Proc, this));
  150. */
  151. m_thread_1 = nullptr;
  152. m_thread_2 = nullptr;
  153.  
  154. return true;
  155. }
  156.  
  157. bool Sphere::initProgram()
  158. {
  159. bool suc = false;
  160.  
  161. m_program = new CCGLProgram();
  162. if (m_program)
  163. {
  164. if (m_program->initWithVertexShaderByteArray(ccPositionTextureColor_v, ccPositionTextureColor_f))
  165. {
  166. if (m_program->link())
  167. {
  168. m_program->updateUniforms();
  169. suc = true;
  170. }
  171. }
  172. }
  173. if (!suc)
  174. {
  175. delete m_program;
  176. m_program = NULL;
  177. }
  178.  
  179. suc = false;
  180.  
  181. m_program_planview = new CCGLProgram();
  182. if (m_program_planview)
  183. {
  184. if (m_program_planview->initWithVertexShaderByteArray(ccPositionTextureColorForSphere_v, ccPositionTextureColor_f))
  185. {
  186. if (m_program_planview->link())
  187. {
  188. m_program_planview->updateUniforms();
  189. suc = true;
  190. }
  191. }
  192. }
  193. if (!suc)
  194. {
  195. delete m_program_planview;
  196. m_program_planview = NULL;
  197. }
  198.  
  199. return suc;
  200. }
  201.  
  202. bool Sphere::initVertexData()
  203. {
  204. m_vertexes = nullptr;
  205. m_colors = nullptr;
  206. m_texcoordes = nullptr;
  207. m_indexes = nullptr;
  208. m_vertexCount = ;
  209. m_indexCount = ;
  210.  
  211. m_sphereTranslateX = 512.0f;
  212. m_sphereTranslateY = 412.0f;
  213. m_rotateYAngle = 0.0f;
  214. m_rotateXAngle = 0.0f;
  215. m_rotateZAngle = 0.0f;
  216.  
  217. m_planViewTranslateX = 512.0f;
  218. m_planViewTranslateY = 150.0f;
  219. m_planViewWidth = 400.0f;
  220. m_planViewHeight = 200.0f;
  221.  
  222. m_sphereRadius = 100.0f;
  223. m_spherePerimeter = 2.0f * 3.1415926f * m_sphereRadius;
  224.  
  225. m_isContinue = false;
  226. m_continueRotateX = 0.0f;
  227. m_continueRotateY = 0.0f;
  228. m_decreateDetal = 0.01f;
  229.  
  230. m_sphereLatitude = 1.0f;
  231. m_sphereLongitude = 1.0f;
  232. m_sphereLatitudeCount = ceil(180.0f / m_sphereLatitude);
  233. m_sphereLongitudeCount = ceil(360.0f / m_sphereLongitude);
  234. m_sphereQuadCount = m_sphereLatitudeCount * m_sphereLongitudeCount;
  235.  
  236. m_sphereTransform.setIdentity();
  237. //m_sphereTransform.rotateY(A_TO_R(90.0f));
  238.  
  239. m_sphereVAO = ;
  240. m_sphereIBO = ;
  241. m_sphereVertexVBO = ;
  242. m_sphereColorVBO = ;
  243. m_sphereTextureVBO = ;
  244.  
  245. return true;
  246. }
  247.  
  248. Sphere::~Sphere()
  249. {
  250. if (m_thread_1)
  251. {
  252. m_isThread1Done = true;
  253. m_thread_1->join();
  254. delete m_thread_1;
  255. }
  256.  
  257. if (m_thread_2)
  258. {
  259. m_isThread2Done = true;
  260. b_transform_planview = true;
  261. cv_transform_planview.notify_all();
  262. m_thread_2->join();
  263. delete m_thread_2;
  264. }
  265.  
  266. if (m_vertexes)
  267. delete[] m_vertexes;
  268. if (m_colors)
  269. delete[] m_colors;
  270. if (m_texcoordes)
  271. delete[] m_texcoordes;
  272. if (m_indexes)
  273. delete[] m_indexes;
  274.  
  275. if (m_sphereVertexVBO > )
  276. glDeleteBuffers(, &m_sphereVertexVBO);
  277. if (m_sphereColorVBO > )
  278. glDeleteBuffers(, &m_sphereColorVBO);
  279. if (m_sphereTextureVBO > )
  280. glDeleteBuffers(, &m_sphereTextureVBO);
  281. if (m_sphereIBO > )
  282. glDeleteBuffers(, &m_sphereIBO);
  283. if (m_sphereVAO > )
  284. glDeleteVertexArrays(, &m_sphereVAO);
  285. }
  286.  
  287. void Sphere::generateVertexesSphere(float radius)
  288. {
  289. float rx = 0.0f, ry = 0.0f, rz = 0.0f;
  290.  
  291. m_vertexes = new GLfloat[m_sphereQuadCount * ];
  292. m_colors = new GLfloat[m_sphereQuadCount * ];
  293. m_texcoordes = new GLfloat[m_sphereQuadCount * ];
  294. m_indexes = new GLuint[m_sphereQuadCount * ];
  295.  
  296. unsigned int offset = * ;
  297. unsigned int textureOffset = * ;
  298. unsigned int stride = m_sphereLatitudeCount / * m_sphereLongitudeCount * offset;
  299. unsigned int textureStride = m_sphereLatitudeCount / * m_sphereLongitudeCount * textureOffset;
  300.  
  301. float unitU = m_sphereLongitude / 360.0f;
  302. float unitV = m_sphereLatitude / 360.0f * ;
  303.  
  304. GLuint vertexIndex = ;
  305.  
  306. for (unsigned int latiIndex = ; latiIndex < m_sphereLatitudeCount / ; ++latiIndex)
  307. {
  308. // 纬度
  309. float latiAngle = m_sphereLatitude * (latiIndex + );
  310. float latiRadian1 = A_TO_R(latiAngle - m_sphereLatitude);
  311. float latiRadian2 = A_TO_R(latiAngle);
  312.  
  313. for (unsigned int longiIndex = ; longiIndex < m_sphereLongitudeCount; ++longiIndex)
  314. {
  315. // 经度
  316. float longiAngle = m_sphereLongitude * (longiIndex + );
  317. float longiRadian1 = A_TO_R(longiAngle - m_sphereLongitude);
  318. float longiRadian2 = A_TO_R(longiAngle);
  319.  
  320. unsigned int index = latiIndex * m_sphereLongitudeCount + longiIndex;
  321.  
  322. // vertex
  323. // 上半球
  324. m_vertexes[index * offset + ] = rx + radius * cos(latiRadian1) * cos(longiRadian1);
  325. m_vertexes[index * offset + ] = ry + radius * sin(latiRadian1);
  326. m_vertexes[index * offset + ] = rz + radius * cos(latiRadian1) *sin(longiRadian1);
  327. m_vertexes[index * offset + ] = 1.0f;
  328. // 上半球
  329. m_vertexes[index * offset + ] = rx + radius * cos(latiRadian1) * cos(longiRadian2);
  330. m_vertexes[index * offset + ] = ry + radius * sin(latiRadian1);
  331. m_vertexes[index * offset + ] = rz + radius * cos(latiRadian1) * sin(longiRadian2);
  332. m_vertexes[index * offset + ] = 1.0f;
  333. // 上半球
  334. m_vertexes[index * offset + ] = rx + radius * cos(latiRadian2) * cos(longiRadian2);
  335. m_vertexes[index * offset + ] = ry + radius * sin(latiRadian2);
  336. m_vertexes[index * offset + ] = rz + radius * cos(latiRadian2) * sin(longiRadian2);
  337. m_vertexes[index * offset + ] = 1.0f;
  338. // 上半球
  339. m_vertexes[index * offset + ] = rx + radius * cos(latiRadian2) * cos(longiRadian1);
  340. m_vertexes[index * offset + ] = ry + radius * sin(latiRadian2);
  341. m_vertexes[index * offset + ] = rz + radius * cos(latiRadian2) * sin(longiRadian1);
  342. m_vertexes[index * offset + ] = 1.0f;
  343. // 下半球
  344. m_vertexes[stride + index * offset + ] = m_vertexes[index * offset + ];
  345. m_vertexes[stride + index * offset + ] = - * m_vertexes[index * offset + ];
  346. m_vertexes[stride + index * offset + ] = m_vertexes[index * offset + ];
  347. m_vertexes[stride + index * offset + ] = 1.0f;
  348. // 下半球
  349. m_vertexes[stride + index * offset + ] = m_vertexes[index * offset + ];
  350. m_vertexes[stride + index * offset + ] = - * m_vertexes[index * offset + ];
  351. m_vertexes[stride + index * offset + ] = m_vertexes[index * offset + ];
  352. m_vertexes[stride + index * offset + ] = 1.0f;
  353. // 下半球
  354. m_vertexes[stride + index * offset + ] = m_vertexes[index * offset + ];
  355. m_vertexes[stride + index * offset + ] = - * m_vertexes[index * offset + ];
  356. m_vertexes[stride + index * offset + ] = m_vertexes[index * offset + ];
  357. m_vertexes[stride + index * offset + ] = 1.0f;
  358. // 下半球
  359. m_vertexes[stride + index * offset + ] = m_vertexes[index * offset + ];
  360. m_vertexes[stride + index * offset + ] = - * m_vertexes[index * offset + ];
  361. m_vertexes[stride + index * offset + ] = m_vertexes[index * offset + ];
  362. m_vertexes[stride + index * offset + ] = 1.0f;
  363.  
  364. // color
  365. //上半球
  366. m_colors[index * offset + ] = 1.0f;
  367. m_colors[index * offset + ] = 1.0f;
  368. m_colors[index * offset + ] = 1.0f;
  369. m_colors[index * offset + ] = 0.6f;
  370. m_colors[index * offset + ] = 1.0f;
  371. m_colors[index * offset + ] = 1.0f;
  372. m_colors[index * offset + ] = 1.0f;
  373. m_colors[index * offset + ] = 0.6f;
  374. m_colors[index * offset + ] = 1.0f;
  375. m_colors[index * offset + ] = 1.0f;
  376. m_colors[index * offset + ] = 1.0f;
  377. m_colors[index * offset + ] = 0.6f;
  378. m_colors[index * offset + ] = 1.0f;
  379. m_colors[index * offset + ] = 1.0f;
  380. m_colors[index * offset + ] = 1.0f;
  381. m_colors[index * offset + ] = 0.6f;
  382. // 下半球
  383. m_colors[stride + index * offset + ] = 1.0f;
  384. m_colors[stride + index * offset + ] = 1.0f;
  385. m_colors[stride + index * offset + ] = 1.0f;
  386. m_colors[stride + index * offset + ] = 0.6f;
  387. m_colors[stride + index * offset + ] = 1.0f;
  388. m_colors[stride + index * offset + ] = 1.0f;
  389. m_colors[stride + index * offset + ] = 1.0f;
  390. m_colors[stride + index * offset + ] = 0.6f;
  391. m_colors[stride + index * offset + ] = 1.0f;
  392. m_colors[stride + index * offset + ] = 1.0f;
  393. m_colors[stride + index * offset + ] = 1.0f;
  394. m_colors[stride + index * offset + ] = 0.6f;
  395. m_colors[stride + index * offset + ] = 1.0f;
  396. m_colors[stride + index * offset + ] = 1.0f;
  397. m_colors[stride + index * offset + ] = 1.0f;
  398. m_colors[stride + index * offset + ] = 0.6f;
  399.  
  400. // texture
  401. float startU = (m_sphereLongitudeCount - longiIndex) * unitU;
  402. float startV = (m_sphereLatitudeCount / - latiIndex) * unitV;
  403. // 上半球
  404. m_texcoordes[index * textureOffset + ] = startU - unitU;
  405. m_texcoordes[index * textureOffset + ] = startV;
  406. m_texcoordes[index * textureOffset + ] = startU;
  407. m_texcoordes[index * textureOffset + ] = startV;
  408. m_texcoordes[index * textureOffset + ] = startU;
  409. m_texcoordes[index * textureOffset + ] = (startV + unitV);
  410. m_texcoordes[index * textureOffset + ] = startU - unitU;
  411. m_texcoordes[index * textureOffset + ] = (startV + unitV);
  412. // 下半球
  413. m_texcoordes[textureStride + index * textureOffset + ] = startU;
  414. m_texcoordes[textureStride + index * textureOffset + ] = 1.0f - (startV + unitV);
  415. m_texcoordes[textureStride + index * textureOffset + ] = startU - unitU;
  416. m_texcoordes[textureStride + index * textureOffset + ] = 1.0f - (startV + unitV);
  417. m_texcoordes[textureStride + index * textureOffset + ] = startU - unitU;;
  418. m_texcoordes[textureStride + index * textureOffset + ] = 1.0f - startV;
  419. m_texcoordes[textureStride + index * textureOffset + ] = startU;
  420. m_texcoordes[textureStride + index * textureOffset + ] = 1.0f - startV;
  421.  
  422. m_indexes[vertexIndex++] = index * offset / ;
  423. m_indexes[vertexIndex++] = index * offset / + ;
  424. m_indexes[vertexIndex++] = index * offset / + ;
  425. m_indexes[vertexIndex++] = index * offset / + ;
  426. m_indexes[vertexIndex++] = (stride + index * offset) / ;
  427. m_indexes[vertexIndex++] = (stride + index * offset) / + ;
  428. m_indexes[vertexIndex++] = (stride + index * offset) / + ;
  429. m_indexes[vertexIndex++] = (stride + index * offset) / + ;
  430.  
  431. m_vertexCount += ;
  432. m_indexCount += ;
  433. }
  434. }
  435. }
  436.  
  437. void Sphere::generateVertexesSphereNew(float radius)
  438. {
  439. unsigned int quadCount = m_sphereLatitudeCount * m_sphereLongitudeCount;
  440. unsigned int vertexCount = (m_sphereLatitudeCount + ) * (m_sphereLongitudeCount + );
  441.  
  442. m_vertexes = new GLfloat[vertexCount * ];
  443. m_colors = new GLfloat[vertexCount * ];
  444. m_texcoordes = new GLfloat[vertexCount * ];
  445. m_indexes = new GLuint[quadCount * ];
  446.  
  447. float unitU = m_sphereLongitude / 360.0f;
  448. float unitV = m_sphereLatitude / 180.0f;
  449.  
  450. for (unsigned int latiIndex = ; latiIndex <= m_sphereLatitudeCount; ++latiIndex)
  451. {
  452. // 纬度
  453. float latiAngle = m_sphereLatitude * latiIndex;
  454. float latiRadian = A_TO_R(latiAngle);
  455.  
  456. latiRadian = HMPI - latiRadian;
  457.  
  458. for (unsigned int longiIndex = ; longiIndex <= m_sphereLongitudeCount; ++longiIndex)
  459. {
  460. // 经度
  461. float longiAngle = m_sphereLongitude * longiIndex;
  462. float longiRadian = A_TO_R(longiAngle);
  463.  
  464. unsigned int index = latiIndex * (m_sphereLongitudeCount + ) + longiIndex;
  465.  
  466. m_vertexes[index * ] = m_sphereRadius * cos(latiRadian) * sin(longiRadian);
  467. m_vertexes[index * + ] = m_sphereRadius * sin(latiRadian);
  468. m_vertexes[index * + ] = m_sphereRadius * cos(latiRadian) * cos(longiRadian);
  469. m_vertexes[index * + ] = 1.0f;
  470.  
  471. m_colors[index * ] = 1.0f;
  472. m_colors[index * + ] = 1.0f;
  473. m_colors[index * + ] = 1.0f;
  474. m_colors[index * + ] = 1.0f;
  475.  
  476. m_texcoordes[index * ] = longiIndex * unitU;
  477. m_texcoordes[index * + ] = latiIndex * unitV;
  478. }
  479. }
  480. m_vertexCount = vertexCount;
  481.  
  482. for (unsigned int i = ; i < m_sphereLatitudeCount; ++i)
  483. {
  484. for (unsigned int j = ; j < m_sphereLongitudeCount; ++j)
  485. {
  486. unsigned index = i * m_sphereLongitudeCount + j;
  487.  
  488. m_indexes[index * ] = i * (m_sphereLongitudeCount + ) + j;
  489. m_indexes[index * + ] = i * (m_sphereLongitudeCount + ) + j + ;
  490. m_indexes[index * + ] = (i + ) * (m_sphereLongitudeCount + ) + j + ;
  491. m_indexes[index * + ] = (i + ) * (m_sphereLongitudeCount + ) + j;
  492. }
  493. }
  494. m_indexCount = quadCount * ;
  495. }
  496.  
  497. void Sphere::generateTexture()
  498. {
  499. CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("images/diqiu.jpg");
  500. if (texture)
  501. m_textureName = texture->getName();
  502. else
  503. assert(, "create texture failed");
  504. }
  505.  
  506. void Sphere::generateVAO()
  507. {
  508. glGenVertexArrays(, &m_sphereVAO);
  509. if (m_sphereVAO > )
  510. {
  511. glBindVertexArray(m_sphereVAO);
  512.  
  513. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_sphereIBO);
  514.  
  515. glEnableVertexAttribArray();
  516. glEnableVertexAttribArray();
  517. glEnableVertexAttribArray();
  518.  
  519. glBindBuffer(GL_ARRAY_BUFFER, m_sphereVertexVBO);
  520. glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, , BUFFER_OFFSET());
  521. glBindBuffer(GL_ARRAY_BUFFER, m_sphereColorVBO);
  522. glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, , BUFFER_OFFSET());
  523. glBindBuffer(GL_ARRAY_BUFFER, m_sphereTextureVBO);
  524. glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, , BUFFER_OFFSET());
  525.  
  526. CHECK_GL_ERROR_DEBUG();
  527. }
  528. else
  529. {
  530. assert();
  531. }
  532.  
  533. glBindVertexArray();
  534. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, );
  535. glBindBuffer(GL_ARRAY_BUFFER, );
  536. }
  537.  
  538. void Sphere::generateIBO()
  539. {
  540. glGenBuffers(, &m_sphereIBO);
  541. if (m_sphereIBO > )
  542. {
  543. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_sphereIBO);
  544. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * m_indexCount, m_indexes, GL_STATIC_DRAW);
  545.  
  546. CHECK_GL_ERROR_DEBUG();
  547.  
  548. delete[] m_indexes;
  549. m_indexes = nullptr;
  550. }
  551.  
  552. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, );
  553. }
  554.  
  555. void Sphere::generateVBO()
  556. {
  557. size_t dataSize = sizeof(GLfloat) * m_vertexCount * ;
  558. GLuint vbos[] = { , , };
  559.  
  560. glGenBuffers(, vbos);
  561. if (vbos[] > )
  562. {
  563. m_sphereVertexVBO = vbos[];
  564.  
  565. glBindBuffer(GL_ARRAY_BUFFER, m_sphereVertexVBO);
  566. glBufferData(GL_ARRAY_BUFFER, dataSize, m_vertexes, GL_STATIC_DRAW);
  567.  
  568. delete[] m_vertexes;
  569. m_vertexes = nullptr;
  570.  
  571. CHECK_GL_ERROR_DEBUG();
  572. }
  573. else
  574. {
  575. assert();
  576. }
  577. if (vbos[] > )
  578. {
  579. m_sphereColorVBO = vbos[];
  580.  
  581. glBindBuffer(GL_ARRAY_BUFFER, m_sphereColorVBO);
  582. glBufferData(GL_ARRAY_BUFFER, dataSize, m_colors, GL_STATIC_DRAW);
  583.  
  584. delete[] m_colors;
  585. m_colors = nullptr;
  586.  
  587. CHECK_GL_ERROR_DEBUG();
  588. }
  589. if (vbos[] > )
  590. {
  591. m_sphereTextureVBO = vbos[];
  592.  
  593. glBindBuffer(GL_ARRAY_BUFFER, m_sphereTextureVBO);
  594. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * m_vertexCount * , m_texcoordes, GL_STATIC_DRAW);
  595.  
  596. delete[] m_texcoordes;
  597. m_texcoordes = nullptr;
  598.  
  599. CHECK_GL_ERROR_DEBUG();
  600. }
  601.  
  602. glBindBuffer(GL_ARRAY_BUFFER, );
  603. }
  604.  
  605. void Sphere::drawBg(const Mat4& transform)
  606. {
  607. CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("images/bg.jpg");
  608. GL::bindTexture2D(texture->getName());
  609. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  610. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  611. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  612. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  613.  
  614. // GLProgram* program = CCShaderCache::sharedShaderCache()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE);
  615. m_program->use();
  616. m_program->setUniformsForBuiltins(transform);
  617.  
  618. CCSize& size = CCDirector::sharedDirector()->getVisibleSize();
  619. GLfloat vertexes[] = {
  620. 0.0f, 0.0f, 0.0f, 1.0f,
  621. 0.0f, , 0.0f, 1.0f,
  622. , , 0.0f, 1.0f,
  623. , 0.0f, 0.0f, 1.0f
  624. };
  625. GLfloat colores[] = {
  626. 1.0f, 1.0f, 1.0f, 1.0f,
  627. 1.0f, 1.0f, 1.0f, 1.0f,
  628. 1.0f, 1.0f, 1.0f, 1.0f,
  629. 1.0f, 1.0f, 1.0f, 1.0f
  630. };
  631. GLfloat texCoordes[] = {
  632. 0.0f, 1.0f,
  633. 0.0f, 0.0f,
  634. 1.0f, 0.0f,
  635. 1.0f, 1.0f
  636. };
  637.  
  638. glEnableVertexAttribArray();
  639. glEnableVertexAttribArray();
  640. glEnableVertexAttribArray();
  641. glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, , vertexes);
  642. glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, , colores);
  643. glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, , texCoordes);
  644.  
  645. glDrawArrays(GL_QUADS, , );
  646.  
  647. glDisableVertexAttribArray();
  648. glDisableVertexAttribArray();
  649. glDisableVertexAttribArray();
  650.  
  651. CHECK_GL_ERROR_DEBUG();
  652. }
  653.  
  654. void Sphere::drawSphere(const Mat4& transform, GLfloat* vertexes)
  655. {
  656. GL::bindTexture2D(m_textureName);
  657. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  658. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  659. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  660. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  661.  
  662. Mat4 _transform;
  663. _transform.setIdentity();
  664. _transform.translate(m_sphereTranslateX, m_sphereTranslateY, 0.0f);
  665. _transform.multiply(m_sphereTransform);
  666.  
  667. m_program->use();
  668. m_program->setUniformsForBuiltins(_transform);
  669.  
  670. CHECK_GL_ERROR_DEBUG();
  671.  
  672. glEnableVertexAttribArray();
  673. glEnableVertexAttribArray();
  674. glEnableVertexAttribArray();
  675.  
  676. if (m_sphereVAO == )
  677. {
  678. glBindBuffer(GL_ARRAY_BUFFER, m_sphereVertexVBO);
  679. glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, , BUFFER_OFFSET());
  680. glBindBuffer(GL_ARRAY_BUFFER, m_sphereColorVBO);
  681. glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, , BUFFER_OFFSET());
  682. glBindBuffer(GL_ARRAY_BUFFER, m_sphereTextureVBO);
  683. glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, , BUFFER_OFFSET());
  684. glBindBuffer(GL_ARRAY_BUFFER, );
  685.  
  686. CHECK_GL_ERROR_DEBUG();
  687.  
  688. if (m_sphereIBO == )
  689. {
  690. if (m_indexes)
  691. glDrawElements(GL_QUADS, m_indexCount, GL_UNSIGNED_INT, m_indexes);
  692. else
  693. glDrawArrays(GL_QUADS, , m_vertexCount);
  694. }
  695. else
  696. {
  697. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_sphereIBO);
  698. glDrawElements(GL_QUADS, m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET());
  699. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, );
  700. }
  701. }
  702. else
  703. {
  704. glBindVertexArray(m_sphereVAO);
  705. glDrawElements(GL_QUADS, m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET());
  706. glBindVertexArray();
  707. }
  708.  
  709. glDisableVertexAttribArray();
  710. glDisableVertexAttribArray();
  711. glDisableVertexAttribArray();
  712.  
  713. CHECK_GL_ERROR_DEBUG();
  714. }
  715.  
  716. void Sphere::drawPlanView(const Mat4& transform, GLfloat* vertexes)
  717. {
  718. GL::bindTexture2D(m_textureName);
  719. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  720. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  721. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  722. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  723.  
  724. Mat4 _transform;
  725. _transform.setIdentity();
  726. //_transform.translate(m_planViewTranslateX, m_planViewTranslateY, 0.0f);
  727. _transform.multiply(m_sphereTransform);
  728.  
  729. m_program_planview->use();
  730. GLfloat _translate[] = { m_planViewTranslateX, m_planViewTranslateY };
  731. GLuint location_radius = m_program_planview->getUniformLocation("radius");
  732. m_program_planview->setUniformLocationWith1f(location_radius, m_sphereRadius);
  733. GLuint location_translate = m_program_planview->getUniformLocation("translate");
  734. m_program_planview->setUniformLocationWith2f(location_translate, _translate[], _translate[]);
  735. m_program_planview->setUniformsForBuiltins(_transform);
  736.  
  737. /*
  738. m_program->use();
  739. m_program->setUniformsForBuiltins(_transform);
  740. */
  741.  
  742. glEnableVertexAttribArray();
  743. glEnableVertexAttribArray();
  744. glEnableVertexAttribArray();
  745.  
  746. if (m_sphereVAO == )
  747. {
  748. glBindBuffer(GL_ARRAY_BUFFER, m_sphereVertexVBO);
  749. glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, , );
  750. glBindBuffer(GL_ARRAY_BUFFER, m_sphereColorVBO);
  751. glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, , );
  752. glBindBuffer(GL_ARRAY_BUFFER, m_sphereTextureVBO);
  753. glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, , );
  754. glBindBuffer(GL_ARRAY_BUFFER, );
  755.  
  756. if (m_sphereIBO == )
  757. {
  758. if (m_indexes)
  759. glDrawElements(GL_QUADS, m_indexCount, GL_UNSIGNED_INT, m_indexes);
  760. else
  761. glDrawArrays(GL_QUADS, , m_vertexCount);
  762. }
  763. else
  764. {
  765. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_sphereIBO);
  766. glDrawElements(GL_QUADS, m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET());
  767. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, );
  768. }
  769. }
  770. else
  771. {
  772. glBindVertexArray(m_sphereVAO);
  773. glDrawElements(GL_QUADS, m_indexCount, GL_UNSIGNED_INT, BUFFER_OFFSET());
  774. glBindVertexArray();
  775. }
  776.  
  777. glDisableVertexAttribArray();
  778. glDisableVertexAttribArray();
  779. glDisableVertexAttribArray();
  780.  
  781. CHECK_GL_ERROR_DEBUG();
  782. }
  783.  
  784. void Sphere::draw(Renderer *renderer, const Mat4& transform, uint32_t flags)
  785. {
  786. glEnable(GL_CULL_FACE);
  787. glFrontFace(GL_CW);
  788.  
  789. GL::blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED.src, BlendFunc::ALPHA_NON_PREMULTIPLIED.dst);
  790.  
  791. this->drawBg(transform);
  792.  
  793. {
  794. if (mx_transform_sphere.try_lock())
  795. {
  796. this->drawSphere(transform, m_vertexes);
  797.  
  798. mx_transform_sphere.unlock();
  799. }
  800. }
  801.  
  802. {
  803. if (mx_transform_planview.try_lock())
  804. {
  805. this->drawPlanView(transform, m_vertexes);
  806.  
  807. mx_transform_planview.unlock();
  808. }
  809. }
  810.  
  811. glEnableVertexAttribArray();
  812. glEnableVertexAttribArray();
  813. glEnableVertexAttribArray();
  814. }
  815.  
  816. void Sphere::visit(Renderer *renderer, const Mat4& parentTransform, uint32_t parentFlags)
  817. {
  818. this->update();
  819.  
  820. Node::visit(renderer, parentTransform, parentFlags);
  821. }
  822.  
  823. void Sphere::transformSphere(float touchOffsetX, float touchOffsetY)
  824. {
  825. m_rotateYAngle += touchOffsetX / m_spherePerimeter * 360.0f;
  826. // m_rotateXAngle -= touchOffsetY / m_spherePerimeter * 360.0f;
  827. // m_rotateZAngle -= touchOffsetX / m_spherePerimeter * 360.0f;
  828.  
  829. if (m_rotateYAngle >= 90.0f && m_rotateYAngle <= 270.0f)
  830. m_rotateXAngle += touchOffsetY / m_spherePerimeter * 360.0f;
  831. else
  832. m_rotateXAngle -= touchOffsetY / m_spherePerimeter * 360.0f;
  833.  
  834. if (m_rotateYAngle < 0.0f)
  835. m_rotateYAngle += 360.0f;
  836. if (m_rotateYAngle > 360.0f)
  837. m_rotateYAngle -= 360.0f;
  838.  
  839. if (m_rotateXAngle < 0.0f)
  840. m_rotateXAngle += 360.0f;
  841. if (m_rotateXAngle >= 360.0f)
  842. m_rotateXAngle -= 360.0f;
  843.  
  844. /*
  845. cocos2d::log("m_rotateXAngle:%f", m_rotateXAngle);
  846. cocos2d::log("m_rotateYAngle:%f", m_rotateYAngle);
  847. cocos2d::log("m_rotateZAngle:%f", m_rotateZAngle);
  848. */
  849.  
  850. Quaternion quat1;
  851. this->createQuaternion(m_rotateXAngle, , , quat1);
  852. Quaternion quat2;
  853. this->createQuaternion(, m_rotateYAngle, , quat2);
  854. Quaternion quat3;
  855. this->createQuaternion(, , m_rotateZAngle, quat3);
  856. Quaternion quat4;
  857. this->createQuaternion(m_rotateXAngle, m_rotateYAngle, m_rotateZAngle, quat4);
  858.  
  859. m_sphereTransform.setIdentity();
  860. //m_sphereTransform.rotate(quat4);
  861. //m_sphereTransform.rotate(quat3);
  862. //m_sphereTransform.rotate(quat2);
  863. //m_sphereTransform.rotate(quat1);
  864. m_sphereTransform.rotate(quat3);
  865. m_sphereTransform.rotate(quat2);
  866. m_sphereTransform.rotate(quat1);
  867.  
  868. // m_sphereTransform.rotateX(A_TO_R(m_rotateXAngle));
  869. // m_sphereTransform.rotateY(A_TO_R(m_rotateYAngle));
  870. }
  871.  
  872. void Sphere::createQuaternion(float rotateX, float rotateY, float rotateZ, Quaternion& quat)
  873. {
  874. float halfRadx = CC_DEGREES_TO_RADIANS(rotateX / .f), halfRady = CC_DEGREES_TO_RADIANS(rotateY / .f), halfRadz = CC_DEGREES_TO_RADIANS(rotateZ / .f);
  875. float coshalfRadx = cosf(halfRadx), sinhalfRadx = sinf(halfRadx), coshalfRady = cosf(halfRady), sinhalfRady = sinf(halfRady), coshalfRadz = cosf(halfRadz), sinhalfRadz = sinf(halfRadz);
  876. quat.x = sinhalfRadx * coshalfRady * coshalfRadz - coshalfRadx * sinhalfRady * sinhalfRadz;
  877. quat.y = coshalfRadx * sinhalfRady * coshalfRadz + sinhalfRadx * coshalfRady * sinhalfRadz;
  878. quat.z = coshalfRadx * coshalfRady * sinhalfRadz - sinhalfRadx * sinhalfRady * coshalfRadz;
  879. quat.w = coshalfRadx * coshalfRady * coshalfRadz + sinhalfRadx * sinhalfRady * sinhalfRadz;
  880. }
  881.  
  882. void Sphere::update()
  883. {
  884. if (m_isContinue)
  885. {
  886. float _rotateX = fabsf(m_continueRotateX);
  887. float _rotateY = fabsf(m_continueRotateY);
  888.  
  889. _rotateX -= m_decreateDetal;
  890. if (_rotateX < 0.0f)
  891. m_continueRotateX = 0.0f;
  892. else
  893. m_continueRotateX += (m_continueRotateX >= 0.0f) ? (-m_decreateDetal) : m_decreateDetal;
  894.  
  895. _rotateY -= m_decreateDetal;
  896. if (_rotateY < 0.0f)
  897. m_continueRotateY = 0.0f;
  898. else
  899. m_continueRotateY += (m_continueRotateY >= 0.0f) ? (-m_decreateDetal) : m_decreateDetal;
  900.  
  901. if (m_continueRotateX == 0.0f && m_continueRotateY == 0.0f)
  902. {
  903. m_isContinue = false;
  904. }
  905. else
  906. {
  907. this->transformSphere(m_continueRotateY, m_continueRotateX);
  908.  
  909. // this->noticeToTransform(m_continueRotateY, m_continueRotateX);
  910. }
  911. }
  912. }
  913.  
  914. void Sphere::onThread1Proc()
  915. {
  916. do {
  917. if (m_isThread1Done)
  918. break;
  919.  
  920. float x = 0.0f, y = 0.0f;
  921. {
  922. std::lock_guard<std::mutex> lock(mx_transform_sphere);
  923.  
  924. if (b_transform_sphere)
  925. {
  926. x = _touchOffsetXCopy = _touchOffsetX;
  927. y = _touchOffsetYCopy = _touchOffsetY;
  928.  
  929. this->transformSphere(x, y);
  930.  
  931. _sphereTransformationCopy = m_sphereTransform;
  932.  
  933. b_transform_sphere = false;
  934. b_transform_planview = true;
  935. cv_transform_planview.notify_all();
  936. }
  937. else
  938. {
  939. std::this_thread::sleep_for(std::chrono::microseconds());
  940. }
  941. }
  942. }
  943. while ();
  944. }
  945.  
  946. void Sphere::onThread2Proc()
  947. {
  948. do {
  949. if (m_isThread2Done)
  950. break;
  951.  
  952. {
  953. std::unique_lock<std::mutex> lock(mx_transform_planview);
  954. cv_transform_planview.wait(lock, [this](){
  955. return b_transform_planview;
  956. });
  957. b_transform_planview = false;
  958. }
  959.  
  960. {
  961. std::lock_guard<std::mutex> lock(mx_transform_planview);
  962. }
  963. }
  964. while ();
  965. }
  966.  
  967. void Sphere::noticeToTransform(float touchOffsetX, float touchOffsetY)
  968. {
  969. {
  970. std::lock_guard<std::mutex> lock(mx_transform_sphere);
  971.  
  972. _touchOffsetX = touchOffsetX;
  973. _touchOffsetY = touchOffsetY;
  974.  
  975. b_transform_sphere = true;
  976. }
  977. }
  978.  
  979. bool Sphere::onTouchBegan(Touch* touch, Event* event)
  980. {
  981. const cocos2d::Vec2& touchPoint = touch->getLocation();
  982.  
  983. float _x = touchPoint.x - m_sphereTranslateX, _y = touchPoint.y - m_sphereTranslateY;
  984. float _dis = _x * _x + _y * _y;
  985.  
  986. if (_dis <= m_sphereRadius * m_sphereRadius)
  987. {
  988. m_isContinue = false;
  989. return true;
  990. }
  991. else
  992. {
  993. return false;
  994. }
  995. }
  996.  
  997. void Sphere::onTouchMoved(Touch* touch, Event* event)
  998. {
  999. const cocos2d::Vec2& touchPoint = touch->getLocation();
  1000. const cocos2d::Vec2& preTouchPoint = touch->getPreviousLocation();
  1001. float touchOffsetX = touchPoint.x - preTouchPoint.x;
  1002. float touchOffsetY = touchPoint.y - preTouchPoint.y;
  1003.  
  1004. this->transformSphere(touchOffsetX, touchOffsetY);
  1005.  
  1006. // this->noticeToTransform(touchOffsetX, touchOffsetY);
  1007.  
  1008. m_continueRotateY = touchOffsetX / m_spherePerimeter * 360.0f;
  1009. m_continueRotateX = touchOffsetY / m_spherePerimeter * 360.0f;
  1010. }
  1011.  
  1012. void Sphere::onTouchEnded(Touch* touch, Event* event)
  1013. {
  1014. m_isContinue = true;
  1015. }

openGL 提升渲染性能 之 顶点数组 VBO IBO VAO的更多相关文章

  1. React爬坑秘籍(一)——提升渲染性能

    React爬坑秘籍(一)--提升渲染性能 ##前言 来到腾讯实习后,有幸八月份开始了腾讯办公助手PC端的开发.因为办公助手主推的是移动端,所以导师也是大胆的让我们实习生来技术选型并开发,他来做code ...

  2. 3D Computer Grapihcs Using OpenGL - 19 Vertex Array Object(顶点数组对象)

    大部分OpenGL教程都会在一开始就讲解VAO,但是该教程的作者认为这是很不合理的,因为要理解它的作用需要建立在我们此前学过的知识基础上.因此直到教程已经进行了一大半,作者才引入VAO这个概念.在我看 ...

  3. OpenGL(十八) 顶点数组和抗锯齿(反走样)设置

    顶点数组函数可以在一个数组里包含大量的与顶点相关的数据,并且可以减少函数的调用.使用顶点数组需要先启用顶点数组功能,使用glEnableClientState函数启用顶点数组,参数可以是GL_VERT ...

  4. Opengl ES之VBO和VAO

    前言 本文主要介绍了什么是VBO/VAO,为什么需要使用VBO/VAO以及如何使用VBO和VAO. VBO 什么是VBO VBO(vertex Buffer Object):顶点缓冲对象.是在显卡存储 ...

  5. 3D硬件加速提升动画性能 与 z-index属性

    目录 1. chrome Layer borders 2. 层创建标准 3. 例子 总结 1. chrome Layer borders <WebKit技术内幕>第二章介绍了网页的结构,其 ...

  6. WebGL2系列之顶点数组对象

    使用了顶点缓冲技术后,绘制效率有了较大的提升.但是还有一点不尽如人意,那就是顶点的位置坐标.法向量.纹理坐标等不同方面的数据每次使用时需要单独指定,重复了一些不必要的工作.WebGL2提供了一种专门用 ...

  7. VBO、VAO和EBO

    Vertex Buffer Object 对于经历过fixed pipeline的我来讲,VBO的出现对于渲染性能提升让人记忆深刻.完了,暴露年龄了~ //immediate mode glBegin ...

  8. OpenGL中glVertex、显示列表(glCallList)、顶点数组(Vertex array)、VBO及VAO区别

    OpenGL中glVertex.显示列表(glCallList).顶点数组(Vertex array).VBO及VAO区别 1.glVertex 最原始的设置顶点方法,在glBegin和glEnd之间 ...

  9. [转]OpenGL通过VBO实现顶点数组绘制顶点

    #include "stdlib.h" #include <OpenGL/glext.h> #include <GLUT/GLUT.h> #define B ...

随机推荐

  1. C#属性访问器

    属性的访问器包含与获取或设置属性有关的可执行语句.访问器声明可以包含 get 访问器或 set 访问器,或者两者均包含.声明采用下列形式之一:get {}set {} get 访问器get 访问器体与 ...

  2. iphone dev 入门实例2:Pass Data Between View Controllers using segue

    Assigning View Controller Class In the first tutorial, we simply create a view controller that serve ...

  3. Segment fault及LINUX core dump详解 (zz)

    C 程序在进行中发生segment fault(core dump)错误,通常与内存操作不当有关,主要有以下几种情况: (1)数组越界. (2)修改了只读内存. (3)scanf("%d&q ...

  4. PLSQL_性能优化工具系列09_SQL Plan Management

    2014-09-24 Created By BaoXinjian

  5. unity jiaoben

    transform.Translate(Input.GetAxis("Horizontal")*Time.deltaTime,0,0); 移动 transform.Translat ...

  6. Good Sentences

    Wine in, truth out One is never too old to learn What is done can not be undone Time tries all thing ...

  7. asp.net读取txt并导入数据库

    源地址:http://www.cnblogs.com/hfzsjz/p/3214649.html

  8. [ActionScript 3.0] AS3 GUID(全局唯一标识符)

    package com.controls { import flash.display.Sprite; import flash.system.Capabilities; public class G ...

  9. try-catch中的finally块

    finally块定义在catch的最后,只能出现一次. 无论程序是否出错都会执行的快板!无条件执行

  10. angularjs之表达式

    一:angularjs表达式的解析 angularjs会在运行$digest循环中自动解析表达式,但有时手动解析表达式也是非常用用的. angularjs通过$parse这个内部服务来进行表达式的运算 ...