-
Notifications
You must be signed in to change notification settings - Fork 0
/
xx0.nim
137 lines (109 loc) · 4.12 KB
/
xx0.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# OpenGL example using SDL2
import sdl2
import opengl
import glu
discard sdl2.init(INIT_EVERYTHING)
var screenWidth: cint = 640
var screenHeight: cint = 480
var window = createWindow("SDL/OpenGL Skeleton", 100, 100, screenWidth, screenHeight, SDL_WINDOW_OPENGL or SDL_WINDOW_RESIZABLE)
# context =
discard window.glCreateContext()
# Initialize OpenGL
loadExtensions()
glClearColor(0.0, 0.0, 0.0, 1.0) # Set background color to black and opaque
glClearDepth(1.0) # Set background depth to farthest
glEnable(GL_DEPTH_TEST) # Enable depth testing for z-culling
glDepthFunc(GL_LEQUAL) # Set the type of depth-test
glShadeModel(GL_SMOOTH) # Enable smooth shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # Nice perspective corrections
proc reshape(newWidth: cint, newHeight: cint) =
glViewport(0, 0, newWidth, newHeight) # Set the viewport to cover the new window
glMatrixMode(GL_PROJECTION) # To operate on the projection matrix
glLoadIdentity() # Reset
gluPerspective(45.0, newWidth / newHeight, 0.1, 100.0) # Enable perspective projection with fovy, aspect, zNear and zFar
proc render() =
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) # Clear color and depth buffers
glMatrixMode(GL_MODELVIEW) # To operate on model-view matrix
glLoadIdentity() # Reset the model-view matrix
glTranslatef(1.5, 0.0, -7.0) # Move right and into the screen
# Render a cube consisting of 6 quads
# Each quad consists of 2 triangles
# Each triangle consists of 3 vertices
glBegin(GL_TRIANGLES) # Begin drawing of triangles
# Top face (y = 1.0f)
glColor3f(0.0, 1.0, 0.0) # Green
glVertex3f( 1.0, 1.0, -1.0)
glVertex3f(-1.0, 1.0, -1.0)
glVertex3f(-1.0, 1.0, 1.0)
glVertex3f( 1.0, 1.0, 1.0)
glVertex3f( 1.0, 1.0, -1.0)
glVertex3f(-1.0, 1.0, 1.0)
# Bottom face (y = -1.0f)
glColor3f(1.0, 0.5, 0.0) # Orange
glVertex3f( 1.0, -1.0, 1.0)
glVertex3f(-1.0, -1.0, 1.0)
glVertex3f(-1.0, -1.0, -1.0)
glVertex3f( 1.0, -1.0, -1.0)
glVertex3f( 1.0, -1.0, 1.0)
glVertex3f(-1.0, -1.0, -1.0)
# Front face (z = 1.0f)
glColor3f(1.0, 0.0, 0.0) # Red
glVertex3f( 1.0, 1.0, 1.0)
glVertex3f(-1.0, 1.0, 1.0)
glVertex3f(-1.0, -1.0, 1.0)
glVertex3f( 1.0, -1.0, 1.0)
glVertex3f( 1.0, 1.0, 1.0)
glVertex3f(-1.0, -1.0, 1.0)
# Back face (z = -1.0f)
glColor3f(1.0, 1.0, 0.0) # Yellow
glVertex3f( 1.0, -1.0, -1.0)
glVertex3f(-1.0, -1.0, -1.0)
glVertex3f(-1.0, 1.0, -1.0)
glVertex3f( 1.0, 1.0, -1.0)
glVertex3f( 1.0, -1.0, -1.0)
glVertex3f(-1.0, 1.0, -1.0)
# Left face (x = -1.0f)
glColor3f(0.0, 0.0, 1.0) # Blue
glVertex3f(-1.0, 1.0, 1.0)
glVertex3f(-1.0, 1.0, -1.0)
glVertex3f(-1.0, -1.0, -1.0)
glVertex3f(-1.0, -1.0, 1.0)
glVertex3f(-1.0, 1.0, 1.0)
glVertex3f(-1.0, -1.0, -1.0)
# Right face (x = 1.0f)
glColor3f(1.0, 0.0, 1.0) # Magenta
glVertex3f(1.0, 1.0, -1.0)
glVertex3f(1.0, 1.0, 1.0)
glVertex3f(1.0, -1.0, 1.0)
glVertex3f(1.0, -1.0, -1.0)
glVertex3f(1.0, 1.0, -1.0)
glVertex3f(1.0, -1.0, 1.0)
glEnd() # End of drawing
window.glSwapWindow() # Swap the front and back frame buffers (double buffering)
# Frame rate limiter
let targetFramePeriod: uint32 = 20 # 20 milliseconds corresponds to 50 fps
var frameTime: uint32 = 0
proc limitFrameRate() =
let now = getTicks()
if frameTime > now:
delay(frameTime - now) # Delay to maintain steady frame rate
frameTime += targetFramePeriod
# Main loop
var
evt = sdl2.defaultEvent
runGame = true
reshape(screenWidth, screenHeight) # Set up initial viewport and projection
while runGame:
while pollEvent(evt):
if evt.kind == QuitEvent:
runGame = false
break
if evt.kind == WindowEvent:
var windowEvent = cast[WindowEventPtr](addr(evt))
if windowEvent.event == WindowEvent_Resized:
let newWidth = windowEvent.data1
let newHeight = windowEvent.data2
reshape(newWidth, newHeight)
render()
limitFrameRate()
destroy window