the asumptions were of course, incorrect
[convexer.git] / __init__.py
index 8d9254e69b49a1ba2e5971712fbc4f2f4bb173a8..05aefbcb1066a2c5592a6c5d9f1ac5c3e55bd0c0 100644 (file)
@@ -48,10 +48,10 @@ cxr_ui_draw_handler = None
 # Batches
 cxr_view_lines = None
 cxr_view_mesh = None
-cxr_mdl_mesh = None
 cxr_jobs_batch = None
 cxr_jobs_inf = []
 cxr_error_inf = None
+cxr_test_mdl = None
 
 cxr_asset_lib = \
 {
@@ -93,9 +93,11 @@ uniform mat4 viewProjectionMatrix;
 
 in vec3 aPos;
 in vec3 aNormal;
+in vec2 aUv;
 
 out vec3 lPos;
 out vec3 lNormal;
+out vec2 lUv;
 
 void main()
 {
@@ -105,14 +107,17 @@ void main()
    gl_Position = viewProjectionMatrix * pWorldPos;
    lNormal = normalize(mat3(transpose(inverse(modelMatrix))) * aNormal);
    lPos = worldPos;
+   lUv = aUv;
 }
 ""","""
 
 uniform vec4 colour;
 uniform vec3 testLightDir;
+uniform sampler2D uBasetexture;
 
 in vec3 lNormal;
 in vec3 lPos;
+in vec2 lUv;
 
 out vec4 FragColor;
 
@@ -146,11 +151,18 @@ vec3 LinearToGamma( vec3 f3linear )
        return pow( f3linear, vec3(1.0 / 2.2) );
 }
 
