Commit d9396fef authored by sumpfralle's avatar sumpfralle

fixed code-style issues


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@499 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 66044a53
...@@ -23,20 +23,21 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -23,20 +23,21 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
import pycam.Cutters import pycam.Cutters
from pycam.Geometry.Point import Point from pycam.Geometry.Point import Point
import ode import ode
try: try:
import OpenGL.GL as GL import OpenGL.GL as GL
GL_enabled = True GL_enabled = True
except: except ImportError:
GL_enabled = False GL_enabled = False
class ODEBlocks: class ODEBlocks:
def __init__(self, tool_settings, (minx, maxx, miny, maxy, minz, maxz), x_steps=None, y_steps=None): def __init__(self, tool_settings, (minx, maxx, miny, maxy, minz, maxz),
x_steps=None, y_steps=None):
self.cutter = pycam.Cutters.get_tool_from_settings(tool_settings) self.cutter = pycam.Cutters.get_tool_from_settings(tool_settings)
# we don't want to use the "material allowance" distance # we don't want to use the "material allowance" distance
self.cutter.set_required_distance(0) self.cutter.set_required_distance(0)
dimx = maxx - minx
dimy = maxy - miny
if x_steps is None: if x_steps is None:
x_steps = 10 x_steps = 10
if y_steps is None: if y_steps is None:
...@@ -55,10 +56,13 @@ class ODEBlocks: ...@@ -55,10 +56,13 @@ class ODEBlocks:
z_pos = self.z_offset + 0.5 * self.z_width z_pos = self.z_offset + 0.5 * self.z_width
self.x_step_width = self.x_width / (self.x_steps - 1) self.x_step_width = self.x_width / (self.x_steps - 1)
self.y_step_width = self.y_width / (self.y_steps - 1) self.y_step_width = self.y_width / (self.y_steps - 1)
for x_pos in [self.x_offset + (index + 0.5) / x_steps * self.x_width for index in range(x_steps)]: for x_pos in [self.x_offset + (index + 0.5) / x_steps * self.x_width \
for y_pos in [self.y_offset + (index + 0.5) / y_steps * self.y_width for index in range(y_steps)]: for index in range(x_steps)]:
for y_pos in [self.y_offset + (index + 0.5) / y_steps \
* self.y_width for index in range(y_steps)]:
body = ode.Body(self.world) body = ode.Body(self.world)
box = ode.GeomBox(self.space, (self.x_step_width, self.y_step_width, self.z_width)) box = ode.GeomBox(self.space, (self.x_step_width,
self.y_step_width, self.z_width))
box.setBody(body) box.setBody(body)
box.setPosition((x_pos, y_pos, z_pos)) box.setPosition((x_pos, y_pos, z_pos))
box.position = Point(x_pos, y_pos, z_pos) box.position = Point(x_pos, y_pos, z_pos)
...@@ -67,7 +71,8 @@ class ODEBlocks: ...@@ -67,7 +71,8 @@ class ODEBlocks:
def process_cutter_movement(self, location_start, location_end): def process_cutter_movement(self, location_start, location_end):
# TODO: fix this workaround in the cutters shape defintions (or in ODE?) # TODO: fix this workaround in the cutters shape defintions (or in ODE?)
# for now we may only move from low x/y values to higher x/y values # for now we may only move from low x/y values to higher x/y values
if (location_start.x > location_end.x) or (location_start.y > location_end.y): if (location_start.x > location_end.x) \
or (location_start.y > location_end.y):
swap = location_start swap = location_start
location_start = location_end location_start = location_end
location_end = location_start location_end = location_start
...@@ -76,8 +81,11 @@ class ODEBlocks: ...@@ -76,8 +81,11 @@ class ODEBlocks:
self.space.add(cutter_shape) self.space.add(cutter_shape)
cutter_shape.space = self.space cutter_shape.space = self.space
cutter_shape.setBody(cutter_body) cutter_shape.setBody(cutter_body)
cutter_position_func(location_start.x, location_start.y, location_start.z) cutter_position_func(location_start.x, location_start.y,
cutter_shape.extend_shape(location_end.x - location_start.x, location_end.y - location_start.y, location_end.z - location_start.z) location_start.z)
cutter_shape.extend_shape(location_end.x - location_start.x,
location_end.y - location_start.y,
location_end.z - location_start.z)
aabb = cutter_shape.getAABB() aabb = cutter_shape.getAABB()
cutter_height = aabb[5] - aabb[4] cutter_height = aabb[5] - aabb[4]
# add a ray along the drill to work around an ODE bug in v0.11.1 # add a ray along the drill to work around an ODE bug in v0.11.1
...@@ -120,52 +128,61 @@ class ODEBlocks: ...@@ -120,52 +128,61 @@ class ODEBlocks:
lower_limit = new_z lower_limit = new_z
loops_left -= 1 loops_left -= 1
del self.boxes[box_index] del self.boxes[box_index]
# the height should never be zero - otherwise ODE will throw a "bNormalizationResult" assertion # The height should never be zero - otherwise ODE will throw a
# "bNormalizationResult" assertion.
new_height = max(new_z - end_height, 0.1) new_height = max(new_z - end_height, 0.1)
z_pos = new_z - new_height / 2.0 z_pos = new_z - new_height / 2.0
new_box = ode.GeomBox(self.space, (aabb[1] - aabb[0], aabb[3] - aabb[2], new_height)) new_box = ode.GeomBox(self.space, (aabb[1] - aabb[0], aabb[3] - aabb[2],
new_height))
new_box.position = Point(x_pos, y_pos, z_pos) new_box.position = Point(x_pos, y_pos, z_pos)
new_box.setBody(box.getBody()) new_box.setBody(box.getBody())
new_box.setPosition((x_pos, y_pos, z_pos)) new_box.setPosition((x_pos, y_pos, z_pos))
self.boxes.insert(box_index, new_box) self.boxes.insert(box_index, new_box)
def get_height_field(self): def get_height_field(self):
""" returns a two-dimensional height field of the current "landscape" """ """ returns a two-dimensional height field of the current "landscape"
"""
result = [] result = []
heights = [] heights = []
for box in self.boxes: for box in self.boxes:
aabb = box.getAABB() aabb = box.getAABB()
heights.append(Point((aabb[1] + aabb[0]) / 2.0, (aabb[3] + aabb[2]) / 2.0, aabb[5])) heights.append(Point((aabb[1] + aabb[0]) / 2.0,
(aabb[3] + aabb[2]) / 2.0, aabb[5]))
for column in range(self.x_steps): for column in range(self.x_steps):
result.append(heights[column * self.y_steps : (column + 1) * self.y_steps]) result.append(heights[
column * self.y_steps : (column + 1) * self.y_steps])
return result return result
def _normal(self, z0, z1, z2): def _normal(self, z0, z1, z2):
nx = self.x_steps / self.x_width nx = self.x_steps / self.x_width
ny = self.y_steps / self.y_width ny = self.y_steps / self.y_width
nz = 1.0 / (self.z_width) nz = 1.0 / (self.z_width)
return (-ny * (z1 - z0) * nz / nx, -nx * (z2 - z1) * nz / ny, nx * ny / nz * 100) return (-ny * (z1 - z0) * nz / nx, -nx * (z2 - z1) * nz / ny,
nx * ny / nz * 100)
def to_OpenGL(self): def to_OpenGL(self):
if not GL_enabled: if not GL_enabled:
return return
height_field = self.get_height_field() height_field = self.get_height_field()
def get_box_height_points(x, y): def get_box_height_points(x, y):
""" Get the positions and heights of the the four top corners of a height box. """ Get the positions and heights of the the four top corners of a
The result is a tuple of four Points (pycam.Geometry.Point) in the following order: height box.
The result is a tuple of four Points (pycam.Geometry.Point) in the
following order:
- left below - left below
- right below - right below
- right above - right above
- left above - left above
("above": greater x position value; "right": greater y position value) ("above": greater x value; "right": greater y value)
The height of each corner point is calculated as the average of the The height of each corner point is calculated as the average of the
four neighbouring boxes. Thus a set of 3x3 adjacent boxes is used four neighbouring boxes. Thus a set of 3x3 adjacent boxes is used
for calculating the heights of the four corners. for calculating the heights of the four corners.
""" """
points = [] points = []
# Go through a set of box index combinations (sharing a common corner). # Go through a set of box index combinations (sharing a common
# The "offsets" tuple is used for indicating the relative position # corner). The "offsets" tuple is used for indicating the relative
# of each corner. # position of each corner.
for offsets, index_list in ( for offsets, index_list in (
((-1, -1), ((x - 1, y - 1), (x, y - 1), (x, y), (x - 1, y))), ((-1, -1), ((x - 1, y - 1), (x, y - 1), (x, y), (x - 1, y))),
((+1, -1), ((x, y - 1), (x, y), (x + 1, y), (x + 1, y - 1))), ((+1, -1), ((x, y - 1), (x, y), (x + 1, y), (x + 1, y - 1))),
...@@ -176,7 +193,8 @@ class ODEBlocks: ...@@ -176,7 +193,8 @@ class ODEBlocks:
x_positions = [] x_positions = []
y_positions = [] y_positions = []
for ix, iy in index_list: for ix, iy in index_list:
if (0 <= ix < len(height_field)) and (0 <= iy < len(height_field[ix])): if (0 <= ix < len(height_field)) \
and (0 <= iy < len(height_field[ix])):
point = height_field[ix][iy] point = height_field[ix][iy]
height_sum += point.z height_sum += point.z
x_positions.append(point.x) x_positions.append(point.x)
...@@ -190,14 +208,17 @@ class ODEBlocks: ...@@ -190,14 +208,17 @@ class ODEBlocks:
else: else:
# There is no adjacent box in x direction. Use the step size # There is no adjacent box in x direction. Use the step size
# to calculate the x value of this edge. # to calculate the x value of this edge.
x_value = height_field[x][y].x + offsets[0] * self.x_step_width / 2.0 x_value = height_field[x][y].x \
+ offsets[0] * self.x_step_width / 2.0
# same as above for y instead of x # same as above for y instead of x
if (min(y_positions) < height_field[x][y].y) \ if (min(y_positions) < height_field[x][y].y) \
or (max(y_positions) > height_field[x][y].y): or (max(y_positions) > height_field[x][y].y):
y_value = (min(y_positions) + max(y_positions)) / 2.0 y_value = (min(y_positions) + max(y_positions)) / 2.0
else: else:
y_value = height_field[x][y].y + offsets[1] * self.y_step_width / 2.0 y_value = height_field[x][y].y \
# Create a Point instance describing the position and the average height. + offsets[1] * self.y_step_width / 2.0
# Create a Point instance describing the position and the
# average height.
points.append(Point(x_value, y_value, height_sum / divisor)) points.append(Point(x_value, y_value, height_sum / divisor))
return points return points
# draw the surface # draw the surface
...@@ -209,20 +230,28 @@ class ODEBlocks: ...@@ -209,20 +230,28 @@ class ODEBlocks:
points_around = get_box_height_points(x, y) points_around = get_box_height_points(x, y)
# Calculate the "normal" of polygon. We picked up three random # Calculate the "normal" of polygon. We picked up three random
# points of this quadrilateral. # points of this quadrilateral.
n = self._normal(points_around[1].z, points_around[2].z, points_around[3].z) n = self._normal(points_around[1].z, points_around[2].z,
points_around[3].z)
GL.glNormal3f(n[0], n[1], n[2]) GL.glNormal3f(n[0], n[1], n[2])
for point in points_around: for point in points_around:
GL.glVertex3f(point.x, point.y, point.z) GL.glVertex3f(point.x, point.y, point.z)
# go through the conditions for an edge box and use the # go through the conditions for an edge box and use the
# appropriate corners for the side faces of the material # appropriate corners for the side faces of the material
for condition, i1, i2 in ((x == 0, 3, 0), (y == 0, 0, 1), (x == self.x_steps - 1, 1, 2), (y == self.y_steps - 1, 2, 3)): for condition, i1, i2 in ((x == 0, 3, 0), (y == 0, 0, 1),
(x == self.x_steps - 1, 1, 2),
(y == self.y_steps - 1, 2, 3)):
# check if this point belongs to an edge of the material # check if this point belongs to an edge of the material
if condition: if condition:
n = self._normal(points_around[1].z, points_around[2].z, points_around[3].z) n = self._normal(points_around[1].z, points_around[2].z,
points_around[3].z)
GL.glNormal3f(n[0], n[1], n[2]) GL.glNormal3f(n[0], n[1], n[2])
GL.glVertex3f(points_around[i1].x, points_around[i1].y, self.z_offset) GL.glVertex3f(points_around[i1].x, points_around[i1].y,
GL.glVertex3f(points_around[i1].x, points_around[i1].y, points_around[i1].z) self.z_offset)
GL.glVertex3f(points_around[i2].x, points_around[i2].y, points_around[i2].z) GL.glVertex3f(points_around[i1].x, points_around[i1].y,
GL.glVertex3f(points_around[i2].x, points_around[i2].y, self.z_offset) points_around[i1].z)
GL.glVertex3f(points_around[i2].x, points_around[i2].y,
points_around[i2].z)
GL.glVertex3f(points_around[i2].x, points_around[i2].y,
self.z_offset)
GL.glEnd() GL.glEnd()
...@@ -24,19 +24,22 @@ import math ...@@ -24,19 +24,22 @@ import math
from pycam.Geometry.Point import Point from pycam.Geometry.Point import Point
import ctypes import ctypes
try: try:
import OpenGL.GL as GL import OpenGL.GL as GL
GL_enabled = True GL_enabled = True
except: except ImportError:
GL_enabled = False GL_enabled = False
EPSILON=1e-8
EPSILON = 1e-8
NUM_PER_CELL_X = 10 NUM_PER_CELL_X = 10
NUM_PER_CELL_Y = 10 NUM_PER_CELL_Y = 10
NUM_CELL_X = 0 NUM_CELL_X = 0
NUM_CELL_Y = 0 NUM_CELL_Y = 0
class ZBufferItem: class ZBufferItem:
def __init__(self, z=0.0): def __init__(self, z=0.0):
self.z = float(z) self.z = float(z)
...@@ -50,7 +53,7 @@ class ZCellItem: ...@@ -50,7 +53,7 @@ class ZCellItem:
class ZBuffer: class ZBuffer:
def __init__(self, minx, maxx, xres, miny, maxy, yres, minz, maxz): def __init__(self, minx, maxx, xres, miny, maxy, yres, minz, maxz):
global NUM_CELL_X, NUM_CELL_Y, NUM_PER_CELL_X, NUM_PER_CELL_Y global NUM_CELL_X, NUM_CELL_Y
self.minx = float(minx) self.minx = float(minx)
self.maxx = float(maxx) self.maxx = float(maxx)
self.miny = float(miny) self.miny = float(miny)
...@@ -65,36 +68,37 @@ class ZBuffer: ...@@ -65,36 +68,37 @@ class ZBuffer:
NUM_CELL_Y = self.yres / NUM_PER_CELL_Y NUM_CELL_Y = self.yres / NUM_PER_CELL_Y
self.x = [0.0] * self.xres self.x = [0.0] * self.xres
for i in range(0,self.xres): for i in range(0, self.xres):
self.x[i] = self.minx+(i * (self.maxx-self.minx)/self.xres) self.x[i] = self.minx+(i * (self.maxx-self.minx)/self.xres)
self.y = [0.0] * self.yres self.y = [0.0] * self.yres
for i in range(0,self.yres): for i in range(0, self.yres):
self.y[i] = self.miny+(i * (self.maxy-self.miny)/self.yres) self.y[i] = self.miny+(i * (self.maxy-self.miny)/self.yres)
self.buf = [ [] ] * self.yres self.buf = [ [] ] * self.yres
for y in range(0,self.yres): for y in range(0, self.yres):
self.buf[y] = [ None ] * self.xres self.buf[y] = [ None ] * self.xres
for x in range(0,self.xres): for x in range(0, self.xres):
self.buf[y][x] = ZBufferItem(self.minz) self.buf[y][x] = ZBufferItem(self.minz)
self.list = [ [] ] * self.yres self.list = [ [] ] * self.yres
for y in range(0,self.yres): for y in range(0, self.yres):
self.list[y] = [ None ] * self.xres self.list[y] = [ None ] * self.xres
for x in range(0,self.xres): for x in range(0, self.xres):
self.list[y][x] = -1 self.list[y][x] = -1
self.cell = [ None ] * NUM_CELL_Y self.cell = [ None ] * NUM_CELL_Y
for y in range(0,NUM_CELL_Y): for y in range(0, NUM_CELL_Y):
self.cell[y] = [ None ] * NUM_CELL_X self.cell[y] = [ None ] * NUM_CELL_X
for x in range(0,NUM_CELL_X): for x in range(0, NUM_CELL_X):
self.cell[y][x] = ZCellItem() self.cell[y][x] = ZCellItem()
def add_wave(self, freq=8, damp=3.0): def add_wave(self, freq=8, damp=3.0):
self.changed = True self.changed = True
rmax = math.sqrt(self.y[0]*self.y[0]+self.x[0]*self.x[0]) rmax = math.sqrt(self.y[0]*self.y[0]+self.x[0]*self.x[0])
for y in range(0,self.yres): for y in range(0, self.yres):
for x in range(0,self.xres): for x in range(0, self.xres):
r = math.sqrt(self.y[y]*self.y[y]+self.x[x]*self.x[x]) r = math.sqrt(self.y[y]*self.y[y]+self.x[x]*self.x[x])
self.buf[y][x].z = 1+math.cos(r/rmax*r/rmax*math.pi*freq)/(1+damp*(r/rmax)) self.buf[y][x].z = 1 + math.cos(r / rmax * r / rmax * math.pi \
* freq) / (1 + damp * (r / rmax))
self.buf[y][x].changed = True self.buf[y][x].changed = True
def add_triangles(self, triangles): def add_triangles(self, triangles):
...@@ -102,29 +106,33 @@ class ZBuffer: ...@@ -102,29 +106,33 @@ class ZBuffer:
self.add_triangle(t) self.add_triangle(t)
def add_triangle(self, t): def add_triangle(self, t):
minx = int((t.minx()-self.minx)/(self.maxx-self.minx)*self.xres)-1 minx = int((t.minx() - self.minx) / (self.maxx - self.minx) \
maxx = int((t.maxx()-self.minx)/(self.maxx-self.minx)*self.xres)+1 * self.xres) - 1
miny = int((t.miny()-self.miny)/(self.maxy-self.miny)*self.yres)-1 maxx = int((t.maxx() - self.minx) / (self.maxx - self.minx) \
maxy = int((t.maxy()-self.miny)/(self.maxy-self.miny)*self.yres)+2 * self.xres) + 1
if minx<0: miny = int((t.miny() - self.miny) / (self.maxy - self.miny) \
minx=0 * self.yres) - 1
if maxx>self.xres-1: maxy = int((t.maxy() - self.miny) / (self.maxy - self.miny) \
maxx = self.xres-1 * self.yres) + 2
if miny<0: if minx < 0:
miny=0 minx = 0
if maxy>self.yres-1: if maxx > self.xres - 1:
maxy = self.yres-1 maxx = self.xres - 1
if miny < 0:
miny = 0
if maxy > self.yres - 1:
maxy = self.yres - 1
for y in range(miny, maxy): for y in range(miny, maxy):
py = self.y[y] py = self.y[y]
for x in range(minx, maxx): for x in range(minx, maxx):
px = self.x[x] px = self.x[x]
v0x = t.p3.x-t.p1.x v0x = t.p3.x - t.p1.x
v0y = t.p3.y-t.p1.y v0y = t.p3.y - t.p1.y
v1x = t.p2.x-t.p1.x v1x = t.p2.x - t.p1.x
v1y = t.p2.y-t.p1.y v1y = t.p2.y - t.p1.y
v2x = px-t.p1.x v2x = px - t.p1.x
v2y = py-t.p1.y v2y = py - t.p1.y
dot00 = v0x*v0x + v0y*v0y dot00 = v0x*v0x + v0y*v0y
dot01 = v0x*v1x + v0y*v1y dot01 = v0x*v1x + v0y*v1y
dot02 = v0x*v2x + v0y*v2y dot02 = v0x*v2x + v0y*v2y
...@@ -134,10 +142,10 @@ class ZBuffer: ...@@ -134,10 +142,10 @@ class ZBuffer:
u = (dot11 * dot02 - dot01 * dot12) * invDenom u = (dot11 * dot02 - dot01 * dot12) * invDenom
v = (dot00 * dot12 - dot01 * dot02) * invDenom v = (dot00 * dot12 - dot01 * dot02) * invDenom
if (u >= -EPSILON) and (v >= -EPSILON) and (u + v <= 1-EPSILON): if (u >= -EPSILON) and (v >= -EPSILON) and (u + v <= 1-EPSILON):
v0z = t.p3.z-t.p1.z v0z = t.p3.z - t.p1.z
v1z = t.p2.z-t.p1.z v1z = t.p2.z - t.p1.z
pz = t.p1.z+v0z*u+v1z*v pz = t.p1.z + v0z * u + v1z * v
if pz>self.buf[y][x].z: if pz > self.buf[y][x].z:
self.buf[y][x].z = pz self.buf[y][x].z = pz
self.buf[y+0][x+0].changed = True self.buf[y+0][x+0].changed = True
self.buf[y+0][x+1].changed = True self.buf[y+0][x+1].changed = True
...@@ -149,38 +157,43 @@ class ZBuffer: ...@@ -149,38 +157,43 @@ class ZBuffer:
cx = c.location.x cx = c.location.x
cy = c.location.y cy = c.location.y
rsq = c.radiussq rsq = c.radiussq
minx = int((c.minx-self.minx)/(self.maxx-self.minx)*self.xres)-1 minx = int((c.minx - self.minx) / (self.maxx - self.minx) * self.xres) \
maxx = int((c.maxx-self.minx)/(self.maxx-self.minx)*self.xres)+1 - 1
miny = int((c.miny-self.miny)/(self.maxy-self.miny)*self.yres)-1 maxx = int((c.maxx - self.minx) / (self.maxx - self.minx) * self.xres) \
maxy = int((c.maxy-self.miny)/(self.maxy-self.miny)*self.yres)+1 + 1
if minx<0: miny = int((c.miny - self.miny) / (self.maxy - self.miny) * self.yres) \
minx=0 - 1
if maxx<0: maxy = int((c.maxy - self.miny) / (self.maxy - self.miny) * self.yres) \
maxx=0 + 1
if minx>self.xres-1: if minx < 0:
minx = self.xres-1 minx = 0
if maxx>self.xres-1: if maxx < 0:
maxx = self.xres-1 maxx = 0
if miny<0: if minx > self.xres - 1:
miny=0 minx = self.xres - 1
if maxy<0: if maxx > self.xres - 1:
maxy=0 maxx = self.xres - 1
if maxy>self.yres-1: if miny < 0:
maxy = self.yres-1 miny = 0
if miny>self.yres-1: if maxy < 0:
miny = self.yres-1 maxy = 0
p = Point(0,0,0) if maxy > self.yres - 1:
zaxis = Point(0,0,-1) maxy = self.yres - 1
if miny > self.yres - 1:
miny = self.yres - 1
p = Point(0, 0, 0)
zaxis = Point(0, 0, -1)
for y in range(miny, maxy): for y in range(miny, maxy):
p.y = py = self.y[y] p.y = py = self.y[y]
for x in range(minx, maxx): for x in range(minx, maxx):
p.x = px = self.x[x] p.x = px = self.x[x]
if (px-cx)*(px-cx)+(py-cy)*(py-cy) <= rsq+EPSILON: if (px - cx) * (px - cx) + (py - cy) * (py - cy) \
(cl,ccp,cp,l) = c.intersect_point(zaxis, p) <= rsq + EPSILON:
(cl, ccp, cp, l) = c.intersect_point(zaxis, p)
if ccp: if ccp:
pz = l pz = l
if pz<self.buf[y][x].z: if pz < self.buf[y][x].z:
self.buf[y][x].z = pz self.buf[y][x].z = pz
self.buf[y+0][x+0].changed = True self.buf[y+0][x+0].changed = True
self.buf[y+0][x+1].changed = True self.buf[y+0][x+1].changed = True
...@@ -194,18 +207,20 @@ class ZBuffer: ...@@ -194,18 +207,20 @@ class ZBuffer:
self.changed = False self.changed = False
def normal(self, z0, z1, z2): def normal(self, z0, z1, z2):
nx = 1.0/self.xres nx = 1.0 / self.xres
ny = 1.0/self.yres ny = 1.0 / self.yres
nz = 1.0/(self.maxz-self.minz) nz = 1.0 / (self.maxz - self.minz)
return -ny*(z1-z0)*nz/nx,-nx*(z2-z1)*nz/ny,nx*ny/nz*100 return (-ny * (z1 - z0) * nz / nx, -nx * (z2 - z1) * nz / ny,
nx * ny / nz * 100)
# the naive way (quads) # the naive way (quads)
def to_OpenGL_1(self): def to_OpenGL_1(self):
GL.glBegin(GL.GL_QUADS) GL.glBegin(GL.GL_QUADS)
for y in range(0,self.yres-1): for y in range(self.yres - 1):
for x in range(0,self.xres-1): for x in range(self.xres - 1):
n = self.normal(self.buf[y+0][x+0].z,self.buf[y+0][x+1].z,self.buf[y+1][x+0].z) n = self.normal(self.buf[y+0][x+0].z, self.buf[y+0][x+1].z,
GL.glNormal3f(n[0],n[1],n[2]) self.buf[y+1][x+0].z)
GL.glNormal3f(n[0], n[1], n[2])
GL.glVertex3f(self.x[x+0], self.y[y+0], self.buf[y+0][x+0].z) GL.glVertex3f(self.x[x+0], self.y[y+0], self.buf[y+0][x+0].z)
GL.glVertex3f(self.x[x+1], self.y[y+0], self.buf[y+0][x+1].z) GL.glVertex3f(self.x[x+1], self.y[y+0], self.buf[y+0][x+1].z)
GL.glVertex3f(self.x[x+1], self.y[y+1], self.buf[y+1][x+1].z) GL.glVertex3f(self.x[x+1], self.y[y+1], self.buf[y+1][x+1].z)
...@@ -215,29 +230,33 @@ class ZBuffer: ...@@ -215,29 +230,33 @@ class ZBuffer:
# use display lists (per quad) # use display lists (per quad)
def to_OpenGL_2(self): def to_OpenGL_2(self):
dx = 1.0/self.xres for y in range(self.yres - 1):
dy = 1.0/self.yres for x in range(self.xres - 1):
dz = 1.0/(self.maxz-self.minz)
for y in range(0,self.yres-1):
for x in range(0,self.xres-1):
# print "z[%f][%f]=%f" % (self.y[y],self.x[x],self.buf[y][x]) # print "z[%f][%f]=%f" % (self.y[y],self.x[x],self.buf[y][x])
if self.buf[y+0][x+0].changed or self.buf[y+0][x+1].changed or self.buf[y+1][x+0].changed or self.buf[y+1][x+1].changed: if self.buf[y+0][x+0].changed or self.buf[y+0][x+1].changed \
or self.buf[y+1][x+0].changed \
or self.buf[y+1][x+1].changed:
if self.buf[y][x].list == -1: if self.buf[y][x].list == -1:
self.buf[y][x].list = GL.glGenLists(1) self.buf[y][x].list = GL.glGenLists(1)
GL.glNewList(self.buf[y][x].list, GL.GL_COMPILE) GL.glNewList(self.buf[y][x].list, GL.GL_COMPILE)
GL.glBegin(GL.GL_QUADS) GL.glBegin(GL.GL_QUADS)
n = self.normal(self.buf[y+0][x+0].z,self.buf[y+0][x+1].z,self.buf[y+1][x+0].z) n = self.normal(self.buf[y+0][x+0].z, self.buf[y+0][x+1].z,
GL.glNormal3f(n[0],n[1],n[2]) self.buf[y+1][x+0].z)
GL.glVertex3f(self.x[x+0], self.y[y+0], self.buf[y+0][x+0].z) GL.glNormal3f(n[0], n[1], n[2])
GL.glVertex3f(self.x[x+1], self.y[y+0], self.buf[y+0][x+1].z) GL.glVertex3f(self.x[x+0], self.y[y+0],
GL.glVertex3f(self.x[x+1], self.y[y+1], self.buf[y+1][x+1].z) self.buf[y+0][x+0].z)
GL.glVertex3f(self.x[x+0], self.y[y+1], self.buf[y+1][x+0].z) GL.glVertex3f(self.x[x+1], self.y[y+0],
self.buf[y+0][x+1].z)
GL.glVertex3f(self.x[x+1], self.y[y+1],
self.buf[y+1][x+1].z)
GL.glVertex3f(self.x[x+0], self.y[y+1],
self.buf[y+1][x+0].z)
GL.glEnd() GL.glEnd()
GL.glEndList() GL.glEndList()
for y in range(0,self.yres-1): for y in range(0, self.yres-1):
for x in range(0,self.xres-1): for x in range(0, self.xres-1):
self.buf[y][x].changed = False self.buf[y][x].changed = False
GL.glCallList(self.buf[y][x].list) GL.glCallList(self.buf[y][x].list)
...@@ -246,13 +265,13 @@ class ZBuffer: ...@@ -246,13 +265,13 @@ class ZBuffer:
dy = self.yres/NUM_CELL_Y dy = self.yres/NUM_CELL_Y
dx = self.xres/NUM_CELL_X dx = self.xres/NUM_CELL_X
for y in range(0,NUM_CELL_Y): for y in range(NUM_CELL_Y):
y0 = y*dy y0 = y * dy
y1 = y0 + dy + 1 y1 = y0 + dy + 1
if y1 > self.yres: if y1 > self.yres:
y1 = self.yres y1 = self.yres
for x in range(0,NUM_CELL_X): for x in range(NUM_CELL_X):
x0 = x*dx x0 = x * dx
x1 = x0 + dx + 1 x1 = x0 + dx + 1
if x1 > self.xres: if x1 > self.xres:
x1 = self.xres x1 = self.xres
...@@ -260,8 +279,8 @@ class ZBuffer: ...@@ -260,8 +279,8 @@ class ZBuffer:
changed = False changed = False
if self.changed: if self.changed:
for yi in range(y0,y1): for yi in range(y0, y1):
for xi in range(x0,x1): for xi in range(x0, x1):
if self.buf[yi][xi].changed: if self.buf[yi][xi].changed:
changed = True changed = True
break break
...@@ -272,33 +291,49 @@ class ZBuffer: ...@@ -272,33 +291,49 @@ class ZBuffer:
if False: if False:
GL.glNewList(self.list[y][x], GL.GL_COMPILE) GL.glNewList(self.list[y][x], GL.GL_COMPILE)
for yi in range(y0,y1-1): for yi in range(y0, y1-1):
GL.glBegin(GL.GL_TRIANGLES) GL.glBegin(GL.GL_TRIANGLES)
for xi in range(x0,x1-1): for xi in range(x0, x1-1):
n = self.normal(self.buf[yi+0][xi+0].z,self.buf[yi+0][xi+1].z,self.buf[yi+1][xi+0].z) n = self.normal(self.buf[yi+0][xi+0].z,
GL.glNormal3f(n[0],n[1],n[2]) self.buf[yi+0][xi+1].z,
GL.glVertex3f(self.x[xi+0], self.y[yi+0], self.buf[yi+0][xi+0].z) self.buf[yi+1][xi+0].z)
GL.glVertex3f(self.x[xi+0], self.y[yi+1], self.buf[yi+1][xi+0].z) GL.glNormal3f(n[0], n[1], n[2])
GL.glVertex3f(self.x[xi+1], self.y[yi+1], self.buf[yi+1][xi+1].z) GL.glVertex3f(self.x[xi+0], self.y[yi+0],
n = self.normal(self.buf[y+1][x+1].z,self.buf[y+0][x+1].z,self.buf[y+1][x+0].z) self.buf[yi+0][xi+0].z)
GL.glNormal3f(n[0],n[1],n[2]) GL.glVertex3f(self.x[xi+0], self.y[yi+1],
GL.glVertex3f(self.x[xi+0], self.y[yi+0], self.buf[yi+0][xi+0].z) self.buf[yi+1][xi+0].z)
GL.glVertex3f(self.x[xi+1], self.y[yi+1], self.buf[yi+1][xi+1].z) GL.glVertex3f(self.x[xi+1], self.y[yi+1],
GL.glVertex3f(self.x[xi+1], self.y[yi+0], self.buf[yi+0][xi+1].z) self.buf[yi+1][xi+1].z)
n = self.normal(self.buf[y+1][x+1].z,
self.buf[y+0][x+1].z,
self.buf[y+1][x+0].z)
GL.glNormal3f(n[0], n[1], n[2])
GL.glVertex3f(self.x[xi+0], self.y[yi+0],
self.buf[yi+0][xi+0].z)
GL.glVertex3f(self.x[xi+1], self.y[yi+1],
self.buf[yi+1][xi+1].z)
GL.glVertex3f(self.x[xi+1], self.y[yi+0],
self.buf[yi+0][xi+1].z)
self.buf[yi][xi].changed = False self.buf[yi][xi].changed = False
GL.glEnd() GL.glEnd()
GL.glEndList() GL.glEndList()
else: else:
GL.glNewList(self.list[y][x], GL.GL_COMPILE) GL.glNewList(self.list[y][x], GL.GL_COMPILE)
for yi in range(y0,y1-1): for yi in range(y0, y1-1):
GL.glBegin(GL.GL_QUADS) GL.glBegin(GL.GL_QUADS)
for xi in range(x0,x1-1): for xi in range(x0, x1-1):
n = self.normal(self.buf[yi+0][xi+0].z,self.buf[yi+0][xi+1].z,self.buf[yi+1][xi+0].z) n = self.normal(self.buf[yi+0][xi+0].z,
GL.glNormal3f(n[0],n[1],n[2]) self.buf[yi+0][xi+1].z,
GL.glVertex3f(self.x[xi+0], self.y[yi+0], self.buf[yi+0][xi+0].z) self.buf[yi+1][xi+0].z)
GL.glVertex3f(self.x[xi+0], self.y[yi+1], self.buf[yi+1][xi+0].z) GL.glNormal3f(n[0], n[1], n[2])
GL.glVertex3f(self.x[xi+1], self.y[yi+1], self.buf[yi+1][xi+1].z) GL.glVertex3f(self.x[xi+0], self.y[yi+0],
GL.glVertex3f(self.x[xi+1], self.y[yi+0], self.buf[yi+0][xi+1].z) self.buf[yi+0][xi+0].z)
GL.glVertex3f(self.x[xi+0], self.y[yi+1],
self.buf[yi+1][xi+0].z)
GL.glVertex3f(self.x[xi+1], self.y[yi+1],
self.buf[yi+1][xi+1].z)
GL.glVertex3f(self.x[xi+1], self.y[yi+0],
self.buf[yi+0][xi+1].z)
self.buf[yi][xi].changed = False self.buf[yi][xi].changed = False
GL.glEnd() GL.glEnd()
GL.glEndList() GL.glEndList()
...@@ -311,7 +346,7 @@ class ZBuffer: ...@@ -311,7 +346,7 @@ class ZBuffer:
num_cell_x = NUM_CELL_X num_cell_x = NUM_CELL_X
num_cell_y = NUM_CELL_Y num_cell_y = NUM_CELL_Y
dy = self.yres/num_cell_y dy = self.yres / num_cell_y
if dy < 2: if dy < 2:
num_cell_y = 1 num_cell_y = 1
dy = self.yres dy = self.yres
...@@ -320,13 +355,13 @@ class ZBuffer: ...@@ -320,13 +355,13 @@ class ZBuffer:
num_cell_x = 1 num_cell_x = 1
dx = self.xres dx = self.xres
for y in range(0,num_cell_y): for y in range(0, num_cell_y):
y0 = y*dy y0 = y * dy
y1 = y0 + dy + 1 y1 = y0 + dy + 1
if y1 > self.yres: if y1 > self.yres:
y1 = self.yres y1 = self.yres
for x in range(0,num_cell_x): for x in range(0, num_cell_x):
x0 = x*dx x0 = x * dx
x1 = x0 + dx + 1 x1 = x0 + dx + 1
if x1 > self.xres: if x1 > self.xres:
x1 = self.xres x1 = self.xres
...@@ -334,8 +369,8 @@ class ZBuffer: ...@@ -334,8 +369,8 @@ class ZBuffer:
changed = False changed = False
if self.changed: if self.changed:
for yi in range(y0,y1): for yi in range(y0, y1):
for xi in range(x0,x1): for xi in range(x0, x1):
if self.buf[yi][xi].changed: if self.buf[yi][xi].changed:
changed = True changed = True
break break
...@@ -344,39 +379,44 @@ class ZBuffer: ...@@ -344,39 +379,44 @@ class ZBuffer:
# print "cell[",y,"][",x,"]=",self.cell[y][x].list # print "cell[",y,"][",x,"]=",self.cell[y][x].list
if self.cell[y][x].list == -1: if self.cell[y][x].list == -1:
self.cell[y][x].list = GL.glGenLists(1) self.cell[y][x].list = GL.glGenLists(1)
self.cell[y][x].array = (ctypes.c_float*3*((y1-y0)*(x1-x0)*2))() self.cell[y][x].array = (ctypes.c_float * 3 \
* ((y1 - y0) * (x1 - x0) * 2))()
GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glEnableClientState(GL.GL_VERTEX_ARRAY)
GL.glVertexPointerf(self.cell[y][x].array) GL.glVertexPointerf(self.cell[y][x].array)
GL.glNewList(self.cell[y][x].list, GL.GL_COMPILE) GL.glNewList(self.cell[y][x].list, GL.GL_COMPILE)
idx = 0 idx = 0
for yi in range(y0,y1-1): for yi in range(y0, y1-1):
lineidx = idx lineidx = idx
for xi in range(x0,x1): for xi in range(x0, x1):
self.buf[yi][xi].changed = False self.buf[yi][xi].changed = False
self.buf[yi+1][xi].changed = False self.buf[yi+1][xi].changed = False
self.cell[y][x].array[idx+0][0] = self.x[xi+0] self.cell[y][x].array[idx+0][0] = self.x[xi+0]
self.cell[y][x].array[idx+0][1] = self.y[yi+0] self.cell[y][x].array[idx+0][1] = self.y[yi+0]
self.cell[y][x].array[idx+0][2] = self.buf[yi+0][xi+0].z self.cell[y][x].array[idx+0][2] = \
self.buf[yi+0][xi+0].z
self.cell[y][x].array[idx+1][0] = self.x[xi+0] self.cell[y][x].array[idx+1][0] = self.x[xi+0]
self.cell[y][x].array[idx+1][1] = self.y[yi+1] self.cell[y][x].array[idx+1][1] = self.y[yi+1]
self.cell[y][x].array[idx+1][2] = self.buf[yi+1][xi+0].z self.cell[y][x].array[idx+1][2] = \
self.buf[yi+1][xi+0].z
idx += 2 idx += 2
GL.glDrawArrays(GL.GL_QUAD_STRIP, lineidx, idx-lineidx+1) GL.glDrawArrays(GL.GL_QUAD_STRIP, lineidx,
idx - lineidx + 1)
GL.glEndList() GL.glEndList()
GL.glCallList(self.cell[y][x].list) GL.glCallList(self.cell[y][x].list)
# use display list with vertex and normal buffers per cell (cell = group of quads) # use display list with vertex and normal buffers per cell
# (cell = group of quads)
def to_OpenGL_5(self): def to_OpenGL_5(self):
num_cell_x = NUM_CELL_X num_cell_x = NUM_CELL_X
num_cell_y = NUM_CELL_Y num_cell_y = NUM_CELL_Y
dy = self.yres/num_cell_y dy = self.yres / num_cell_y
if dy < 2: if dy < 2:
num_cell_y = 1 num_cell_y = 1
dy = self.yres dy = self.yres
dx = self.xres/num_cell_x dx = self.xres / num_cell_x
if dx < 2: if dx < 2:
num_cell_x = 1 num_cell_x = 1
dx = self.xres dx = self.xres
...@@ -384,13 +424,13 @@ class ZBuffer: ...@@ -384,13 +424,13 @@ class ZBuffer:
GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glEnableClientState(GL.GL_VERTEX_ARRAY)
GL.glEnableClientState(GL.GL_NORMAL_ARRAY) GL.glEnableClientState(GL.GL_NORMAL_ARRAY)
for y in range(0,num_cell_y): for y in range(num_cell_y):
y0 = y*dy y0 = y * dy
y1 = y0 + dy + 1 y1 = y0 + dy + 1
if y1 > self.yres: if y1 > self.yres:
y1 = self.yres y1 = self.yres
for x in range(0,num_cell_x): for x in range(num_cell_x):
x0 = x*dx x0 = x * dx
x1 = x0 + dx + 1 x1 = x0 + dx + 1
if x1 > self.xres: if x1 > self.xres:
x1 = self.xres x1 = self.xres
...@@ -398,8 +438,8 @@ class ZBuffer: ...@@ -398,8 +438,8 @@ class ZBuffer:
changed = False changed = False
if self.changed: if self.changed:
for yi in range(y0,y1): for yi in range(y0, y1):
for xi in range(x0,x1): for xi in range(x0, x1):
if self.buf[yi][xi].changed: if self.buf[yi][xi].changed:
changed = True changed = True
break break
...@@ -408,32 +448,40 @@ class ZBuffer: ...@@ -408,32 +448,40 @@ class ZBuffer:
# print "cell[",y,"][",x,"]=",self.cell[y][x].list # print "cell[",y,"][",x,"]=",self.cell[y][x].list
if self.cell[y][x].list == -1: if self.cell[y][x].list == -1:
self.cell[y][x].list = GL.glGenLists(1) self.cell[y][x].list = GL.glGenLists(1)
self.cell[y][x].vertex = (ctypes.c_float*3*((y1-y0)*(x1-x0)*4))() self.cell[y][x].vertex = (ctypes.c_float * 3 \
self.cell[y][x].normal = (ctypes.c_float*3*((y1-y0)*(x1-x0)*4))() * ((y1 - y0) * (x1 - x0) * 4))()
self.cell[y][x].normal = (ctypes.c_float * 3 \
* ((y1 - y0) * (x1 - x0) * 4))()
GL.glVertexPointerf(self.cell[y][x].vertex) GL.glVertexPointerf(self.cell[y][x].vertex)
GL.glNormalPointerf(self.cell[y][x].normal) GL.glNormalPointerf(self.cell[y][x].normal)
GL.glNewList(self.cell[y][x].list, GL.GL_COMPILE) GL.glNewList(self.cell[y][x].list, GL.GL_COMPILE)
idx = 0 idx = 0
for yi in range(y0,y1-1): for yi in range(y0, y1-1):
lineidx = idx lineidx = idx
for xi in range(x0,x1-1): for xi in range(x0, x1-1):
self.buf[yi][xi].changed = False self.buf[yi][xi].changed = False
self.buf[yi+1][xi].changed = False self.buf[yi+1][xi].changed = False
self.cell[y][x].vertex[idx+0][0] = self.x[xi+0] self.cell[y][x].vertex[idx+0][0] = self.x[xi+0]
self.cell[y][x].vertex[idx+0][1] = self.y[yi+0] self.cell[y][x].vertex[idx+0][1] = self.y[yi+0]
self.cell[y][x].vertex[idx+0][2] = self.buf[yi+0][xi+0].z self.cell[y][x].vertex[idx+0][2] = \
self.buf[yi+0][xi+0].z
self.cell[y][x].vertex[idx+1][0] = self.x[xi+0] self.cell[y][x].vertex[idx+1][0] = self.x[xi+0]
self.cell[y][x].vertex[idx+1][1] = self.y[yi+1] self.cell[y][x].vertex[idx+1][1] = self.y[yi+1]
self.cell[y][x].vertex[idx+1][2] = self.buf[yi+1][xi+0].z self.cell[y][x].vertex[idx+1][2] = \
self.buf[yi+1][xi+0].z
self.cell[y][x].vertex[idx+2][0] = self.x[xi+1] self.cell[y][x].vertex[idx+2][0] = self.x[xi+1]
self.cell[y][x].vertex[idx+2][1] = self.y[yi+1] self.cell[y][x].vertex[idx+2][1] = self.y[yi+1]
self.cell[y][x].vertex[idx+2][2] = self.buf[yi+1][xi+1].z self.cell[y][x].vertex[idx+2][2] = \
self.buf[yi+1][xi+1].z
self.cell[y][x].vertex[idx+3][0] = self.x[xi+1] self.cell[y][x].vertex[idx+3][0] = self.x[xi+1]
self.cell[y][x].vertex[idx+3][1] = self.y[yi+0] self.cell[y][x].vertex[idx+3][1] = self.y[yi+0]
self.cell[y][x].vertex[idx+3][2] = self.buf[yi+0][xi+1].z self.cell[y][x].vertex[idx+3][2] = \
self.buf[yi+0][xi+1].z
n = self.normal(self.buf[yi+0][xi+0].z,self.buf[yi+0][xi+1].z,self.buf[yi+1][xi+0].z) n = self.normal(self.buf[yi+0][xi+0].z,
self.buf[yi+0][xi+1].z,
self.buf[yi+1][xi+0].z)
self.cell[y][x].normal[idx+0][0] = n[0] self.cell[y][x].normal[idx+0][0] = n[0]
self.cell[y][x].normal[idx+0][1] = n[1] self.cell[y][x].normal[idx+0][1] = n[1]
self.cell[y][x].normal[idx+0][2] = n[2] self.cell[y][x].normal[idx+0][2] = n[2]
...@@ -456,7 +504,8 @@ class ZBuffer: ...@@ -456,7 +504,8 @@ class ZBuffer:
GL.glDisableClientState(GL.GL_VERTEX_ARRAY) GL.glDisableClientState(GL.GL_VERTEX_ARRAY)
GL.glDisableClientState(GL.GL_NORMAL_ARRAY) GL.glDisableClientState(GL.GL_NORMAL_ARRAY)
# use display list with vertex and normal and index buffers per cell (cell = group of quads) # use display list with vertex and normal and index buffers per cell
# (cell = group of quads)
def to_OpenGL_6(self): def to_OpenGL_6(self):
num_cell_x = NUM_CELL_X num_cell_x = NUM_CELL_X
num_cell_y = NUM_CELL_Y num_cell_y = NUM_CELL_Y
...@@ -474,13 +523,13 @@ class ZBuffer: ...@@ -474,13 +523,13 @@ class ZBuffer:
GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glEnableClientState(GL.GL_VERTEX_ARRAY)
GL.glEnableClientState(GL.GL_NORMAL_ARRAY) GL.glEnableClientState(GL.GL_NORMAL_ARRAY)
for y in range(0,num_cell_y): for y in range(num_cell_y):
y0 = y*dy y0 = y * dy
y1 = y0 + dy + 1 y1 = y0 + dy + 1
if y1 > self.yres: if y1 > self.yres:
y1 = self.yres y1 = self.yres
for x in range(0,num_cell_x): for x in range(num_cell_x):
x0 = x*dx x0 = x * dx
x1 = x0 + dx + 1 x1 = x0 + dx + 1
if x1 > self.xres: if x1 > self.xres:
x1 = self.xres x1 = self.xres
...@@ -488,8 +537,8 @@ class ZBuffer: ...@@ -488,8 +537,8 @@ class ZBuffer:
changed = False changed = False
if self.changed: if self.changed:
for yi in range(y0,y1-1): for yi in range(y0, y1 - 1):
for xi in range(x0,x1-1): for xi in range(x0, x1 - 1):
if self.buf[yi][xi].changed: if self.buf[yi][xi].changed:
changed = True changed = True
break break
...@@ -498,26 +547,33 @@ class ZBuffer: ...@@ -498,26 +547,33 @@ class ZBuffer:
#print "cell[",y,"][",x,"]=",self.cell[y][x].list #print "cell[",y,"][",x,"]=",self.cell[y][x].list
if self.cell[y][x].list == -1: if self.cell[y][x].list == -1:
self.cell[y][x].list = GL.glGenLists(1) self.cell[y][x].list = GL.glGenLists(1)
self.cell[y][x].vertex = (ctypes.c_float*3*((y1-y0)*(x1-x0)))() self.cell[y][x].vertex = (ctypes.c_float * 3 \
self.cell[y][x].normal = (ctypes.c_float*3*((y1-y0)*(x1-x0)))() * ((y1-y0) * (x1 - x0)))()
self.cell[y][x].index = (ctypes.c_ushort*(4*(y1-y0-1)*(x1-x0-1)))() self.cell[y][x].normal = (ctypes.c_float * 3 \
* ((y1-y0) * (x1 - x0)))()
self.cell[y][x].index = (ctypes.c_ushort \
* (4 * (y1 - y0 - 1) * (x1 - x0 - 1)))()
GL.glIndexPointers(self.cell[y][x].index) GL.glIndexPointers(self.cell[y][x].index)
GL.glVertexPointerf(self.cell[y][x].vertex) GL.glVertexPointerf(self.cell[y][x].vertex)
GL.glNormalPointerf(self.cell[y][x].normal) GL.glNormalPointerf(self.cell[y][x].normal)
GL.glNewList(self.cell[y][x].list, GL.GL_COMPILE) GL.glNewList(self.cell[y][x].list, GL.GL_COMPILE)
idx = 0 idx = 0
for yi in range(y0,y1): for yi in range(y0, y1):
for xi in range(x0,x1): for xi in range(x0, x1):
self.buf[yi][xi].changed = False self.buf[yi][xi].changed = False
self.cell[y][x].vertex[idx][0] = self.x[xi] self.cell[y][x].vertex[idx][0] = self.x[xi]
self.cell[y][x].vertex[idx][1] = self.y[yi] self.cell[y][x].vertex[idx][1] = self.y[yi]
self.cell[y][x].vertex[idx][2] = self.buf[yi][xi].z self.cell[y][x].vertex[idx][2] = self.buf[yi][xi].z
if xi==self.xres-1 or yi==self.yres-1: if (xi==self.xres - 1) or (yi == self.yres - 1):
n = self.normal(self.buf[yi-1][xi-1].z,self.buf[yi-1][xi].z,self.buf[yi][xi-1].z) n = self.normal(self.buf[yi-1][xi-1].z,
self.buf[yi-1][xi].z,
self.buf[yi][xi-1].z)
else: else:
n = self.normal(self.buf[yi][xi].z,self.buf[yi][xi+1].z,self.buf[yi+1][xi].z) n = self.normal(self.buf[yi][xi].z,
self.buf[yi][xi+1].z,
self.buf[yi+1][xi].z)
self.cell[y][x].normal[idx][0] = n[0] self.cell[y][x].normal[idx][0] = n[0]
self.cell[y][x].normal[idx][1] = n[1] self.cell[y][x].normal[idx][1] = n[1]
...@@ -526,15 +582,20 @@ class ZBuffer: ...@@ -526,15 +582,20 @@ class ZBuffer:
idx += 1 idx += 1
idx = 0 idx = 0
for yi in range(y0,y1-1): for yi in range(y0, y1 - 1):
for xi in range(x0,x1-1): for xi in range(x0, x1 - 1):
self.cell[y][x].index[idx+0] = (yi-y0+0)*(x1-x0) + (xi-x0+0) self.cell[y][x].index[idx+0] = (yi - y0 + 0) \
self.cell[y][x].index[idx+1] = (yi-y0+1)*(x1-x0) + (xi-x0+0) * (x1 - x0) + (xi - x0 + 0)
self.cell[y][x].index[idx+2] = (yi-y0+1)*(x1-x0) + (xi-x0+1) self.cell[y][x].index[idx+1] = (yi - y0 + 1) \
self.cell[y][x].index[idx+3] = (yi-y0+0)*(x1-x0) + (xi-x0+1) * (x1 - x0) + (xi - x0 + 0)
self.cell[y][x].index[idx+2] = (yi - y0 + 1) \
* (x1 - x0) + (xi - x0 + 1)
self.cell[y][x].index[idx+3] = (yi - y0 + 0) \
* (x1 - x0) + (xi - x0 + 1)
idx += 4 idx += 4
GL.glDrawElements(GL.GL_QUADS, idx, GL.GL_UNSIGNED_SHORT, self.cell[y][x].index) GL.glDrawElements(GL.GL_QUADS, idx, GL.GL_UNSIGNED_SHORT,
self.cell[y][x].index)
GL.glEndList() GL.glEndList()
GL.glCallList(self.cell[y][x].list) GL.glCallList(self.cell[y][x].list)
......
...@@ -21,4 +21,5 @@ You should have received a copy of the GNU General Public License ...@@ -21,4 +21,5 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>. along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
""" """
all = ['ZBuffer', 'ODEBlocks'] __all__ = ['ZBuffer', 'ODEBlocks']
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment