-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcommon.nim
71 lines (57 loc) · 2.07 KB
/
common.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
import paranim/gl, paranim/gl/uniforms, paranim/gl/attributes
import nimgl/opengl
import glm
from sequtils import map
from std/math import nil
import paranim/math as pmath
type
Game* = object of RootGame
deltaTime*: float
totalTime*: float
frameWidth*: int
frameHeight*: int
proc project*[UniT, AttrT](entity: var Entity[UniT, AttrT], left: GLfloat, right: GLfloat, bottom: GLfloat, top: GLfloat, near: GLfloat, far: GLfloat) =
entity.uniforms.u_matrix.project(left, right, bottom, top, near, far)
proc project*[UniT, AttrT](entity: var Entity[UniT, AttrT], fieldOfView: GLfloat, aspect: GLfloat, near: GLfloat, far: GLfloat) =
entity.uniforms.u_matrix.project(fieldOfView, aspect, near, far)
proc translate*[UniT, AttrT](entity: var Entity[UniT, AttrT], x: GLfloat, y: GLfloat, z: GLfloat) =
entity.uniforms.u_matrix.translate(x, y, z)
proc scale*[UniT, AttrT](entity: var Entity[UniT, AttrT], x: GLfloat, y: GLfloat, z: GLfloat) =
entity.uniforms.u_matrix.scale(x, y, z)
proc rotateX*[UniT, AttrT](entity: var Entity[UniT, AttrT], angle: GLFloat) =
entity.uniforms.u_matrix.rotateX(angle)
proc rotateY*[UniT, AttrT](entity: var Entity[UniT, AttrT], angle: GLFloat) =
entity.uniforms.u_matrix.rotateY(angle)
proc rotateZ*[UniT, AttrT](entity: var Entity[UniT, AttrT], angle: GLFloat) =
entity.uniforms.u_matrix.rotateZ(angle)
proc invert*[UniT, AttrT](entity: var Entity[UniT, AttrT], cam: Mat4x4[GLfloat]) =
entity.uniforms.u_matrix.invert(cam)
proc degToRad*(degrees: GLfloat): GLfloat =
(degrees * math.PI) / 180f
# textured 3D entity
const threeDTextureVertexShader* =
"""
#version 330
uniform mat4 u_matrix;
in vec4 a_position;
in vec2 a_texcoord;
out vec2 v_texcoord;
void main()
{
gl_Position = u_matrix * a_position;
v_texcoord = a_texcoord;
v_texcoord.y = 1.0 - v_texcoord.y; // flip y axis
}
"""
const threeDTextureFragmentShader* =
"""
#version 330
precision mediump float;
uniform sampler2D u_texture;
in vec2 v_texcoord;
out vec4 outColor;
void main()
{
outColor = texture(u_texture, v_texcoord);
}
"""