chart.go 17 KB

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