aboutsummaryrefslogtreecommitdiff
path: root/boltzgas/visual/view.py
blob: c1f92e6e0778b45c9a85bdeb06f53d05eb89e10e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from OpenGL.GL   import *
from OpenGL.GLUT import *

from pyrr import matrix44, quaternion

import numpy as np

from .shader import Shader

particle_shader = (
    """
        #version 430

        in vec3 particle_pos;

        uniform vec4 camera_pos;

        void main(){
            if (length(gl_PointCoord - vec2(0.5)) > 0.5) {
                discard;
            }

            vec3 n = vec3(gl_PointCoord - vec2(0.5), 0.);
            n.z = -sqrt(1.0 - length(n));
            n = normalize(n);

            vec3 dir = normalize(camera_pos.xyz - particle_pos);
            vec3 color = vec3(1.,1.,1.) * dot(dir, n);

            gl_FragColor = vec4(color.xyz, 0.0);
        }
    """,
    """
        #version 430

        layout (location=0) in vec4 particles;

        out vec3 color;
        out vec3 particle_pos;

        uniform mat4 projection;
        uniform mat4 rotation;

        uniform vec3 face_color;
        uniform vec3 trace_color;
        uniform uint trace_id;
        uniform vec4 camera_pos;

        void main() {
            gl_Position = projection * rotation * vec4(particles.xyz, 1.);
            particle_pos = gl_Position.xyz;
        }
    """,
    ['projection', 'rotation', 'face_color', 'trace_color', 'trace_id', 'camera_pos']
)

decoration_shader = (
    """
        #version 430

        in vec3 color;

        void main(){
            gl_FragColor = vec4(color.xyz, 0.0);
        }
    """,
    """
        #version 430

        in vec3 vertex;

        out vec3 color;

        uniform mat4 projection;
        uniform vec3 face_color;

        void main() {
            gl_Position = projection * vec4(vertex, 1.);
            color = face_color;
        }
    """,
    ['projection', 'face_color']
)

texture_shader = (
    """
        #version 430

        in vec2 tex_coord;

        uniform sampler2D picture[2];

        uniform float mixing;

        void main() {
            gl_FragColor = mix(texture(picture[0], tex_coord), texture(picture[1], tex_coord), mixing);
        }
    """,
    """
        #version 430

        layout (location=0) in vec2 screen_vertex;
        layout (location=1) in vec2 texture_vertex;

        out vec2 tex_coord;

        uniform mat4 projection;

        void main() {
            gl_Position = projection * vec4(screen_vertex, 0.0, 1.0);
            tex_coord = texture_vertex;
        }
    """,
    ['picture','projection','mixing']
)


class Projection:
    def __init__(self, distance):
        self.distance = distance
        self.ratio    = 4./3.
        self.update()

    def update(self):
        projection = matrix44.create_perspective_projection(20.0, self.ratio, 0.1, 5000.0)
        look = matrix44.create_look_at(
            eye    = [0, 0, -self.distance],
            target = [0, 0, 0],
            up     = [0,-1, 0])

        self.matrix = np.matmul(look, projection)

    def update_ratio(self, width, height, update_viewport = True):
        if update_viewport:
            glViewport(0,0,width,height)

        self.ratio = width/height
        self.update()

    def update_distance(self, change):
        self.distance += change
        self.update()

    def get(self):
        return self.matrix

class Rotation:
    def __init__(self, shift, x = np.pi, z = np.pi):
        self.matrix = matrix44.create_from_translation(shift),
        self.rotation_x = quaternion.Quaternion()
        self.update(x,z)

    def shift(self, x, z):
        self.matrix = np.matmul(
            self.matrix,
            matrix44.create_from_translation([x,0,z])
        )
        self.inverse_matrix = np.linalg.inv(self.matrix)

    def update(self, x, z):
        rotation_x = quaternion.Quaternion(quaternion.create_from_eulers([x,0,0]))
        rotation_z = self.rotation_x.conjugate.cross(
                quaternion.Quaternion(quaternion.create_from_eulers([0,0,z])))
        self.rotation_x = self.rotation_x.cross(rotation_x)

        self.matrix = np.matmul(
            self.matrix,
            matrix44.create_from_quaternion(rotation_z.cross(self.rotation_x))
        )
        self.inverse_matrix = np.linalg.inv(self.matrix)

    def get(self):
        return self.matrix

    def get_inverse(self):
        return self.inverse_matrix



class View:
    def __init__(self, gas, decorations, windows):
        self.gas = gas
        self.decorations = decorations
        self.windows = windows

        self.texture_shader    = Shader(*texture_shader)
        self.particle_shader   = Shader(*particle_shader)
        self.decoration_shader = Shader(*decoration_shader)

        self.projection3d = Projection(distance = 6)
        self.rotation3d = Rotation([-1, -1, -1/2], 5*np.pi/4, np.pi/4)
        self.camera_pos = np.matmul([0,self.projection3d.distance,0,1], self.rotation3d.get_inverse())

    def reshape(self, width, height):
        glViewport(0,0,width,height)

        world_height = 1.4
        world_width = world_height / height * width

        projection = Projection(10)

        projection  = matrix44.create_orthogonal_projection(-world_width/2, world_width/2, -world_height/2, world_height/2, -1, 1)
        translation = matrix44.create_from_translation([-1.05, -1.0/2, 0])

        self.pixels_per_unit = height / world_height
        self.projection = np.matmul(translation, projection)

    def display(self):
        glClearColor(0.,0.,0.,1.)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        self.decoration_shader.use()
        glUniformMatrix4fv(self.decoration_shader.uniform['projection'], 1, False, np.asfortranarray(self.projection))
        for decoration in self.decorations:
            decoration.display(self.decoration_shader.uniform)

        self.texture_shader.use()
        glUniformMatrix4fv(self.texture_shader.uniform['projection'], 1, False, np.asfortranarray(self.projection))
        for window in self.windows:
            window.display(self.texture_shader.uniform)

        self.particle_shader.use()
        #glUniformMatrix4fv(self.particle_shader.uniform['projection'], 1, False, np.asfortranarray(self.projection))

        glUniformMatrix4fv(self.particle_shader.uniform['projection'], 1, False, np.ascontiguousarray(self.projection3d.get()))
        glUniformMatrix4fv(self.particle_shader.uniform['rotation'],   1, False, np.ascontiguousarray(self.rotation3d.get()))

        glUniform3f(self.particle_shader.uniform['face_color'],  1., 1., 1.)
        glUniform3f(self.particle_shader.uniform['trace_color'], 1., 0., 0.)
        glUniform1ui(self.particle_shader.uniform['trace_id'], -1)
        glUniform4fv(self.particle_shader.uniform['camera_pos'], 1, self.camera_pos)

        glEnable(GL_POINT_SPRITE)
        glPointSize(2*self.gas.radius*self.pixels_per_unit)
        self.gas.gl_draw_particles()

        glutSwapBuffers()