-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathglobe.py
More file actions
111 lines (94 loc) · 3.95 KB
/
globe.py
File metadata and controls
111 lines (94 loc) · 3.95 KB
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
import pygame
import math
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from PIL import Image
import numpy
def read_texture(filename):
"""
Reads an image file and converts to a OpenGL-readable textID format
"""
img = Image.open(filename)
img_data = numpy.array(list(img.getdata()), numpy.int8)
textID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, textID)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
img.size[0], img.size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
return textID
def main():
pygame.init()
display = (400, 400)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
pygame.display.set_caption('PyOpenGLobe')
pygame.key.set_repeat(1, 10) # allows press and hold of buttons
gluPerspective(40, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0, 0.0, -5) # sets initial zoom so we can see globe
lastPosX = 0
lastPosY = 0
texture = read_texture('world.jpg')
while True:
for event in pygame.event.get(): # user avtivities are called events
# Exit cleanly if user quits window
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Rotation with arrow keys
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
glRotatef(1, 0, 1, 0)
if event.key == pygame.K_RIGHT:
glRotatef(1, 0, -1, 0)
if event.key == pygame.K_UP:
glRotatef(1, -1, 0, 0)
if event.key == pygame.K_DOWN:
glRotatef(1, 1, 0, 0)
# Zoom in and out with mouse wheel
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4: # wheel rolled up
glScaled(1.05, 1.05, 1.05)
if event.button == 5: # wheel rolled down
glScaled(0.95, 0.95, 0.95)
# Rotate with mouse click and drag
if event.type == pygame.MOUSEMOTION:
x, y = event.pos
dx = x - lastPosX
dy = y - lastPosY
mouseState = pygame.mouse.get_pressed()
if mouseState[0]:
modelView = (GLfloat * 16)()
mvm = glGetFloatv(GL_MODELVIEW_MATRIX, modelView)
# To combine x-axis and y-axis rotation
temp = (GLfloat * 3)()
temp[0] = modelView[0]*dy + modelView[1]*dx
temp[1] = modelView[4]*dy + modelView[5]*dx
temp[2] = modelView[8]*dy + modelView[9]*dx
norm_xy = math.sqrt(temp[0]*temp[0] + temp[1]
* temp[1] + temp[2]*temp[2])
glRotatef(math.sqrt(dx*dx+dy*dy),
temp[0]/norm_xy, temp[1]/norm_xy, temp[2]/norm_xy)
lastPosX = x
lastPosY = y
# Creates Sphere and wraps texture
glEnable(GL_DEPTH_TEST)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
qobj = gluNewQuadric()
gluQuadricTexture(qobj, GL_TRUE)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, texture)
gluSphere(qobj, 1, 50, 50)
gluDeleteQuadric(qobj)
glDisable(GL_TEXTURE_2D)
# Displays pygame window
pygame.display.flip()
pygame.time.wait(10)
main()