import os

from objloader import Obj
from PIL import Image
from pyrr import Matrix44

import moderngl
from ported._example import Example


class LoadingOBJ(Example):
    title = "Loading OBJ"
    gl_version = (3, 3)

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.obj = Obj.open(os.path.join(os.path.dirname(__file__), 'data', 'sitting_dummy.obj'))
        self.wood = Image.open(os.path.join(os.path.dirname(__file__), 'data', 'wood.jpg'))

        self.prog = self.ctx.program(
            vertex_shader='''
                #version 330

                uniform mat4 Mvp;

                in vec3 in_vert;
                in vec3 in_norm;
                in vec2 in_text;

                out vec3 v_vert;
                out vec3 v_norm;
                out vec2 v_text;

                void main() {
                    v_vert = in_vert;
                    v_norm = in_norm;
                    v_text = in_text;
                    gl_Position = Mvp * vec4(v_vert, 1.0);
                }
            ''',
            fragment_shader='''
                #version 330

                uniform sampler2D Texture;
                uniform vec4 Color;
                uniform vec3 Light;

                in vec3 v_vert;
                in vec3 v_norm;
                in vec2 v_text;

                out vec4 f_color;

                void main() {
                    float lum = dot(normalize(v_norm), normalize(v_vert - Light));
                    lum = acos(lum) / 3.14159265;
                    lum = clamp(lum, 0.0, 1.0);
                    lum = lum * lum;
                    lum = smoothstep(0.0, 1.0, lum);
                    lum *= smoothstep(0.0, 80.0, v_vert.z) * 0.3 + 0.7;
                    lum = lum * 0.8 + 0.2;

                    vec3 color = texture(Texture, v_text).rgb;
                    color = color * (1.0 - Color.a) + Color.rgb * Color.a;
                    f_color = vec4(color * lum, 1.0);
                }
            ''',
        )

        self.light = self.prog['Light']
        self.color = self.prog['Color']
        self.mvp = self.prog['Mvp']

        self.texture = self.ctx.texture(self.wood.size, 3, self.wood.tobytes())
        self.texture.build_mipmaps()

        self.vbo = self.ctx.buffer(self.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')

    def render(self, time, frame_time):
        self.ctx.clear(1.0, 1.0, 1.0)
        self.ctx.enable(moderngl.DEPTH_TEST)

        proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0)
        lookat = Matrix44.look_at(
            (-85, -180, 140),
            (0.0, 0.0, 65.0),
            (0.0, 0.0, 1.0),
        )

        self.light.value = (-140.0, -300.0, 350.0)
        self.color.value = (1.0, 1.0, 1.0, 0.25)
        self.mvp.write((proj * lookat).astype('f4').tobytes())

        self.texture.use()
        self.vao.render()


if __name__ == '__main__':
    LoadingOBJ.run()

@@ -8,56 +8,8 @@
 os%0A%0A
-from objloader import Obj%0Afrom PIL import Image%0A
 from
@@ -30,16 +30,16 @@
 atrix44%0A
+
 %0Aimport 
@@ -256,65 +256,24 @@
 j = 
-Obj.open(os.path.join(os.path.dirname(__file__), 'data', 
+self.load_scene(
 'sit
@@ -288,17 +288,16 @@
 my.obj')
-)
 %0A       
@@ -306,74 +306,39 @@
 elf.
-wood = Image.open(os.path.join(os.path.dirname(__file__), 'data', 
+texture = self.load_texture_2d(
 'woo
@@ -344,17 +344,16 @@
 od.jpg')
-)
 %0A%0A      
@@ -502,28 +502,32 @@
  in vec3 in_
-vert
+position
 ;%0A          
@@ -539,32 +539,34 @@
  in vec3 in_norm
+al
 ;%0A              
@@ -577,25 +577,31 @@
  vec2 in_tex
-t
+coord_0
 ;%0A%0A         
@@ -753,20 +753,24 @@
 rt = in_
-vert
+position
 ;%0A      
@@ -795,24 +795,26 @@
 rm = in_norm
+al
 ;%0A          
@@ -838,17 +838,23 @@
 = in_tex
-t
+coord_0
 ;%0A      
@@ -892,22 +892,27 @@
  * vec4(
-v_vert
+in_position
 , 1.0);%0A
@@ -2040,117 +2040,73 @@
     
-self.texture = self.ctx.texture(self.wood.size, 3, self.wood.tobytes())%0A        self.texture.build_mipmaps()%0A
+# Create a vao from the first root node (attribs are auto mapped)
 %0A   
@@ -2116,17 +2116,17 @@
   self.v
-b
+a
 o = self
@@ -2130,161 +2130,53 @@
 elf.
-ctx.buffer(self.obj.pack('vx vy vz nx ny nz tx ty'))%0A        self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert', 'in_norm', 'in_text'
+obj.root_nodes%5B0%5D.mesh.vao.instance(self.prog
 )%0A%0A 

Use built in loaders