issue #2426:Add Drawprimitives.lua and Drawprimitives lua test sample

This commit is contained in:
samuele3hu 2013-07-24 10:15:16 +08:00
parent 4d3cb57718
commit e9df175e9a
6 changed files with 557 additions and 150 deletions

View File

@ -1,139 +1,182 @@
require "DrawPrimitives"
local SceneIdx = -1
local MAX_LAYER = 2
local function DrawPrimitivesMainLayer()
local kItemTagBasic = 1000
local testCount = 1
local maxCases = testCount
local curCase = 0
local size = CCDirector:sharedDirector():getWinSize()
local curLayer = nil
local background = nil
local labelAtlas = nil
local baseLayer_entry = nil
local s = CCDirector:sharedDirector():getWinSize()
local function getBaseLayer()
local layer = CCLayer:create()
local item1 = CCMenuItemImage:create(s_pPathB1, s_pPathB2)
local item2 = CCMenuItemImage:create(s_pPathR1, s_pPathR2)
local item3 = CCMenuItemImage:create(s_pPathF1, s_pPathF2)
local item4 = CCMenuItemToggle:create(CCMenuItemFont:create("Free Movement"))
item4:addSubItem(CCMenuItemFont:create("Relative Movement"))
item4:addSubItem(CCMenuItemFont:create("Grouped Movement"))
item1:registerScriptTapHandler(backCallback)
item2:registerScriptTapHandler(restartCallback)
item3:registerScriptTapHandler(nextCallback)
labelAtlas = CCLabelAtlas:create("0000", "fps_images.png", 12, 32, string.byte('.'))
layer:addChild(labelAtlas, 100)
labelAtlas:setPosition(ccp(s.width - 66, 50))
layer:setTouchEnabled(false)
Helper.initWithLayer(layer)
return layer
end
local function drawPrimitivesTest()
local layer = getBaseLayer()
local function OrderCallbackMenu()
local function backCallback()
curCase = curCase - 1
if curCase < 0 then
curCase = curCase + maxCases
end
ShowCurrentTest()
end
ccDrawLine( ccp(0, s.height), ccp(s.width, 0) );
glLineWidth( 5.0);
ccDrawColor4B(255,0,0,255);
ccDrawLine(ccp(0, 0), ccp(s.width, s.height) );
local function restartCallback()
ShowCurrentTest()
end
-- ccPointSize(64);
-- ccDrawColor4B(0,0,255,128);
-- ccDrawPoint( VisibleRect::center() );
--
-- CHECK_GL_ERROR_DEBUG();
--
-- // draw 4 small points
-- CCPoint points[] = { ccp(60,60), ccp(70,70), ccp(60,70), ccp(70,60) };
-- ccPointSize(4);
-- ccDrawColor4B(0,255,255,255);
-- ccDrawPoints( points, 4);
--
-- CHECK_GL_ERROR_DEBUG();
--
-- // draw a green circle with 10 segments
-- glLineWidth(16);
-- ccDrawColor4B(0, 255, 0, 255);
-- ccDrawCircle( VisibleRect::center(), 100, 0, 10, false);
--
-- CHECK_GL_ERROR_DEBUG();
--
-- // draw a green circle with 50 segments with line to center
-- glLineWidth(2);
-- ccDrawColor4B(0, 255, 255, 255);
-- ccDrawCircle( VisibleRect::center(), 50, CC_DEGREES_TO_RADIANS(90), 50, true);
--
-- CHECK_GL_ERROR_DEBUG();
--
-- // open yellow poly
-- ccDrawColor4B(255, 255, 0, 255);
-- glLineWidth(10);
-- CCPoint vertices[] = { ccp(0,0), ccp(50,50), ccp(100,50), ccp(100,100), ccp(50,100) };
-- ccDrawPoly( vertices, 5, false);
--
-- CHECK_GL_ERROR_DEBUG();
--
-- // filled poly
-- glLineWidth(1);
-- CCPoint filledVertices[] = { ccp(0,120), ccp(50,120), ccp(50,170), ccp(25,200), ccp(0,170) };
-- ccDrawSolidPoly(filledVertices, 5, Color4F(0.5f, 0.5f, 1, 1 ) );
--
--
-- // closed purble poly
-- ccDrawColor4B(255, 0, 255, 255);
-- glLineWidth(2);
-- CCPoint vertices2[] = { ccp(30,130), ccp(30,230), ccp(50,200) };
-- ccDrawPoly( vertices2, 3, true);
--
-- CHECK_GL_ERROR_DEBUG();
--
-- // draw quad bezier path
-- ccDrawQuadBezier(VisibleRect::leftTop(), VisibleRect::center(), VisibleRect::rightTop(), 50);
--
-- CHECK_GL_ERROR_DEBUG();
--
-- // draw cubic bezier path
-- ccDrawCubicBezier(VisibleRect::center(), ccp(VisibleRect::center().x+30,VisibleRect::center().y+50), ccp(VisibleRect::center().x+60,VisibleRect::center().y-50),VisibleRect::right(),100);
--
-- CHECK_GL_ERROR_DEBUG();
--
-- //draw a solid polygon
-- CCPoint vertices3[] = {ccp(60,160), ccp(70,190), ccp(100,190), ccp(90,160)};
-- ccDrawSolidPoly( vertices3, 4, Color4F(1,1,0,1) );
--
-- // restore original values
-- glLineWidth(1);
-- ccDrawColor4B(255,255,255,255);
-- ccPointSize(1);
--
-- CHECK_GL_ERROR_DEBUG();
return layer
end
local function nextCallback()
curCase = curCase + 1
curCase = curCase % maxCases
ShowCurrentTest()
end
---------------------------------
-- DrawPrimitives Test
---------------------------------
function CreateDrawPrimitivesTestLayer()
if SceneIdx == 0 then return drawPrimitivesTest()
end
local ordercallbackmenu = CCMenu:create()
local size = CCDirector:sharedDirector():getWinSize()
local item1 = CCMenuItemImage:create(s_pPathB1, s_pPathB2)
item1:registerScriptTapHandler(backCallback)
ordercallbackmenu:addChild(item1,kItemTagBasic)
local item2 = CCMenuItemImage:create(s_pPathR1, s_pPathR2)
item2:registerScriptTapHandler(restartCallback)
ordercallbackmenu:addChild(item2,kItemTagBasic)
local item3 = CCMenuItemImage:create(s_pPathF1, s_pPathF2)
ordercallbackmenu:addChild(item3,kItemTagBasic)
item3:registerScriptTapHandler(nextCallback)
item1:setPosition(CCPoint(size.width / 2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2))
item2:setPosition(CCPoint(size.width / 2, item2:getContentSize().height / 2))
item3:setPosition(CCPoint(size.width / 2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2))
ordercallbackmenu:setPosition(ccp(0, 0))
return ordercallbackmenu
end
local function GetTitle()
if 0 == curCase then
return "Draw primitives"
elseif 1 == curCase then
return "Test DrawNode"
end
end
local function GetSubTitle()
if 0 == curCase then
return "Drawing Primitives by call gl funtions"
elseif 1 == curCase then
return "Testing DrawNode - batched draws. Concave polygons are BROKEN"
end
end
local function InitTitle(layer)
--Title
local lableTitle = CCLabelTTF:create(GetTitle(), "Arial", 40)
layer:addChild(lableTitle, 15)
lableTitle:setPosition(CCPoint(size.width / 2, size.height - 32))
lableTitle:setColor(Color3B(255, 255, 40))
--SubTitle
local subLabelTitle = CCLabelTTF:create(GetSubTitle(), "Thonburi", 16)
layer:addChild(subLabelTitle, 15)
subLabelTitle:setPosition(CCPoint(size.width / 2, size.height - 80))
end
local function createDrawPrimitivesEffect()
local layer = CCLayer:create()
InitTitle(layer)
local glNode = gl.glNodeCreate()
glNode:setContentSize(CCSize(size.width, size.height))
glNode:setAnchorPoint(CCPoint(0.5, 0.5))
local function primitivesDraw()
ccDrawLine(VisibleRect:leftBottom(), VisibleRect:rightTop() )
gl.lineWidth( 5.0 )
ccDrawColor4B(255,0,0,255)
ccDrawLine( VisibleRect:leftTop(), VisibleRect:rightBottom() )
ccPointSize(64)
ccDrawColor4B(0, 0, 255, 128)
ccDrawPoint(VisibleRect:center())
local points = {CCPoint(60,60), CCPoint(70,70), CCPoint(60,70), CCPoint(70,60) }
ccPointSize(4)
ccDrawColor4B(0,255,255,255)
ccDrawPoints(points,4)
gl.lineWidth(16)
ccDrawColor4B(0, 255, 0, 255)
ccDrawCircle( VisibleRect:center(), 100, 0, 10, false)
gl.lineWidth(2)
ccDrawColor4B(0, 255, 255, 255)
ccDrawCircle( VisibleRect:center(), 50, math.pi / 2, 50, true)
gl.lineWidth(2)
ccDrawColor4B(255, 0, 255, 255)
ccDrawSolidCircle( VisibleRect:center() + CCPoint(140,0), 40, math.rad(90), 50, 1.0, 1.0)
ccDrawColor4B(255, 255, 0, 255)
gl.lineWidth(10)
local yellowPoints = { CCPoint(0,0), CCPoint(50,50), CCPoint(100,50), CCPoint(100,100), CCPoint(50,100)}
ccDrawPoly( yellowPoints, 5, false)
gl.lineWidth(1)
local filledVertices = { CCPoint(0,120), CCPoint(50,120), CCPoint(50,170), CCPoint(25,200), CCPoint(0,170) }
local colorFilled = { 0.5, 0.5, 1, 1 }
ccDrawSolidPoly(filledVertices, 5, colorFilled)
ccDrawColor4B(255, 0, 255, 255)
gl.lineWidth(2)
local closePoints= { CCPoint(30,130), CCPoint(30,230), CCPoint(50,200) }
ccDrawPoly( closePoints, 3, true)
ccDrawQuadBezier(VisibleRect:leftTop(), VisibleRect:center(), VisibleRect:rightTop(), 50)
ccDrawCubicBezier(VisibleRect:center(), CCPoint(VisibleRect:center().x + 30, VisibleRect:center().y + 50), CCPoint(VisibleRect:center().x + 60,VisibleRect:center().y - 50), VisibleRect:right(), 100)
local solidvertices = {CCPoint(60,160), CCPoint(70,190), CCPoint(100,190), CCPoint(90,160)}
local colorsolid= { 1, 1, 0, 1 }
ccDrawSolidPoly( solidvertices, 4, colorsolid )
gl.lineWidth(1)
ccDrawColor4B(255,255,255,255)
ccPointSize(1)
end
glNode:registerScriptDrawHandler(primitivesDraw)
layer:addChild(glNode,-10)
glNode:setPosition( size.width / 2, size.height / 2)
return layer
end
local function createLayerByCurCase(curCase)
if 0 == curCase then
return createDrawPrimitivesEffect()
end
end
function ShowCurrentTest()
local curScene = CCScene:create()
if nil ~= curScene then
curLayer = createLayerByCurCase(curCase)
if nil ~= curLayer then
curScene:addChild(curLayer)
curLayer:addChild(OrderCallbackMenu(),15)
curScene:addChild(CreateBackMenuItem())
CCDirector:sharedDirector():replaceScene(curScene)
end
end
end
curLayer = createLayerByCurCase(curCase)
curLayer:addChild(OrderCallbackMenu(),15)
return curLayer
end
function DrawPrimitivesTest()
cclog("DrawPrimitivesTest")
local scene = CCScene:create()
Helper.createFunctionTable = {
drawPrimitivesTest
}
scene:addChild(drawPrimitivesTest())
scene:addChild(CreateBackMenuItem())
return scene
local scene = CCScene:create()
scene:addChild(DrawPrimitivesMainLayer())
scene:addChild(CreateBackMenuItem())
return scene
end

