-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize.py
159 lines (131 loc) · 4.35 KB
/
visualize.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
# Code adopted from http://blog.bitify.co.uk/2013/11/3d-opengl-visualisation-of-data-from.html
import pygame
import urllib
import socket
import sys
import struct
from OpenGL.GL import *
from OpenGL.GLU import *
from math import radians
from pygame.locals import *
SCREEN_SIZE = (800, 600)
SCALAR = .5
SCALAR2 = 0.2
def resize(width, height):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, float(width) / height, 0.001, 10.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(0.0, 1.0, -5.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0)
def init():
glEnable(GL_DEPTH_TEST)
glClearColor(0.0, 0.0, 0.0, 0.0)
glShadeModel(GL_SMOOTH)
glEnable(GL_BLEND)
glEnable(GL_POLYGON_SMOOTH)
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glLightfv(GL_LIGHT0, GL_AMBIENT, (0.3, 0.3, 0.3, 1.0));
def run():
pygame.init()
soc = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
soc.connect("/tmp/socket")
screen = pygame.display.set_mode(SCREEN_SIZE, HWSURFACE | OPENGL | DOUBLEBUF)
resize(*SCREEN_SIZE)
init()
clock = pygame.time.Clock()
cube = Cube((0.0, 0.0, 0.0), (.5, .5, .7))
angle = 0
while True:
then = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == QUIT:
return
if event.type == KEYUP and event.key == K_ESCAPE:
return
values = soc.recv(50).split();
x_angle = values[0];
y_angle = values[1];
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glColor((1.,1.,1.))
glLineWidth(1)
glBegin(GL_LINES)
for x in range(-20, 22, 2):
glVertex3f(x/10.,-1,-1)
glVertex3f(x/10.,-1,1)
for x in range(-20, 22, 2):
glVertex3f(x/10.,-1, 1)
glVertex3f(x/10., 1, 1)
for z in range(-10, 12, 2):
glVertex3f(-2, -1, z/10.)
glVertex3f( 2, -1, z/10.)
for z in range(-10, 12, 2):
glVertex3f(-2, -1, z/10.)
glVertex3f(-2, 1, z/10.)
for z in range(-10, 12, 2):
glVertex3f( 2, -1, z/10.)
glVertex3f( 2, 1, z/10.)
for y in range(-10, 12, 2):
glVertex3f(-2, y/10., 1)
glVertex3f( 2, y/10., 1)
for y in range(-10, 12, 2):
glVertex3f(-2, y/10., 1)
glVertex3f(-2, y/10., -1)
for y in range(-10, 12, 2):
glVertex3f(2, y/10., 1)
glVertex3f(2, y/10., -1)
glEnd()
glPushMatrix()
glRotate(float(x_angle), 1, 0, 0)
glRotate(float(y_angle), 0, 0, 1)
cube.render()
glPopMatrix()
pygame.display.flip()
class Cube(object):
def __init__(self, position, color):
self.position = position
self.color = color
# Cube information
num_faces = 6
vertices = [ (-1.0, -0.05, 0.5),
(1.0, -0.05, 0.5),
(1.0, 0.05, 0.5),
(-1.0, 0.05, 0.5),
(-1.0, -0.05, -0.5),
(1.0, -0.05, -0.5),
(1.0, 0.05, -0.5),
(-1.0, 0.05, -0.5) ]
normals = [ (0.0, 0.0, +1.0), # front
(0.0, 0.0, -1.0), # back
(+1.0, 0.0, 0.0), # right
(-1.0, 0.0, 0.0), # left
(0.0, +1.0, 0.0), # top
(0.0, -1.0, 0.0) ] # bottom
vertex_indices = [ (0, 1, 2, 3), # front
(4, 5, 6, 7), # back
(1, 5, 6, 2), # right
(0, 4, 7, 3), # left
(3, 2, 6, 7), # top
(0, 1, 5, 4) ] # bottom
def render(self):
then = pygame.time.get_ticks()
glColor(self.color)
vertices = self.vertices
# Draw all 6 faces of the cube
glBegin(GL_QUADS)
for face_no in xrange(self.num_faces):
glNormal3dv(self.normals[face_no])
v1, v2, v3, v4 = self.vertex_indices[face_no]
glVertex(vertices[v1])
glVertex(vertices[v2])
glVertex(vertices[v3])
glVertex(vertices[v4])
glEnd()
if __name__ == "__main__":
run()