def display(self, mode_2d=False): glEnable(GL_LINE_SMOOTH) orig_linewidth = (GLfloat)() glGetFloatv(GL_LINE_WIDTH, orig_linewidth) glLineWidth(3.0) glCallList(self.display_list) glLineWidth(orig_linewidth) glDisable(GL_LINE_SMOOTH)
def orientation(self, vec): mat = (gl.GLfloat * 16)() gl.glPushMatrix() gl.glLoadIdentity() gl.gluLookAt(0., 0., 0., vec[0], vec[1], vec[2], 0, 1, 0) gl.glGetFloatv(gl.GL_MODELVIEW_MATRIX, mat) gl.glPopMatrix() mat = np.array(mat).reshape(4, 4) # rot_mat = rotutils.rotation_matrix_between_vectors(self.orientation0, vec) self.rotation = self.rotation.from_matrix(mat[:3, :3])
def update(self): glPushMatrix() glLoadIdentity() glTranslatef(*self.position) glRotatef(self.pitch, 1, 0, 0) glRotatef(self.yaw, 0, 1, 0) glRotatef(self.roll, 0, 0, 1) glScalef(*self._mesh_scale) glGetFloatv(GL_MODELVIEW_MATRIX, self._model_view_matrix) glPopMatrix()
def toScreenSpace(self, v): """ Returns a point that had the current camera translation applied to it """ p = Vec2d(v) a = (gl.GLfloat * 16)() # our transformation matrix gl.glGetFloatv(gl.GL_MODELVIEW_MATRIX, a) return Vec2d( # matrix multiplication with z ignored # see: http://stackoverflow.com/questions/9328992/opengl-transformations p.x*a[0] + p.y*a[4] + 0*a[8] + 1*a[12], p.x*a[1] + p.y*a[5] + 0*a[9] + 1*a[13], )
def get_world_pos(self): modelview = to_gl_float([0] * 16) gl.glGetFloatv(gl.GL_MODELVIEW_MATRIX, modelview) modelview_list = [[modelview[w + h * 4] for w in range(4)] for h in range(4)] self.inv_modelview = numpy.linalg.inv(modelview_list) self.up_vector = list(self.inv_modelview[1][:3]) self.forward_vector = list(self.inv_modelview[2][:3]) self.world_pos = self.inv_modelview[3][:3] self.current_chunk, self.current_tile = get_chunk_pos(self.world_pos)
def gl_color_get(self): """ Initialize the matrix with the contents of the OpenGL color matrix :return: the current matrix :rtype: matrix """ ctypes_matrix = (gl.GLfloat * 16)() gl.glGetFloatv(gl.GL_COLOR_MATRIX, ctypes_matrix) for i in range(0, 4): for j in range(0, 4): self.matrix[i, j] = ctypes_matrix[i + 4 * j] return self.matrix
def gl_projection_get(self): """Initialize the matrix with the contents of the OpenGL projection matrix :return: the current matrix :rtype: matrix """ ctypes_matrix = (gl.GLfloat * 16)() gl.glGetFloatv(gl.GL_PROJECTION_MATRIX, ctypes_matrix) for i in range(0, 4): for j in range(0, 4): self.matrix[i, j] = ctypes_matrix[i + 4 * j] return self
def getFloatv(parName): """Get a single float parameter value, return it as a Python float. Parameters ---------- pName : :obj:`float' OpenGL property enum to query. Returns ------- int """ val = GL.GLfloat() GL.glGetFloatv(parName, val) return float(val.value)
def move(self, dx, dy): """Moves with camer in left/right and forward/backward direction from point of viewer @param dx: moves in righ/left direction @param dy: moves in forward/backward direction""" cameraDX = self.radius * dx * 5 * self.sensitivity cameraDY = self.radius * dy * 5 * self.sensitivity glMatrixMode(GL_MODELVIEW) # GL_MODELVIEW_MATRIX glPushMatrix() glLoadIdentity() glRotatef(-self.fi, 0, 0, 1) glTranslatef(cameraDX, cameraDY, 0) glRotatef(self.fi, 0, 0, 1) self.modelview = (GLfloat * 16)() glGetFloatv(GL_MODELVIEW_MATRIX, self.modelview) self.position.x += self.modelview[12] self.position.y += self.modelview[13] self.position.z += self.modelview[14] glPopMatrix()
def toModelSpace(self, v): """ Returns a point in modelspace that would be displayed at v """ # note: don't call this too often, it's not that efficient. p = Vec2d(v) a = (gl.GLfloat * 16)() # our transformation matrix gl.glGetFloatv(gl.GL_MODELVIEW_MATRIX, a) i = inverse([ # our inverse matrix [a[0], a[4], a[ 8], a[12]], [a[1], a[5], a[ 9], a[13]], [a[2], a[6], a[10], a[14]], [a[3], a[7], a[11], a[15]], ]) return Vec2d( # matrix multiplication with z ignored # (note: our matrix format is a little different to toModelView()) p.x*i[0][0] + p.y*i[0][1] + 0*i[0][2] + 1*i[0][3], p.x*i[1][0] + p.y*i[1][1] + 0*i[1][2] + 1*i[1][3], )
def move(self, dx, dy): """Moves with camer in left/right and forward/backward direction from point of viewer @param dx: moves in righ/left direction @param dy: moves in forward/backward direction""" cameraDX = self.radius * dx *5* self.sensitivity cameraDY = self.radius * dy *5* self.sensitivity glMatrixMode(GL_MODELVIEW)# GL_MODELVIEW_MATRIX glPushMatrix() glLoadIdentity() glRotatef(-self.fi, 0, 0, 1) glTranslatef(cameraDX, cameraDY, 0) glRotatef(self.fi, 0, 0, 1) self.modelview = (GLfloat * 16)() glGetFloatv(GL_MODELVIEW_MATRIX, self.modelview) self.position.x += self.modelview[12] self.position.y += self.modelview[13] self.position.z += self.modelview[14] glPopMatrix()
def get_spherical_rotatation(p1, p2, width, height, theta_multiplier): v1 = get_sphere_mapping(p1[0], p1[1], width, height) v2 = get_sphere_mapping(p2[0], p2[1], width, height) d = min(max([dot(v1, v2), -1]), 1) if abs(d - 1.0) < 0.000001: return None raxis = norm(cross(v1, v2)) rtheta = theta_multiplier * rad2deg * _acos(d) pgl.glPushMatrix() pgl.glLoadIdentity() pgl.glRotatef(rtheta, *raxis) mat = (c_float * 16)() pgl.glGetFloatv(pgl.GL_MODELVIEW_MATRIX, mat) pgl.glPopMatrix() return mat
def get_spherical_rotatation(p1, p2, width, height, theta_multiplier): v1 = get_sphere_mapping(p1[0], p1[1], width, height) v2 = get_sphere_mapping(p2[0], p2[1], width, height) d = min(max([dot(v1, v2), -1]), 1) if abs(d - 1.0) < 0.000001: return None raxis = norm( cross(v1, v2) ) rtheta = theta_multiplier * rad2deg * _acos(d) pgl.glPushMatrix() pgl.glLoadIdentity() pgl.glRotatef(rtheta, *raxis) mat = (c_float*16)() pgl.glGetFloatv(pgl.GL_MODELVIEW_MATRIX, mat) pgl.glPopMatrix() return mat
def set_state(self): gl.glPushAttrib( gl.GL_ENABLE_BIT ) gl.glBindTexture( gl.GL_TEXTURE_2D, self.texture.id ) gl.glEnable( gl.GL_POINT_SPRITE ) gl.glDisable( gl.GL_LIGHTING ) if self.attenuate: quadratic = (gl.GLfloat * 3)() quadratic[:] = (1.0, 0.0, 0.01) gl.glPointParameterfv( gl.GL_POINT_DISTANCE_ATTENUATION, quadratic ) max_size = gl.GLfloat() gl.glGetFloatv( gl.GL_POINT_SIZE_MAX, max_size ) gl.glPointParameterf( gl.GL_POINT_FADE_THRESHOLD_SIZE, 60.0 ) gl.glPointParameterf( gl.GL_POINT_SIZE_MIN, 1.0 ) gl.glPointParameterf( gl.GL_POINT_SIZE_MAX, max_size ) gl.glTexEnvf( gl.GL_POINT_SPRITE, gl.GL_COORD_REPLACE, gl.GL_TRUE ) gl.glPointSize( self.point_size )
def get_texcoords(self, normals, texcoords=None): if not texcoords: texcoords = [0.] * (len(normals) // 3) inverted_light = (gl.GLfloat * 16)() mv_matrix = (gl.GLfloat * 16)() new_light = (gl.GLfloat * 3)() gl.glGetFloatv(gl.GL_MODELVIEW_MATRIX, mv_matrix) math3d.c_invert_matrix4(inverted_light, mv_matrix) math3d.c_transform_vector3(new_light, self._light_dir, inverted_light) # TODO - compute and apply additional matrix inverse on object transformation new_light[0] -= inverted_light[12] new_light[1] -= inverted_light[13] new_light[2] -= inverted_light[14] # Need to check to see if we should explicitly free memory for this new_light = euclid.Vector3(*new_light[:]).normalized() for idx in xrange(0, len(normals), 3): normal = euclid.Vector3(*normals[idx:idx+3]) texcoord = new_light.dot(normal) texcoords[idx // 3] = texcoord return texcoords
def _display_movements(self, has_vbo): self.vertex_buffer.bind() if has_vbo: glVertexPointer(3, GL_FLOAT, 0, None) else: glVertexPointer(3, GL_FLOAT, 0, self.vertex_buffer.ptr) self.vertex_color_buffer.bind() if has_vbo: glColorPointer(4, GL_FLOAT, 0, None) else: glColorPointer(4, GL_FLOAT, 0, self.vertex_color_buffer.ptr) self.index_buffer.bind() start = 1 layer_selected = self.num_layers_to_draw <= self.max_layers if layer_selected: end_prev_layer = self.layer_stops[self.num_layers_to_draw - 1] else: end_prev_layer = 0 end = self.layer_stops[min(self.num_layers_to_draw, self.max_layers)] glDisableClientState(GL_COLOR_ARRAY) glColor4f(*self.color_printed) # Draw printed stuff until end or end_prev_layer cur_end = min(self.printed_until, end) if not self.only_current: if 1 <= end_prev_layer <= cur_end: self._draw_elements(1, end_prev_layer) elif cur_end >= 1: self._draw_elements(1, cur_end) glEnableClientState(GL_COLOR_ARRAY) # Draw nonprinted stuff until end_prev_layer start = max(cur_end, 1) if end_prev_layer >= start: if not self.only_current: self._draw_elements(start, end_prev_layer) cur_end = end_prev_layer # Draw current layer if layer_selected: glDisableClientState(GL_COLOR_ARRAY) # Backup & increase line width orig_linewidth = (GLfloat)() glGetFloatv(GL_LINE_WIDTH, orig_linewidth) glLineWidth(2.0) glColor4f(*self.color_current_printed) if cur_end > end_prev_layer: self._draw_elements(end_prev_layer + 1, cur_end) glColor4f(*self.color_current) if end > cur_end: self._draw_elements(cur_end + 1, end) # Restore line width glLineWidth(orig_linewidth) glEnableClientState(GL_COLOR_ARRAY) # Draw non printed stuff until end (if not ending at a given layer) start = max(self.printed_until, 1) if not layer_selected and end >= start: self._draw_elements(start, end) self.vertex_buffer.unbind() self.vertex_color_buffer.unbind()
def draw_objects(self): '''called in the middle of ondraw after the buffer has been cleared''' self.create_objects() glPushMatrix() glTranslatef(0, 0, -self.dist) glMultMatrixd(build_rotmatrix(self.basequat)) # Rotate according to trackball glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0.2, 0.2, 0.2, 1)) glTranslatef(- self.build_dimensions[3] - self.platform.width / 2, - self.build_dimensions[4] - self.platform.depth / 2, 0) # Move origin to bottom left of platform # Draw platform glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glDisable(GL_LIGHTING) self.platform.draw() glEnable(GL_LIGHTING) # Draw mouse glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) inter = self.mouse_to_plane(self.mousepos[0], self.mousepos[1], plane_normal = (0, 0, 1), plane_offset = 0, local_transform = False) if inter is not None: glPushMatrix() glTranslatef(inter[0], inter[1], inter[2]) glBegin(GL_TRIANGLES) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(1, 0, 0, 1)) glNormal3f(0, 0, 1) glVertex3f(2, 2, 0) glVertex3f(-2, 2, 0) glVertex3f(-2, -2, 0) glVertex3f(2, -2, 0) glVertex3f(2, 2, 0) glVertex3f(-2, -2, 0) glEnd() glPopMatrix() # Draw objects glDisable(GL_CULL_FACE) glPushMatrix() glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0.3, 0.7, 0.5, 1)) for i in self.parent.models: model = self.parent.models[i] glPushMatrix() glTranslatef(*(model.offsets)) glRotatef(model.rot, 0.0, 0.0, 1.0) glTranslatef(*(model.centeroffset)) glScalef(*model.scale) model.batch.draw() glPopMatrix() glPopMatrix() glEnable(GL_CULL_FACE) # Draw cutting plane if self.parent.cutting: # FIXME: make this a proper Actor axis = self.parent.cutting_axis fixed_dist = self.parent.cutting_dist dist, plane_width, plane_height = self.get_cutting_plane(axis, fixed_dist) if dist is not None: glPushMatrix() if axis == "x": glRotatef(90, 0, 1, 0) glRotatef(90, 0, 0, 1) glTranslatef(0, 0, dist) elif axis == "y": glRotatef(90, 1, 0, 0) glTranslatef(0, 0, -dist) elif axis == "z": glTranslatef(0, 0, dist) glDisable(GL_CULL_FACE) glBegin(GL_TRIANGLES) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0, 0.9, 0.15, 0.3)) glNormal3f(0, 0, self.parent.cutting_direction) glVertex3f(plane_width, plane_height, 0) glVertex3f(0, plane_height, 0) glVertex3f(0, 0, 0) glVertex3f(plane_width, 0, 0) glVertex3f(plane_width, plane_height, 0) glVertex3f(0, 0, 0) glEnd() glEnable(GL_CULL_FACE) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glEnable(GL_LINE_SMOOTH) orig_linewidth = (GLfloat)() glGetFloatv(GL_LINE_WIDTH, orig_linewidth) glLineWidth(4.0) glBegin(GL_LINE_LOOP) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0, 0.8, 0.15, 1)) glVertex3f(0, 0, 0) glVertex3f(0, plane_height, 0) glVertex3f(plane_width, plane_height, 0) glVertex3f(plane_width, 0, 0) glEnd() glLineWidth(orig_linewidth) glDisable(GL_LINE_SMOOTH) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) glPopMatrix() glPopMatrix()
def generate_map(self): scene_radius = 95.0 light_to_scene_distance = math.sqrt( light_pos[0]**2 + light_pos[1]**2 + light_pos[2]**2) near_plane = light_to_scene_distance - scene_radius field_of_view = math3d.radians_to_degrees(2.0 * math.atan(scene_radius / float(light_to_scene_distance))) tmp = (gl.GLfloat * 16)() gl.glMatrixMode( gl.GL_PROJECTION ) gl.glLoadIdentity() gl.gluPerspective( field_of_view, 1.0, near_plane, near_plane + (2.0 * scene_radius) ) gl.glGetFloatv( gl.GL_PROJECTION_MATRIX, tmp ) gl.light_projection = euclid.Matrix4() gl.light_projection[0:16] = tmp[:] # Switch to light's point of view gl.glMatrixMode( gl.GL_MODELVIEW ) gl.glLoadIdentity() gl.gluLookAt( self.light.pos.x, self.light.pos.y, self.light.pos.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ) gl.glGetFloatv( gl.GL_MODELVIEW_MATRIX, tmp ) light_mview = euclid.Matrix4() light_mview[0:16] = tmp[:] #glViewport(0, 0, shadow_width, shadow_height) # Clear the depth buffer only gl.glClear( gl.GL_DEPTH_BUFFER_BIT ) # Remember the current shade model gl.glPushAttrib( gl.GL_ENABLE_BIT ) prev_shade_model = cypes.GLint() gl.glGetIntegerv( gl.GL_SHADE_MODEL, prev_shade_model ) # All we care about here is resulting depth values gl.glShadeModel( gl.GL_FLAT ) gl.glDisable( gl.GL_LIGHTING ) gl.glDisable( gl.GL_COLOR_MATERIAL ) gl.glDisable( gl.GL_NORMALIZE ) gl.glColorMask( 0, 0, 0, 0 ) # Overcome imprecision gl.glEnable( gl.GL_POLYGON_OFFSET_FILL ) #draw_models(False) self.context.render( exclude=self.exclude_shadows ) # Copy depth values into depth texture gl.glCopyTexImage2D( gl.GL_TEXTURE_2D, 0, gl.GL_DEPTH_COMPONENT, 0, 0, shadow_width, shadow_height, 0 ) # Restore normal drawing state gl.glShadeModel( gl.GL_SMOOTH ) gl.glEnable( gl.GL_LIGHTING ) gl.glEnable( gl.GL_COLOR_MATERIAL ) gl.glEnable( gl.GL_NORMALIZE ) gl.glColorMask( 1, 1, 1, 1 ) gl.glDisable( gl.GL_POLYGON_OFFSET_FILL ) # Setup up the texture matrix which will be use in eye-linear # texture mapping self.tex_matrix = euclid.Matrix4() self.tex_matrix.translate(0.5, 0.5, 0.5).scale(0.5, 0.5, 0.5) tmp_matrix.scale(0.5, 0.5, 0.5) tex_matrix = (tmp_matrix * light_projection) * light_mview self.tex_matrix.transpose() # Give us immediate access to ctypes arrays self.tex_matrix = ( (gl.GLfloat * 4)(*self.tex_matrix[0:4]), (gl.GLfloat * 4)(*self.tex_matrix[4:8]), (gl.GLfloat * 4)(*self.tex_matrix[8:12]), (gl.GLfloat * 4)(*self.tex_matrix[12:16]) )
def _display_movements(self, mode_2d=False): self.vertex_buffer.bind() has_vbo = isinstance(self.vertex_buffer, VertexBufferObject) if has_vbo: glVertexPointer(3, GL_FLOAT, 0, None) else: glVertexPointer(3, GL_FLOAT, 0, self.vertex_buffer.ptr) self.vertex_color_buffer.bind() if has_vbo: glColorPointer(4, GL_FLOAT, 0, None) else: glColorPointer(4, GL_FLOAT, 0, self.vertex_color_buffer.ptr) start = 0 if self.num_layers_to_draw <= self.max_layers: end_prev_layer = self.layer_stops[self.num_layers_to_draw - 1] else: end_prev_layer = -1 end = self.layer_stops[min(self.num_layers_to_draw, self.max_layers)] glDisableClientState(GL_COLOR_ARRAY) glColor4f(*self.color_printed) # Draw printed stuff until end or end_prev_layer cur_end = min(self.printed_until, end) if not self.only_current: if 0 <= end_prev_layer <= cur_end: glDrawArrays(GL_LINES, start, end_prev_layer) elif cur_end >= 0: glDrawArrays(GL_LINES, start, cur_end) glEnableClientState(GL_COLOR_ARRAY) # Draw nonprinted stuff until end_prev_layer start = max(cur_end, 0) if end_prev_layer >= start: if not self.only_current: glDrawArrays(GL_LINES, start, end_prev_layer - start) cur_end = end_prev_layer # Draw current layer if end_prev_layer >= 0: glDisableClientState(GL_COLOR_ARRAY) # Backup & increase line width orig_linewidth = (GLfloat)() glGetFloatv(GL_LINE_WIDTH, orig_linewidth) glLineWidth(2.0) glColor4f(*self.color_current_printed) if cur_end > end_prev_layer: glDrawArrays(GL_LINES, end_prev_layer, cur_end - end_prev_layer) glColor4f(*self.color_current) if end > cur_end: glDrawArrays(GL_LINES, cur_end, end - cur_end) # Restore line width glLineWidth(orig_linewidth) glEnableClientState(GL_COLOR_ARRAY) # Draw non printed stuff until end (if not ending at a given layer) start = max(self.printed_until, 0) end = end - start if end_prev_layer < 0 and end > 0 and not self.only_current: glDrawArrays(GL_LINES, start, end) self.vertex_buffer.unbind() self.vertex_color_buffer.unbind()
def get_float(self, enum) -> float: """Get a float limit""" value = c_float() gl.glGetFloatv(enum, value) return value.value