Skip to content

Commit 2f92a21

Browse files
committed
createeffect command now sets up an effect class with a shader
1 parent 3530412 commit 2f92a21

File tree

8 files changed

+135
-46
lines changed

8 files changed

+135
-46
lines changed

README.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ Manual setup (OS X / Linux):
7676
Controls
7777
========
7878

79-
- Currently you can control the camera with ``A``, ``W``, ``S`` and ``D``
79+
- Currently you can control the camera with ``A``, ``W``, ``S`` and ``D``.
80+
``Q`` and ``E`` for y axis movements
81+
- Standard yaw/pitch camera rotation with mouse
8082
- ``ESC`` for exit
8183
- ``SPACE`` for pause
8284
- ``X`` for taking ``png`` screenshot

demosys/core/management/commands/createeffect.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,30 @@ def handle(self, *args, **options):
2121
if os.path.exists(name):
2222
print(f"Directory {name} already exist. Aborting.")
2323
return
24+
25+
os.mkdir(name)
26+
os.mkdir(os.path.join(name, 'textures'))
27+
os.mkdir(os.path.join(name, 'shaders'))
28+
29+
# Create effect.py
30+
with open(os.path.join(name, 'effect.py'), 'w') as fd:
31+
fd.write(default_effect())
32+
33+
# Create default.glsl
34+
with open(os.path.join(name, 'shaders', 'default.glsl'), 'w') as fd:
35+
fd.write(default_shader())
36+
37+
38+
def default_effect():
39+
file_path = os.path.join(root_path(), 'effects/default/effect.py')
40+
return open(file_path, 'r').read()
41+
42+
43+
def default_shader():
44+
file_path = os.path.join(root_path(), 'effects/default/shaders/default.glsl')
45+
return open(file_path, 'r').read()
46+
47+
48+
def root_path():
49+
module_dir = os.path.dirname(globals()['__file__'])
50+
return os.path.dirname(os.path.dirname(os.path.dirname(module_dir)))

demosys/effects/default/effect.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from demosys.effects import Effect
2+
from demosys.opengl import geometry
3+
from OpenGL import GL
4+
from pyrr import matrix44
5+
6+
7+
class DefaultEffect(Effect):
8+
"""Generated default efffect"""
9+
def init(self):
10+
self.shader = self.get_shader("default.glsl")
11+
self.cube = geometry.cube(4.0, 4.0, 4.0)
12+
13+
def draw(self, time, target):
14+
GL.glEnable(GL.GL_DEPTH_TEST)
15+
16+
# Rotate and translate
17+
m_mv = self.create_transformation(rotation=(time * 1.2, time * 2.1, time * 0.25),
18+
translation=(0.0, 0.0, -4.0))
19+
20+
# Apply the rotation and translation from the system camera
21+
m_mv = matrix44.multiply(m_mv, self.sys_camera.view_matrix)
22+
23+
# Create normal matrix from model-view
24+
m_normal = self.create_normal_matrix(m_mv)
25+
26+
# Draw the cube
27+
self.cube.bind(self.shader)
28+
self.shader.uniform_mat4("m_proj", self.sys_camera.projection)
29+
self.shader.uniform_mat4("m_mv", m_mv)
30+
self.shader.uniform_mat3("m_normal", m_normal)
31+
self.cube.draw()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#version 410
2+
3+
#if defined VERTEX_SHADER
4+
5+
in vec3 in_position;
6+
in vec3 in_normal;
7+
8+
uniform mat4 m_proj;
9+
uniform mat4 m_mv;
10+
uniform mat3 m_normal;
11+
12+
out vec3 normal;
13+
out vec2 uv;
14+
15+
void main() {
16+
gl_Position = m_proj * m_mv * vec4(in_position, 1.0);
17+
normal = m_normal * in_normal;
18+
}
19+
20+
#elif defined FRAGMENT_SHADER
21+
22+
out vec4 fragColor;
23+
uniform float time;
24+
25+
in vec3 normal;
26+
27+
void main()
28+
{
29+
vec3 dir = vec3(0.0, 0.0, 1.0);
30+
float l = dot(dir, normal);
31+
fragColor = vec4(l);
32+
}
33+
34+
#endif

