axmol/cocos/2d/CCDrawingPrimitives.cpp

589 lines
18 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2013 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
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
*
* USE DrawNode instead
2012-11-14 18:05:15 +08:00
*
*/
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "2d/CCDrawingPrimitives.h"
#include <string.h>
#include <cmath>
2014-04-30 08:37:36 +08:00
#include "base/ccTypes.h"
#include "base/ccMacros.h"
#include "CCGL.h"
2014-04-30 08:37:36 +08:00
#include "base/CCDirector.h"
2014-04-27 01:35:57 +08:00
#include "2d/ccGLStateCache.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "2d/CCShaderCache.h"
#include "2d/CCGLProgram.h"
#include "2d/CCActionCatmullRom.h"
2014-04-30 08:37:36 +08:00
#include "renderer/CCRenderer.h"
NS_CC_BEGIN
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
namespace DrawPrimitives {
static bool s_initialized = false;
static GLProgram* s_shader = nullptr;
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;
#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
static void lazy_init( void )
{
if( ! s_initialized ) {
//
// Position and 1 color passed as a uniform (to simulate glColor4ub )
//
s_shader = ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR);
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();
s_initialized = true;
}
}
// When switching from backround to foreground on android, we want the params to be initialized again
void init()
{
2013-02-27 09:38:30 +08:00
lazy_init();
}
void free()
2013-02-27 09:38:30 +08:00
{
CC_SAFE_RELEASE_NULL(s_shader);
s_initialized = false;
}
2014-04-15 17:29:23 +08:00
void drawPoint( const Vector2& point )
{
lazy_init();
2014-04-02 15:25:37 +08:00
Vector2 p;
p.x = point.x;
p.y = point.y;
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
s_shader->use();
s_shader->setUniformsForBuiltins();
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
s_shader->setUniformLocationWith1f(s_pointSizeLocation, s_pointSize);
#ifdef EMSCRIPTEN
setGLBufferData(&p, 8);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, &p);
#endif // EMSCRIPTEN
glDrawArrays(GL_POINTS, 0, 1);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,1);
}
2014-04-15 17:29:23 +08:00
void drawPoints( const Vector2 *points, unsigned int numberOfPoints )
{
lazy_init();
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
s_shader->use();
s_shader->setUniformsForBuiltins();
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
s_shader->setUniformLocationWith1f(s_pointSizeLocation, s_pointSize);
// XXX: Mac OpenGL error. arrays can't go out of scope before draw is executed
2014-04-02 15:25:37 +08:00
Vector2* newPoints = new Vector2[numberOfPoints];
// iPhone and 32-bit machines optimization
if( sizeof(Vector2) == sizeof(Vector2) )
{
#ifdef EMSCRIPTEN
setGLBufferData((void*) points, numberOfPoints * sizeof(Vector2));
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, points);
#endif // EMSCRIPTEN
}
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;
}
#ifdef EMSCRIPTEN
// 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-04-02 15:25:37 +08:00
setGLBufferData(newPoints, numberOfPoints * sizeof(Vector2));
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, newPoints);
#endif // EMSCRIPTEN
}
glDrawArrays(GL_POINTS, 0, (GLsizei) numberOfPoints);
CC_SAFE_DELETE_ARRAY(newPoints);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, numberOfPoints);
}
2014-04-15 17:29:23 +08:00
void drawLine( const Vector2& origin, const Vector2& destination )
{
lazy_init();
2014-04-02 15:25:37 +08:00
Vector2 vertices[2] = {
Vector2(origin.x, origin.y),
Vector2(destination.x, destination.y)
};
s_shader->use();
s_shader->setUniformsForBuiltins();
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
#ifdef EMSCRIPTEN
setGLBufferData(vertices, 16);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
#endif // EMSCRIPTEN
glDrawArrays(GL_LINES, 0, 2);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,2);
}
2014-04-15 17:29:23 +08:00
void drawRect( Vector2 origin, Vector2 destination )
2012-06-12 01:43:07 +08:00
{
drawLine(Vector2(origin.x, origin.y), Vector2(destination.x, origin.y));
drawLine(Vector2(destination.x, origin.y), Vector2(destination.x, destination.y));
drawLine(Vector2(destination.x, destination.y), Vector2(origin.x, destination.y));
drawLine(Vector2(origin.x, destination.y), Vector2(origin.x, origin.y));
2012-06-12 01:43:07 +08:00
}
2014-04-15 17:29:23 +08:00
void drawSolidRect( Vector2 origin, Vector2 destination, Color4F color )
2012-06-12 01:43:07 +08:00
{
2014-04-15 17:29:23 +08:00
Vector2 vertices[] = {
2012-06-12 01:43:07 +08:00
origin,
Vector2(destination.x, origin.y),
2012-06-12 01:43:07 +08:00
destination,
Vector2(origin.x, destination.y)
2012-06-12 01:43:07 +08:00
};
drawSolidPoly(vertices, 4, color );
2012-06-12 01:43:07 +08:00
}
2014-04-15 17:29:23 +08:00
void drawPoly( const Vector2 *poli, unsigned int numberOfPoints, bool closePolygon )
{
lazy_init();
s_shader->use();
s_shader->setUniformsForBuiltins();
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
// iPhone and 32-bit machines optimization
if( sizeof(Vector2) == sizeof(Vector2) )
2012-09-14 17:54:53 +08:00
{
#ifdef EMSCRIPTEN
setGLBufferData((void*) poli, numberOfPoints * sizeof(Vector2));
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, poli);
#endif // EMSCRIPTEN
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);
}
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-04-02 15:25:37 +08:00
Vector2* newPoli = new Vector2[numberOfPoints];
for( unsigned int i=0; i<numberOfPoints;i++) {
newPoli[i].x = poli[i].x;
newPoli[i].y = poli[i].y;
}
#ifdef EMSCRIPTEN
2014-04-02 15:25:37 +08:00
setGLBufferData(newPoli, numberOfPoints * sizeof(Vector2));
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, newPoli);
#endif // EMSCRIPTEN
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-09-14 17:54:53 +08:00
CC_SAFE_DELETE_ARRAY(newPoli);
}
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, numberOfPoints);
}
2014-04-15 17:29:23 +08:00
void drawSolidPoly( const Vector2 *poli, unsigned int numberOfPoints, Color4F color )
{
lazy_init();
s_shader->use();
s_shader->setUniformsForBuiltins();
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &color.r, 1);
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
// XXX: Mac OpenGL error. arrays can't go out of scope before draw is executed
2014-04-02 15:25:37 +08:00
Vector2* newPoli = new Vector2[numberOfPoints];
// iPhone and 32-bit machines optimization
if( sizeof(Vector2) == sizeof(Vector2) )
{
#ifdef EMSCRIPTEN
setGLBufferData((void*) poli, numberOfPoints * sizeof(Vector2));
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, poli);
#endif // EMSCRIPTEN
}
else
{
// Mac on 64-bit
for( unsigned int i=0; i<numberOfPoints;i++)
{
2014-04-02 15:25:37 +08:00
newPoli[i] = Vector2( poli[i].x, poli[i].y );
}
#ifdef EMSCRIPTEN
2014-04-02 15:25:37 +08:00
setGLBufferData(newPoli, numberOfPoints * sizeof(Vector2));
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, newPoli);
#endif // EMSCRIPTEN
}
glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) numberOfPoints);
CC_SAFE_DELETE_ARRAY(newPoli);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, numberOfPoints);
}
2014-04-15 17:29:23 +08:00
void drawCircle( const Vector2& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY)
{
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;
vertices[i*2] = j;
vertices[i*2+1] = k;
}
vertices[(segments+1)*2] = center.x;
vertices[(segments+1)*2+1] = center.y;
s_shader->use();
s_shader->setUniformsForBuiltins();
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
#ifdef EMSCRIPTEN
setGLBufferData(vertices, sizeof(GLfloat)*2*(segments+2));
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
#endif // EMSCRIPTEN
glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments+additionalSegment);
::free( vertices );
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,segments+additionalSegment);
}
2014-04-15 17:29:23 +08:00
void drawCircle( const Vector2& center, float radius, float angle, unsigned int segments, bool drawLineToCenter)
{
drawCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f);
}
2014-04-15 17:29:23 +08:00
void drawSolidCircle( const Vector2& center, float radius, float angle, unsigned int segments, float scaleX, float scaleY)
{
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;
s_shader->use();
s_shader->setUniformsForBuiltins();
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
#ifdef EMSCRIPTEN
setGLBufferData(vertices, sizeof(GLfloat)*2*(segments+2));
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
#endif // EMSCRIPTEN
glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) segments+1);
::free( vertices );
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,segments+1);
}
2014-04-15 17:29:23 +08:00
void drawSolidCircle( const Vector2& center, float radius, float angle, unsigned int segments)
{
drawSolidCircle(center, radius, angle, segments, 1.0f, 1.0f);
}
2014-04-15 17:29:23 +08:00
void drawQuadBezier(const Vector2& origin, const Vector2& control, const Vector2& destination, unsigned int segments)
{
lazy_init();
2014-04-02 15:25:37 +08:00
Vector2* vertices = new Vector2[segments + 1];
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;
s_shader->use();
s_shader->setUniformsForBuiltins();
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
#ifdef EMSCRIPTEN
2014-04-02 15:25:37 +08:00
setGLBufferData(vertices, (segments + 1) * sizeof(Vector2));
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
#endif // EMSCRIPTEN
glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments + 1);
CC_SAFE_DELETE_ARRAY(vertices);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,segments+1);
}
void drawCatmullRom( PointArray *points, unsigned int segments )
2012-06-12 01:43:07 +08:00
{
drawCardinalSpline( points, 0.5f, segments );
2012-06-12 01:43:07 +08:00
}
void drawCardinalSpline( PointArray *config, float tension, unsigned int segments )
2012-06-12 01:43:07 +08:00
{
lazy_init();
2014-04-02 15:25:37 +08:00
Vector2* vertices = new Vector2[segments + 1];
2012-06-12 01:43:07 +08:00
2013-12-28 14:34:52 +08:00
ssize_t p;
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++) {
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;
lt = (dt - deltaT * (float)p) / deltaT;
2012-06-12 01:43:07 +08:00
}
// Interpolate
Vector2 pp0 = config->getControlPointAtIndex(p-1);
Vector2 pp1 = config->getControlPointAtIndex(p+0);
Vector2 pp2 = config->getControlPointAtIndex(p+1);
Vector2 pp3 = config->getControlPointAtIndex(p+2);
2012-06-12 01:43:07 +08:00
Vector2 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;
}
s_shader->use();
s_shader->setUniformsForBuiltins();
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*)&s_color.r, 1);
2012-06-12 01:43:07 +08:00
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
2012-06-12 01:43:07 +08:00
#ifdef EMSCRIPTEN
2014-04-02 15:25:37 +08:00
setGLBufferData(vertices, (segments + 1) * sizeof(Vector2));
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
#endif // EMSCRIPTEN
2012-06-12 01:43:07 +08:00
glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments + 1);
CC_SAFE_DELETE_ARRAY(vertices);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,segments+1);
2012-06-12 01:43:07 +08:00
}
2014-04-15 17:29:23 +08:00
void drawCubicBezier(const Vector2& origin, const Vector2& control1, const Vector2& control2, const Vector2& destination, unsigned int segments)
{
lazy_init();
2014-04-02 15:25:37 +08:00
Vector2* vertices = new Vector2[segments + 1];
float t = 0;
for(unsigned int i = 0; i < segments; i++)
{
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;
s_shader->use();
s_shader->setUniformsForBuiltins();
s_shader->setUniformLocationWith4fv(s_colorLocation, (GLfloat*) &s_color.r, 1);
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
#ifdef EMSCRIPTEN
2014-04-02 15:25:37 +08:00
setGLBufferData(vertices, (segments + 1) * sizeof(Vector2));
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
#endif // EMSCRIPTEN
glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments + 1);
CC_SAFE_DELETE_ARRAY(vertices);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,segments+1);
}
void setDrawColor4F( GLfloat r, GLfloat g, GLfloat b, GLfloat a )
{
s_color.r = r;
s_color.g = g;
s_color.b = b;
s_color.a = a;
}
void setPointSize( GLfloat pointSize )
{
s_pointSize = pointSize * CC_CONTENT_SCALE_FACTOR();
//TODO :glPointSize( pointSize );
}
void setDrawColor4B( GLubyte r, GLubyte g, GLubyte b, GLubyte a )
{
s_color.r = r/255.0f;
s_color.g = g/255.0f;
s_color.b = b/255.0f;
s_color.a = a/255.0f;
}
} // DrawPrimitives namespace
NS_CC_END