Skip to content

Commit 6e081d9

Browse files
committed
Make the default example a bit less ugly
1 parent 6fc10ed commit 6e081d9

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

TODO.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
## TODO
33

4+
- Split up README into several files
45
- Properly verify all settings
56
- Write documentation (readthedocs)
67
- CLI tool creating new projects and effects.

demosys/opengl/shader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def compile(self):
230230
message = GL.glGetShaderInfoLog(self.shader)
231231
if message:
232232
self.print()
233-
raise ShaderError("Failed to compile {} {}: {}".format(self.type_name(), self.name, message))
233+
raise ShaderError("Failed to compile {} {}: {}".format(self.type_name(), self.name, message.decode()))
234234

235235
def print(self):
236236
print("---[ START {} ]---".format(self.name))

demosys_test/cube/effect.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# import math
1+
import math
22
from demosys.effects import effect
33
from demosys.opengl import geometry, FBO
44
# from pyrr import Vector3
@@ -18,7 +18,7 @@ def init(self):
1818
self.cube = geometry.cube(4.0, 4.0, 4.0)
1919
v = 100.0
2020
r = (-v, v)
21-
self.points = geometry.points_random_3d(100_000, range_x=r, range_y=r, range_z=r, seed=7656456)
21+
self.points = geometry.points_random_3d(50_000, range_x=r, range_y=r, range_z=r, seed=7656456)
2222
self.quad = geometry.quad_fs()
2323
self.fbo = FBO.create(512, 512, depth=True)
2424

@@ -44,7 +44,7 @@ def draw(self, time, target):
4444
self.fbo.release()
4545

4646
# Test camera
47-
self.sys_camera.set_projection(near=1, far=1000)
47+
self.sys_camera.set_projection(near=0.1, far=1000)
4848
# self.sys_camera.set_position(10.0, 0.0, 10.0)
4949
# self.sys_camera.set_position(math.sin(time) * 10,
5050
# math.sin(time * 10),
@@ -58,6 +58,8 @@ def draw(self, time, target):
5858
self.cube_shader2.uniform_mat4("ModelViewM", view_m)
5959
self.cube_shader2.uniform_mat3("NormalM", normal_m)
6060
self.cube_shader2.uniform_sampler_2d(0, "texture0", self.fbo.color_buffers[0])
61+
self.cube_shader2.uniform_1f("time", time)
62+
self.cube_shader2.uniform_3f("lightpos", 0.0, 0.0, 0.0)
6163
self.points.draw(mode=GL.GL_POINTS)
6264

6365
GL.glClearColor(0.5, 0.5, 0.5, 1)

demosys_test/cube/shaders/cube/cube_texture_light.glsl

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,36 @@ uniform float time;
2323

2424
in vec3 normal;
2525
in vec2 uv0;
26+
in vec3 lightdir;
27+
in vec3 eyepos;
28+
2629

2730
void main()
2831
{
32+
vec4 diffuse = vec4(0.5, 0.5, 0.5, 1.0);
33+
vec4 ambient = vec4(0.2, 0.2, 0.2, 1.0);
34+
vec4 specular = vec4(1.0, 1.0, 1.0, 1.0);
35+
float shininess = 0.5;
36+
2937
vec4 c = texture(texture0, uv0);
30-
vec3 dir = vec3(0.0, 0.0, 1.0);
31-
float d = dot(dir, normal);
32-
fragColor = c * d;
38+
vec4 spec = vec4(0.0);
39+
40+
vec3 n = normalize(normal);
41+
vec3 l = normalize(lightdir);
42+
vec3 e = normalize(eyepos);
43+
44+
float intensity = max(dot(n,l), 0.0);
45+
if (intensity > 0.0) {
46+
vec3 h = normalize(l + e);
47+
float intSpec = max(dot(h, n), 0.0);
48+
spec = specular * pow(intSpec, shininess);
49+
}
50+
float att = clamp(1.0 - length(eyepos)/200.0, 0.0, 1.0);
51+
att *= att;
52+
fragColor = c * max(intensity * diffuse + spec, ambient) * att;
3353
}
3454

55+
3556
#elif defined GEOMETRY_SHADER
3657

3758
layout (points) in;
@@ -40,9 +61,13 @@ layout (triangle_strip, max_vertices = 24) out; // 4 vertices per side of the cu
4061
uniform mat4 ProjM;
4162
uniform mat4 ModelViewM;
4263
uniform mat3 NormalM;
64+
uniform vec3 lightpos;
65+
uniform float time;
4366

4467
out vec2 uv0;
4568
out vec3 normal;
69+
out vec3 lightdir;
70+
out vec3 eyepos;
4671

4772
// Define the 8 corners of a cube (back plane, front plane (counter clockwise))
4873
vec3 cube_corners[8] = vec3[] (
@@ -58,7 +83,9 @@ vec3 cube_corners[8] = vec3[] (
5883

5984
#define EMIT_V(POS, UV, NORMAL) \
6085
uv0 = UV; \
61-
normal = NormalM * NORMAL; \
86+
normal = normalize(NormalM * NORMAL); \
87+
lightdir = lightpos - POS.xyz; \
88+
eyepos = -POS.xyz; \
6289
gl_Position = ProjM * vec4(POS, 1.0); \
6390
EmitVertex()
6491

@@ -78,6 +105,10 @@ void main()
78105
for(i = 0; i < 8; i++)
79106
{
80107
vec3 pos = point.xyz + cube_corners[i] * 0.5;
108+
pos.y += sin(time + length(gl_in[0].gl_Position.xyz));
109+
pos.x += cos(time + gl_in[0].gl_Position.x);
110+
pos.z += cos(time + gl_in[0].gl_Position.z);
111+
81112
corners[i] = (ModelViewM * vec4(pos, 1.0)).xyz;
82113
}
83114
EMIT_QUAD(3, 2, 0, 1, vec3( 0.0, 0.0, -1.0)); // back

0 commit comments

Comments
 (0)