demosys/opengl/geometry.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ def quad_2d(width, height, xpos, ypos):
4545
vao.add_array_buffer(GL.GL_FLOAT, pos)
4646
vao.add_array_buffer(GL.GL_FLOAT, normals)
4747
vao.add_array_buffer(GL.GL_FLOAT, uvs)
48-
vao.map_buffer(pos, "in_Position", 3)
48+
vao.map_buffer(pos, "in_position", 3)
4949
vao.map_buffer(normals, "in_normal", 3)
50-
vao.map_buffer(uvs, "in_UV0", 2)
50+
vao.map_buffer(uvs, "in_uv", 2)
5151
vao.build()
5252
return vao
5353

@@ -186,11 +186,11 @@ def cube(width, height, depth, normals=True, uvs=True):
186186
vao.add_array_buffer(GL.GL_FLOAT, normals)
187187
if uvs:
188188
vao.add_array_buffer(GL.GL_FLOAT, uvs)
189-
vao.map_buffer(pos, "in_Position", 3)
189+
vao.map_buffer(pos, "in_position", 3)
190190
if normals:
191-
vao.map_buffer(normals, "in_Normal", 3)
191+
vao.map_buffer(normals, "in_normal", 3)
192192
if uvs:
193-
vao.map_buffer(uvs, "in_UV0", 2)
193+
vao.map_buffer(uvs, "in_uv", 2)
194194
vao.build()
195195
return vao
196196

@@ -217,6 +217,6 @@ def gen():
217217
pos = VBO(numpy.array(list(gen()), dtype=numpy.float32))
218218
vao = VAO("geometry:points_random")
219219
vao.add_array_buffer(GL.GL_FLOAT, pos)
220-
vao.map_buffer(pos, "in_Position", 3)
220+
vao.map_buffer(pos, "in_position", 3)
221221
vao.build()
222222
return vao

demosys_test/cube/effect.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
class CubeEffect(effect.Effect):
99
"""Simple effect drawing a textured cube"""
10-
depth_testing = True
11-
1210
def init(self):
1311
self.cube_shader1 = self.get_shader('cube/cube_multi_fade.glsl')
1412
self.cube_shader2 = self.get_shader('cube/cube_texture_light.glsl')
@@ -35,9 +33,9 @@ def draw(self, time, target):
3533
proj_m = self.create_projection(fov=60.0, ratio=1.0)
3634

3735
self.cube.bind(self.cube_shader1)
38-
self.cube_shader1.uniform_mat4("ProjM", proj_m)
39-
self.cube_shader1.uniform_mat4("ModelViewM", mv_m)
40-
self.cube_shader1.uniform_mat3("NormalM", normal_m)
36+
self.cube_shader1.uniform_mat4("m_proj", proj_m)
37+
self.cube_shader1.uniform_mat4("m_mv", mv_m)
38+
self.cube_shader1.uniform_mat3("m_normal", normal_m)
4139
self.cube_shader1.uniform_sampler_2d(0, "texture0", self.texture1)
4240
self.cube_shader1.uniform_sampler_2d(1, "texture1", self.texture2)
4341
self.cube_shader1.uniform_1f("time", time)
@@ -56,9 +54,9 @@ def draw(self, time, target):
5654
normal_m = self.create_normal_matrix(view_m)
5755

5856
self.points.bind(self.cube_shader2)
59-
self.cube_shader2.uniform_mat4("ProjM", self.sys_camera.projection)
60-
self.cube_shader2.uniform_mat4("ModelViewM", view_m)
61-
self.cube_shader2.uniform_mat3("NormalM", normal_m)
57+
self.cube_shader2.uniform_mat4("m_proj", self.sys_camera.projection)
58+
self.cube_shader2.uniform_mat4("m_mv", view_m)
59+
self.cube_shader2.uniform_mat3("m_normal", normal_m)
6260
self.cube_shader2.uniform_sampler_2d(0, "texture0", self.fbo.color_buffers[0])
6361
self.cube_shader2.uniform_1f("time", time)
6462
self.cube_shader2.uniform_3f("lightpos", 0.0, 0.0, 0.0)

demosys_test/cube/shaders/cube/cube_multi_fade.glsl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
#if defined VERTEX_SHADER
44

5-
in vec3 in_Position;
6-
in vec3 in_Normal;
7-
in vec2 in_UV0;
5+
in vec3 in_position;
6+
in vec3 in_normal;
7+
in vec2 in_uv;
88

9-
uniform mat4 ProjM;
10-
uniform mat4 ModelViewM;
11-
uniform mat3 NormalM;
9+
uniform mat4 m_proj;
10+
uniform mat4 m_mv;
11+
uniform mat3 m_normal;
1212

1313
out vec3 normal;
14-
out vec2 uv0;
14+
out vec2 uv;
1515

