void BasicSimulator::updateProjectionData(const float positionScale, ProjectionPtr leftProj, ProjectionPtr rightProj) { updateInternalData(positionScale); gmtl::Matrix44f camera_pos = getCameraPos(); gmtl::Vec3f camera_trans = gmtl::makeTrans<gmtl::Vec3f>(camera_pos); //mCameraProj->calcViewMatrix(camera_pos); // -- Calculate camera (eye) Positions -- // vprDEBUG(vprDBG_ALL, vprDBG_HEX_LVL) << "[vrj::BasicSimulator::updateProjectionData()] Getting cam position" << std::endl << vprDEBUG_FLUSH; vprDEBUG(vprDBG_ALL, vprDBG_HEX_LVL) << "CamPos:" << camera_trans << std::endl << vprDEBUG_FLUSH; // Compute location of left and right eyes float interocular_dist = mSimViewport->getUser()->getInterocularDistance(); interocular_dist *= positionScale; // Scale into correct units float eye_offset = interocular_dist / 2.0f; // Distance to move eye // NOTE: Eye coord system is -z forward, x-right, y-up const gmtl::Point3f left_eye_pos( camera_pos * gmtl::Point3f(-eye_offset, 0.0f, 0.0f) ); const gmtl::Point3f right_eye_pos( camera_pos * gmtl::Point3f(eye_offset, 0.0f, 0.0f) ); leftProj->calcViewMatrix(left_eye_pos, positionScale); rightProj->calcViewMatrix(right_eye_pos, positionScale); }
void Technique::updateShaderParameters(const RenderContext& context) { m_viewportResolution->setValue(context.viewportResolution); ProjectionPtr projection = context.camera->getProjection(); m_farClipDistance->setValue(projection->getFarClipDistance()); m_maxClipSpaceDepth->setValue(projection->getMaxClipSpaceDepth()); glm::mat4 viewProj = context.camera->getViewProjectionMatrix(); m_viewProjectionMatrix->setValue(viewProj); m_modelViewProjectionMatrix->setValue(viewProj * context.sceneNode->getTransform()); glm::mat4 invModelMatrix = glm::inverse(context.sceneNode->getTransform()); m_invModelMatrix->setValue(invModelMatrix); { // camera glm::vec4 v = invModelMatrix * glm::vec4(context.camera->getPosition(), 1); glm::vec3 pos(v.x, v.y, v.z); m_cameraPosition_modelSpace->setValue(pos); m_cameraDirection_modelSpace->setValue(glm::normalize(pos)); m_cameraForwardDirection_modelSpace->setValue(glm::mat3(invModelMatrix) * context.camera->getDirection()); } if (context.light) { glm::vec3 dir = glm::inverse(context.sceneNode->getOrientation()) * context.light->getDirection(); m_lightDirection_modelSpace->setValue(-dir); // light direction in shader is direction to light const ShadowProjector* projector = context.light->getShadowProjector(); if (projector) { static glm::mat4 biasMatrix( 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.5, 1.0 ); glm::mat4 shadowModelViewProj = projector->getCamera()->getViewProjectionMatrix() * context.sceneNode->getTransform(); m_shadowModelViewProjectionMatrix->setValue(biasMatrix * shadowModelViewProj); m_shadowMaxClipSpaceDepth->setValue(projector->getCamera()->getProjection()->getMaxClipSpaceDepth()); m_invShadowViewProjectionMatrix->setValue(glm::inverse(biasMatrix * projector->getCamera()->getViewProjectionMatrix())); } } else { m_lightDirection_modelSpace->setValue(vec3Zero()); } }
void TensorFlowRepresentation::update(const ProjectionPtr projection, const Vector &delta) { VectorProjection *vp = dynamic_cast<VectorProjection*>(projection.get()); if (vp) { Vector v; read(projection, &v, NULL); batch_.push_back({vp->vector, v+delta}); } else throw Exception("representation/tensorflow requires a projector returning a VectorProjection"); }
double ANNRepresentation::read(const ProjectionPtr &projection, Vector *result, Vector *stddev) const { VectorProjection *vp = dynamic_cast<VectorProjection*>(projection.get()); if (vp) { double hidden[MAX_NODES], output[MAX_NODES]; size_t hidden_params = inputs_+bias_+recurrent_; // Calculate hidden activation for (size_t hh=0; hh < hiddens_; ++hh) { const double *w = &weights_[hidden_params*hh]; double act = 0; if (bias_) act += w[inputs_]; // Bias if (recurrent_) act += state_[hh]*w[inputs_+bias_]; // Recurrence for (size_t ii=0; ii < inputs_; ++ii) act += w[ii]*vp->vector[ii]; hidden[hh] = activate(act); } // TODO: Remember state // memcpy(state_.data(), hidden, hiddens_*sizeof(double)); // Calculate output activation for (size_t oo=0; oo < outputs_; ++oo) { const double *w = &weights_[hidden_params*hiddens_+(hiddens_+bias_)*oo]; double act = 0; if (bias_) act += w[hiddens_]; // Bias for (size_t hh=0; hh < hiddens_; ++hh) act += w[hh]*hidden[hh]; output[oo] = activate(act); } // Normalize outputs from 0..1 to output_min_..output_max_ result->resize(outputs_); for (size_t oo=0; oo < outputs_; ++oo) (*result)[oo] = output_min_[oo]+output[oo]*(output_max_[oo]-output_min_[oo]); } else throw Exception("representation/parameterized/ann requires a projector returning a VectorProjection"); if (stddev) stddev->clear(); return (*result)[0]; }
void TensorFlowRepresentation::enqueue(const ProjectionPtr &projection) { VectorProjection *vp = dynamic_cast<VectorProjection*>(projection.get()); if (vp) { auto input_map = input_.tensor<float, 2>(); for (size_t jj=0; jj < input_map.dimension(1); ++jj) input_map(counter_, jj) = vp->vector[jj]; counter_++; } else throw Exception("representation/tensorflow requires a projector returning a VectorProjection"); }
void TensorFlowRepresentation::enqueue(const ProjectionPtr &projection, const Vector &target) { VectorProjection *vp = dynamic_cast<VectorProjection*>(projection.get()); if (vp) { auto input_map = input_.tensor<float, 2>(); for (size_t ii=0; ii < input_map.dimension(1); ++ii) input_map(counter_, ii) = vp->vector[ii]; auto target_map = target_.tensor<float, 2>(); for (size_t ii=0; ii < target_map.dimension(1); ++ii) target_map(counter_, ii) = target[ii]; counter_++; } else throw Exception("representation/tensorflow requires a projector returning a VectorProjection"); }
void TensorFlowRepresentation::write(const ProjectionPtr projection, const Vector &target, const Vector &alpha) { VectorProjection *vp = dynamic_cast<VectorProjection*>(projection.get()); if (vp) { if (prod(alpha) == 1) { batch_.push_back({vp->vector, target}); } else { Vector v; read(projection, &v, NULL); batch_.push_back({vp->vector, (1-alpha)*v + alpha*target}); } } else throw Exception("representation/tensorflow requires a projector returning a VectorProjection"); }
double TensorFlowRepresentation::read(const ProjectionPtr &projection, Vector *result, Vector *stddev) const { VectorProjection *vp = dynamic_cast<VectorProjection*>(projection.get()); if (vp) { Tensor input(tensorflow::DT_FLOAT, TensorShape({1, (int)vp->vector.size()})); auto input_map = input.tensor<float, 2>(); for (size_t ii=0; ii < vp->vector.size(); ++ii) input_map(0, ii) = vp->vector[ii]; Tensor learning(tensorflow::DT_BOOL, TensorShape()); learning.scalar<bool>()() = false; std::vector<std::pair<std::string, tensorflow::Tensor>> inputs = {{input_layer_, input}}; if (!learning_phase_.empty()) inputs.push_back({learning_phase_, learning}); std::vector<Tensor> outputs; Status run_status = session_->Run(inputs, {output_layer_}, {}, &outputs); if (!run_status.ok()) { ERROR(run_status.ToString()); throw Exception("Could not run prediction graph"); } auto output_map = outputs[0].tensor<float, 2>(); result->resize(output_map.dimension(1)); for (size_t ii=0; ii < result->size(); ++ii) (*result)[ii] = output_map(0, ii); } else throw Exception("representation/tensorflow requires a projector returning a VectorProjection"); if (stddev) *stddev = Vector(); return (*result)[0]; }
/** * Draw the projections. * * @post Draws the projections. * If withApex, then it draws the frustums with different colors. * If !withApex, then just draws the surfaces in all white. */ void BasicSimulator::drawProjections(const bool drawFrustum, const gmtl::Vec3f& surfColor, const float scaleFactor) { const float ALPHA_VALUE(0.25f); DisplayManager* display_man = vrj::opengl::DrawManager::instance()->getDisplayManager(); display_man->updateProjections(scaleFactor); // Update all projections for drawing std::vector<vrj::DisplayPtr> disps = display_man->getAllDisplays(); gmtl::Vec3f apex, ur, lr, ul, ll; ProjectionPtr proj; for ( unsigned int i = 0; i < disps.size(); ++i ) { for ( unsigned int v = 0; v < disps[i]->getNumViewports(); ++v ) { ViewportPtr view_port = disps[i]->getViewport(v); if ( view_port->isSurface() ) { // Get a pointer to the surface viewport. SurfaceViewportPtr surf_vp = boost::dynamic_pointer_cast<SurfaceViewport>(view_port); vprASSERT(surf_vp.get() != NULL); for ( unsigned int proj_num = 0; proj_num < 2; ++proj_num ) { if ( 0 == proj_num ) { proj = surf_vp->getLeftProj(); } else { proj = surf_vp->getRightProj(); } // Create color values that are unique // Basically count in binary (skipping 0), and use the first 3 // digits. That will give six colors const int red_on = i & 0x1; const int green_on = (i >> 1) & 0x1; const int blue_on = (i >> 2) & 0x1; float red(0.0f), green(0.0f), blue(0.0f); if ( red_on > 0 ) { red = 1.0f; } if ( green_on > 0 ) { green = 1.0f; } if ( blue_on > 0 ) { blue = 1.0f; } if ( ! red_on && ! blue_on && ! green_on ) // Case of 0's (black is bad) { red = blue = green = 0.75f; } gmtl::Vec3f surf_color; if ( drawFrustum ) { surf_color = gmtl::Vec3f(red, blue, green); } else { surf_color = surfColor; } gmtl::Vec3f apex_color(surf_color); if ( 1 == proj_num ) // Right eye { apex_color = gmtl::Vec3f(1.0f, 1.0f, 1.0f) - apex_color; // Invert it } // Compute scaled colors for the corners. // The lower left is going to be lighter, and the upper right // is going to be darker. const float ll_scale(0.10f); const float ul_scale(0.55f); const float ur_scale(1.0f); const gmtl::Vec4f ll_clr(ll_scale * surf_color[0], ll_scale * surf_color[1], ll_scale * surf_color[2], ALPHA_VALUE); const gmtl::Vec4f ul_clr(ul_scale * surf_color[0], ul_scale * surf_color[1], ul_scale * surf_color[2], ALPHA_VALUE); const gmtl::Vec4f lr_clr(ul_scale * surf_color[0], ul_scale * surf_color[1], ul_scale * surf_color[2], ALPHA_VALUE); const gmtl::Vec4f ur_clr(ur_scale * surf_color[0], ur_scale * surf_color[1], ur_scale * surf_color[2], ALPHA_VALUE); // Draw the thingy proj->getFrustumApexAndCorners(apex, ur, lr, ul, ll); vprDEBUG(vrjDBG_DRAW_MGR, vprDBG_STATE_LVL) << "apex: " << apex << std::endl << vprDEBUG_FLUSH; glColor4fv(&apex_color[0]); glPushMatrix(); if ( drawFrustum ) { drawLine(apex, ur); drawLine(apex, lr); drawLine(apex, ul); drawLine(apex, ll); } glColor4fv(&ur_clr[0]); // Draw the outline drawLine(ur, lr); drawLine(lr, ll); drawLine(ll, ul); drawLine(ul, ur); // Draw the surface glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBegin(GL_TRIANGLES); glColor4fv(ll_clr.mData); glVertex3fv(ll.mData); glColor4fv(lr_clr.mData); glVertex3fv(lr.mData); glColor4fv(ur_clr.mData); glVertex3fv(ur.mData); glColor4fv(ur_clr.mData); glVertex3fv(ur.mData); glColor4fv(ul_clr.mData); glVertex3fv(ul.mData); glColor4fv(ll_clr.mData); glVertex3fv(ll.mData); glEnd(); glDisable(GL_BLEND); glPopMatrix(); } } // if surface } // for viewports } // for disps