View File

@ -58,7 +58,7 @@ local _allTests = {
{ isSupported = true, name = "CocosDenshionTest" , create_func = CocosDenshionTestMain },
{ isSupported = false, name = "CurlTest" , create_func= CurlTestMain },
{ isSupported = true, name = "CurrentLanguageTest" , create_func= CurrentLanguageTestMain },
{ isSupported = false, name = "DrawPrimitivesTest" , create_func= DrawPrimitivesTest },
{ isSupported = true, name = "DrawPrimitivesTest" , create_func= DrawPrimitivesTest },
{ isSupported = true, name = "EffectsTest" , create_func = EffectsTest },
{ isSupported = true, name = "EffectAdvancedTest" , create_func = EffectAdvancedTestMain },
{ isSupported = true, name = "ExtensionsTest" , create_func= ExtensionsTestMain },

View File

@ -1 +1 @@
32e637c6b271e1eea27e207c13116f43c310c0d4
d83a378b9954c34cb506cdee439ef636f9679f4f

View File

@ -1 +1 @@
1e099e4c734f57f72adc2c665a8ab9e941b10417
39955341a288e37bd0f6107ff35bda7e0c6be972

View File

@ -16,26 +16,26 @@ rawset(_G,"ccpLineIntersect",ccpLineIntersect)
local function CCPointMake(x,y)
deprecatedTip("CCPointMake","CCPoint:__call")
return CCPoint:__call(x,y)
deprecatedTip("CCPointMake(x,y)","CCPoint(x,y)")
return CCPoint(x,y)
end
rawset(_G,"CCPointMake",CCPointMake)
local function ccp(x,y)
deprecatedTip("ccp","CCPoint:__call")
return CCPoint:__call(x,y)
deprecatedTip("ccp(x,y)","CCPoint(x,y)")
return CCPoint(x,y)
end
rawset(_G,"ccp",ccp)
local function CCSizeMake(width,height)
deprecatedTip("CCSizeMake","CCSize:__call")
return CCSize:__call(width,height)
deprecatedTip("CCSizeMake(width,height)","CCSize(width,height)")
return CCSize(width,height)
end
rawset(_G,"CCSizeMake",CCSizeMake)
local function CCRectMake(x,y,width,height)
deprecatedTip("CCRectMake","CCRect:__call")
return CCRect:__call(x,y,width,height)
deprecatedTip("CCRectMake(x,y,width,height)","CCRect(x,y,width,height)")
return CCRect(x,y,width,height)
end
rawset(_G,"CCRectMake",CCRectMake)
@ -162,8 +162,8 @@ rawset(_G,"ccpClamp",ccpClamp)
local function ccpFromSize(sz)
deprecatedTip("ccpFromSize","CCPoint:__call")
return CCPoint:__call(sz)
deprecatedTip("ccpFromSize(sz)","CCPoint(sz)")
return CCPoint(sz)
end
rawset(_G,"ccpFromSize",ccpFromSize)
@ -180,8 +180,8 @@ end
rawset(_G,"ccpFuzzyEqual",ccpFuzzyEqual)
local function ccpCompMult(pt1,pt2)
deprecatedTip("ccpCompMult","CCPoint:__call")
return CCPoint:__call(pt1.x * pt2.x , pt1.y * pt2.y)
deprecatedTip("ccpCompMult","CCPoint")
return CCPoint(pt1.x * pt2.x , pt1.y * pt2.y)
end
rawset(_G,"ccpCompMult",ccpCompMult)
@ -450,9 +450,3 @@ local function sharedEngine()
return SimpleAudioEngine:getInstance()
end
rawset(SimpleAudioEngine,"sharedEngine",sharedEngine)

View File

@ -0,0 +1,370 @@
dp_initialized = false
dp_shader = nil
dp_colorLocation = -1
dp_color = { 1.0, 1.0, 1.0, 1.0 }
dp_pointSizeLocation = -1
dp_pointSize = 1.0
kShader_Position_uColor = "ShaderPosition_uColor"
local targetPlatform = CCApplication:getInstance():getTargetPlatform()
local function lazy_init()
if not dp_initialized then
dp_shader = CCShaderCache:getInstance():getProgram(kShader_Position_uColor)
--dp_shader:retain()
if nil ~= dp_shader then
dp_colorLocation = gl.getUniformLocation( dp_shader:getProgram(), "u_color")
dp_pointSizeLocation = gl.getUniformLocation( dp_shader:getProgram(), "u_pointSize")
dp_Initialized = true
end
end
if nil == dp_shader then
print("Error:dp_shader is nil!")
return false
end
return true
end
local function setDrawProperty()
gl.glEnableVertexAttribs( CCConstants.VERTEX_ATTRIB_FLAG_POSITION )
dp_shader:use()
dp_shader:setUniformsForBuiltins()
dp_shader:setUniformLocationWith4fv(dp_colorLocation, dp_color, 1)
end
function ccDrawInit()
lazy_init()
end
function ccDrawFree()
dp_initialized = false
end
function ccDrawColor4f(r,g,b,a)
dp_color[1] = r
dp_color[2] = g
dp_color[3] = b
dp_color[4] = a
end
function ccPointSize(pointSize)
dp_pointSize = pointSize * CCDirector:sharedDirector():getContentScaleFactor()
end
function ccDrawColor4B(r,g,b,a)
dp_color[1] = r / 255.0
dp_color[2] = g / 255.0
dp_color[3] = b / 255.0
dp_color[4] = a / 255.0
end
function ccDrawPoint(point)
if not lazy_init() then
return
end
local VertexBuffer = { }
local function initBuffer()
VertexBuffer.buffer_id = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER,VertexBuffer.buffer_id)
local vertices = { point.x,point.y}
gl.bufferData(gl.ARRAY_BUFFER,2,vertices,gl.STATIC_DRAW)
gl.bindBuffer(gl.ARRAY_BUFFER, 0)
end
initBuffer()
setDrawProperty()
dp_shader:setUniformLocationWith1f(dp_pointSizeLocation, dp_pointSize)
gl.bindBuffer(gl.ARRAY_BUFFER,VertexBuffer.buffer_id)
gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
gl.drawArrays(gl.POINTS,0,1)
gl.bindBuffer(gl.ARRAY_BUFFER,0)
end
function ccDrawPoints(points,numOfPoint)
if not lazy_init() then
return
end
local VertexBuffer = {}
local i = 1
local function initBuffer()
VertexBuffer.buffer_id = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, VertexBuffer.buffer_id)
local vertices = {}
for i = 1, numOfPoint do
vertices[2 * i - 1] = points[i].x
vertices[2 * i] = points[i].y
end
gl.bufferData(gl.ARRAY_BUFFER, numOfPoint * 2, vertices, gl.STATIC_DRAW)
gl.bindBuffer(gl.ARRAY_BUFFER, 0)
end
initBuffer()
setDrawProperty()
dp_shader:setUniformLocationWith1f(dp_pointSizeLocation, dp_pointSize)
gl.bindBuffer(gl.ARRAY_BUFFER,VertexBuffer.buffer_id)
gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
gl.drawArrays(gl.POINTS,0,numOfPoint)
gl.bindBuffer(gl.ARRAY_BUFFER,0)
end
function ccDrawLine(origin,destination)
if not lazy_init() then
return
end
local VertexBuffer = {}
local function initBuffer()
VertexBuffer.buffer_id = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, VertexBuffer.buffer_id)
local vertices = { origin.x, origin.y, destination.x, destination.y}
gl.bufferData(gl.ARRAY_BUFFER,4,vertices,gl.STATIC_DRAW)
gl.bindBuffer(gl.ARRAY_BUFFER, 0)
end
initBuffer()
setDrawProperty()
gl.bindBuffer(gl.ARRAY_BUFFER,VertexBuffer.buffer_id)
gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
gl.drawArrays(gl.LINES ,0,2)
gl.bindBuffer(gl.ARRAY_BUFFER,0)
end
function ccDrawPoly(points,numOfPoints,closePolygon)
if not lazy_init() then
return
end
local VertexBuffer = {}
local i = 1
local function initBuffer()
VertexBuffer.buffer_id = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, VertexBuffer.buffer_id)
local vertices = {}
for i = 1, numOfPoints do
vertices[2 * i - 1] = points[i].x
vertices[2 * i] = points[i].y
end
gl.bufferData(gl.ARRAY_BUFFER, numOfPoints * 2, vertices, gl.STATIC_DRAW)
gl.bindBuffer(gl.ARRAY_BUFFER, 0)
end
initBuffer()
setDrawProperty()
gl.bindBuffer(gl.ARRAY_BUFFER,VertexBuffer.buffer_id)
gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
if closePolygon then
gl.drawArrays(gl.LINE_LOOP , 0, numOfPoints)
else
gl.drawArrays(gl.LINE_STRIP, 0, numOfPoints)
end
gl.bindBuffer(gl.ARRAY_BUFFER,0)
end
function ccDrawSolidPoly(points,numOfPoints,color)
if not lazy_init() then
return
end
local VertexBuffer = {}
local i = 1
local function initBuffer()
VertexBuffer.buffer_id = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, VertexBuffer.buffer_id)
local vertices = {}
for i = 1, numOfPoints do
vertices[2 * i - 1] = points[i].x
vertices[2 * i] = points[i].y
end
gl.bufferData(gl.ARRAY_BUFFER, numOfPoints * 2, vertices, gl.STATIC_DRAW)
gl.bindBuffer(gl.ARRAY_BUFFER, 0)
end
initBuffer()
gl.glEnableVertexAttribs( CCConstants.VERTEX_ATTRIB_FLAG_POSITION )
dp_shader:use()
dp_shader:setUniformsForBuiltins()
dp_shader:setUniformLocationWith4fv(dp_colorLocation, color, 1)
gl.bindBuffer(gl.ARRAY_BUFFER,VertexBuffer.buffer_id)
gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
gl.drawArrays(gl.TRIANGLE_FAN , 0, numOfPoints)
gl.bindBuffer(gl.ARRAY_BUFFER,0)
end
function ccDrawRect(origin,destination)
ccDrawLine(CCPoint:__call(origin.x, origin.y), CCPoint:__call(destination.x, origin.y))
ccDrawLine(CCPoint:__call(destination.x, origin.y), CCPoint:__call(destination.x, destination.y))
ccDrawLine(CCPoint:__call(destination.x, destination.y), CCPoint:__call(origin.x, destination.y))
ccDrawLine(CCPoint:__call(origin.x, destination.y), CCPoint:__call(origin.x, origin.y))
end
function ccDrawSolidRect( origin,destination,color )
local vertices = { origin, CCPoint:__call(destination.x, origin.y) , destination, CCPoint:__call(origin.x, destination.y) }
ccDrawSolidPoly(vertices,4,color)
end
function ccDrawCircleScale( center, radius, angle, segments,drawLineToCenter,scaleX,scaleY)
if not lazy_init() then
return
end
local additionalSegment = 1
if drawLineToCenter then
additionalSegment = additionalSegment + 1
end
local vertexBuffer = { }
local function initBuffer()
local coef = 2.0 * math.pi / segments
local i = 1
local vertices = {}
vertexBuffer.buffer_id = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id)
for i = 1, segments + 1 do
local rads = (i - 1) * coef
local j = radius * math.cos(rads + angle) * scaleX + center.x
local k = radius * math.sin(rads + angle) * scaleY + center.y
vertices[i * 2 - 1] = j
vertices[i * 2] = k
end
vertices[(segments + 2) * 2 - 1] = center.x
vertices[(segments + 2) * 2] = center.y
gl.bufferData(gl.ARRAY_BUFFER, (segments + 2) * 2, vertices, gl.STATIC_DRAW)
gl.bindBuffer(gl.ARRAY_BUFFER, 0)
end
initBuffer()
setDrawProperty()
gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id)
gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
gl.drawArrays(gl.LINE_STRIP , 0, segments + additionalSegment)
gl.bindBuffer(gl.ARRAY_BUFFER,0)
end
function ccDrawCircle(center, radius, angle, segments, drawLineToCenter)
ccDrawCircleScale(center, radius, angle, segments, drawLineToCenter, 1.0, 1.0)
end
function ccDrawSolidCircle(center, radius, angle, segments,scaleX,scaleY)
if not lazy_init() then
return
end
local vertexBuffer = { }
local function initBuffer()
local coef = 2.0 * math.pi / segments
local i = 1
local vertices = {}
vertexBuffer.buffer_id = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id)
for i = 1, segments + 1 do
local rads = (i - 1) * coef
local j = radius * math.cos(rads + angle) * scaleX + center.x
local k = radius * math.sin(rads + angle) * scaleY + center.y
vertices[i * 2 - 1] = j
vertices[i * 2] = k
end
vertices[(segments + 2) * 2 - 1] = center.x
vertices[(segments + 2) * 2] = center.y
gl.bufferData(gl.ARRAY_BUFFER, (segments + 2) * 2, vertices, gl.STATIC_DRAW)
gl.bindBuffer(gl.ARRAY_BUFFER, 0)
end
initBuffer()
setDrawProperty()
gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id)
gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
gl.drawArrays(gl.TRIANGLE_FAN , 0, segments + 1)
gl.bindBuffer(gl.ARRAY_BUFFER,0)
end
function ccDrawQuadBezier(origin, control, destination, segments)
if not lazy_init() then
return
end
local vertexBuffer = { }
local function initBuffer()
local vertices = { }
local i = 1
local t = 0.0
for i = 1, segments do
vertices[2 * i - 1] = math.pow(1 - t,2) * origin.x + 2.0 * (1 - t) * t * control.x + t * t * destination.x
vertices[2 * i] = math.pow(1 - t,2) * origin.y + 2.0 * (1 - t) * t * control.y + t * t * destination.y
t = t + 1.0 / segments
end
vertices[2 * (segments + 1) - 1] = destination.x
vertices[2 * (segments + 1)] = destination.y
vertexBuffer.buffer_id = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id)
gl.bufferData(gl.ARRAY_BUFFER, (segments + 1) * 2, vertices, gl.STATIC_DRAW)
gl.bindBuffer(gl.ARRAY_BUFFER, 0)
end
initBuffer()
setDrawProperty()
gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id)
gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
gl.drawArrays(gl.LINE_STRIP , 0, segments + 1)
gl.bindBuffer(gl.ARRAY_BUFFER,0)
end
function ccDrawCubicBezier(origin, control1, control2, destination, segments)
if not lazy_init then
return
end
local vertexBuffer = { }
local function initBuffer()
local vertices = { }
local t = 0
local i = 1
for i = 1, segments do
vertices[2 * i - 1] = math.pow(1 - t,3) * origin.x + 3.0 * math.pow(1 - t, 2) * t * control1.x + 3.0 * (1 - t) * t * t * control2.x + t * t * t * destination.x
vertices[2 * i] = math.pow(1 - t,3) * origin.y + 3.0 * math.pow(1 - t, 2) * t * control1.y + 3.0 * (1 - t) * t * t * control2.y + t * t * t * destination.y
t = t + 1.0 / segments
end
vertices[2 * (segments + 1) - 1] = destination.x
vertices[2 * (segments + 1)] = destination.y
vertexBuffer.buffer_id = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id)
gl.bufferData(gl.ARRAY_BUFFER, (segments + 1) * 2, vertices, gl.STATIC_DRAW)
gl.bindBuffer(gl.ARRAY_BUFFER, 0)
end
initBuffer()
setDrawProperty()
gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id)
gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
gl.drawArrays(gl.LINE_STRIP , 0, segments + 1)
gl.bindBuffer(gl.ARRAY_BUFFER,0)
end