-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtu_02_texture_without_normal.py
177 lines (147 loc) · 4.94 KB
/
tu_02_texture_without_normal.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
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
# import os,sys
# sys.path.append(os.path.abspath(os.path.dirname(__file__)))
from OpenGL.GL import * # pylint: disable=W0614
import glm
from utils.textureLoader import textureLoader
from utils.glutWindow import GlutWindow
from utils.shaderLoader import Shader
g_vertex_buffer_data = [
-1.0,-1.0,-1.0,
-1.0,-1.0, 1.0,
-1.0, 1.0, 1.0,
1.0, 1.0,-1.0,
-1.0,-1.0,-1.0,
-1.0, 1.0,-1.0,
1.0,-1.0, 1.0,
-1.0,-1.0,-1.0,
1.0,-1.0,-1.0,
1.0, 1.0,-1.0,
1.0,-1.0,-1.0,
-1.0,-1.0,-1.0,
-1.0,-1.0,-1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0,-1.0,
1.0,-1.0, 1.0,
-1.0,-1.0, 1.0,
-1.0,-1.0,-1.0,
-1.0, 1.0, 1.0,
-1.0,-1.0, 1.0,
1.0,-1.0, 1.0,
1.0, 1.0, 1.0,
1.0,-1.0,-1.0,
1.0, 1.0,-1.0,
1.0,-1.0,-1.0,
1.0, 1.0, 1.0,
1.0,-1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0,-1.0,
-1.0, 1.0,-1.0,
1.0, 1.0, 1.0,
-1.0, 1.0,-1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
1.0,-1.0, 1.0]
g_uv_buffer_data = [
0.000059, 1.0-0.000004,
0.000103, 1.0-0.336048,
0.335973, 1.0-0.335903,
1.000023, 1.0-0.000013,
0.667979, 1.0-0.335851,
0.999958, 1.0-0.336064,
0.667979, 1.0-0.335851,
0.336024, 1.0-0.671877,
0.667969, 1.0-0.671889,
1.000023, 1.0-0.000013,
0.668104, 1.0-0.000013,
0.667979, 1.0-0.335851,
0.000059, 1.0-0.000004,
0.335973, 1.0-0.335903,
0.336098, 1.0-0.000071,
0.667979, 1.0-0.335851,
0.335973, 1.0-0.335903,
0.336024, 1.0-0.671877,
1.000004, 1.0-0.671847,
0.999958, 1.0-0.336064,
0.667979, 1.0-0.335851,
0.668104, 1.0-0.000013,
0.335973, 1.0-0.335903,
0.667979, 1.0-0.335851,
0.335973, 1.0-0.335903,
0.668104, 1.0-0.000013,
0.336098, 1.0-0.000071,
0.000103, 1.0-0.336048,
0.000004, 1.0-0.671870,
0.336024, 1.0-0.671877,
0.000103, 1.0-0.336048,
0.336024, 1.0-0.671877,
0.335973, 1.0-0.335903,
0.667969, 1.0-0.671889,
1.000004, 1.0-0.671847,
0.667979, 1.0-0.335851
]
class Tu01Win(GlutWindow):
class GLContext(object):
pass
def init_opengl(self):
glClearColor(0.0,0,0.4,0)
glDepthFunc(GL_LESS)
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
def init_context(self):
self.context = self.GLContext()
# vertex = glGenVertexArrays(1) # pylint: disable=W0612
# glBindVertexArray(vertex)
self.shader = shader = Shader()
shader.initShaderFromGLSL(["glsl/tu02/vertex.glsl"],["glsl/tu02/fragment.glsl"])
self.context.MVP_ID = glGetUniformLocation(shader.program,"MVP")
self.context.TextureID = glGetUniformLocation(shader.program, "myTextureSampler")
texture = textureLoader("resources/tu02/uvtemplate.tga")
#texture = textureLoader("resources/tu02/uvtemplate.dds")
self.context.textureGLID = texture.textureGLID
self.context.vertexbuffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER,self.context.vertexbuffer)
glBufferData(GL_ARRAY_BUFFER,len(g_vertex_buffer_data)*4,(GLfloat * len(g_vertex_buffer_data))(*g_vertex_buffer_data),GL_STATIC_DRAW)
if(texture.inversedVCoords):
for index in range(0,len(g_uv_buffer_data)):
if(index % 2):
g_uv_buffer_data[index] = 1.0 - g_uv_buffer_data[index]
self.context.uvbuffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER,self.context.uvbuffer)
glBufferData(GL_ARRAY_BUFFER,len(g_uv_buffer_data)*4,(GLfloat * len(g_uv_buffer_data))(*g_uv_buffer_data),GL_STATIC_DRAW)
def calc_MVP(self,width=1920,height=1080):
self.context.Projection = glm.perspective(glm.radians(45.0),float(width)/float(height),0.1,1000.0)
self.context.View = glm.lookAt(glm.vec3(4,3,-3), # Camera is at (4,3,-3), in World Space
glm.vec3(0,0,0), #and looks at the (0.0.0))
glm.vec3(0,1,0) ) #Head is up (set to 0,-1,0 to look upside-down)
#fixed Cube Size
self.context.Model= glm.mat4(1.0)
#print self.context.Model
self.context.MVP = self.context.Projection * self.context.View * self.context.Model
def resize(self,Width,Height):
glViewport(0, 0, Width, Height)
self.calc_MVP(Width,Height)
def ogl_draw(self):
# print("draw++")
#print self.context.MVP
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
self.shader.begin()
glUniformMatrix4fv(self.context.MVP_ID,1,GL_FALSE,glm.value_ptr(self.context.MVP))
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, self.context.textureGLID)
glUniform1i(self.context.TextureID, 0)
glEnableVertexAttribArray(0)
glBindBuffer(GL_ARRAY_BUFFER, self.context.vertexbuffer)
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,None)
glEnableVertexAttribArray(1)
glBindBuffer(GL_ARRAY_BUFFER, self.context.uvbuffer)
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,None)
glDrawArrays(GL_TRIANGLES, 0, 12*3) # 12*3 indices starting at 0 -> 12 triangles
glDisableVertexAttribArray(0)
glDisableVertexAttribArray(1)
self.shader.end()
if __name__ == "__main__":
win = Tu01Win()
win.init_opengl()
win.init_context()
win.run()