-
Notifications
You must be signed in to change notification settings - Fork 1
/
zoompan.py-e
213 lines (187 loc) · 6.16 KB
/
zoompan.py-e
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
#!/usr/bin/env python
# Copyright 2011, Andrew Wilson
# Licensed under the MIT license:
# http://www.opensource.org/licenses/MIT
#
# Minecraft mapping - Rendering something with OpenGL
#
# With great thanks to Joe Groff:
# http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Chapter-1:-The-Graphics-Pipeline.html
from OpenGL import GL
import pygame, pygame.image, pygame.key
from pygame.locals import *
from opengl_tools import *
import math
vertex_shader = '''\
#version 130
uniform vec2 screen_dimensions;
uniform vec2 cam_position;
uniform float zoom;
uniform float texture_dimension;
uniform float map_dimension;
in vec4 position;
out vec2 texcoord;
void main()
{
gl_Position.xy =
(
(position.xy / 2.0 + 0.5)
* texture_dimension
- cam_position
)
* 2.0
* zoom
/ screen_dimensions;
gl_Position.zw = vec2(0.0, 1.0);
texcoord = position.xy * 0.5 + 0.5;
}
'''
fragment_shader = '''\
#version 130
const float TILE_COUNT = 512.0;
const float INV_TILE_COUNT = 1.0 / TILE_COUNT;
uniform sampler2D texture_atlas;
uniform usampler2D map_texture;
uniform float zoom;
in vec2 texcoord;
out vec4 fragcolor;
void main()
{
vec2 theta;
theta = (mod(texcoord, INV_TILE_COUNT) * TILE_COUNT); //*2.0 - 1.0;
uvec4 map_sample = texture(map_texture, texcoord);
uint uphi = map_sample.x % 16u;
uint vphi = 15u - (map_sample.x / 16u);
vec2 phi = vec2(uphi, vphi);
vec2 atlas_point = phi / 16.0 + theta / 16.0;
fragcolor = textureGrad(texture_atlas, atlas_point, vec2(1/512.0/zoom,0), vec2(0,1/512.0/zoom));
}
'''
class Resources(object):
pass
def make_resources():
minecraft_map = pygame.image.load('numbered_texture_atlas.png')
atlas = pygame.image.load('numbered_texture_atlas.png')
vertex_buffer_data = float_array(
-1.0, -1.0, 0.0, 1.0,
1.0, -1.0, 0.0, 1.0,
-1.0, 1.0, 0.0, 1.0,
1.0, 1.0, 0.0, 1.0)
element_buffer_data = short_array(
0,1,2,3)
resources = Resources()
resources.vertex_buffer = make_buffer(
GL.GL_ARRAY_BUFFER,
vertex_buffer_data,
vertex_buffer_data.nbytes)
resources.element_buffer = make_buffer(
GL.GL_ELEMENT_ARRAY_BUFFER,
element_buffer_data,
element_buffer_data.nbytes)
resources.map_texture = make_texture(
image=minecraft_map, interpolate=False, alpha=True, integer=True)
resources.texture_atlas = make_texture(
image=atlas, interpolate=True, alpha=True, maxlod=4.0)
resources.program = assemble_shader_program(
vertex_shader,
fragment_shader,
uniform_names=[
'screen_dimensions',
'cam_position',
'zoom',
'texture_dimension',
'texture_atlas',
'map_texture'],
attribute_names=[
'position'])
return resources
def render(resources, position, zoom, screen_dimensions):
screen_w, screen_h = screen_dimensions
GL.glViewport(0,0,screen_w,screen_h)
GL.glClearColor(0.4, 0.4, 0.4, 1.0)
GL.glClear(GL.GL_COLOR_BUFFER_BIT)
GL.glUseProgram(resources.program.program)
uniforms = resources.program.uniforms
GL.glUniform2f(uniforms['screen_dimensions'], screen_w, screen_h)
GL.glUniform2f(uniforms['cam_position'], position[0], position[1])
GL.glUniform1f(uniforms['zoom'], zoom)
GL.glUniform1f(uniforms['texture_dimension'], 8192)
GL.glActiveTexture(GL.GL_TEXTURE0)
GL.glBindTexture(GL.GL_TEXTURE_2D, resources.map_texture)
GL.glUniform1i(resources.program.uniforms['map_texture'], 0)
GL.glActiveTexture(GL.GL_TEXTURE1)
GL.glBindTexture(GL.GL_TEXTURE_2D, resources.texture_atlas)
GL.glUniform1i(resources.program.uniforms['texture_atlas'], 1)
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, resources.vertex_buffer)
GL.glVertexAttribPointer(
resources.program.attributes['position'],
4, # size
GL.GL_FLOAT, # type
GL.GL_FALSE, # normalized?
ctypes.sizeof(GLfloat)*4, # stride
None # offset
)
position_attribute = resources.program.attributes['position']
GL.glEnableVertexAttribArray(position_attribute)
GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, resources.element_buffer)
GL.glDrawElements(
GL.GL_TRIANGLE_STRIP,
4,
GL.GL_UNSIGNED_SHORT,
None)
GL.glDisableVertexAttribArray(position_attribute)
pygame.display.flip()
def main():
video_flags = OPENGL|DOUBLEBUF
pygame.init()
screen_dimensions = 800, 600
surface = pygame.display.set_mode(
screen_dimensions, video_flags)
resources = make_resources()
frames = 0
done = 0
zoom = 1.0
position = [256, 8192 - 256]
dragging = False
draglast = 0,0
while not done:
while 1:
event = pygame.event.poll()
if event.type == NOEVENT:
break
if event.type == KEYDOWN:
pass
if event.type == QUIT:
done = 1
if event.type == MOUSEMOTION:
if dragging:
mx,my = event.pos
lx,ly = draglast
dx = (mx - lx)/zoom
dy = (my - ly)/zoom
position[0] -= dx
position[1] += dy
draglast = mx, my
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
draglast = event.pos
dragging = True
if event.button == 4:
zoom *= 2.0
if zoom > 16:
zoom = 16.0
if event.button == 5:
zoom /= 2.0
if zoom < 1.0/32.0:
zoom = 1.0/32.0
x,y = position
x = math.floor(x * zoom + 0.5) / zoom
y = math.floor(y * zoom + 0.5) / zoom
position = [x,y]
if event.type == MOUSEBUTTONUP:
if event.button == 1:
dragging = False
render(resources, position, zoom, screen_dimensions)
frames += 1
if __name__ == '__main__':
main()