1616
void main() {
17-
gl_Position = ProjM * ModelViewM * vec4(in_Position, 1.0);
18-
normal = NormalM * in_Normal;
19-
uv0 = in_UV0;
17+
gl_Position = m_proj * m_mv * vec4(in_position, 1.0);
18+
normal = m_normal * in_normal;
19+
uv = in_uv;
2020
}
2121

2222
#elif defined FRAGMENT_SHADER
@@ -27,12 +27,12 @@ uniform sampler2D texture1;
2727
uniform float time;
2828

2929
in vec3 normal;
30-
in vec2 uv0;
30+
in vec2 uv;
3131

3232
void main()
3333
{
34-
vec4 c1 = texture(texture0, uv0);
35-
vec4 c2 = texture(texture1, uv0);
34+
vec4 c1 = texture(texture0, uv);
35+
vec4 c2 = texture(texture1, uv);
3636
float m1 = sin(time * 1.0) / 2.0 + 0.5;
3737
float m2 = 1.0 - m1;
3838

demosys_test/cube/shaders/cube/cube_texture_light.glsl

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22

33
#if defined VERTEX_SHADER
44

5-
in vec3 in_Position;
5+
in vec3 in_position;
66

7-
uniform mat4 ProjM;
8-
uniform mat4 ModelViewM;
9-
uniform mat3 NormalM;
10-
11-
out vec3 normal;
12-
out vec2 uv0;
7+
uniform mat4 m_proj;
8+
uniform mat4 m_mv;
9+
uniform mat3 m_normal;
1310

1411
void main() {
15-
gl_Position = vec4(in_Position, 1.0);
12+
gl_Position = vec4(in_position, 1.0);
1613
}
1714

1815
#elif defined FRAGMENT_SHADER
@@ -22,7 +19,7 @@ uniform sampler2D texture0;
2219
uniform float time;
2320

2421
in vec3 normal;
25-
in vec2 uv0;
22+
in vec2 uv;
2623
in vec3 lightdir;
2724
in vec3 eyepos;
2825

@@ -34,7 +31,7 @@ void main()
3431
vec4 specular = vec4(1.0, 1.0, 1.0, 1.0);
3532
float shininess = 0.5;
3633

37-
vec4 c = texture(texture0, uv0);
34+
vec4 c = texture(texture0, uv);
3835
vec4 spec = vec4(0.0);
3936

4037
vec3 n = normalize(normal);
@@ -58,13 +55,13 @@ void main()
5855
layout (points) in;
5956
layout (triangle_strip, max_vertices = 24) out; // 4 vertices per side of the cube
6057

61-
uniform mat4 ProjM;
62-
uniform mat4 ModelViewM;
63-
uniform mat3 NormalM;
58+
uniform mat4 m_proj;
59+
uniform mat4 m_mv;
60+
uniform mat3 m_normal;
6461
uniform vec3 lightpos;
6562
uniform float time;
6663

67-
out vec2 uv0;
64+
out vec2 uv;
6865
out vec3 normal;
6966
out vec3 lightdir;
7067
out vec3 eyepos;
@@ -82,11 +79,11 @@ vec3 cube_corners[8] = vec3[] (
8279
);
8380

8481
#define EMIT_V(POS, UV, NORMAL) \
85-
uv0 = UV; \
86-
normal = normalize(NormalM * NORMAL); \
82+
uv = UV; \
83+
normal = normalize(m_normal * NORMAL); \
8784
lightdir = lightpos - POS.xyz; \
8885
eyepos = -POS.xyz; \
89-
gl_Position = ProjM * vec4(POS, 1.0); \
86+
gl_Position = m_proj * vec4(POS, 1.0); \
9087
EmitVertex()
9188

9289
#define EMIT_QUAD(P1, P2, P3, P4, NORMAL) \
@@ -109,7 +106,7 @@ void main()
109106
pos.x += cos(time + gl_in[0].gl_Position.x);
110107
pos.z += cos(time + gl_in[0].gl_Position.z);
111108

112-
corners[i] = (ModelViewM * vec4(pos, 1.0)).xyz;
109+
corners[i] = (m_mv * vec4(pos, 1.0)).xyz;
113110
}
114111
EMIT_QUAD(3, 2, 0, 1, vec3( 0.0, 0.0, -1.0)); // back
115112
EMIT_QUAD(6, 7, 5, 4, vec3( 0.0, 0.0, 1.0)); // front

0 commit comments

Comments
 (0)