We have a texture!
dkl9

dkl9 commited on 2025-182 02:14:40
Showing 3 changed files, with 22 additions and 5 deletions.

... ...
@@ -30,6 +30,7 @@ void main() {{
30 30
         normalize(camPos - positionF), reflect(-lightDir, normalF)
31 31
     ), 0.0), reflsF.w);
32 32
     gl_FragColor = vec4((ambient + diffuse + specular) * colF.xyz, colF.w);
33
+    gl_FragColor = texture2D(tex, texcF);
33 34
 }}
34 35
 """
35 36
 
... ...
@@ -46,6 +47,16 @@ def init_display():
46 47
         pygame.QUIT, pygame.WINDOWRESIZED, pygame.WINDOWSIZECHANGED
47 48
     ])
48 49
     pygame.display.set_caption("Voxels")
50
+    pli = pygame.image.load("favicon.png").convert()
51
+    (tw, th) = pli.get_size()
52
+    img_data = np.array(list(pygame.image.tobytes(pli, "RGB")), np.uint8)
53
+    tex = glGenTextures(1)
54
+    glBindTexture(GL_TEXTURE_2D, tex)
55
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
56
+    glTexImage2D(
57
+        GL_TEXTURE_2D, 0, GL_RGB, tw, th,
58
+        0, GL_RGB, GL_UNSIGNED_BYTE, img_data
59
+    )
49 60
     sp = shaders.compileProgram(
50 61
         shaders.compileShader(VS, GL_VERTEX_SHADER),
51 62
         shaders.compileShader(FS, GL_FRAGMENT_SHADER)
... ...
@@ -53,6 +64,7 @@ def init_display():
53 64
     for (k, v) in UNIFORMS.items():
54 65
         v[2] = glGetUniformLocation(sp, k)
55 66
     glUseProgram(sp)
67
+    glUniform1i(UNIFORMS["tex"][2], 0)
56 68
     glEnable(GL_DEPTH_TEST)
57 69
     glEnable(GL_BLEND)
58 70
     glEnable(GL_CULL_FACE)
... ...
@@ -5,11 +5,14 @@ import pygame
5 5
 
6 6
 FLOAT = np.float32
7 7
 VOXEL_TYPE = np.uint8
8
-VERTEX_ATTRS = ["vec3 position", "vec4 col", "vec3 normal", "vec4 refls"]
8
+VERTEX_ATTRS = [
9
+    "vec3 position", "vec4 col", "vec3 normal", "vec4 refls", "vec2 texc"
10
+]
9 11
 UNIFORMS = {
10 12
     "MVP": ["vert", "mat4", None],
11 13
     "lightPos": ["frag", "vec4", None],
12
-    "camPos": ["frag", "vec3", None]
14
+    "camPos": ["frag", "vec3", None],
15
+    "tex": ["frag", "sampler2D", None]
13 16
 }
14 17
 
15 18
 LIGHT_POS = [5, 30, 10]
... ...
@@ -74,6 +77,11 @@ def update_attr(ent, n, l=None):
74 77
                 l = ent.norm_list()
75 78
             case "refls":
76 79
                 l = ent.mat_list()[1]
80
+            case "texc":
81
+                l = np.astype(np.tile(
82
+                    [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
83
+                    (int(np.ceil(len(ent.verts) / 3)), 1)
84
+                )[:len(ent.verts)], FLOAT)
77 85
     if ent.vao:
78 86
         glBindBuffer(GL_ARRAY_BUFFER, ent.vbos[i][0])
79 87
         if l.shape[0] != ent.vbos[i][1]:
... ...
@@ -1,10 +1,7 @@
1 1
 from libs import *
2 2
 from display import init_display
3
-from entity import Entity
4 3
 from mapgen import MapGen
5
-from mesh import SquareMesh
6 4
 from player import Player
7
-from region import WorldRegion
8 5
 
9 6
 def debug_mode(mg, cam):
10 7
     pass
11 8