def _update_key(self, key): is_dirty = False MOVE_SPD = 0.02 if key == glfw.KEY_A: self.move.x += MOVE_SPD is_dirty = True elif key == glfw.KEY_W: self.move.y += MOVE_SPD is_dirty = True elif key == glfw.KEY_D: self.move.x -= MOVE_SPD is_dirty = True elif key == glfw.KEY_S: self.move.y -= MOVE_SPD is_dirty = True MOVEMENT_CONSTRAINT = 0.8 self.move = glm.clamp( self.move, glm.vec2(-MOVEMENT_CONSTRAINT), glm.vec2(MOVEMENT_CONSTRAINT) ) if is_dirty: self.renderer.update_input_move((self.move.x, self.move.y))
def __init__(self): Entity.__init__(self, position=glm.vec2(0, 0), color=glm.vec3(1.0, 0.0, 1.0), rectangles=[Rectangle(glm.vec2(0, 0), 10, 10)]) self.last_pos = glm.vec2()
def get_height(x, z, terrain): unit_size = (2 * terrain.size) / terrain.sub_div dz = x - (terrain.position.x - terrain.size) - unit_size / 2 dx = z - (terrain.position.z - terrain.size) - unit_size / 2 gx = math.floor(dx / (unit_size)) gz = math.floor(dz / (unit_size)) if gx < 0 or gz < 0 or gx > len(terrain.heights) - 2 or gz > len( terrain.heights) - 2: return terrain.position.y else: sx = (dx % unit_size) / unit_size sz = (dz % unit_size) / unit_size if sx <= 1 - sz: height = barycentric(glm.vec3(0, terrain.heights[gx][gz], 0), glm.vec3(0, terrain.heights[gx][gz + 1], 1), glm.vec3(1, terrain.heights[gx + 1][gz], 0), glm.vec2(sx, sz)) else: height = barycentric( glm.vec3(1, terrain.heights[gx + 1][gz], 0), glm.vec3(0, terrain.heights[gx][gz + 1], 1), glm.vec3(1, terrain.heights[gx + 1][gz + 1], 1), glm.vec2(sx, sz)) return height + terrain.position.y
def __init__(self): self.device = None self.queue = None self.rasterizationSamples = vk.VK_SAMPLE_COUNT_1_BIT self.subpass = 0 self.vertexBuffer = vks.vulkanbuffer.Buffer() self.indexBuffer = vks.vulkanbuffer.Buffer() self.vertexCount = 0 self.indexCount = 0 self.shaders = [] self.descriptorPool = None self.descriptorSetLayout = None self.descriptorSet = None self.pipelineLayout = None self.pipeline = None self.fontMemory = vk.VK_NULL_HANDLE self.fontImage = vk.VK_NULL_HANDLE self.fontView = vk.VK_NULL_HANDLE self.sampler = None self.pushConstBlock = {'scale': glm.vec2(), 'translate': glm.vec2()} self.pushConstBlockArray = np.array(glm.vec4(glm.vec2(), glm.vec2())) self.visible = True self.updated = False self.scale = 1.0 imgui.create_context() #self.style = imgui.get_style() # don't know yet how to change self.style.color(imgui.COLOR_*) io = imgui.get_io() io.font_global_scale = self.scale
def world_to_screen(self, world_pos: vec3) -> Union[vec2, None]: """ Convert a world 3D position to a screen 2D position. Returns None if the position is not in the screen """ rel = world_pos - self.position # distance along the screen's z axis dist = dot(rel, self.direction) if dist < 10: return None absolute_y = dot(rel, self.up) absolute_x = dot(rel, self.horizontal) screen_size = vec2(self.screen_size) pos = (vec2( absolute_x / dist * self.screen_dist, absolute_y / dist * self.screen_dist, ) + screen_size / 2) pos.y = self.screen_size.y - pos.y return pos
def computeTangentBasis(self, vertex, uv, normal): tangents = [] bitangents = [] for idx in range(0, len(vertex) / 9): offset = idx * 9 v0 = vertex[offset:offset + 3] v1 = vertex[offset + 3:offset + 6] v2 = vertex[offset + 6:offset + 9] offset = idx * 6 uv0 = uv[offset:offset + 2] uv1 = uv[offset + 2:offset + 4] uv2 = uv[offset + 4:offset + 6] #print v0,v1,v2 deltaPos1 = glm.vec3([v1[0] - v0[0], v1[1] - v0[1], v1[2] - v0[2]]) deltaPos2 = glm.vec3([v2[0] - v0[0], v2[1] - v0[1], v2[2] - v0[2]]) deltaUV1 = glm.vec2([uv1[0] - uv0[0], uv1[1] - uv0[1]]) deltaUV2 = glm.vec2([uv2[0] - uv0[0], uv2[1] - uv0[1]]) r = 1.0 / (deltaUV1.x * deltaUV2.y - deltaUV1.y * deltaUV2.x) tangent = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y) * r bitangent = (deltaPos2 * deltaUV1.x - deltaPos1 * deltaUV2.x) * r tangents.extend([tangent.x, tangent.y, tangent.z]) tangents.extend([tangent.x, tangent.y, tangent.z]) tangents.extend([tangent.x, tangent.y, tangent.z]) bitangents.extend([bitangent.x, bitangent.y, bitangent.z]) bitangents.extend([bitangent.x, bitangent.y, bitangent.z]) bitangents.extend([bitangent.x, bitangent.y, bitangent.z]) return tangents, bitangents
def angleBetween(self, a3, b3): a = glm.vec2(a3) b = glm.vec2(b3) p = glm.vec2(-b[1], b[0]) b_coord = glm.dot(a, b) p_coord = glm.dot(a, p) return math.atan2(p_coord, b_coord)
def move(self): linearVelocity = glm.vec2(0.0, 0.0) if self.followPath == True: mapDestinationPosition = self.pathFollower.getWaypoint() if mapDestinationPosition == None: return glm.vec2(0.0, 0.0) worldDestinationPosition = self.bot.map.getWorldPosition( mapDestinationPosition) vector = glm.vec2( worldDestinationPosition[0] - self.bot.transform.position[0], worldDestinationPosition[1] - self.bot.transform.position[1]) if glm.length(vector) <= self.minSeekDistance: self.pathFollower.increaseWaypoint() mapDestinationPosition = self.pathFollower.getWaypoint() if mapDestinationPosition == None: return glm.vec2(0.0, 0.0) worldDestinationPosition = self.bot.map.getWorldPosition( mapDestinationPosition) linearVelocity = self.seekPosition(self.bot.transform.position, worldDestinationPosition) else: if self.worldDestinationDirection == None: return glm.vec2(0.0, 0.0) linearVelocity = self.seekDirection(self.worldDestinationDirection) return linearVelocity
def __init__(self, basePos: glm.vec2, duration: float, maxCount: int): self.basePos = basePos self.baseRot = 0.0 self.duration = duration self.velocity = glm.vec2(0.0) self.rotationVelocity = 0.0 self._sizeBegin = glm.vec2(0.0) self._sizeEnd = glm.vec2(0.0) self.sizeChange = False self._colorBegin = glm.vec4(1.0) self._colorEnd = glm.vec4(1.0) self.colorChange = False self.randomizeMovement = False self.fadeOut = False self.__texture = None self.maxCount = max(maxCount, ParticleSystemComponent.MaxCount) self.particlePool = [] for _ in range(self.maxCount): self.particlePool.append(Particle()) self.activeParticleIdx = self.maxCount - 1
def __init__(self): program = pxng.ShaderProgram('FractalShader') program.add_shader(resource('fractal.vert'), pxng.ShaderType.Vertex) program.add_shader(resource('fractal.frag'), pxng.ShaderType.Fragment) program.compile_and_link() program.add_uniform('projection_view', glm.mat4x4) program.add_uniform('model', glm.mat4x4) program.add_uniform('iterations', glm.ivec1) program.add_uniform('fractal_space', glm.dvec4) self._program = program self._vao = pxng.VertexArrayObject(GL_TRIANGLES) self._vao.attach_buffer( pxng.BufferObject(data_type=glm.vec3)) # vertex buffer self._vao.attach_buffer( pxng.BufferObject(data_type=glm.vec2)) # texture buffer self._vao.add_quad( glm.vec3(0, 0, 0), glm.vec3(0, 1, 0), glm.vec3(1, 1, 0), glm.vec3(1, 0, 0), ) self._vao.set_texture(glm.vec2(0, 1), glm.vec2(0, 0), glm.vec2(1, 0), glm.vec2(1, 1))
def __init__(self): self.pos = glm.vec2(random.uniform(-2.0 * math.pi, 2.0 * math.pi), random.uniform(-2.0 * math.pi, 2.0 * math.pi)) self.vel = glm.vec2(random.uniform(-MAX_SPEED, MAX_SPEED), random.uniform(-MAX_SPEED, MAX_SPEED)) self.col = genRandomStrongColour() self.age = 0.0 self.eol = random.uniform(1, 3) # end of life
def write(fnt, sttr, p = None, mw = None): if p is None: p = glm.vec2(0) else: p = glm.vec2(p) s = glm.vec2(p) _wd.use(_font_program) _font_program.uniform('matrix', _matrix) _font_program.uniform('color', _color) _wd.use(_font_vao) _program.font('tex') lc = None for c in sttr: if lc: a = fnt.advance(lc, c) else: a = 0 if c == '\n' or mw and p.x + a + fnt.advance(c) > mw: p.x = s.x p.y -= fnt.height() lc = None continue g = fnt.glyph(c) p.x += a _font_vbo.data(struct.pack('ffffffffffffffff', g.vertices.x1 + p.x, g.vertices.y1 + p.y, g.texcoords.x1, g.texcoords.y1, g.vertices.x1 + p.x, g.vertices.y2 + p.y, g.texcoords.x1, g.texcoords.y2, g.vertices.x2 + p.x, g.vertices.y2 + p.y, g.texcoords.x2, g.texcoords.y2, g.vertices.x2 + p.x, g.vertices.y1 + p.y, g.texcoords.x2, g.texcoords.y1, ), 0) _wd.draw('triangle fan', 4) lc = c _wd.use(_vao) _wd.use(_program)
def create_h(position, size=100, thickness=10): side_offset = size / 2.0 - thickness / 2.0 return [ Rectangle(position * 1, size, thickness), Rectangle(glm.vec2(position.x + side_offset, position.y), thickness, size), Rectangle(glm.vec2(position.x - side_offset, position.y), thickness, size) ]
def __init__(self, engine, pos, size, down_tex, normal_tex, over_tex): Rectangle.__init__(self) self.down_tex = engine.graphics.get_texture(down_tex, False) self.normal_tex = engine.graphics.get_texture(normal_tex, False) self.over_tex = engine.graphics.get_texture(over_tex, False) self.pos = glm.vec2(*pos) self.size = glm.vec2(*size) self.texture = self.normal_tex
def __init__(self, pos, size, angle=0): self.radius = 0 self.pos = glm.vec3(*pos) self.half_size = glm.vec3(*size) * .5 rads = glm.radians(angle) cos = math.cos(rads) sin = math.sin(rads) self.forward = glm.vec2(cos, -sin) self.right = glm.vec2(sin, cos)
def mouse_button_callback(self, window, button, action, mods): if button != GLFW_MOUSE_BUTTON_LEFT: return if action == GLFW_PRESS: self.drag = True xpos, ypos = glfwGetCursorPos(self.glfw_window) self.drag_start = glm.vec2(xpos, ypos) elif action == GLFW_RELEASE: self.drag = False self.rotate_vec = self.drag_vector self.drag_vec = glm.vec2(0)
def __init__(self, textsheet, textsheetPath, xmlPath): self.textSheet = textsheet self.textSheetPath = textsheetPath self.XML = xmlPath self.Text = "" self.position = glm.vec2(0, 0) self.size = glm.vec2(0, 0) self.rotation = float(0) self.color = glm.vec3(1.0, 1.0, 1.0) self.Grid = glm.vec2(12, 14) Resources.LoadTexture(textsheetPath, 1, textsheet)
def __init__(self, engine, tex_name, color, i): Rectangle.__init__(self) self.engine = engine self.pos = glm.vec2(-1 + self.spacing, 1 - ((self.height + self.spacing) * (i + 1))) self.size = glm.vec2(self.width, self.height * .5) self.size.x = 0 self.target_value = 1 self.color = color self.tex = engine.graphics.get_texture(tex_name)
def seekPosition(self, worldOriginPosition, worldDestinationPosition): # positions vector = glm.vec2(worldDestinationPosition[0] - worldOriginPosition[0], worldDestinationPosition[1] - worldOriginPosition[1]) if glm.length(vector) <= self.minSeekDistance: return glm.vec2(0.0, 0.0) direction = vector if glm.length(vector) > 0.0: direction = glm.normalize(vector) acceleration = direction * self.maxLinearVelocity return acceleration
def __init__(self): super().__init__() self.mouseClipped = False self.mouseMovedThisFrame = False self.f_cut = 4.0 self.xfilter = Filter(self.f_cut) self.yfilter = Filter(self.f_cut) self.lookVelocity = glm.vec2(0.0, 0.0) self.lookAcceleration = glm.vec2(0.0, 0.0) self.relativeMouseMotion = glm.vec2(0.0, 0.0) self.brake = 0.85
def __init__(self): self.position = glm.vec2(0.0) self.velocity = glm.vec2(0.0) self.size = glm.vec2(0.0) self.rotation = 0.0 self.rotationVelocity = 0.0 self.transform = glm.mat4(1.0) self.life = 0.0 self.isAlive = False self.color = glm.vec4(1.0) self.texture = None
def __generate_arrowhead_method1(self, scale) -> []: arrow_points = [] z = vec3(0, 0, 1) rads = glm.radians(270) - self.__calc_angle() for p in self.__arrow_head(): p = vec4(p.x, p.y, 0, 0) p = p*scale M = glm.rotate(mat4(1), rads, z) p = M*p p = vec2(p.x, p.y) p = p+vec2(self.end.x, self.end.y) arrow_points.append(p) return arrow_points
def vertex(self, i, v, t, n, uv=None): self.vertices[i] = v if (self.texcoords is None): self.texcoords = [] self.texcoords[i] = t if t is not None else glm.vec2(0.0) if (self.normals is None): self.normals = [] self.normals[i] = n if n is not None else glm.vec3(0.0) if (self.globalUVs is None): self.globalUVs = [] self.globalUVs[i] = uv if uv is not None else glm.vec2(0.0)
def JobsWithSpiralRenderPattern(self, scene, horizontal_frame_step, vertical_frame_step): # generate worker jobs frame_step = glm.vec2(horizontal_frame_step, vertical_frame_step) origin = glm.vec2(self.width / 2, self.height / 2) - frame_step for (x, y) in spiral(int(self.width / horizontal_frame_step) + 1, int(self.height / vertical_frame_step) + 1): from_point = glm.vec2(x, y) * frame_step + origin to_point = from_point + frame_step from_point.x = glm.clamp(from_point.x, 0, self.width) from_point.y = glm.clamp(from_point.y, 0, self.height) to_point.x = glm.clamp(to_point.x, 0, self.width) to_point.y = glm.clamp(to_point.y, 0, self.height) render_job = RenderJob(self.frame, scene, int(from_point.x), int(to_point.x), int(from_point.y), int(to_point.y)) self.render_queue.put(render_job)
def direction(self) -> glm.vec2(): if not self.has_physics: a = self._rotation / 180. * math.pi return glm.vec2(-math.sin(a), math.cos(a)) else: pos, quat = pybullet.getBasePositionAndOrientation( bodyUniqueId=self._body_id, physicsClientId=self._physics_client_id, ) mat3 = pybullet.getMatrixFromQuaternion(quat) #print( # [round(v, 2) for v in quat], # [round(v, 2) for v in mat3], #) return glm.vec2(mat3[2], mat3[5])#, mat3[8])
def __init__(self, start, end, y=0, height=16): self.y = y self.height = height self.radius = .4 self.start = glm.vec2(*start) end = glm.vec2(*end) self.center = (self.start + end) * .5 diff = end - self.start self.half_length = glm.length(diff) * .5 self.direction = glm.normalize(diff) self.normal = glm.normalize(glm.vec2(-diff.y, diff.x))
def make_env_with_best_settings_for_front_back(envName): env = make_env_with_best_settings_for_analytical2(envName) envOrg=env.env.env.env envOrg.extraRewardType = -1 envOrg.targetPosReward = True #True envOrg.targetPosRewardEarlyTerminateByDistTime = 10.0 # if no cur target reached in this time envOrg.targetDistRewardMultiplier = 10.0 envOrg.targetPosRewardDesiredSpeed = 0.15 envOrg.progressMultiplier = 1.0 envOrg.progressMinClip = -1.5 envOrg.progressMaxClip = 1.5 envOrg.progressDirChassisMultiplier = 5.0 envOrg.analyticReward = False #True envOrg.analyticRewardType = 0 envOrg.simulatedRewardMultiplier = 0.3 envOrg.advancedLevel = True #False #True envOrg.ground_pos_random_x = 2.0 envOrg.ground_pos_random_y = 2.0 distLen = 1.0 envOrg.tasks = [ [ #front back [glm.vec2(distLen,0.0), glm.vec2(1.0,0.0), 1.0], [glm.vec2(0.0,0.0), glm.vec2(1.0,0.0), 1.0], ], [ #back front [glm.vec2(-distLen,0.0), glm.vec2(1.0,0.0), 1.0], [glm.vec2(0.0,0.0), glm.vec2(1.0,0.0), 1.0], ], ] ''' [ # left right [glm.vec2(0,distLen), glm.vec2(1.0,0.0), 1.0], [glm.vec2(0.0,0.0), glm.vec2(1.0,0.0), 1.0], ], [ # rot left right [glm.vec2(0.0,0.0), glm.vec2(0.0,1.0), 1.0], [glm.vec2(0.0,0.0), glm.vec2(1.0,0.0), 1.0], ], ''' return env
def cylinderSDF(p, h, r): inOutRadius = glm.length(p.xy) - r inOutHeight = glm.abs(p.z) - h / 2.0 insideDistance = glm.min(glm.max(inOutRadius, inOutHeight), 0.0) outsideDistance = glm.length( glm.max(glm.vec2(inOutRadius, inOutHeight), 0.0)) return insideDistance + outsideDistance
def draw(p = None, s = None, r = None, o = None): if r is None: r = 0 if p is None: p = glm.vec2(0) if s is None: s = glm.vec2(1) if o is None: o = glm.vec2(0) global _texcoordsdirty _program.uniform('matrix', _matrix.translate(glm.vec3(p, 0)).rotate(r, glm.vec3(0, 0, 1)).scale(glm.vec3(s, 1)).translate(glm.vec3(-o, 0)).scale(glm.vec3(_img.size, 1))) q = p + glm.vec2(_img.size) * s if _texcoordsdirty: _vbo.data(_itc, s_f6_4) _texcoordsdirty = False _vbo.data(_itc, 0) _wd.draw('triangle fan', 4) _program.uniform('matrix', _matrix)
def V(*args): lenargs = len(args) if lenargs == 2: return glm.vec2(*args) elif lenargs == 4: return glm.vec4(*args) return glm.vec3(*args)
def integrate(self): inter_rec = ir.IntersectionRecord() for y in range(self.buffer.v_resolution): # In order to watch the progress of the rendering, a percentage will be displayed # as the image is created progress = "Progress: {:0.1f}%".format( 100.0 * y / (self.buffer.v_resolution - 1)) sys.stdout.write(progress) backspace(len(progress)) sys.stdout.flush() # Now rendering multiple samples per pixel (Antialiasing) for x in range(self.buffer.h_resolution): antialiasing = glm.vec3(.0, .0, .0) inter_rec.color = glm.vec3(.0, .0, .0) for sample in range(self.samples): inter_rec.t = sys.float_info.max my_ray = self.camera.getWorldSpaceRay( glm.vec2(x + random(), y + random())) if self.scene.intersect(my_ray, inter_rec): inter_rec.color = glm.vec3( inter_rec.color.x * (1 / (inter_rec.t * 0.5)), inter_rec.color.y * (1 / (inter_rec.t * 0.5)), inter_rec.color.z * (1 / (inter_rec.t * 0.5))) antialiasing = glm.vec3( antialiasing.x + inter_rec.color.x, antialiasing.y + inter_rec.color.y, antialiasing.z + inter_rec.color.z) self.buffer.buffer_data[x][y] = glm.vec3( antialiasing.x / self.samples, antialiasing.y / self.samples, antialiasing.z / self.samples) print("\nDONE!", file=sys.stderr)
def DrawString(self, system, string, position=glm.vec2(0.0, 0.0), size=glm.vec2(24, 24), color=glm.vec3(1.0, 1.0, 1.0)): count = 0 for char in string: xStr, yStr = self.GetCoords(char) selected = glm.vec2(xStr, yStr) newPos = position.x newPos = newPos + (size.x * count) self.DrawChar(system, glm.vec2(newPos, position.y), size, self.rotation, color, self.Grid, selected) count = count + 1
def draw(self, v1, v2): basic.image(self.image) vv1 = glm.ivec2(v1 / self.size // self.chunkSize) vv2 = glm.ivec2((v2 + self.chunkSize / 2) / self.size // self.chunkSize) + 1 w = self.image.size / self.size s = glm.vec2(1) / w rcs = range(self.chunkSize) lt = None for y in range(vv1.y, vv2.y): for x in range(vv1.x, vv2.x): c = glm.ivec2(x, y) if c in self.chunks: cc = self.chunks[c] ccc = c * self.size * self.chunkSize for yy in rcs: ccyy = cc[yy] for xx in rcs: t = ccyy[xx] if t: p1 = ccc + glm.ivec2(xx, yy) * self.size if(p1.x + self.size < v1.x or p1.y + self.size < v1.y or p1.x > v2.x or p1.y > v2.y): continue t -= 1 if lt != t: t1 = glm.vec2(t % w.x, t // w.x) / w basic.texcoord(t1, t1 + s) lt = t basic.quad(p1, p1 + self.size)
def texcoord(p1 = None, p2 = None, p3 = None, p4 = None): global _texcoordsdirty if p1 is None: p1 = glm.vec2(0) p2 = glm.vec2(0, 1) p3 = glm.vec2(1) p4 = glm.vec2(1, 0) elif p2 is None: p2 = p3 = p4 = p1 elif p3 is None: p3 = p2 p4 = glm.vec2(p3.x, p1.y) p2 = glm.vec2(p1.x, p3.y) else: raise Exception('Texcoord accepts zero, one, two, or four arguments.') _texcoordsdirty = True _vbo.data(bytes(p1) + bytes(p2) + bytes(p3) + bytes(p4), s_f6_4)
def _ttype_to_imgoffs(self, t): w = self.image.size / self.size return glm.vec2(t % w.x, t // w.x) / w
def get_mouse_position(self): return glm.vec2(*glfw.get_cursor_pos(self._wnd))
def _cursor_pos_callback(self, _, xpos, ypos): self.on_mouse_move(events.MouseMoveEvent(glm.vec2(xpos, ypos)))