float __floatunsisf(unsigned int i) { float_t res; res.data = uint_to_float(i); return res.val; }
bool viewport::try_unproject(vec3& world_position, const mat44& view_projection_transform, const point& screen_position) const { if (_width == 0 || _height == 0) { return false; } vec4 in; in._x = ((int_to_float(screen_position._x - uint_to_int(_x)) / uint_to_float(_width)) * 2.f) - 1.f; in._y = ((int_to_float(screen_position._y - uint_to_int(_y)) / uint_to_float(_height)) * 2.f) - 1.f; in._y *= -1.f; in._z = 0.f; //0 = near plane, 1 = far plane in._w = 1.f; vec4 out = in * make_mat44_inverted(view_projection_transform); if (is_approx(out._w, 0.f, 0.0001f)) { return false; } world_position._x = out._x / out._w; world_position._y = out._y / out._w; world_position._z = out._z / out._w; return true; }
void d3d9_prim2d_lines::render_segments(const vec2* points, unsigned int point_count, const color& color) { ASSERT(_is_rendering); if (point_count > 1) { static_assert(sizeof(solar::vec2) == sizeof(D3DXVECTOR2), "solar::vec2 cannot be casted to D3DXVECTOR2"); auto d3dx_points = reinterpret_cast<const D3DXVECTOR2*>(points); if (_viewport.X != 0 || _viewport.Y != 0) { //NOTE: ID3DXLine will internally transform all the points by the viewport's top left position. This causes problems because //all clients expect the points to be in screen space, not in the current viewport space. D3DMATRIX old_projection; D3D9_VERIFY(_context.get_device()->GetTransform(D3DTS_PROJECTION, &old_projection)); D3DXMATRIX new_projection; ::D3DXMatrixTranslation(&new_projection, -uint_to_float(_viewport.X), -uint_to_float(_viewport.Y), 0.f); new_projection *= old_projection; D3D9_VERIFY(_context.get_device()->SetTransform(D3DTS_PROJECTION, &new_projection)); D3D9_VERIFY(_d3dx_line->Draw(d3dx_points, point_count, color.to_argb32())); D3D9_VERIFY(_context.get_device()->SetTransform(D3DTS_PROJECTION, &old_projection)); } else { D3D9_VERIFY(_d3dx_line->Draw(d3dx_points, point_count, color.to_argb32())); } } }
float viewport::get_aspect_ratio() const { if (_height > 0) { return uint_to_float(_width) / uint_to_float(_height); } return 0.f; }