DrawPrimitives.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. local dp_initialized = false
  2. local dp_shader = nil
  3. local dp_colorLocation = -1
  4. local dp_color = { 1.0, 1.0, 1.0, 1.0 }
  5. local dp_pointSizeLocation = -1
  6. local dp_pointSize = 1.0
  7. local SHADER_NAME_POSITION_U_COLOR = "ShaderPosition_uColor"
  8. local targetPlatform = cc.Application:getInstance():getTargetPlatform()
  9. local function lazy_init()
  10. if not dp_initialized then
  11. dp_shader = cc.ShaderCache:getInstance():getProgram(SHADER_NAME_POSITION_U_COLOR)
  12. --dp_shader:retain()
  13. if nil ~= dp_shader then
  14. dp_colorLocation = gl.getUniformLocation( dp_shader:getProgram(), "u_color")
  15. dp_pointSizeLocation = gl.getUniformLocation( dp_shader:getProgram(), "u_pointSize")
  16. dp_Initialized = true
  17. end
  18. end
  19. if nil == dp_shader then
  20. print("Error:dp_shader is nil!")
  21. return false
  22. end
  23. return true
  24. end
  25. local function setDrawProperty()
  26. gl.glEnableVertexAttribs( CCConstants.VERTEX_ATTRIB_FLAG_POSITION )
  27. dp_shader:use()
  28. dp_shader:setUniformsForBuiltins()
  29. dp_shader:setUniformLocationWith4fv(dp_colorLocation, dp_color, 1)
  30. end
  31. function ccDrawInit()
  32. lazy_init()
  33. end
  34. function ccDrawFree()
  35. dp_initialized = false
  36. end
  37. function ccDrawColor4f(r,g,b,a)
  38. dp_color[1] = r
  39. dp_color[2] = g
  40. dp_color[3] = b
  41. dp_color[4] = a
  42. end
  43. function ccPointSize(pointSize)
  44. dp_pointSize = pointSize * cc.Director:getInstance():getContentScaleFactor()
  45. end
  46. function ccDrawColor4B(r,g,b,a)
  47. dp_color[1] = r / 255.0
  48. dp_color[2] = g / 255.0
  49. dp_color[3] = b / 255.0
  50. dp_color[4] = a / 255.0
  51. end
  52. function ccDrawPoint(point)
  53. if not lazy_init() then
  54. return
  55. end
  56. local vertexBuffer = { }
  57. local function initBuffer()
  58. vertexBuffer.buffer_id = gl.createBuffer()
  59. gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id)
  60. local vertices = { point.x,point.y}
  61. gl.bufferData(gl.ARRAY_BUFFER,2,vertices,gl.STATIC_DRAW)
  62. gl.bindBuffer(gl.ARRAY_BUFFER, 0)
  63. end
  64. initBuffer()
  65. setDrawProperty()
  66. dp_shader:setUniformLocationWith1f(dp_pointSizeLocation, dp_pointSize)
  67. gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id)
  68. gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
  69. gl.drawArrays(gl.POINTS,0,1)
  70. gl.bindBuffer(gl.ARRAY_BUFFER,0)
  71. end
  72. function ccDrawPoints(points,numOfPoint)
  73. if not lazy_init() then
  74. return
  75. end
  76. local vertexBuffer = {}
  77. local i = 1
  78. local function initBuffer()
  79. vertexBuffer.buffer_id = gl.createBuffer()
  80. gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id)
  81. local vertices = {}
  82. for i = 1, numOfPoint do
  83. vertices[2 * i - 1] = points[i].x
  84. vertices[2 * i] = points[i].y
  85. end
  86. gl.bufferData(gl.ARRAY_BUFFER, numOfPoint * 2, vertices, gl.STATIC_DRAW)
  87. gl.bindBuffer(gl.ARRAY_BUFFER, 0)
  88. end
  89. initBuffer()
  90. setDrawProperty()
  91. dp_shader:setUniformLocationWith1f(dp_pointSizeLocation, dp_pointSize)
  92. gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id)
  93. gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
  94. gl.drawArrays(gl.POINTS,0,numOfPoint)
  95. gl.bindBuffer(gl.ARRAY_BUFFER,0)
  96. end
  97. function ccDrawLine(origin,destination)
  98. if not lazy_init() then
  99. return
  100. end
  101. local vertexBuffer = {}
  102. local function initBuffer()
  103. vertexBuffer.buffer_id = gl.createBuffer()
  104. gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id)
  105. local vertices = { origin.x, origin.y, destination.x, destination.y}
  106. gl.bufferData(gl.ARRAY_BUFFER,4,vertices,gl.STATIC_DRAW)
  107. gl.bindBuffer(gl.ARRAY_BUFFER, 0)
  108. end
  109. initBuffer()
  110. setDrawProperty()
  111. gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id)
  112. gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
  113. gl.drawArrays(gl.LINES ,0,2)
  114. gl.bindBuffer(gl.ARRAY_BUFFER,0)
  115. end
  116. function ccDrawPoly(points,numOfPoints,closePolygon)
  117. if not lazy_init() then
  118. return
  119. end
  120. local vertexBuffer = {}
  121. local i = 1
  122. local function initBuffer()
  123. vertexBuffer.buffer_id = gl.createBuffer()
  124. gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id)
  125. local vertices = {}
  126. for i = 1, numOfPoints do
  127. vertices[2 * i - 1] = points[i].x
  128. vertices[2 * i] = points[i].y
  129. end
  130. gl.bufferData(gl.ARRAY_BUFFER, numOfPoints * 2, vertices, gl.STATIC_DRAW)
  131. gl.bindBuffer(gl.ARRAY_BUFFER, 0)
  132. end
  133. initBuffer()
  134. setDrawProperty()
  135. gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id)
  136. gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
  137. if closePolygon then
  138. gl.drawArrays(gl.LINE_LOOP , 0, numOfPoints)
  139. else
  140. gl.drawArrays(gl.LINE_STRIP, 0, numOfPoints)
  141. end
  142. gl.bindBuffer(gl.ARRAY_BUFFER,0)
  143. end
  144. function ccDrawSolidPoly(points,numOfPoints,color)
  145. if not lazy_init() then
  146. return
  147. end
  148. local vertexBuffer = {}
  149. local i = 1
  150. local function initBuffer()
  151. vertexBuffer.buffer_id = gl.createBuffer()
  152. gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id)
  153. local vertices = {}
  154. for i = 1, numOfPoints do
  155. vertices[2 * i - 1] = points[i].x
  156. vertices[2 * i] = points[i].y
  157. end
  158. gl.bufferData(gl.ARRAY_BUFFER, numOfPoints * 2, vertices, gl.STATIC_DRAW)
  159. gl.bindBuffer(gl.ARRAY_BUFFER, 0)
  160. end
  161. initBuffer()
  162. gl.glEnableVertexAttribs( CCConstants.VERTEX_ATTRIB_FLAG_POSITION )
  163. dp_shader:use()
  164. dp_shader:setUniformsForBuiltins()
  165. dp_shader:setUniformLocationWith4fv(dp_colorLocation, color, 1)
  166. gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id)
  167. gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
  168. gl.drawArrays(gl.TRIANGLE_FAN , 0, numOfPoints)
  169. gl.bindBuffer(gl.ARRAY_BUFFER,0)
  170. end
  171. function ccDrawRect(origin,destination)
  172. ccDrawLine(CCPoint:__call(origin.x, origin.y), CCPoint:__call(destination.x, origin.y))
  173. ccDrawLine(CCPoint:__call(destination.x, origin.y), CCPoint:__call(destination.x, destination.y))
  174. ccDrawLine(CCPoint:__call(destination.x, destination.y), CCPoint:__call(origin.x, destination.y))
  175. ccDrawLine(CCPoint:__call(origin.x, destination.y), CCPoint:__call(origin.x, origin.y))
  176. end
  177. function ccDrawSolidRect( origin,destination,color )
  178. local vertices = { origin, CCPoint:__call(destination.x, origin.y) , destination, CCPoint:__call(origin.x, destination.y) }
  179. ccDrawSolidPoly(vertices,4,color)
  180. end
  181. function ccDrawCircleScale( center, radius, angle, segments,drawLineToCenter,scaleX,scaleY)
  182. if not lazy_init() then
  183. return
  184. end
  185. local additionalSegment = 1
  186. if drawLineToCenter then
  187. additionalSegment = additionalSegment + 1
  188. end
  189. local vertexBuffer = { }
  190. local function initBuffer()
  191. local coef = 2.0 * math.pi / segments
  192. local i = 1
  193. local vertices = {}
  194. vertexBuffer.buffer_id = gl.createBuffer()
  195. gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id)
  196. for i = 1, segments + 1 do
  197. local rads = (i - 1) * coef
  198. local j = radius * math.cos(rads + angle) * scaleX + center.x
  199. local k = radius * math.sin(rads + angle) * scaleY + center.y
  200. vertices[i * 2 - 1] = j
  201. vertices[i * 2] = k
  202. end
  203. vertices[(segments + 2) * 2 - 1] = center.x
  204. vertices[(segments + 2) * 2] = center.y
  205. gl.bufferData(gl.ARRAY_BUFFER, (segments + 2) * 2, vertices, gl.STATIC_DRAW)
  206. gl.bindBuffer(gl.ARRAY_BUFFER, 0)
  207. end
  208. initBuffer()
  209. setDrawProperty()
  210. gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id)
  211. gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
  212. gl.drawArrays(gl.LINE_STRIP , 0, segments + additionalSegment)
  213. gl.bindBuffer(gl.ARRAY_BUFFER,0)
  214. end
  215. function ccDrawCircle(center, radius, angle, segments, drawLineToCenter)
  216. ccDrawCircleScale(center, radius, angle, segments, drawLineToCenter, 1.0, 1.0)
  217. end
  218. function ccDrawSolidCircle(center, radius, angle, segments,scaleX,scaleY)
  219. if not lazy_init() then
  220. return
  221. end
  222. local vertexBuffer = { }
  223. local function initBuffer()
  224. local coef = 2.0 * math.pi / segments
  225. local i = 1
  226. local vertices = {}
  227. vertexBuffer.buffer_id = gl.createBuffer()
  228. gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id)
  229. for i = 1, segments + 1 do
  230. local rads = (i - 1) * coef
  231. local j = radius * math.cos(rads + angle) * scaleX + center.x
  232. local k = radius * math.sin(rads + angle) * scaleY + center.y
  233. vertices[i * 2 - 1] = j
  234. vertices[i * 2] = k
  235. end
  236. vertices[(segments + 2) * 2 - 1] = center.x
  237. vertices[(segments + 2) * 2] = center.y
  238. gl.bufferData(gl.ARRAY_BUFFER, (segments + 2) * 2, vertices, gl.STATIC_DRAW)
  239. gl.bindBuffer(gl.ARRAY_BUFFER, 0)
  240. end
  241. initBuffer()
  242. setDrawProperty()
  243. gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id)
  244. gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
  245. gl.drawArrays(gl.TRIANGLE_FAN , 0, segments + 1)
  246. gl.bindBuffer(gl.ARRAY_BUFFER,0)
  247. end
  248. function ccDrawQuadBezier(origin, control, destination, segments)
  249. if not lazy_init() then
  250. return
  251. end
  252. local vertexBuffer = { }
  253. local function initBuffer()
  254. local vertices = { }
  255. local i = 1
  256. local t = 0.0
  257. for i = 1, segments do
  258. vertices[2 * i - 1] = math.pow(1 - t,2) * origin.x + 2.0 * (1 - t) * t * control.x + t * t * destination.x
  259. vertices[2 * i] = math.pow(1 - t,2) * origin.y + 2.0 * (1 - t) * t * control.y + t * t * destination.y
  260. t = t + 1.0 / segments
  261. end
  262. vertices[2 * (segments + 1) - 1] = destination.x
  263. vertices[2 * (segments + 1)] = destination.y
  264. vertexBuffer.buffer_id = gl.createBuffer()
  265. gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id)
  266. gl.bufferData(gl.ARRAY_BUFFER, (segments + 1) * 2, vertices, gl.STATIC_DRAW)
  267. gl.bindBuffer(gl.ARRAY_BUFFER, 0)
  268. end
  269. initBuffer()
  270. setDrawProperty()
  271. gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id)
  272. gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
  273. gl.drawArrays(gl.LINE_STRIP , 0, segments + 1)
  274. gl.bindBuffer(gl.ARRAY_BUFFER,0)
  275. end
  276. function ccDrawCubicBezier(origin, control1, control2, destination, segments)
  277. if not lazy_init then
  278. return
  279. end
  280. local vertexBuffer = { }
  281. local function initBuffer()
  282. local vertices = { }
  283. local t = 0
  284. local i = 1
  285. for i = 1, segments do
  286. 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
  287. 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
  288. t = t + 1.0 / segments
  289. end
  290. vertices[2 * (segments + 1) - 1] = destination.x
  291. vertices[2 * (segments + 1)] = destination.y
  292. vertexBuffer.buffer_id = gl.createBuffer()
  293. gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id)
  294. gl.bufferData(gl.ARRAY_BUFFER, (segments + 1) * 2, vertices, gl.STATIC_DRAW)
  295. gl.bindBuffer(gl.ARRAY_BUFFER, 0)
  296. end
  297. initBuffer()
  298. setDrawProperty()
  299. gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id)
  300. gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0)
  301. gl.drawArrays(gl.LINE_STRIP , 0, segments + 1)
  302. gl.bindBuffer(gl.ARRAY_BUFFER,0)
  303. end