void Renderer::updateLookPos() { ALLEGRO_TRANSFORM look; al_copy_transform(&look, &camera_transform_); for(int i = 0; i < 4; i++) { al_translate_transform_3d(&look, 0.0, 0.0, 1.0); Vector3D pos; unProject(&look, pos); int x = (int)floor(pos.x+0.5), z = (int)floor(pos.z+0.5); int cx = x / 16, cz = z / 16; //int bx = x % 16, bz = z % 16; // getChunk will cause regions and chunks to be loaded, but they should already be loaded by now... auto it = chunkData_.find(getChunkKey(cx, cz)); if(it != chunkData_.end() && it->second) { //RendererChunk *rc = it->second; // DO STUFF } } }
vec3 camera_t::screen_to_world_coord(vec2 const& screen_coord) const { vec4 vp; vp.xy = viewport.bottom_left; vp.zw = viewport.size_d2 * 2.0f; return unProject(vec3(screen_coord, 0.0f), view_matrix(), projection_ortho(), vp).xyz; }
bool ComputeWaterSimulation::handlePointerInput(NvInputDeviceType::Enum device, NvPointerActionType::Enum action, uint32_t modifiers, int32_t count, NvPointerEvent* points) { if (mTouchDown || !NvSampleApp::handlePointerInput(device, action, modifiers, count, points)) { if (action == NvPointerActionType::UP) { mTouchDown = false; } else if (action == NvPointerActionType::DOWN) { // check if we hit any water surface nv::vec3f point = unProject(points[0].m_x, points[0].m_y); nv::vec2f gridPos; // project onto each wave for(uint32_t i = 0; i < mNumWaves; i++) { if (mWaves[i]->mapPointXZToGridPos(point, gridPos)) { mDisturbance = nv::vec4f(point.x, point.z, mSettings.Size, mSettings.Strength); mTouchDown = true; } } } else if (action == NvPointerActionType::MOTION && mTouchDown) { nv::vec3f point = unProject(points[0].m_x, points[0].m_y); nv::vec2f gridPos; // project onto each wave for(uint32_t i = 0; i < mNumWaves; i++) { if (mWaves[i]->mapPointXZToGridPos(point, gridPos)) { mDisturbance = nv::vec4f(point.x, point.z, mSettings.Size, mSettings.Strength); } } } } return mTouchDown; }
void PointProjector::unProject(HomogeneousPoint3fVector &points, Eigen::MatrixXi &indexImage, const Eigen::MatrixXf &depthImage) const { points.resize(depthImage.rows()*depthImage.cols()); int count = 0; indexImage.resize(depthImage.rows(), depthImage.cols()); HomogeneousPoint3f* point = &points[0]; int cpix=0; for (int c=0; c<depthImage.cols(); c++){ const float* f = &depthImage(0,c); int* i =&indexImage(0,c); for (int r=0; r<depthImage.rows(); r++, f++, i++){ if (!unProject(*point, r,c,*f)){ *i=-1; continue; } point++; cpix++; *i=count; count++; } } points.resize(count); }
Eigen::Vector3d Camera::unProject(const QPoint& p) const { return unProject(p, parent()->center()); }
Eigen::Vector3d Camera::unProject(const QPoint& p, const Eigen::Vector3d& ref) const { return unProject( Eigen::Vector3d( p.x(), p.y(), project(ref).z() )); }
void Renderer::getWorldPos(Vector3D &pos) { unProject(&camera_transform_, pos); }
Vector3f Camera::unProject(const Vector2f &point, const Vector3f &reference) const { return unProject(Vector3f(point.x(), point.y(), project(reference).z())); }
void ComputeWaterSimulation::runWaterSimulation() { // update surface params if (mGridSize != mPrevGridSize || mNumWaves != mPrevNumWaves) { initWaves(mNumWaves); mPrevNumWaves = mNumWaves; mPrevGridSize = mGridSize; } // add rain if (mSettings.AddRain) { if (mSettings.Frequency != 0 && mDisturbance.w <= 0.0f) { if (++mRainFrame >= mSettings.Frequency) { mRainFrame = 0; mDisturbance = nv::vec4f(Rand(-2.0f, 2.0f), Rand(-2.0f, 2.0f), mSettings.Size, Rand(-mSettings.Strength, mSettings.Strength)); } } } // project ray using joystick if (mJoystickDown) { // use screen center nv::vec3f point = unProject((float)m_width / 2, (float)m_height / 2); nv::vec2f gridPos; // project onto each wave for(uint32_t i = 0; i < mNumWaves; i++) { if (mWaves[i]->mapPointXZToGridPos(point, gridPos)) { mDisturbance = nv::vec4f(point.x, point.z, mSettings.Size, mSettings.Strength); } } } // sync CPU and GPU buffers if settings changed if (mSettings.UseComputeShader != mPrevFrameUseComputeShader) { if (mSettings.UseComputeShader) { for(uint32_t i = 0; i < mNumWaves; i++) { void* ptr; // sync height data glBindBuffer(GL_SHADER_STORAGE_BUFFER, mHeightBuffer[i]); ptr = glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, mWaves[i]->getSimulation().m_heightFieldSize, GL_MAP_WRITE_BIT); memcpy(ptr, mWaves[i]->getSimulation().getHeightField(), mWaves[i]->getSimulation().m_heightFieldSize); glUnmapBuffer(GL_SHADER_STORAGE_BUFFER); glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); // sync velocity data glBindBuffer(GL_SHADER_STORAGE_BUFFER, mVelocityBuffer[i]); ptr = glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, mWaves[i]->getSimulation().m_heightFieldSize, GL_MAP_WRITE_BIT); memcpy(ptr, mWaves[i]->getSimulation().getVelocity(), mWaves[i]->getSimulation().m_heightFieldSize); glUnmapBuffer(GL_SHADER_STORAGE_BUFFER); glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); } } else { for(uint32_t i = 0; i < mNumWaves; i++) { void* ptr; // sync height data glBindBuffer(GL_SHADER_STORAGE_BUFFER, mHeightBuffer[i]); ptr = glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, mWaves[i]->getSimulation().m_heightFieldSize, GL_MAP_READ_BIT); memcpy(mWaves[i]->getSimulation().getHeightField(), ptr, mWaves[i]->getSimulation().m_heightFieldSize); glUnmapBuffer(GL_SHADER_STORAGE_BUFFER); glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); // sync velocity data glBindBuffer(GL_SHADER_STORAGE_BUFFER, mVelocityBuffer[i]); ptr = glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, mWaves[i]->getSimulation().m_heightFieldSize, GL_MAP_READ_BIT); memcpy(mWaves[i]->getSimulation().getVelocity(), ptr, mWaves[i]->getSimulation().m_heightFieldSize); glUnmapBuffer(GL_SHADER_STORAGE_BUFFER); glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); } //HACK: simulate once even if animation disabled bool mCurAnimate = mSettings.Animate; mSettings.Animate = true; simulateWaterCPU(); mSettings.Animate = mCurAnimate; } mPrevFrameUseComputeShader = mSettings.UseComputeShader; } if (mSettings.UseComputeShader) { simulateWaterGPU(); } else { simulateWaterCPU(); } // reset disturbance mDisturbance = nv::vec4f(0.0f); }