source: framspy/gui/visual/loader.py @ 1300

Last change on this file since 1300 was 1198, checked in by Maciej Komosinski, 23 months ago

Added simple Python GUI for Framsticks library/server

File size: 4.7 KB
Line 
1import OpenGL.GL as gl
2from OpenGL.raw.GL.VERSION.GL_1_5 import GL_STATIC_DRAW
3from gui.visual.rawModel import RawModel
4from PIL import Image
5import numpy as np
6from typing import List
7
8class Loader:
9    def __init__(self) -> None:
10        self.vaos = []
11        self.vbos = []
12        self.textures = []
13
14    def loadToVao5(self, positions, textureCoords, normals, indieces, vertexCount: int):
15        vaoid = self.createVAO()
16        self.bindIndicesBuffer(indieces)
17        self.storeDataInAttributeList(0, 3, positions)
18        self.storeDataInAttributeList(1, 2, textureCoords)
19        self.storeDataInAttributeList(2, 3, normals)
20        self.unbindVAO()
21        return RawModel(vaoid, vertexCount)
22
23    def loadToVao3(self, positions, textureCoords, vertexCount: int):
24        vaoid = self.createVAO()
25        self.storeDataInAttributeList(0, 3, positions)
26        self.storeDataInAttributeList(1, 2, textureCoords)
27        self.unbindVAO()
28        return RawModel(vaoid, vertexCount)
29
30    def loadToVao2(self, positions, colors, vertexCount: int):
31        vaoid = self.createVAO()
32        self.storeDataInAttributeList(0, 3, positions)
33        self.storeDataInAttributeList(1, 3, colors)
34        self.unbindVAO()
35        return RawModel(vaoid, vertexCount)
36
37    def loadToVao1(self, positions, vertexCount: int):
38        vaoid = self.createVAO()
39        self.storeDataInAttributeList(0, 3, positions)
40        self.unbindVAO()
41        return RawModel(vaoid, vertexCount)
42
43    def loadTexture(self, filename: str) -> int:
44        image = Image.open(filename)
45        img_data = np.array(list(image.getdata()), np.uint8)
46
47        textureid = gl.glGenTextures(1)
48        gl.glBindTexture(gl.GL_TEXTURE_2D, textureid)
49        colors = gl.GL_RGB
50        if image.mode == 'RGBA':
51            colors = gl.GL_RGBA
52        gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB, image.width, image.height, 0, colors, gl.GL_UNSIGNED_BYTE, img_data)
53       
54        gl.glGenerateMipmap(gl.GL_TEXTURE_2D)
55        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR_MIPMAP_LINEAR),
56        gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_LOD_BIAS, -2.4)
57
58        self.textures.append(textureid)
59        return textureid
60
61    def loadCubemap(self, filenames: List[str]) -> int:
62        if len(filenames) != 6:
63            raise Exception("filenames must contain exactly 6 textures")
64        textureid = gl.glGenTextures(1)
65        gl.glBindTexture(gl.GL_TEXTURE_CUBE_MAP, textureid)
66
67        for i, filename in enumerate(filenames):
68            image = Image.open(filename)
69            img_data = np.array(list(image.getdata()), np.uint8)
70            colors = gl.GL_RGB
71            if image.mode == "RGBA":
72                colors = gl.GL_RGBA
73            gl.glTexImage2D(gl.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, gl.GL_RGB, image.width, image.height, 0, colors, gl.GL_UNSIGNED_BYTE, img_data)
74
75        gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)
76        gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
77        gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE)
78        gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE)
79        gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, gl.GL_TEXTURE_WRAP_R, gl.GL_CLAMP_TO_EDGE)
80
81        return textureid
82
83    def cleanUp(self):
84        for vao in self.vaos:
85            gl.glDeleteVertexArrays(1, vao)
86
87        for vbo in self.vbos:
88            gl.glDeleteBuffers(1, vbo)
89
90        for texture in self.textures:
91            gl.glDeleteTextures(1, texture)
92
93    def createVAO(self):
94        vao_id = gl.glGenVertexArrays(1)
95        self.vaos.append(vao_id)
96        gl.glBindVertexArray(vao_id)
97        return vao_id
98
99    def storeDataInAttributeList(self, attributeNumber: int, coordinateSize: int, data: list):
100        vboid = gl.glGenBuffers(1)
101        self.vbos.append(vboid)
102        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vboid)
103        npdata = np.array(data, dtype=np.float32)
104        gl.glBufferData(gl.GL_ARRAY_BUFFER, len(data) * 4, npdata, gl.GL_STATIC_DRAW)
105        gl.glVertexAttribPointer(attributeNumber, coordinateSize, gl.GL_FLOAT, gl.GL_FALSE, 0, None)
106
107        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0)
108
109    def unbindVAO(self):
110        gl.glBindVertexArray(0)
111
112    def bindIndicesBuffer(self, indices):
113        vboid = gl.glGenBuffers(1)
114        self.vbos.append(vboid)
115        gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, vboid)
116        npindices = np.array(indices, dtype=np.int32)
117        gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, len(indices) * 4, npindices, GL_STATIC_DRAW)
118        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0)
Note: See TracBrowser for help on using the repository browser.