2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2013 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* IMPORTANT IMPORTANT IMPORTANT IMPORTANT
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* LEGACY FUNCTIONS
|
|
|
|
*
|
2013-06-20 14:13:12 +08:00
|
|
|
* USE DrawNode instead
|
2012-11-14 18:05:15 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCDrawingPrimitives.h"
|
2014-02-08 11:37:44 +08:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <cmath>
|
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccTypes.h"
|
|
|
|
#include "base/ccMacros.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCGL.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "renderer/CCGLProgramCache.h"
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/CCGLProgram.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCActionCatmullRom.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
#ifndef M_PI
|
|
|
|
#define M_PI 3.14159265358979323846
|
|
|
|
#endif
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
namespace DrawPrimitives {
|
|
|
|
|
2013-07-26 05:49:43 +08:00
|
|
|
static bool s_initialized = false;
|
2013-12-18 17:47:20 +08:00
|
|
|
static GLProgram* s_shader = nullptr;
|
2013-07-26 05:49:43 +08:00
|
|
|
static int s_colorLocation = -1;
|
|
|
|
static Color4F s_color(1.0f,1.0f,1.0f,1.0f);
|
|
|
|
static int s_pointSizeLocation = -1;
|
|
|
|
static GLfloat s_pointSize = 1.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-04-23 09:08:25 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
static GLuint s_bufferObject = 0;
|
|
|
|
static GLuint s_bufferSize = 0;
|
|
|
|
|
|
|
|
static void setGLBufferData(void *buf, GLuint bufSize)
|
|
|
|
{
|
|
|
|
if(s_bufferSize < bufSize)
|
|
|
|
{
|
|
|
|
if(s_bufferObject)
|
|
|
|
{
|
|
|
|
glDeleteBuffers(1, &s_bufferObject);
|
|
|
|
}
|
|
|
|
glGenBuffers(1, &s_bufferObject);
|
|
|
|
s_bufferSize = bufSize;
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, s_bufferObject);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, bufSize, buf, GL_DYNAMIC_DRAW);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, s_bufferObject);
|
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, 0, bufSize, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // EMSCRIPTEN
|
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
static void lazy_init()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 05:49:43 +08:00
|
|
|
if( ! s_initialized ) {
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//
|
2012-09-16 05:19:14 +08:00
|
|
|
// Position and 1 color passed as a uniform (to simulate glColor4ub )
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
2014-05-10 09:39:25 +08:00
|
|
|
s_shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR);
|
2013-07-26 05:49:43 +08:00
|
|
|
s_shader->retain();
|
2013-02-27 09:38:30 +08:00
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
s_colorLocation = s_shader->getUniformLocation("u_color");
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
s_pointSizeLocation = s_shader->getUniformLocation("u_pointSize");
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 05:49:43 +08:00
|
|
|
s_initialized = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-16 10:18:06 +08:00
|
|
|
// When switching from backround to foreground on android, we want the params to be initialized again
|
2013-07-26 09:42:53 +08:00
|
|
|
void init()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-02-27 09:38:30 +08:00
|
|
|
lazy_init();
|
|
|
|
}
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void free()
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
2013-07-26 05:49:43 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(s_shader);
|
|
|
|
s_initialized = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
void drawPoint(const Vec2& point)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
lazy_init();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 p;
|
2012-04-19 14:35:52 +08:00
|
|
|
p.x = point.x;
|
|
|
|
p.y = point.y;
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
|
2013-07-26 05:49:43 +08:00
|
|
|
s_shader->use();
|
|
|
|
s_shader->setUniformsForBuiltins();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 05:49:43 +08:00
|
|
|
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
|
|
|
|
s_shader->setUniformLocationWith1f(s_pointSizeLocation, s_pointSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-04-09 12:06:36 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
2013-04-23 09:08:25 +08:00
|
|
|
setGLBufferData(&p, 8);
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-23 09:08:25 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, &p);
|
2013-04-09 12:06:36 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2013-04-23 09:08:25 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
glDrawArrays(GL_POINTS, 0, 1);
|
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,1);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void drawPoints( const Vec2 *points, unsigned int numberOfPoints )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
lazy_init();
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
|
2013-07-26 05:49:43 +08:00
|
|
|
s_shader->use();
|
|
|
|
s_shader->setUniformsForBuiltins();
|
|
|
|
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
|
|
|
|
s_shader->setUniformLocationWith1f(s_pointSizeLocation, s_pointSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// XXX: Mac OpenGL error. arrays can't go out of scope before draw is executed
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2* newPoints = new Vec2[numberOfPoints];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// iPhone and 32-bit machines optimization
|
2014-05-15 01:07:09 +08:00
|
|
|
if( sizeof(Vec2) == sizeof(Vec2) )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-04-23 09:08:25 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
2014-05-15 01:07:09 +08:00
|
|
|
setGLBufferData((void*) points, numberOfPoints * sizeof(Vec2));
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-23 09:08:25 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, points);
|
2013-04-23 09:08:25 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Mac on 64-bit
|
|
|
|
for( unsigned int i=0; i<numberOfPoints;i++) {
|
|
|
|
newPoints[i].x = points[i].x;
|
|
|
|
newPoints[i].y = points[i].y;
|
|
|
|
}
|
2013-04-09 12:06:36 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
2013-04-23 09:08:25 +08:00
|
|
|
// Suspect Emscripten won't be emitting 64-bit code for a while yet,
|
|
|
|
// but want to make sure this continues to work even if they do.
|
2014-05-15 01:07:09 +08:00
|
|
|
setGLBufferData(newPoints, numberOfPoints * sizeof(Vec2));
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-23 09:08:25 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, newPoints);
|
2013-04-09 12:06:36 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2013-04-23 09:08:25 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
glDrawArrays(GL_POINTS, 0, (GLsizei) numberOfPoints);
|
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(newPoints);
|
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, numberOfPoints);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
void drawLine(const Vec2& origin, const Vec2& destination)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
lazy_init();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 vertices[2] = {
|
|
|
|
Vec2(origin.x, origin.y),
|
|
|
|
Vec2(destination.x, destination.y)
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2013-07-26 05:49:43 +08:00
|
|
|
s_shader->use();
|
|
|
|
s_shader->setUniformsForBuiltins();
|
|
|
|
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
|
2013-04-09 12:06:36 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
2013-04-23 09:08:25 +08:00
|
|
|
setGLBufferData(vertices, 16);
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-23 09:08:25 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
|
2013-04-09 12:06:36 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-04-19 14:35:52 +08:00
|
|
|
glDrawArrays(GL_LINES, 0, 2);
|
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,2);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void drawRect( Vec2 origin, Vec2 destination )
|
2012-06-12 01:43:07 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
drawLine(Vec2(origin.x, origin.y), Vec2(destination.x, origin.y));
|
|
|
|
drawLine(Vec2(destination.x, origin.y), Vec2(destination.x, destination.y));
|
|
|
|
drawLine(Vec2(destination.x, destination.y), Vec2(origin.x, destination.y));
|
|
|
|
drawLine(Vec2(origin.x, destination.y), Vec2(origin.x, origin.y));
|
2012-06-12 01:43:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
void drawSolidRect(Vec2 origin, Vec2 destination, Color4F color)
|
2012-06-12 01:43:07 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 vertices[] = {
|
2012-06-12 01:43:07 +08:00
|
|
|
origin,
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2(destination.x, origin.y),
|
2012-06-12 01:43:07 +08:00
|
|
|
destination,
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2(origin.x, destination.y)
|
2012-06-12 01:43:07 +08:00
|
|
|
};
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
drawSolidPoly(vertices, 4, color );
|
2012-06-12 01:43:07 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
void drawPoly(const Vec2 *poli, unsigned int numberOfPoints, bool closePolygon)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
lazy_init();
|
|
|
|
|
2013-07-26 05:49:43 +08:00
|
|
|
s_shader->use();
|
|
|
|
s_shader->setUniformsForBuiltins();
|
|
|
|
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// iPhone and 32-bit machines optimization
|
2014-05-15 01:07:09 +08:00
|
|
|
if( sizeof(Vec2) == sizeof(Vec2) )
|
2012-09-14 17:54:53 +08:00
|
|
|
{
|
2013-04-23 09:08:25 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
2014-05-15 01:07:09 +08:00
|
|
|
setGLBufferData((void*) poli, numberOfPoints * sizeof(Vec2));
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-23 09:08:25 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, poli);
|
2013-04-23 09:08:25 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-14 17:54:53 +08:00
|
|
|
if( closePolygon )
|
|
|
|
glDrawArrays(GL_LINE_LOOP, 0, (GLsizei) numberOfPoints);
|
|
|
|
else
|
|
|
|
glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) numberOfPoints);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Mac on 64-bit
|
2012-09-14 17:54:53 +08:00
|
|
|
// XXX: Mac OpenGL error. arrays can't go out of scope before draw is executed
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2* newPoli = new Vec2[numberOfPoints];
|
2012-04-19 14:35:52 +08:00
|
|
|
for( unsigned int i=0; i<numberOfPoints;i++) {
|
|
|
|
newPoli[i].x = poli[i].x;
|
|
|
|
newPoli[i].y = poli[i].y;
|
|
|
|
}
|
2013-04-23 09:08:25 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
2014-05-15 01:07:09 +08:00
|
|
|
setGLBufferData(newPoli, numberOfPoints * sizeof(Vec2));
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-23 09:08:25 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, newPoli);
|
2013-04-23 09:08:25 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-14 17:54:53 +08:00
|
|
|
if( closePolygon )
|
|
|
|
glDrawArrays(GL_LINE_LOOP, 0, (GLsizei) numberOfPoints);
|
|
|
|
else
|
|
|
|
glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) numberOfPoints);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-14 17:54:53 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(newPoli);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, numberOfPoints);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
void drawSolidPoly(const Vec2 *poli, unsigned int numberOfPoints, Color4F color)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
lazy_init();
|
|
|
|
|
2013-07-26 05:49:43 +08:00
|
|
|
s_shader->use();
|
|
|
|
s_shader->setUniformsForBuiltins();
|
|
|
|
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &color.r, 1);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// XXX: Mac OpenGL error. arrays can't go out of scope before draw is executed
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2* newPoli = new Vec2[numberOfPoints];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// iPhone and 32-bit machines optimization
|
2014-05-28 17:41:34 +08:00
|
|
|
if (sizeof(Vec2) == sizeof(Vec2))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-04-23 09:08:25 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
2014-05-15 01:07:09 +08:00
|
|
|
setGLBufferData((void*) poli, numberOfPoints * sizeof(Vec2));
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-23 09:08:25 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, poli);
|
2013-04-23 09:08:25 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Mac on 64-bit
|
2014-05-28 17:41:34 +08:00
|
|
|
for(unsigned int i = 0; i < numberOfPoints; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
newPoli[i] = Vec2( poli[i].x, poli[i].y );
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-04-23 09:08:25 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
2014-05-15 01:07:09 +08:00
|
|
|
setGLBufferData(newPoli, numberOfPoints * sizeof(Vec2));
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-23 09:08:25 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, newPoli);
|
2013-04-23 09:08:25 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) numberOfPoints);
|
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(newPoli);
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, numberOfPoints);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void drawCircle( const Vec2& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
lazy_init();
|
|
|
|
|
|
|
|
int additionalSegment = 1;
|
|
|
|
if (drawLineToCenter)
|
|
|
|
additionalSegment++;
|
|
|
|
|
|
|
|
const float coef = 2.0f * (float)M_PI/segments;
|
|
|
|
|
|
|
|
GLfloat *vertices = (GLfloat*)calloc( sizeof(GLfloat)*2*(segments+2), 1);
|
|
|
|
if( ! vertices )
|
|
|
|
return;
|
|
|
|
|
|
|
|
for(unsigned int i = 0;i <= segments; i++) {
|
|
|
|
float rads = i*coef;
|
2012-09-14 18:00:11 +08:00
|
|
|
GLfloat j = radius * cosf(rads + angle) * scaleX + center.x;
|
|
|
|
GLfloat k = radius * sinf(rads + angle) * scaleY + center.y;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
vertices[i*2] = j;
|
|
|
|
vertices[i*2+1] = k;
|
|
|
|
}
|
|
|
|
vertices[(segments+1)*2] = center.x;
|
|
|
|
vertices[(segments+1)*2+1] = center.y;
|
|
|
|
|
2013-07-26 05:49:43 +08:00
|
|
|
s_shader->use();
|
|
|
|
s_shader->setUniformsForBuiltins();
|
|
|
|
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-04-09 12:06:36 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
2013-04-23 09:08:25 +08:00
|
|
|
setGLBufferData(vertices, sizeof(GLfloat)*2*(segments+2));
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-23 09:08:25 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
|
2013-04-09 12:06:36 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-04-19 14:35:52 +08:00
|
|
|
glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments+additionalSegment);
|
|
|
|
|
2013-07-25 22:38:55 +08:00
|
|
|
::free( vertices );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,segments+additionalSegment);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void drawCircle( const Vec2& center, float radius, float angle, unsigned int segments, bool drawLineToCenter)
|
2012-09-20 11:55:12 +08:00
|
|
|
{
|
2013-07-26 09:42:53 +08:00
|
|
|
drawCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f);
|
2012-09-20 11:55:12 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void drawSolidCircle( const Vec2& center, float radius, float angle, unsigned int segments, float scaleX, float scaleY)
|
2013-07-03 04:39:16 +08:00
|
|
|
{
|
|
|
|
lazy_init();
|
|
|
|
|
|
|
|
const float coef = 2.0f * (float)M_PI/segments;
|
|
|
|
|
|
|
|
GLfloat *vertices = (GLfloat*)calloc( sizeof(GLfloat)*2*(segments+2), 1);
|
|
|
|
if( ! vertices )
|
|
|
|
return;
|
|
|
|
|
|
|
|
for(unsigned int i = 0;i <= segments; i++) {
|
|
|
|
float rads = i*coef;
|
|
|
|
GLfloat j = radius * cosf(rads + angle) * scaleX + center.x;
|
|
|
|
GLfloat k = radius * sinf(rads + angle) * scaleY + center.y;
|
|
|
|
|
|
|
|
vertices[i*2] = j;
|
|
|
|
vertices[i*2+1] = k;
|
|
|
|
}
|
|
|
|
vertices[(segments+1)*2] = center.x;
|
|
|
|
vertices[(segments+1)*2+1] = center.y;
|
|
|
|
|
2013-07-26 05:49:43 +08:00
|
|
|
s_shader->use();
|
|
|
|
s_shader->setUniformsForBuiltins();
|
|
|
|
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
|
2013-07-03 04:39:16 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
|
2013-07-03 04:39:16 +08:00
|
|
|
|
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
setGLBufferData(vertices, sizeof(GLfloat)*2*(segments+2));
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-07-03 04:39:16 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
|
2013-07-03 04:39:16 +08:00
|
|
|
#endif // EMSCRIPTEN
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) segments+1);
|
|
|
|
|
2013-07-25 22:38:55 +08:00
|
|
|
::free( vertices );
|
2013-07-03 04:39:16 +08:00
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,segments+1);
|
2013-07-03 04:39:16 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void drawSolidCircle( const Vec2& center, float radius, float angle, unsigned int segments)
|
2013-07-03 04:39:16 +08:00
|
|
|
{
|
2013-07-26 09:42:53 +08:00
|
|
|
drawSolidCircle(center, radius, angle, segments, 1.0f, 1.0f);
|
2013-07-03 04:39:16 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void drawQuadBezier(const Vec2& origin, const Vec2& control, const Vec2& destination, unsigned int segments)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
lazy_init();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2* vertices = new Vec2[segments + 1];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
float t = 0.0f;
|
|
|
|
for(unsigned int i = 0; i < segments; i++)
|
|
|
|
{
|
|
|
|
vertices[i].x = powf(1 - t, 2) * origin.x + 2.0f * (1 - t) * t * control.x + t * t * destination.x;
|
|
|
|
vertices[i].y = powf(1 - t, 2) * origin.y + 2.0f * (1 - t) * t * control.y + t * t * destination.y;
|
|
|
|
t += 1.0f / segments;
|
|
|
|
}
|
|
|
|
vertices[segments].x = destination.x;
|
|
|
|
vertices[segments].y = destination.y;
|
|
|
|
|
2013-07-26 05:49:43 +08:00
|
|
|
s_shader->use();
|
|
|
|
s_shader->setUniformsForBuiltins();
|
|
|
|
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-04-09 12:06:36 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
2014-05-15 01:07:09 +08:00
|
|
|
setGLBufferData(vertices, (segments + 1) * sizeof(Vec2));
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-23 09:08:25 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
|
2013-04-09 12:06:36 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-04-19 14:35:52 +08:00
|
|
|
glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments + 1);
|
|
|
|
CC_SAFE_DELETE_ARRAY(vertices);
|
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,segments+1);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void drawCatmullRom( PointArray *points, unsigned int segments )
|
2012-06-12 01:43:07 +08:00
|
|
|
{
|
2013-07-26 09:42:53 +08:00
|
|
|
drawCardinalSpline( points, 0.5f, segments );
|
2012-06-12 01:43:07 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void drawCardinalSpline( PointArray *config, float tension, unsigned int segments )
|
2012-06-12 01:43:07 +08:00
|
|
|
{
|
|
|
|
lazy_init();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2* vertices = new Vec2[segments + 1];
|
2012-06-12 01:43:07 +08:00
|
|
|
|
2013-12-28 14:34:52 +08:00
|
|
|
ssize_t p;
|
2012-08-01 15:30:12 +08:00
|
|
|
float lt;
|
|
|
|
float deltaT = 1.0f / config->count();
|
2012-06-12 01:43:07 +08:00
|
|
|
|
|
|
|
for( unsigned int i=0; i < segments+1;i++) {
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float dt = (float)i / segments;
|
2012-06-12 01:43:07 +08:00
|
|
|
|
|
|
|
// border
|
|
|
|
if( dt == 1 ) {
|
|
|
|
p = config->count() - 1;
|
|
|
|
lt = 1;
|
|
|
|
} else {
|
|
|
|
p = dt / deltaT;
|
2012-08-01 15:30:12 +08:00
|
|
|
lt = (dt - deltaT * (float)p) / deltaT;
|
2012-06-12 01:43:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Interpolate
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 pp0 = config->getControlPointAtIndex(p-1);
|
|
|
|
Vec2 pp1 = config->getControlPointAtIndex(p+0);
|
|
|
|
Vec2 pp2 = config->getControlPointAtIndex(p+1);
|
|
|
|
Vec2 pp3 = config->getControlPointAtIndex(p+2);
|
2012-06-12 01:43:07 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 newPos = ccCardinalSplineAt( pp0, pp1, pp2, pp3, tension, lt);
|
2012-06-12 01:43:07 +08:00
|
|
|
vertices[i].x = newPos.x;
|
|
|
|
vertices[i].y = newPos.y;
|
|
|
|
}
|
|
|
|
|
2013-07-26 05:49:43 +08:00
|
|
|
s_shader->use();
|
|
|
|
s_shader->setUniformsForBuiltins();
|
|
|
|
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*)&s_color.r, 1);
|
2012-06-12 01:43:07 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
|
2012-06-12 01:43:07 +08:00
|
|
|
|
2013-04-09 12:06:36 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
2014-05-15 01:07:09 +08:00
|
|
|
setGLBufferData(vertices, (segments + 1) * sizeof(Vec2));
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-23 09:08:25 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
|
2013-04-09 12:06:36 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-06-12 01:43:07 +08:00
|
|
|
glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments + 1);
|
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(vertices);
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,segments+1);
|
2012-06-12 01:43:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void drawCubicBezier(const Vec2& origin, const Vec2& control1, const Vec2& control2, const Vec2& destination, unsigned int segments)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
lazy_init();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2* vertices = new Vec2[segments + 1];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
float t = 0;
|
2014-05-28 17:41:34 +08:00
|
|
|
for (unsigned int i = 0; i < segments; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
vertices[i].x = powf(1 - t, 3) * origin.x + 3.0f * powf(1 - t, 2) * t * control1.x + 3.0f * (1 - t) * t * t * control2.x + t * t * t * destination.x;
|
|
|
|
vertices[i].y = powf(1 - t, 3) * origin.y + 3.0f * powf(1 - t, 2) * t * control1.y + 3.0f * (1 - t) * t * t * control2.y + t * t * t * destination.y;
|
|
|
|
t += 1.0f / segments;
|
|
|
|
}
|
|
|
|
vertices[segments].x = destination.x;
|
|
|
|
vertices[segments].y = destination.y;
|
|
|
|
|
2013-07-26 05:49:43 +08:00
|
|
|
s_shader->use();
|
|
|
|
s_shader->setUniformsForBuiltins();
|
|
|
|
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-04-09 12:06:36 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
2014-05-15 01:07:09 +08:00
|
|
|
setGLBufferData(vertices, (segments + 1) * sizeof(Vec2));
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-23 09:08:25 +08:00
|
|
|
#else
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
|
2013-04-09 12:06:36 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-04-19 14:35:52 +08:00
|
|
|
glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments + 1);
|
|
|
|
CC_SAFE_DELETE_ARRAY(vertices);
|
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,segments+1);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void setDrawColor4F( GLfloat r, GLfloat g, GLfloat b, GLfloat a )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 05:49:43 +08:00
|
|
|
s_color.r = r;
|
|
|
|
s_color.g = g;
|
|
|
|
s_color.b = b;
|
|
|
|
s_color.a = a;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void setPointSize( GLfloat pointSize )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 05:49:43 +08:00
|
|
|
s_pointSize = pointSize * CC_CONTENT_SCALE_FACTOR();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//TODO :glPointSize( pointSize );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
void setDrawColor4B( GLubyte r, GLubyte g, GLubyte b, GLubyte a )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 05:49:43 +08:00
|
|
|
s_color.r = r/255.0f;
|
|
|
|
s_color.g = g/255.0f;
|
|
|
|
s_color.b = b/255.0f;
|
|
|
|
s_color.a = a/255.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
} // DrawPrimitives namespace
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|