-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
118 lines (110 loc) · 5.04 KB
/
test.py
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
import shader
from rotation import Rotation
from PySide.QtGui import QImage
from PySide.QtOpenGL import QGLWidget
from OpenGL.GL import *
import numpy
class SkyBoxShaderProgram(shader.ShaderProgram):
def __init__(self):
shader.ShaderProgram.__init__(self)
self.vertex_shader = """
varying vec4 position;
uniform samplerCube skybox;
uniform float eye_shift;
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
position = gl_Vertex;
position.x += eye_shift;
}
"""
self.fragment_shader = """
varying vec4 position;
uniform samplerCube skybox;
uniform float eye_shift;
void main()
{
vec3 tex_coord = -position.xyz;
vec4 box_color = textureCube(skybox, tex_coord);
gl_FragColor = box_color;
}
"""
def __enter__(self):
shader.ShaderProgram.__enter__(self)
self.skybox = glGetUniformLocation(self.shader_program, "skybox")
self.eye_shift = glGetUniformLocation(self.shader_program, "eye_shift")
def fname_to_tex(file_name):
img = QImage()
img.load(file_name)
img2 = QGLWidget.convertToGLFormat(img)
img3 = str(img2.bits())
return img3
class SkyBox:
def __init__(self):
self.is_initialized = False
self.shader = SkyBoxShaderProgram()
def init_gl(self):
if self.is_initialized:
return
self.texture_id = glGenTextures(1)
glEnable(GL_TEXTURE_CUBE_MAP)
glBindTexture(GL_TEXTURE_CUBE_MAP, self.texture_id)
# Define all 6 faces
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
if True:
img = QImage()
img.load("skybox/miramar_lf.tif")
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA8, img.width(), img.height(),
0, GL_RGBA, GL_UNSIGNED_BYTE, fname_to_tex("skybox/miramar_ft.tif"))
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA8, img.width(), img.height(),
0, GL_RGBA, GL_UNSIGNED_BYTE, fname_to_tex("skybox/miramar_bk.tif"))
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA8, img.width(), img.height(),
0, GL_RGBA, GL_UNSIGNED_BYTE, fname_to_tex("skybox/miramar_dn.tif"))
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA8, img.width(), img.height(),
0, GL_RGBA, GL_UNSIGNED_BYTE, fname_to_tex("skybox/miramar_up.tif"))
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA8, img.width(), img.height(),
0, GL_RGBA, GL_UNSIGNED_BYTE, fname_to_tex("skybox/miramar_rt.tif"))
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA8, img.width(), img.height(),
0, GL_RGBA, GL_UNSIGNED_BYTE, fname_to_tex("skybox/miramar_lf.tif"))
else:
test_img = numpy.array(256 * [50,50,128,255], 'uint8')
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA8, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, test_img)
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA8, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, test_img)
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA8, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, test_img)
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA8, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, test_img)
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA8, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, test_img)
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA8, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, test_img)
glDisable(GL_TEXTURE_CUBE_MAP)
self.shader.init_gl()
self.is_initialized = True
def paint_gl(self, camera):
if not self.is_initialized:
self.init_gl()
# print "painting skybox"
glPushAttrib(GL_TEXTURE_BIT | GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_DEPTH_BUFFER_BIT)
glDisable(GL_DEPTH_TEST)
glDepthMask(False)
glEnable(GL_TEXTURE_CUBE_MAP)
glBindTexture(GL_TEXTURE_CUBE_MAP, self.texture_id)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_REPEAT)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
# remove camera rotation
old_rotation = camera.rotation
camera.rotation = Rotation()
with camera:
z = 0.5 * (camera.zNear - camera.zFar)
w = 10*camera.zFar;
with self.shader:
glUniform1i(self.shader.skybox, 0)
glUniform1f(self.shader.eye_shift, camera.eye_shift_in_ground)
glBegin(GL_QUADS)
glVertex3d( w, w, z)
glVertex3d(-w, w, z)
glVertex3d(-w, -w, z)
glVertex3d( w, -w, z)
glEnd()
camera.rotation = old_rotation
glPopAttrib()