-
Notifications
You must be signed in to change notification settings - Fork 1
/
opengl2.py
183 lines (157 loc) · 4.93 KB
/
opengl2.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
178
179
180
181
#!/usr/bin/env python
# Copyright 2011, Annette 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.GL import *
import pygame, pygame.image, pygame.key
from pygame.locals import *
from opengl_tools import *
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;
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 = texture2D(texture_atlas, atlas_point);
}
'''
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_ARRAY_BUFFER,
vertex_buffer_data,
vertex_buffer_data.nbytes)
resources.element_buffer = make_buffer(
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
glViewport(0,0,screen_w,screen_h)
glClearColor(0.4, 0.4, 0.4, 1.0)
glClear(GL_COLOR_BUFFER_BIT)
glUseProgram(resources.program.program)
uniforms = resources.program.uniforms
glUniform2f(uniforms['screen_dimensions'], screen_w, screen_h)
glUniform2f(uniforms['cam_position'], position[0], position[1])
glUniform1f(uniforms['zoom'], zoom)
glUniform1f(uniforms['texture_dimension'], 8192)
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, resources.map_texture)
glUniform1i(resources.program.uniforms['map_texture'], 0)
glActiveTexture(GL_TEXTURE1)
glBindTexture(GL_TEXTURE_2D, resources.texture_atlas)
glUniform1i(resources.program.uniforms['texture_atlas'], 1)
glBindBuffer(GL_ARRAY_BUFFER, resources.vertex_buffer)
glVertexAttribPointer(
resources.program.attributes['position'],
4, # size
GL_FLOAT, # type
GL_FALSE, # normalized?
ctypes.sizeof(GLfloat)*4, # stride
None # offset
)
position_attribute = resources.program.attributes['position']
glEnableVertexAttribArray(position_attribute)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, resources.element_buffer)
glDrawElements(
GL_TRIANGLE_STRIP,
4,
GL_UNSIGNED_SHORT,
None)
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] #//256.0, 256.0]
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
render(resources, position, zoom, screen_dimensions)
frames += 1
if __name__ == '__main__':
main()