Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
void main() {
float lum = clamp(dot(normalize(Light - v_vert), normalize(v_norm)), 0.0, 1.0) * 0.8 + 0.2;
f_color = vec4(texture(Texture, v_text).rgb * lum, 1.0);
}
''',
)
obj = Obj.open(data.find('crate.obj'))
img = Image.open(data.find('crate.png')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB')
self.texture = self.ctx.texture(img.size, 3, img.tobytes())
self.sampler = self.ctx.sampler(self.texture)
self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz tx ty'))
self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert', 'in_norm', 'in_text')
self.vao.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler, 0)])
def render(self):
angle = self.wnd.time
self.ctx.screen.viewport = self.wnd.viewport
self.ctx.clear(1.0, 1.0, 1.0)
with self.ctx.scope(mgl.DEPTH_TEST):
camera_pos = (np.cos(angle) * 20.0, np.sin(angle) * 20.0, 5.0)
proj = Matrix44.perspective_projection(45.0, self.wnd.ratio, 0.1, 1000.0)
lookat = Matrix44.look_at(
camera_pos,
(0.0, 0.0, 0.5),
(0.0, 0.0, 1.0),
)
self.prog['Mvp'] = (proj * lookat).astype('f4').tobytes()
self.prog['Light'] = camera_pos
# self.vbo2.write(Matrix33.from_z_rotation(self.wnd.time).astype('f4').tobytes(), offset=24)
self.vbo2.write(b''.join(struct.pack(
'15f',
*car['color'],
img1 = Image.open(data.find('crate.png')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB')
img2 = Image.open(data.find('rock.jpg')).transpose(Image.FLIP_TOP_BOTTOM).convert('RGB')
self.texture1 = self.ctx.texture(img1)
self.texture2 = self.ctx.texture(img2)
self.sampler1 = self.ctx.sampler(self.texture1)
self.sampler2 = self.ctx.sampler(self.texture2)
self.vbo1 = self.ctx.buffer(obj1.pack('vx vy vz nx ny nz tx ty'))
self.vbo2 = self.ctx.buffer(obj2.pack('vx vy vz nx ny nz tx ty'))
self.vbo3 = self.ctx.buffer(obj3.pack('vx vy vz nx ny nz tx ty'))
self.vao1 = self.ctx.simple_vertex_array(self.prog, self.vbo1, 'in_vert', 'in_norm', 'in_text')
self.vao1.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler2, 0)])
self.vao2 = self.ctx.simple_vertex_array(self.prog, self.vbo2, 'in_vert', 'in_norm', 'in_text')
self.vao2.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler1, 0)])
self.vao3 = self.ctx.simple_vertex_array(self.prog, self.vbo3, 'in_vert', 'in_norm', 'in_text')
self.vao3.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler2, 0)])
with self.ctx.recorder:
self.ctx.clear(1.0, 1.0, 1.0)
self.vao1.render()
self.vao2.render()
self.vao3.render()
self.bytecode = self.ctx.recorder.dump()
print(self.bytecode)
def render(self):
self.ctx.screen.viewport = self.wnd.viewport
self.ctx.clear(1.0, 1.0, 1.0)
with self.ctx.scope(mgl.DEPTH_TEST):
proj = Matrix44.perspective_projection(45.0, self.wnd.ratio, 0.1, 1000.0)
lookat = Matrix44.look_at(
(40.0, 30.0, 30.0),
(0.0, 0.0, 0.0),
(0.0, 0.0, 1.0),
)
self.prog['Mvp'] = (proj * lookat).astype('f4').tobytes()
self.vao.render(mgl.LINES)
self.texture1 = self.ctx.texture(img1)
self.texture2 = self.ctx.texture(img2)
self.sampler1 = self.ctx.sampler(self.texture1)
self.sampler2 = self.ctx.sampler(self.texture2)
self.vbo1 = self.ctx.buffer(obj1.pack('vx vy vz nx ny nz tx ty'))
self.vbo2 = self.ctx.buffer(obj2.pack('vx vy vz nx ny nz tx ty'))
self.vbo3 = self.ctx.buffer(obj3.pack('vx vy vz nx ny nz tx ty'))
self.vao1 = self.ctx.simple_vertex_array(self.prog, self.vbo1, 'in_vert', 'in_norm', 'in_text')
self.vao1.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler2, 0)])
self.vao2 = self.ctx.simple_vertex_array(self.prog, self.vbo2, 'in_vert', 'in_norm', 'in_text')
self.vao2.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler1, 0)])
self.vao3 = self.ctx.simple_vertex_array(self.prog, self.vbo3, 'in_vert', 'in_norm', 'in_text')
self.vao3.scope = self.ctx.scope(mgl.DEPTH_TEST, samplers=[(self.sampler2, 0)])
with self.ctx.recorder:
self.ctx.clear(1.0, 1.0, 1.0)
self.vao1.render()
self.vao2.render()
self.vao3.render()
self.bytecode = self.ctx.recorder.dump()
print(self.bytecode)
def render(self):
width, height = self.wnd.size
self.ctx.screen.viewport = self.wnd.viewport
self.ctx.clear(1.0, 1.0, 1.0)
with self.ctx.scope(mgl.DEPTH_TEST):
proj = Matrix44.perspective_projection(45.0, width / height, 0.1, 1000.0)
lookat = Matrix44.look_at(
(47.697, -8.147, 24.498),
(0.0, 0.0, 8.0),
(0.0, 0.0, 1.0),
)
rotate = Matrix44.from_z_rotation(np.sin(self.wnd.time) * 0.5 + 0.2)
self.prog['UseTexture'] = False
self.prog['Light'] = (67.69, -8.14, 52.49)
self.prog['Mvp'] = (proj * lookat * rotate).astype('f4').tobytes()
self.prog['Color'] = (0.67, 0.49, 0.29)
def render(self):
angle = self.wnd.time * 0.2
self.ctx.screen.viewport = self.wnd.viewport
self.ctx.clear(1.0, 1.0, 1.0)
with self.ctx.scope(mgl.DEPTH_TEST):
proj = Matrix44.perspective_projection(45.0, self.wnd.ratio, 0.1, 1000.0)
lookat = Matrix44.look_at(
(np.cos(angle), np.sin(angle), 0.8),
(0.0, 0.0, 0.1),
(0.0, 0.0, 1.0),
)
self.prog['Mvp'] = (proj * lookat).astype('f4').tobytes()
self.vao.render(mgl.TRIANGLE_STRIP)
def render(self):
self.ctx.screen.viewport = self.wnd.viewport
self.ctx.clear(1.0, 1.0, 1.0)
self.bg_sampler.use()
with self.ctx.scope(mgl.BLEND):
self.canvas_vao.render(mgl.TRIANGLE_STRIP)
with self.ctx.scope(mgl.DEPTH_TEST):
proj = Matrix44.perspective_projection(30.0, self.wnd.ratio, 1.0, 1000.0)
lookat = Matrix44.look_at(
(46.748, -280.619, 154.391),
(-23.844, 2.698, 44.493),
(0.0, 0.0, 1.0),
)
self.prog['Mvp'] = (proj * lookat).astype('f4').tobytes()
self.prog['Light'] = (-143.438, -159.072, 213.268)
self.mug_sampler.use()
self.mug_vao.render()
with self.ctx.scope(mgl.DEPTH_TEST | mgl.BLEND):
self.sticker_sampler.use()
self.sticker_vao.render(mgl.TRIANGLE_STRIP)
ImageDraw.ImageDraw(img4).text((50, 30), 'i4', '#000', ImageFont.truetype('arial', 128))
ImageDraw.ImageDraw(img5).text((50, 30), 'i5', '#000', ImageFont.truetype('arial', 128))
ImageDraw.ImageDraw(img6).text((50, 30), 'i6', '#000', ImageFont.truetype('arial', 128))
def join(*images):
return b''.join(img.tobytes('raw', 'RGB', 0, -1) for img in images)
self.texture = self.ctx.texture_cube((200, 200), 3, join(img1, img2, img3, img4, img5, img6))
self.sampler = self.ctx.sampler(self.texture)
self.sampler.filter = mgl.LINEAR
self.sampler.use()
obj = Obj.open(data.find('sitting_dummy.obj'))
self.vbo = self.ctx.buffer(obj.pack('vx vy vz nx ny nz'))
self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert', 'in_norm')
self.scope = self.ctx.scope(mgl.DEPTH_TEST)
with self.ctx.scope(mgl.BLEND):
self.canvas_vao.render(mgl.TRIANGLE_STRIP)
with self.ctx.scope(mgl.DEPTH_TEST):
proj = Matrix44.perspective_projection(30.0, self.wnd.ratio, 1.0, 1000.0)
lookat = Matrix44.look_at(
(46.748, -280.619, 154.391),
(-23.844, 2.698, 44.493),
(0.0, 0.0, 1.0),
)
self.prog['Mvp'] = (proj * lookat).astype('f4').tobytes()
self.prog['Light'] = (-143.438, -159.072, 213.268)
self.mug_sampler.use()
self.mug_vao.render()
with self.ctx.scope(mgl.DEPTH_TEST | mgl.BLEND):
self.sticker_sampler.use()
self.sticker_vao.render(mgl.TRIANGLE_STRIP)