-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrealtime_glut.nim
68 lines (55 loc) · 2.1 KB
/
realtime_glut.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
## This example show how to have real time cairo using glut backend
import opengl/glut, opengl, opengl/glu, math
import cairo
let
w: int32 = 256
h: int32 = 256
var
surface = imageSurfaceCreate(FORMAT_ARGB32, w, h)
frameCount = 0
proc display() {.cdecl.} =
## Called every frame by GLUT
# draw shiny sphere on gradient background
var ctx = surface.create()
var linerGradient = patternCreateLinear(0.0, 0.0, 0.0, 256.0)
linerGradient.addColorStopRgba(1, 0, 0, 0, 1)
linerGradient.addColorStopRgba(0, 1, 1, 1, 1)
ctx.rectangle(0, 0, 256, 256)
ctx.setSource(linerGradient)
ctx.fill()
var radialGradient = patternCreateRadial(115.2, 102.4, 25.6, 102.4, 102.4, 128.0)
radialGradient.addColorStopRgba(0, 1, 1, 1, 1)
radialGradient.addColorStopRgba(1, 0, 0, 0, 1)
ctx.setSource(radialGradient)
ctx.arc(128.0, 128.0 + sin(float(frameCount)/10.0) * 20, 76.8, 0, 2 * PI)
ctx.fill()
# update texture with new pixels from surface
var dataPtr = surface.getData()
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, GLsizei w, GLsizei h, GL_RGBA, GL_UNSIGNED_BYTE, dataPtr);
# draw a quad over the whole screen
glClear(GL_COLOR_BUFFER_BIT)
glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0); glVertex2d(-1.0, +1.0)
glTexCoord2d(1.0, 0.0); glVertex2d(+1.0, +1.0)
glTexCoord2d(1.0, 1.0); glVertex2d(+1.0, -1.0)
glTexCoord2d(0.0, 1.0); glVertex2d(-1.0, -1.0)
glEnd();
glutSwapBuffers()
inc frameCount
glutPostRedisplay() # ask glut to draw next frame
var argc: cint = 0
glutInit(addr argc, nil)
glutInitDisplayMode(GLUT_DOUBLE)
glutInitWindowSize(w, h)
discard glutCreateWindow("Real time GLUT/Cairo example")
glutDisplayFunc(display)
loadExtensions()
# allocate a texture and bind it
var dataPtr = surface.getData()
glTexImage2D(GL_TEXTURE_2D, 0, 3, GLsizei w, GLsizei h, 0, GL_RGBA, GL_UNSIGNED_BYTE, dataPtr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glEnable(GL_TEXTURE_2D);
glutMainLoop()