chart.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. package gui
  2. import (
  3. "fmt"
  4. "github.com/g3n/engine/core"
  5. "github.com/g3n/engine/geometry"
  6. "github.com/g3n/engine/gls"
  7. "github.com/g3n/engine/graphic"
  8. "github.com/g3n/engine/material"
  9. "github.com/g3n/engine/math32"
  10. "github.com/g3n/engine/renderer/shader"
  11. "math"
  12. )
  13. func init() {
  14. shader.AddShader("shaderChartVertex", shaderChartVertex)
  15. shader.AddShader("shaderChartFrag", shaderChartFrag)
  16. shader.AddProgram("shaderChart", "shaderChartVertex", "shaderChartFrag")
  17. }
  18. //
  19. //
  20. // ChartLine implements a panel which can contain several line charts
  21. //
  22. //
  23. type ChartLine struct {
  24. Panel // Embedded panel
  25. left float32 // Left margin in pixels
  26. bottom float32 // Bottom margin in pixels
  27. top float32 // Top margin in pixels
  28. firstX float32 // Value for the first x label
  29. stepX float32 // Step for the next x label
  30. countStepX float32 // Number of values per x step
  31. minY float32 // Minimum Y value
  32. maxY float32 // Maximum Y value
  33. autoY bool // Auto range flag for Y values
  34. formatX string // String format for scale X labels
  35. formatY string // String format for scale Y labels
  36. title *Label // Optional title label
  37. scaleX *ChartScaleX // X scale panel
  38. scaleY *ChartScaleY // Y scale panel
  39. labelsX []*Label // Array of scale X labels
  40. labelsY []*Label // Array of scale Y labels
  41. graphs []*LineGraph // Array of line graphs
  42. }
  43. const (
  44. deltaLine = 0.001 // Delta in NDC for lines over the boundary
  45. )
  46. // NewChartLine creates and returns a new line chart panel with
  47. // the specified dimensions in pixels.
  48. func NewChartLine(width, height float32) *ChartLine {
  49. cl := new(ChartLine)
  50. cl.Panel.Initialize(width, height)
  51. cl.left = 34
  52. cl.bottom = 20
  53. cl.top = 10
  54. cl.firstX = 0
  55. cl.stepX = 1
  56. cl.countStepX = 0
  57. cl.minY = -10.0
  58. cl.maxY = 10.0
  59. cl.autoY = false
  60. cl.formatX = "%v"
  61. cl.formatY = "%v"
  62. return cl
  63. }
  64. //func (cl *ChartLine) SetMargins(left, bottom float32) {
  65. //
  66. // cl.baseX, cl.baseY = cl.Pix2NDC(left, bottom)
  67. // cl.recalc()
  68. //}
  69. // SetTitle sets the chart title
  70. func (cl *ChartLine) SetTitle(title *Label) {
  71. if cl.title != nil {
  72. cl.Remove(cl.title)
  73. cl.title = nil
  74. }
  75. if title != nil {
  76. cl.Add(title)
  77. cl.title = title
  78. }
  79. cl.recalc()
  80. }
  81. // SetFormatX sets the string format of the X scale labels
  82. func (cl *ChartLine) SetFormatX(format string) {
  83. cl.formatX = format
  84. cl.updateLabelsX()
  85. }
  86. // SetFormatY sets the string format of the Y scale labels
  87. func (cl *ChartLine) SetFormatY(format string) {
  88. cl.formatY = format
  89. cl.updateLabelsY()
  90. }
  91. // SetScaleX sets the X scale number of lines and color
  92. func (cl *ChartLine) SetScaleX(lines int, color *math32.Color) {
  93. if cl.scaleX != nil {
  94. cl.ClearScaleX()
  95. }
  96. // Add scale lines
  97. cl.scaleX = newChartScaleX(cl, lines, color)
  98. cl.Add(cl.scaleX)
  99. // Add scale labels
  100. // The positions of the labels will be set by 'recalc()'
  101. value := cl.firstX
  102. for i := 0; i < lines; i++ {
  103. l := NewLabel(fmt.Sprintf(cl.formatX, value))
  104. cl.Add(l)
  105. cl.labelsX = append(cl.labelsX, l)
  106. value += cl.stepX
  107. }
  108. cl.recalc()
  109. }
  110. // ClearScaleX removes the X scale if it was previously set
  111. func (cl *ChartLine) ClearScaleX() {
  112. if cl.scaleX == nil {
  113. return
  114. }
  115. // Remove and dispose scale lines
  116. cl.Remove(cl.scaleX)
  117. cl.scaleX.Dispose()
  118. // Remove and dispose scale labels
  119. for i := 0; i < len(cl.labelsX); i++ {
  120. label := cl.labelsX[i]
  121. cl.Remove(label)
  122. label.Dispose()
  123. }
  124. cl.labelsX = cl.labelsX[0:0]
  125. cl.scaleX = nil
  126. }
  127. // SetScaleY sets the Y scale number of lines and color
  128. func (cl *ChartLine) SetScaleY(lines int, color *math32.Color) {
  129. if cl.scaleY != nil {
  130. cl.ClearScaleY()
  131. }
  132. if lines < 2 {
  133. lines = 2
  134. }
  135. // Add scale lines
  136. cl.scaleY = newChartScaleY(cl, lines, color)
  137. cl.Add(cl.scaleY)
  138. // Add scale labels
  139. // The position of the labels will be set by 'recalc()'
  140. value := cl.minY
  141. step := (cl.maxY - cl.minY) / float32(lines-1)
  142. for i := 0; i < lines; i++ {
  143. l := NewLabel(fmt.Sprintf(cl.formatY, value))
  144. cl.Add(l)
  145. cl.labelsY = append(cl.labelsY, l)
  146. value += step
  147. }
  148. cl.recalc()
  149. }
  150. // ClearScaleY removes the Y scale if it was previously set
  151. func (cl *ChartLine) ClearScaleY() {
  152. if cl.scaleY == nil {
  153. return
  154. }
  155. // Remove and dispose scale lines
  156. cl.Remove(cl.scaleY)
  157. cl.scaleY.Dispose()
  158. // Remove and dispose scale labels
  159. for i := 0; i < len(cl.labelsY); i++ {
  160. label := cl.labelsY[i]
  161. cl.Remove(label)
  162. label.Dispose()
  163. }
  164. cl.labelsY = cl.labelsY[0:0]
  165. cl.scaleY = nil
  166. }
  167. // SetRangeX sets the X scale labels and range per step
  168. // firstX is the value of first label of the x scale
  169. // stepX is the step to be added to get the next x scale label
  170. // countStepX is the number of elements of the data buffer for each line step
  171. func (cl *ChartLine) SetRangeX(firstX float32, stepX float32, countStepX float32) {
  172. cl.firstX = firstX
  173. cl.stepX = stepX
  174. cl.countStepX = countStepX
  175. cl.updateGraphs()
  176. }
  177. // SetRangeY sets the minimum and maximum values of the y scale
  178. func (cl *ChartLine) SetRangeY(min float32, max float32) {
  179. if cl.autoY {
  180. return
  181. }
  182. cl.minY = min
  183. cl.maxY = max
  184. cl.updateGraphs()
  185. }
  186. // SetRangeYauto sets the state of the auto
  187. func (cl *ChartLine) SetRangeYauto(auto bool) {
  188. cl.autoY = auto
  189. if !auto {
  190. return
  191. }
  192. cl.updateGraphs()
  193. }
  194. // Returns the current y range
  195. func (cl *ChartLine) RangeY() (minY, maxY float32) {
  196. return cl.minY, cl.maxY
  197. }
  198. // AddLine adds a line graph to the chart
  199. func (cl *ChartLine) AddGraph(color *math32.Color, data []float32) *LineGraph {
  200. graph := newLineGraph(cl, color, data)
  201. cl.graphs = append(cl.graphs, graph)
  202. cl.Add(graph)
  203. cl.recalc()
  204. cl.updateGraphs()
  205. return graph
  206. }
  207. // RemoveGraph removes and disposes of the specified graph from the chart
  208. func (cl *ChartLine) RemoveGraph(g *LineGraph) {
  209. cl.Remove(g)
  210. g.Dispose()
  211. for pos, current := range cl.graphs {
  212. if current == g {
  213. copy(cl.graphs[pos:], cl.graphs[pos+1:])
  214. cl.graphs[len(cl.graphs)-1] = nil
  215. cl.graphs = cl.graphs[:len(cl.graphs)-1]
  216. break
  217. }
  218. }
  219. if !cl.autoY {
  220. return
  221. }
  222. cl.updateGraphs()
  223. }
  224. // updateLabelsX updates the X scale labels text
  225. func (cl *ChartLine) updateLabelsX() {
  226. if cl.scaleX == nil {
  227. return
  228. }
  229. pstep := (cl.ContentWidth() - cl.left) / float32(len(cl.labelsX))
  230. value := cl.firstX
  231. for i := 0; i < len(cl.labelsX); i++ {
  232. label := cl.labelsX[i]
  233. label.SetText(fmt.Sprintf(cl.formatX, value))
  234. px := cl.left + float32(i)*pstep
  235. label.SetPosition(px, cl.ContentHeight()-cl.bottom)
  236. value += cl.stepX
  237. }
  238. }
  239. // updateLabelsY updates the Y scale labels text and positions
  240. func (cl *ChartLine) updateLabelsY() {
  241. if cl.scaleY == nil {
  242. return
  243. }
  244. th := float32(0)
  245. if cl.title != nil {
  246. th = cl.title.height
  247. }
  248. nlines := cl.scaleY.lines
  249. vstep := (cl.maxY - cl.minY) / float32(nlines-1)
  250. pstep := (cl.ContentHeight() - th - cl.top - cl.bottom) / float32(nlines-1)
  251. value := cl.minY
  252. for i := 0; i < nlines; i++ {
  253. label := cl.labelsY[i]
  254. label.SetText(fmt.Sprintf(cl.formatY, value))
  255. px := cl.left - 2 - label.Width()
  256. if px < 0 {
  257. px = 0
  258. }
  259. py := cl.ContentHeight() - cl.bottom - float32(i)*pstep
  260. label.SetPosition(px, py-label.Height()/2)
  261. value += vstep
  262. }
  263. }
  264. // calcRangeY calculates the minimum and maximum y values for all graphs
  265. func (cl *ChartLine) calcRangeY() {
  266. if !cl.autoY || len(cl.graphs) == 0 {
  267. return
  268. }
  269. minY := float32(math.MaxFloat32)
  270. maxY := -float32(math.MaxFloat32)
  271. for g := 0; g < len(cl.graphs); g++ {
  272. graph := cl.graphs[g]
  273. for x := 0; x < len(graph.data); x++ {
  274. vy := graph.data[x]
  275. if vy < minY {
  276. minY = vy
  277. }
  278. if vy > maxY {
  279. maxY = vy
  280. }
  281. }
  282. }
  283. cl.minY = minY
  284. cl.maxY = maxY
  285. }
  286. // updateGraphs should be called when the range the scales change or
  287. // any graph data changes
  288. func (cl *ChartLine) updateGraphs() {
  289. cl.calcRangeY()
  290. cl.updateLabelsX()
  291. cl.updateLabelsY()
  292. for i := 0; i < len(cl.graphs); i++ {
  293. g := cl.graphs[i]
  294. g.updateData()
  295. }
  296. }
  297. // recalc recalculates the positions of the inner panels
  298. func (cl *ChartLine) recalc() {
  299. // Center title position
  300. if cl.title != nil {
  301. xpos := (cl.ContentWidth() - cl.title.width) / 2
  302. cl.title.SetPositionX(xpos)
  303. }
  304. // Recalc scale X and its labels
  305. if cl.scaleX != nil {
  306. cl.scaleX.recalc()
  307. cl.updateLabelsX()
  308. }
  309. // Recalc scale Y and its labels
  310. if cl.scaleY != nil {
  311. cl.scaleY.recalc()
  312. cl.updateLabelsY()
  313. }
  314. // Recalc graphs
  315. for i := 0; i < len(cl.graphs); i++ {
  316. g := cl.graphs[i]
  317. g.recalc()
  318. cl.SetTopChild(g)
  319. }
  320. }
  321. //
  322. //
  323. // ChartScaleX is a panel with GL_LINES geometry which draws the chart X horizontal scale axis,
  324. // vertical lines and line labels.
  325. //
  326. //
  327. type ChartScaleX struct {
  328. Panel // Embedded panel
  329. chart *ChartLine // Container chart
  330. lines int // Number of vertical lines
  331. bounds gls.Uniform4f // Bound uniform in OpenGL window coordinates
  332. mat chartMaterial // Chart material
  333. }
  334. // newChartScaleX creates and returns a pointer to a new ChartScaleX for the specified
  335. // chart, number of lines and color
  336. func newChartScaleX(chart *ChartLine, lines int, color *math32.Color) *ChartScaleX {
  337. sx := new(ChartScaleX)
  338. sx.chart = chart
  339. sx.lines = lines
  340. sx.bounds.Init("Bounds")
  341. // Appends bottom horizontal line
  342. positions := math32.NewArrayF32(0, 0)
  343. positions.Append(0, -1+deltaLine, 0, 1, -1+deltaLine, 0)
  344. // Appends vertical lines
  345. step := 1 / float32(lines)
  346. for i := 0; i < lines; i++ {
  347. nx := float32(i) * step
  348. if i == 0 {
  349. nx += deltaLine
  350. }
  351. positions.Append(nx, 0, 0, nx, -1, 0)
  352. }
  353. // Creates geometry and adds VBO
  354. geom := geometry.NewGeometry()
  355. geom.AddVBO(gls.NewVBO().AddAttrib("VertexPosition", 3).SetBuffer(positions))
  356. // Initializes the panel graphic
  357. gr := graphic.NewGraphic(geom, gls.LINES)
  358. sx.mat.Init(color)
  359. gr.AddMaterial(sx, &sx.mat, 0, 0)
  360. sx.Panel.InitializeGraphic(chart.ContentWidth(), chart.ContentHeight(), gr)
  361. sx.recalc()
  362. return sx
  363. }
  364. // recalc recalculates the position and size of this scale inside its parent
  365. func (sx *ChartScaleX) recalc() {
  366. py := sx.chart.top
  367. if sx.chart.title != nil {
  368. py += sx.chart.title.Height()
  369. }
  370. sx.SetPosition(sx.chart.left, py)
  371. sx.SetSize(sx.chart.ContentWidth()-sx.chart.left, sx.chart.ContentHeight()-py-sx.chart.bottom)
  372. }
  373. // RenderSetup is called by the renderer before drawing this graphic
  374. // It overrides the original panel RenderSetup
  375. // Calculates the model matrix and transfer to OpenGL.
  376. func (sx *ChartScaleX) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
  377. //log.Error("ChartScaleX RenderSetup:%v", sx.pospix)
  378. // Sets model matrix and transfer to shader
  379. var mm math32.Matrix4
  380. sx.SetModelMatrix(gs, &mm)
  381. sx.modelMatrixUni.SetMatrix4(&mm)
  382. sx.modelMatrixUni.Transfer(gs)
  383. // Sets bounds in OpenGL window coordinates and transfer to shader
  384. _, _, _, height := gs.GetViewport()
  385. sx.bounds.Set(sx.pospix.X, float32(height)-sx.pospix.Y, sx.width, sx.height)
  386. sx.bounds.Transfer(gs)
  387. }
  388. //
  389. //
  390. // ChartScaleY is a panel with LINE geometry which draws the chart Y vertical scale axis,
  391. // horizontal and labels.
  392. //
  393. //
  394. type ChartScaleY struct {
  395. Panel // Embedded panel
  396. chart *ChartLine // Container chart
  397. lines int // Number of horizontal lines
  398. bounds gls.Uniform4f // Bound uniform in OpenGL window coordinates
  399. mat chartMaterial // Chart material
  400. }
  401. // newChartScaleY creates and returns a pointer to a new ChartScaleY for the specified
  402. // chart, number of lines and color
  403. func newChartScaleY(chart *ChartLine, lines int, color *math32.Color) *ChartScaleY {
  404. if lines < 2 {
  405. lines = 2
  406. }
  407. sy := new(ChartScaleY)
  408. sy.chart = chart
  409. sy.lines = lines
  410. sy.bounds.Init("Bounds")
  411. // Appends left vertical line
  412. positions := math32.NewArrayF32(0, 0)
  413. positions.Append(0+deltaLine, 0, 0, 0+deltaLine, -1, 0)
  414. // Appends horizontal lines
  415. step := 1 / float32(lines-1)
  416. for i := 0; i < lines; i++ {
  417. ny := -1 + float32(i)*step
  418. if i == 0 {
  419. ny += deltaLine
  420. }
  421. if i == lines-1 {
  422. ny -= deltaLine
  423. }
  424. positions.Append(0, ny, 0, 1, ny, 0)
  425. }
  426. // Creates geometry and adds VBO
  427. geom := geometry.NewGeometry()
  428. geom.AddVBO(gls.NewVBO().AddAttrib("VertexPosition", 3).SetBuffer(positions))
  429. // Initializes the panel with this graphic
  430. gr := graphic.NewGraphic(geom, gls.LINES)
  431. sy.mat.Init(color)
  432. gr.AddMaterial(sy, &sy.mat, 0, 0)
  433. sy.Panel.InitializeGraphic(chart.ContentWidth(), chart.ContentHeight(), gr)
  434. sy.recalc()
  435. return sy
  436. }
  437. // recalc recalculates the position and size of this scale inside its parent
  438. func (sy *ChartScaleY) recalc() {
  439. py := sy.chart.top
  440. if sy.chart.title != nil {
  441. py += sy.chart.title.Height()
  442. }
  443. sy.SetPosition(sy.chart.left, py)
  444. sy.SetSize(sy.chart.ContentWidth()-sy.chart.left, sy.chart.ContentHeight()-py-sy.chart.bottom)
  445. }
  446. // RenderSetup is called by the renderer before drawing this graphic
  447. // It overrides the original panel RenderSetup
  448. // Calculates the model matrix and transfer to OpenGL.
  449. func (sy *ChartScaleY) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
  450. //log.Error("ChartScaleY RenderSetup:%v", sy.pospix)
  451. // Sets model matrix and transfer to shader
  452. var mm math32.Matrix4
  453. sy.SetModelMatrix(gs, &mm)
  454. sy.modelMatrixUni.SetMatrix4(&mm)
  455. sy.modelMatrixUni.Transfer(gs)
  456. // Sets bounds in OpenGL window coordinates and transfer to shader
  457. _, _, _, height := gs.GetViewport()
  458. sy.bounds.Set(sy.pospix.X, float32(height)-sy.pospix.Y, sy.width, sy.height)
  459. sy.bounds.Transfer(gs)
  460. }
  461. //
  462. //
  463. // LineGraph
  464. //
  465. //
  466. type LineGraph struct {
  467. Panel // Embedded panel
  468. chart *ChartLine // Container chart
  469. color math32.Color // Line color
  470. data []float32 // Data y
  471. bounds gls.Uniform4f // Bound uniform in OpenGL window coordinates
  472. mat chartMaterial // Chart material
  473. vbo *gls.VBO
  474. positions math32.ArrayF32
  475. }
  476. func newLineGraph(chart *ChartLine, color *math32.Color, y []float32) *LineGraph {
  477. lg := new(LineGraph)
  478. lg.bounds.Init("Bounds")
  479. lg.chart = chart
  480. lg.color = *color
  481. lg.data = y
  482. // Creates geometry and adds VBO with positions
  483. geom := geometry.NewGeometry()
  484. lg.vbo = gls.NewVBO().AddAttrib("VertexPosition", 3)
  485. lg.positions = math32.NewArrayF32(0, 0)
  486. lg.vbo.SetBuffer(lg.positions)
  487. geom.AddVBO(lg.vbo)
  488. // Initializes the panel with this graphic
  489. gr := graphic.NewGraphic(geom, gls.LINE_STRIP)
  490. lg.mat.Init(&lg.color)
  491. gr.AddMaterial(lg, &lg.mat, 0, 0)
  492. lg.Panel.InitializeGraphic(lg.chart.ContentWidth(), lg.chart.ContentHeight(), gr)
  493. lg.SetData(y)
  494. return lg
  495. }
  496. // SetColor sets the color of the graph
  497. func (lg *LineGraph) SetColor(color *math32.Color) {
  498. lg.mat.color.SetColor(color)
  499. }
  500. // SetData sets the graph data
  501. func (lg *LineGraph) SetData(data []float32) {
  502. lg.data = data
  503. lg.updateData()
  504. }
  505. // SetLineWidth sets the graph line width
  506. func (lg *LineGraph) SetLineWidth(width float32) {
  507. lg.mat.SetLineWidth(width)
  508. }
  509. func (lg *LineGraph) updateData() {
  510. lines := 1
  511. if lg.chart.scaleX != nil {
  512. lines = lg.chart.scaleX.lines
  513. }
  514. step := 1.0 / (float32(lines) * lg.chart.countStepX)
  515. positions := math32.NewArrayF32(0, 0)
  516. rangeY := lg.chart.maxY - lg.chart.minY
  517. for i := 0; i < len(lg.data); i++ {
  518. px := float32(i) * step
  519. vy := lg.data[i]
  520. py := -1 + ((vy - lg.chart.minY) / rangeY)
  521. positions.Append(px, py, 0)
  522. }
  523. lg.vbo.SetBuffer(positions)
  524. }
  525. func (lg *LineGraph) recalc() {
  526. py := lg.chart.top
  527. if lg.chart.title != nil {
  528. py += lg.chart.title.Height()
  529. }
  530. px := lg.chart.left
  531. w := lg.chart.ContentWidth() - lg.chart.left
  532. h := lg.chart.ContentHeight() - py - lg.chart.bottom
  533. lg.SetPosition(px, py)
  534. lg.SetSize(w, h)
  535. }
  536. // RenderSetup is called by the renderer before drawing this graphic
  537. // It overrides the original panel RenderSetup
  538. // Calculates the model matrix and transfer to OpenGL.
  539. func (lg *LineGraph) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
  540. //log.Error("LineGraph RenderSetup:%v with/height: %v/%v", lg.posclip, lg.wclip, lg.hclip)
  541. // Sets model matrix and transfer to shader
  542. var mm math32.Matrix4
  543. lg.SetModelMatrix(gs, &mm)
  544. lg.modelMatrixUni.SetMatrix4(&mm)
  545. lg.modelMatrixUni.Transfer(gs)
  546. // Sets bounds in OpenGL window coordinates and transfer to shader
  547. _, _, _, height := gs.GetViewport()
  548. lg.bounds.Set(lg.pospix.X, float32(height)-lg.pospix.Y, lg.width, lg.height)
  549. lg.bounds.Transfer(gs)
  550. }
  551. //
  552. //
  553. // Chart material (for lines)
  554. //
  555. //
  556. type chartMaterial struct {
  557. material.Material // Embedded material
  558. color *gls.Uniform3f // Emissive color uniform
  559. }
  560. func (cm *chartMaterial) Init(color *math32.Color) {
  561. cm.Material.Init()
  562. cm.SetShader("shaderChart")
  563. // Creates uniforms and adds to material
  564. cm.color = gls.NewUniform3f("MatColor")
  565. // Set initial values
  566. cm.color.SetColor(color)
  567. }
  568. func (cm *chartMaterial) RenderSetup(gs *gls.GLS) {
  569. cm.Material.RenderSetup(gs)
  570. cm.color.Transfer(gs)
  571. }
  572. //
  573. // Vertex Shader template
  574. //
  575. const shaderChartVertex = `
  576. #version {{.Version}}
  577. // Vertex attributes
  578. {{template "attributes" .}}
  579. // Input uniforms
  580. uniform mat4 ModelMatrix;
  581. uniform vec3 MatColor;
  582. // Outputs for fragment shader
  583. out vec3 Color;
  584. void main() {
  585. Color = MatColor;
  586. // Set position
  587. vec4 pos = vec4(VertexPosition.xyz, 1);
  588. vec4 posclip = ModelMatrix * pos;
  589. gl_Position = posclip;
  590. }
  591. `
  592. //
  593. // Fragment Shader template
  594. //
  595. const shaderChartFrag = `
  596. #version {{.Version}}
  597. // Input uniforms from vertex shader
  598. in vec3 Color;
  599. // Input uniforms
  600. uniform vec4 Bounds;
  601. // Output
  602. out vec4 FragColor;
  603. void main() {
  604. // Discard fragment outside of the received bounds in OpenGL window pixel coordinates
  605. // Bounds[0] - x
  606. // Bounds[1] - y
  607. // Bounds[2] - width
  608. // Bounds[3] - height
  609. if (gl_FragCoord.x < Bounds[0] || gl_FragCoord.x > Bounds[0] + Bounds[2]) {
  610. discard;
  611. }
  612. if (gl_FragCoord.y > Bounds[1] || gl_FragCoord.y < Bounds[1] - Bounds[3]) {
  613. discard;
  614. }
  615. FragColor = vec4(Color, 1.0);
  616. }
  617. `