+vec3 GammaToLinear( vec3 f3gamma )
+{
+   return pow( f3gamma, vec3(2.2) );
+}
+
 void main()
 {
    vec3 tangentSpaceNormal = vec3( 0.0, 0.0, 1.0 );
    vec4 normalTexel = vec4(1.0,1.0,1.0,1.0);
-   vec4 baseColor = colour;
+   vec3 colorInput = GammaToLinear( texture( uBasetexture, lUv ).rgb );
+
+   vec4 baseColor = vec4( colorInput * colour.rgb, 1.0 );
 
        //normalTexel = tex2D( BumpmapSampler, i.detailOrBumpTexCoord );
        //tangentSpaceNormal = 2.0 * normalTexel - 1.0;
@@ -229,7 +241,7 @@ def cxr_ui(_,context):
 
 def cxr_draw():
    global cxr_view_shader, cxr_view_mesh, cxr_view_lines, cxr_mdl_shader,\
-          cxr_mdl_mesh
+          cxr_mdl_mesh, cxr_test_mdl
 
    cxr_view_shader.bind()
 
@@ -245,29 +257,33 @@ def cxr_draw():
    if cxr_view_mesh != None:
       gpu.state.depth_test_set('LESS_EQUAL')
       gpu.state.blend_set('ADDITIVE')
-
+      
       cxr_view_mesh.draw( cxr_view_shader )
 
-   if cxr_mdl_mesh != None:
-      gpu.state.depth_mask_set(True)
-      gpu.state.depth_test_set('LESS_EQUAL')
-      gpu.state.face_culling_set('FRONT')
-      gpu.state.blend_set('NONE')
-      cxr_mdl_shader.bind()
-      cxr_mdl_shader.uniform_float('colour',(0.5,0.5,0.5,1.0))
-      cxr_mdl_shader.uniform_float("viewProjectionMatrix", \
-            bpy.context.region_data.perspective_matrix)
+   # Models
+   gpu.state.depth_mask_set(True)
+   gpu.state.depth_test_set('LESS_EQUAL')
+   gpu.state.face_culling_set('FRONT')
+   gpu.state.blend_set('NONE')
 
+   cxr_mdl_shader.bind()
+   cxr_mdl_shader.uniform_float("viewProjectionMatrix", \
+         bpy.context.region_data.perspective_matrix)
+
+   if cxr_test_mdl != None:
+      cxr_mdl_shader.uniform_float('colour',(1.0,1.0,1.0,1.0))
+
+      #temp light dir
       testmdl = bpy.context.scene.objects['target']
       light = bpy.context.scene.objects['point']
       relative = light.location - testmdl.location 
       relative.normalize()
-
       cxr_mdl_shader.uniform_float("modelMatrix", testmdl.matrix_world)
       cxr_mdl_shader.uniform_float("testLightDir", relative)
 
-
-      cxr_mdl_mesh.draw( cxr_mdl_shader )
+      for part in cxr_test_mdl:
+         cxr_mdl_shader.uniform_sampler("uBasetexture", part[0]['basetexture'])
+         part[1].draw( cxr_mdl_shader )
 
 def cxr_jobs_update_graph(jobs):
    global cxr_jobs_batch, cxr_ui_shader, cxr_jobs_inf
@@ -627,7 +643,8 @@ libcxr_lightpatch_bsp = extern( "cxr_lightpatch_bsp", [c_char_p], None )
 # Binary file formats and FS
 libcxr_fs_set_gameinfo = extern( "cxr_fs_set_gameinfo", [c_char_p], c_int32 )
 libcxr_fs_exit = extern( "cxr_fs_exit", [], None )
-libcxr_fs_get = extern( "cxr_fs_get", [c_char_p, c_int32], c_char_p )
+libcxr_fs_get = extern( "cxr_fs_get", [c_char_p, c_int32], c_void_p )
+libcxr_fs_free = extern( "cxr_fs_free", [c_void_p], None )
 libcxr_fs_find = extern( "cxr_fs_find", [c_char_p, POINTER(fs_locator)],\
       c_int32 )
 
@@ -648,7 +665,7 @@ libcxr_funcs = [ libcxr_decompose, libcxr_free_world, libcxr_begin_vmf, \
                  libcxr_vdf_kv, libcxr_lightpatch_bsp, libcxr_write_test_data,\
                  libcxr_world_preview, libcxr_free_tri_mesh, \
                  libcxr_fs_set_gameinfo, libcxr_fs_exit, libcxr_fs_get, \
-                 libcxr_fs_find,\
+                 libcxr_fs_find, libcxr_fs_free, \
                  libcxr_valve_load_model, libcxr_valve_free_model,\
                  libcxr_valve_load_material, libcxr_valve_free_material ]
 
@@ -717,8 +734,15 @@ libnbvtf_convert = extern( "nbvtf_convert", \
       [c_char_p,c_int32,c_int32,c_int32,c_int32,c_int32,c_uint32,c_char_p], \
       c_int32 )
 
+libnbvtf_read = extern( "nbvtf_read", \
+      [c_void_p,POINTER(c_int32),POINTER(c_int32), c_int32], \
+      POINTER(c_uint8) )
+
+libnbvtf_free = extern( "nbvtf_free", [POINTER(c_uint8)], None )
+
 libnbvtf_init = extern( "nbvtf_init", [], None )
-libnbvtf_funcs = [ libnbvtf_convert, libnbvtf_init ]
+libnbvtf_funcs = [ libnbvtf_convert, libnbvtf_init, libnbvtf_read, \
+                   libnbvtf_free ]
 
 # Loading
 # --------------------------
@@ -1770,7 +1794,7 @@ class CXR_INIT_FS_OPERATOR(bpy.types.Operator):
 
       return {'FINISHED'}
 
-def cxr_load_texture( path ):
+def cxr_load_texture( path, is_normal ):
    global cxr_asset_lib
 
    if path in cxr_asset_lib['textures']:
@@ -1778,9 +1802,36 @@ def cxr_load_texture( path ):
 
    print( F"cxr_load_texture( '{path}' )" )
 
-   # TODO
+   pvtf = libcxr_fs_get.call( path.encode('utf-8'), 0 )
+
+   if not pvtf:
+      print( "vtf failed to load" )
+      cxr_asset_lib['textures'][path] = None
+      return None
+
+   x = c_int32(0)
+   y = c_int32(0)
+
+   img_data = libnbvtf_read.call( pvtf, pointer(x), pointer(y), \
+         c_int32(is_normal) )
+
+   x = x.value
+   y = y.value
+
+   if not img_data:
+      print( "vtf failed to decode" )
+      libcxr_fs_free.call( pvtf )
+      cxr_asset_lib['textures'][path] = None
+      return None
+
+   img_buf = gpu.types.Buffer('FLOAT', [x*y*4], [_/255.0 for _ in img_data[:x*y*4]])
 
-   tex = cxr_asset_lib['textures'][path] = None
+   tex = cxr_asset_lib['textures'][path] = \
+         gpu.types.GPUTexture( size=(x,y), layers=0, is_cubemap=False,\
+                               format='RGBA8', data=img_buf )
+   
+   libnbvtf_free.call( img_data )
+   libcxr_fs_free.call( pvtf )
    return tex
 
 def cxr_load_material( path ):
@@ -1792,15 +1843,19 @@ def cxr_load_material( path ):
    print( F"cxr_load_material( '{path}' )" )
    
    pvmt = libcxr_valve_load_material.call( path.encode( 'utf-8') )
-   vmt = pvmt[0]
+   
+   if not pvmt:
+      cxr_asset_lib['materials'][path] = None
+      return None
 
+   vmt = pvmt[0]
    mat = cxr_asset_lib['materials'][path] = {}
 
    if vmt.basetexture:
-      mat['basetexture'] = cxr_load_texture( vmt.basetexture.decode('utf-8') )
+      mat['basetexture'] = cxr_load_texture( vmt.basetexture.decode('utf-8'), 0)
    
    if vmt.bumpmap:
-      mat['bumpmap'] = cxr_load_texture( vmt.bumpmap.decode('utf-8') )
+      mat['bumpmap'] = cxr_load_texture( vmt.bumpmap.decode('utf-8'), 1)
 
    libcxr_valve_free_material.call( pvmt )
 
@@ -1870,15 +1925,9 @@ class CXR_LOAD_MODEL_OPERATOR(bpy.types.Operator):
    bl_label="Load model"
 
    def execute(_,context):
-      global cxr_mdl_mesh, cxr_mdl_shader, cxr_asset_lib
-
-      test_mdl = cxr_load_model_full( bpy.context.scene.cxr_data.dev_mdl )
+      global cxr_test_mdl, cxr_mdl_shader, cxr_asset_lib
 
-      if test_mdl != None:
-         # just draw first batch part for now
-         cxr_mdl_mesh = test_mdl[0][1]
-      else:
-         cxr_mdl_mesh = None
+      cxr_test_mdl = cxr_load_model_full( bpy.context.scene.cxr_data.dev_mdl )
 
       scene_redraw()
       return {'FINISHED'}