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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
#-*- coding:utf-8 -*-
# Pybik -- A 3 dimensional magic cube game.
# Copyright © 2009-2012 B. Clausius <barcc@gmx.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Ported from GNUbik
# Original filename: glarea-common.c
# Original copyright and license: 2003 John Darrington, GPL3+
global __name__, __file__, __package__
import cython
#pxd from gl cimport *
#pxd from glu cimport *
#pxd from math cimport *
#pxd from cube_c cimport *
#pxd from drwBlock_c cimport *
#px-5
from OpenGL.GL import *
from OpenGL.GLU import *
from math import cos, sin, tan, pi as M_PI
from cube import *
from drwBlock import *
import cube
from debug import debug
debug('Importing module:', __name__)
debug(' from package:', __package__)
debug(' compiled:', cython.compiled)
#pxd ctypedef float Quarternion[4]
#px-
Quarternion = None
# Start with the unit quarternion
cython.declare(cube_view = Quarternion)
#px-
cube_view = [0.] * 4
cube_view[0] = 0.
cube_view[1] = 0.
cube_view[2] = 0.
cube_view[3] = 1.
j8 = [ (0.5625, 0.4375),
(0.0625, 0.9375),
(0.3125, 0.6875),
(0.6875, 0.8125),
(0.8125, 0.1875),
(0.9375, 0.5625),
(0.4375, 0.0625),
(0.1875, 0.3125),
]
# Functions for rotation
#pxd cdef void quarternion_to_matrix(p_Vector M, float* q)
def quarternion_to_matrix(M, q):
cython.declare(
q00 = cython.float,
q01 = cython.float,
q02 = cython.float,
q11 = cython.float,
q12 = cython.float,
q22 = cython.float,
q03 = cython.float,
q13 = cython.float,
q23 = cython.float,
q33 = cython.float,
)
q00 = q[0] * q[0]
q01 = q[0] * q[1]
q02 = q[0] * q[2]
q03 = q[0] * q[3]
q11 = q[1] * q[1]
q12 = q[1] * q[2]
q13 = q[1] * q[3]
q22 = q[2] * q[2]
q23 = q[2] * q[3]
q33 = q[3] * q[3]
M[0][0] = q33 + q00 - q11 - q22
M[1][1] = q33 - q00 + q11 - q22
M[2][2] = q33 - q00 - q11 + q22
M[3][3] = q33 + q00 + q11 + q22
M[0][1] = 2*(q01 - q23); M[0][2] = 2*(q02 + q13); M[1][2] = 2*(q12 - q03)
M[1][0] = 2*(q01 + q23); M[2][0] = 2*(q02 - q13); M[2][1] = 2*(q12 + q03)
M[0][3] = M[1][3] = M[2][3] = 0.0
M[3][0] = M[3][1] = M[3][2] = 0.0
def set_rotation_xy(x, y):
cython.declare(
radiansx = cython.float,
radiansy = cython.float,
sx = cython.float,
sy = cython.float,
cx = cython.float,
cy = cython.float)
x %= 180
if y < -45: y = -45
elif y > 45: y = 45
radiansx = -M_PI * x / 180.0
radiansy = -M_PI * y / 180.0
sx = sin(radiansx)
sy = sin(radiansy)
cx = cos(radiansx)
cy = cos(radiansy)
cube_view[0] = sy * cx
cube_view[1] = cy * sx
cube_view[2] = -sy * sx
cube_view[3] = cy * cx
return x, y
# display functions
cython.declare(
fovy = cython.float,
cp_near = cython.float,
cp_far = cython.float,
bounding_sphere_radius = cython.float)
fovy = 33.0 # field of view angle
cp_near = -1
cp_far = -1
bounding_sphere_radius = 0
# Wrapper to set the modelview matrix
#pxd cdef void modelViewInit()
def modelViewInit():
cython.declare(
m = Matrix)
#px-
m = Matrix()
# Update viewer position in modelview matrix
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(0, 0, bounding_sphere_radius + cp_near,
0, 0, 0, # center
0.0, 1.0, 0.0) # up
quarternion_to_matrix(m, cube_view)
glMultMatrixM(m)
#
# DM 3-Jan-2004
#
# Add a couple of 90 degree turns to get the top and right faces in their
# logical positions when the program starts up.
glRotatef(90.0, 1, 0, 0)
glRotatef(-90.0, 0, 0, 1)
#pxd cdef inline int* glGetViewport(int* v):
#pxd glGetIntegerv(GL_VIEWPORT, v)
#pxd return v
#px-
def glGetViewport(unused_v): return glGetIntegerv(GL_VIEWPORT)
# accFrustum and accPerspective are taken from p397 of the Red Book
#pxd cdef void accFrustum(double rad, double zNear, double zFar, double pixdx, double pixdy)
def accFrustum(rad, zNear, zFar, pixdx, pixdy):
cython.declare(
viewport_ = cython.int[4],
viewport = cython.p_int,
dx = cython.double,
dy = cython.double,
wsize = cython.double)
#px-
viewport_ = None
viewport = glGetViewport(viewport_)
wsize = 2*rad
dx = -pixdx * wsize / viewport[2]
dy = -pixdy * wsize / viewport[3]
glFrustum(dx-rad, dy+rad, dy-rad, dy+rad, zNear, zFar)
#pxd cdef void projection_init(int jitter)
def projection_init(jitter):
global bounding_sphere_radius, cp_near, cp_far
bounding_sphere_radius = 2 * cube.get_cube_dimension()
cp_near = bounding_sphere_radius / tan(fovy * M_PI / 360.0)
cp_far = cp_near + 2*bounding_sphere_radius
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
accFrustum(bounding_sphere_radius, cp_near, cp_far, j8[jitter][0], j8[jitter][1])
def graphics_area_init(lighting):
lighting_init(lighting)
glClearColor(0., 0., 0., 0.)
projection_init(0)
modelViewInit()
def display():
glClearColor(background_color.red, background_color.green, background_color.blue, 1.)
projection_init(0)
modelViewInit()
glClear(GL_ACCUM_BUFFER_BIT)
for jitter in xrange(8):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
projection_init(jitter)
modelViewInit()
draw_cube()
glAccum(GL_ACCUM, 1.0/8.0)
glAccum(GL_RETURN, 1.0)
#pxd cdef enum:
#pxd BUFSIZE = 512
#pxd ctypedef GLuint SelectBuf[BUFSIZE]
#px-2
BUFSIZE = 512
SelectBuf = lambda: [0]*BUFSIZE
cython.declare(
selectBuf = SelectBuf
)
#px-
selectBuf = SelectBuf()
#pxd cdef void glSelectBuffer_()
def glSelectBuffer_():
#px-
glSelectBuffer(BUFSIZE); return
glSelectBuffer(BUFSIZE, selectBuf)
#pxd ctypedef unsigned char *p_uchar
#pxd cdef inline p_uchar glReadPixel(int x, int y, p_uchar pixel):
#pxd glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel); return pixel
#px-3
p_uchar = cython.p_char
def glReadPixel(x, y, pixel):
return glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, [[[0, 0, 0]]])[0][0]
def pick_polygons(lighting, x, y, granularity, pick_mode=1):
cython.declare(
viewport_ = cython.int[4],
viewport = cython.p_int,
pixel = cython.uchar[3],
pixel_ = p_uchar)
#px-2
viewport_ = None
pixel = [0]*3
viewport = glGetViewport(viewport_)
if x < 0 or y < 0 or x >= viewport[2] or y >= viewport[3]:
return None
if lighting:
set_lighting(False)
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
gluPickMatrix(x, y, granularity, granularity, viewport)
gluPerspective(fovy, 1, cp_near, cp_far)
glClearColor(0., 0., 0., 0.)
modelViewInit()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
if pick_mode == 1:
pick_cube()
else:
pick2_cube()
glMatrixMode(GL_PROJECTION)
glPopMatrix()
pixel_ = glReadPixel(x, y, pixel)
if lighting:
set_lighting(True)
if pixel_[2] == 0:
return None
if pick_mode == 1:
return (((pixel_[0]-68)<<8) | pixel_[1]) >> 3, pixel_[1] & 0x7, pixel_[2] / 0x55
else:
return pixel_[0], pixel_[1], pixel_[2]
#pxd cdef void lighting_init(bint lighting)
def lighting_init(lighting):
cython.declare(
light_ambient = cython.float[4],
mat_diffuse = cython.float[4],
mat_specular = cython.float[4],
mat_shininess = cython.float,
light_position = cython.float[4])
#px-4
light_ambient = [0]*4
mat_diffuse = [0]*4
mat_specular = [0]*4
light_position = [0]*4
init_vector(light_ambient, [0.6, 0.6, 0.6, 1.0])
init_vector(mat_diffuse, [0.8, 0.8, 0.8, 1.0])
init_vector(mat_specular, [0.2, 0.2, 0.2, 1.0])
mat_shininess = 1.0
init_vector(light_position, [-1.0, 0.5, 0.0, 0.0])
glShadeModel(GL_SMOOTH)
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular)
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse)
glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess)
glLightfv(GL_LIGHT0, GL_POSITION, light_position)
glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient)
if lighting:
glEnable(GL_LIGHTING)
else:
glDisable(GL_LIGHTING)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_LIGHT0)
glEnable(GL_LIGHT1)
#pxd cpdef set_lighting(int enable)
def set_lighting(enable):
if enable:
glEnable(GL_LIGHTING)
else:
glDisable(GL_LIGHTING)
#pxd cdef struct _Color:
#pxd float red
#pxd float green
#pxd float blue
#px-2
class _Color: pass
background_color = _Color()
cython.declare(background_color = _Color)
def set_background_color(red, green, blue):
background_color.red = red
background_color.green = green
background_color.blue = blue
#pxd cpdef resize(int width, int height)
def resize(width, height):
cython.declare(
min_dim = cython.int)
min_dim = min(width, height)
# Ensure that cube is always the same proportions
glViewport((width-min_dim) / 2, (height-min_dim) / 2, min_dim, min_dim)
|