void updateSearch() { if (!valid(init) || !valid(goal)) { return; } if (!checker or (changes & (GoalChanged | InflationChanged))) { delete checker; checker = 0; checker = new BipedChecker(&grid, goal.x(), goal.y(), inflate_h, inflate_z); } searchResult = 0; helper.clear(); double start = gettimeasdouble(); bipedTrajectory.clear(); searchTrajectory(); /*searchResult = helper.search(init.x(), init.y(), initTheta, goal.x(), goal.y(), goalr, 0, // TODO: don't hardcode checker, maxDepth, viewDepth);*/ planTime = gettimeasdouble() - start; changes = NoChange; }
Texture::Pointer TextureFactory::genNoiseTexture(const vec2i& size, bool norm, const std::string& id) { DataStorage<vec4ub> randata(size.square()); for (size_t i = 0; i < randata.size(); ++i) { vec4 rand_f = vec4(randomFloat(-1.0f, 1.0f), randomFloat(-1.0f, 1.0f), randomFloat(-1.0f, 1.0f), randomFloat(-1.0f, 1.0f)); if (norm) rand_f.xyz().normalize(); randata[i].x = static_cast<unsigned char>(255.0f * clamp(0.5f + 0.5f * rand_f.x, 0.0f, 1.0f)); randata[i].y = static_cast<unsigned char>(255.0f * clamp(0.5f + 0.5f * rand_f.y, 0.0f, 1.0f)); randata[i].z = static_cast<unsigned char>(255.0f * clamp(0.5f + 0.5f * rand_f.z, 0.0f, 1.0f)); randata[i].w = static_cast<unsigned char>(255.0f * clamp(0.5f + 0.5f * rand_f.w, 0.0f, 1.0f)); } TextureDescription::Pointer desc = TextureDescription::Pointer::create(); desc->data = BinaryDataStorage(4 * size.square()); desc->target = TextureTarget::Texture_2D; desc->internalformat = TextureFormat::RGBA; desc->format = TextureFormat::RGBA; desc->type = DataType::UnsignedChar; desc->size = size; desc->mipMapCount = 1; desc->layersCount = 1; desc->bitsPerPixel = 32; etCopyMemory(desc->data.data(), randata.data(), randata.dataSize()); return Texture::Pointer::create(renderContext(), desc, id, false); }
Texture TextureFactory::genNoiseTexture(const vec2i& size, bool norm, const std::string& id) { DataStorage<vec4ub> randata(size.square()); for (size_t i = 0; i < randata.size(); ++i) { vec4 rand_f = vec4(randomFloat(-1.0f, 1.0f), randomFloat(-1.0f, 1.0f), randomFloat(-1.0f, 1.0f), randomFloat(-1.0f, 1.0f)); randata[i] = vec4f_to_4ub(norm ? vec4(rand_f.xyz().normalize(), rand_f.w) : rand_f); } TextureDescription::Pointer desc(new TextureDescription); desc->data = BinaryDataStorage(4 * size.square()); desc->target = GL_TEXTURE_2D; desc->format = GL_RGBA; desc->internalformat = GL_RGBA; desc->type = GL_UNSIGNED_BYTE; desc->size = size; desc->mipMapCount = 1; desc->layersCount = 1; desc->bitsPerPixel = 32; etCopyMemory(desc->data.data(), randata.data(), randata.dataSize()); return Texture(new TextureData(renderContext(), desc, id, false)); }
void setup() { size = window->getFramebufferSize(); if (grabber.open(0, size.x(), size.y()) < 0) throw "Failed to open capture device"; if (grabber.start() < 0) throw "Failed to start capture device"; }
int Grid::cellType(vec2i coord) { int index = coord.y() * d_cols + coord.x(); if (d_map[index]) { return 1; } else { d_map[index] = 1; return 0; } }
EGLAPI EGLSurface EGLAPIENTRY HookEglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint* attrib_list) { // Apply scaler setting ANativeWindow* window = Android_JNI_GetNativeWindow(); ANativeWindow_setBuffersGeometry(window, g_android_scaler_resolution.x(), g_android_scaler_resolution.y(), 0); return eglCreateWindowSurface(dpy, config, win, attrib_list); }
// Initialize the 'renderer_' member. No other members have been initialized at // this point. bool Game::InitializeRenderer() { #ifdef __ANDROID__ vec2i window_size = GetWindowSize(); if (fplbase::IsTvDevice()) { window_size = vec2i(kAndroidTvMaxScreenWidth, kAndroidTvMaxScreenHeight); } #else vec2i window_size(1200, 800); #endif // __ANDROID__ if (!renderer_.Initialize(window_size, GetConfig().window_title()->c_str())) { LogError("Renderer initialization error: %s\n", renderer_.last_error().c_str()); return false; } #ifdef __ANDROID__ // Restart the app if HW scaler setting failed. auto retry = fplbase::LoadPreference("HWScalerRetry", 0); const auto kMaxRetry = 3; auto current_window_size = fplbase::AndroidGetScalerResolution(); if (current_window_size.x() != window_size.x() || current_window_size.y() != window_size.y()) { if (retry < kMaxRetry) { LogError("Restarting application."); fplbase::SavePreference("HWScalerRetry", retry + 1); fplbase::RelaunchApplication(); return false; } // The HW may not support the API. Fallback to native resolution pass until // the API success next time. } else { // HW scaler setting was success. Clear retry counter. fplbase::SavePreference("HWScalerRetry", 0); } #endif // __ANDROID__ renderer_.set_color(mathfu::kOnes4f); // Initialize the first frame as black. renderer_.ClearFrameBuffer(mathfu::kZeros4f); #ifdef ANDROID_HMD vec2i size = fplbase::AndroidGetScalerResolution(); const vec2i viewport_size = size.x() && size.y() ? size : renderer_.window_size(); fplbase::InitializeUndistortFramebuffer(viewport_size.x(), viewport_size.y()); #endif // ANDROID_HMD #if ZOOSHI_OVERDRAW_DEBUG renderer_.SetBlendMode(BlendMode::kBlendModeAdd); renderer_.force_blend_mode() = BlendMode::kBlendModeAdd; renderer_.override_pixel_shader() = "void main() { gl_FragColor = vec4(0.2, 0.2, 0.2, 1); }"; #endif // ZOOSHI_OVERDRAW_DEBUG return true; }
void AndroidPreCreateWindow() { // Apply scaler setting prior creating surface if (g_android_scaler_resolution.x() && g_android_scaler_resolution.y()) { // Initialize OpenGL function pointers inside SDL if (SDL_GL_LoadLibrary(NULL) < 0) { SDL_LogError(SDL_LOG_CATEGORY_ERROR, "couldn't initialize OpenGL library\n"); } // Hook eglCreateWindowSurface call SDL_VideoDevice* device = SDL_GetVideoDevice(); device->egl_data->eglCreateWindowSurface = HookEglCreateWindowSurface; } }
void Renderer::readFramebufferData(const vec2i& size, TextureFormat format, DataType dataType, BinaryDataStorage& data) { ET_ASSERT((8 * data.size()) >= (size.square() * bitsPerPixelForTextureFormat(format, dataType))); glReadPixels(0, 0, size.x, size.y, textureFormatValue(format), dataTypeValue(dataType), data.data()); checkOpenGLError("glReadPixels"); }
namespace fpl { // Quick hack for HW scaler setting static vec2i g_android_scaler_resolution; void AndroidSetScalerResolution(const vec2i& resolution) { // Check against the real size of the device JNIEnv* env = reinterpret_cast<JNIEnv*>(SDL_AndroidGetJNIEnv()); jobject activity = reinterpret_cast<jobject>(SDL_AndroidGetActivity()); jclass fpl_class = env->GetObjectClass(activity); jmethodID get_size = env->GetMethodID(fpl_class, "GetLandscapedSize", "()[I"); jintArray size = (jintArray)env->CallObjectMethod(activity, get_size); jint* size_ints = env->GetIntArrayElements(size, NULL); int width = std::min(size_ints[0], resolution.x()); int height = std::min(size_ints[1], resolution.y()); g_android_scaler_resolution = vec2i(width, height); // Update the underlying activity with the scaled resolution jmethodID set_resolution = env->GetMethodID(fpl_class, "SetHeadMountedDisplayResolution", "(II)V"); env->CallVoidMethod(activity, set_resolution, width, height); env->ReleaseIntArrayElements(size, size_ints, JNI_ABORT); env->DeleteLocalRef(size); env->DeleteLocalRef(fpl_class); env->DeleteLocalRef(activity); } const vec2i& AndroidGetScalerResolution() { return g_android_scaler_resolution; } EGLAPI EGLSurface EGLAPIENTRY HookEglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint* attrib_list) { // Apply scaler setting ANativeWindow* window = Android_JNI_GetNativeWindow(); ANativeWindow_setBuffersGeometry(window, g_android_scaler_resolution.x(), g_android_scaler_resolution.y(), 0); return eglCreateWindowSurface(dpy, config, win, attrib_list); } void AndroidPreCreateWindow() { // Apply scaler setting prior creating surface if (g_android_scaler_resolution.x() && g_android_scaler_resolution.y()) { // Initialize OpenGL function pointers inside SDL if (SDL_GL_LoadLibrary(NULL) < 0) { SDL_LogError(SDL_LOG_CATEGORY_ERROR, "couldn't initialize OpenGL library\n"); } // Hook eglCreateWindowSurface call SDL_VideoDevice* device = SDL_GetVideoDevice(); device->egl_data->eglCreateWindowSurface = HookEglCreateWindowSurface; } } } // namespace fpl
bool valid(const vec2i& p) { return ( p.x() >= 0 && p.y() >= 0 && size_t(p.x()) < grid.nx() && size_t(p.y()) < grid.ny() && grid(p.x(), p.y()) ); }
void AndroidSetScalerResolution(const vec2i& resolution) { // Check against the real size of the device JNIEnv* env = reinterpret_cast<JNIEnv*>(SDL_AndroidGetJNIEnv()); jobject activity = reinterpret_cast<jobject>(SDL_AndroidGetActivity()); jclass fpl_class = env->GetObjectClass(activity); jmethodID get_size = env->GetMethodID(fpl_class, "GetLandscapedSize", "()[I"); jintArray size = (jintArray)env->CallObjectMethod(activity, get_size); jint* size_ints = env->GetIntArrayElements(size, NULL); int width = std::min(size_ints[0], resolution.x()); int height = std::min(size_ints[1], resolution.y()); g_android_scaler_resolution = vec2i(width, height); // Update the underlying activity with the scaled resolution jmethodID set_resolution = env->GetMethodID(fpl_class, "SetHeadMountedDisplayResolution", "(II)V"); env->CallVoidMethod(activity, set_resolution, width, height); env->ReleaseIntArrayElements(size, size_ints, JNI_ABORT); env->DeleteLocalRef(size); env->DeleteLocalRef(fpl_class); env->DeleteLocalRef(activity); }
void Program::setUniform(int nLoc, uint32_t type, const vec2i& value, bool forced) { #if !defined(ET_CONSOLE_APPLICATION) if (nLoc == -1) return; (void)type; ET_ASSERT(type == GL_INT_VEC2); ET_ASSERT(apiHandleValid()); if (forced || ((_vec2iCache.count(nLoc) == 0) || (_vec2iCache[nLoc] != value))) { _vec2iCache[nLoc] = value; glUniform2iv(nLoc, 1, value.data()); checkOpenGLError("glUniform2iv"); } #endif }
void draw() { window->clear(); grabber.update(); grabber.draw(size.x() / 4, size.y() / 4, size.x() / 2, size.y() / 2); }
vec2i operator-(const vec2i& v1, const vec2i &v2){ return vec2i(v1.peekx()-v2.peekx(), v1.peeky()-v2.peeky()); }
BinaryDataStorage Renderer::readFramebufferData(const vec2i& size, TextureFormat format, DataType dataType) { BinaryDataStorage result(size.square() * bitsPerPixelForTextureFormat(format, dataType)); readFramebufferData(size, format, dataType, result); return result; }
vec2i operator*(const int k, const vec2i& v1){ return vec2i(v1.peekx()*k, v1.peeky()*k); }
int dot(const vec2i& v1, const vec2i& v2){ return ((v1.peekx()*v2.peekx())+ (v1.peeky()*v2.peeky())); }
void Shader::setUniform(const std::string &name, const vec2i v, bool warn) { glUniform2i(uniform(name, warn), v.x(), v.y()); }
void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, grid_texture); glColor3ub(255,255,255); glBegin(GL_QUADS); glTexCoord2f(1, 0); glVertex2f(grid.nx(), 0); glTexCoord2f(0, 0); glVertex2f(0, 0); glTexCoord2f(0, 1); glVertex2f(0, grid.ny()); glTexCoord2f(1, 1); glVertex2f(grid.nx(), grid.ny()); glEnd(); glDisable(GL_TEXTURE_2D); if (valid(init)) { glPushMatrix(); glColor3ub(63,255,63); glTranslated(init.x(), init.y(), 0); gluDisk(quadric, 0, goalr, 32, 1); glRotated(initTheta*180/M_PI, 0, 0, 1); glBegin(GL_TRIANGLES); glVertex2f(6, 0); glVertex2f(0, 2); glVertex2f(0, -2); glEnd(); glPopMatrix(); } if (valid(goal)) { glPushMatrix(); glColor3ub(255,63,63); glTranslated(goal.x(), goal.y(), 0); gluDisk(quadric, 0, goalr, 32, 1); glPopMatrix(); } draw(searchResult, true); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluOrtho2D(0, width, 0, height); std::ostringstream ostr; ostr << "Goal pos: (" << goal.x() << ", " << goal.y() << ")\n" << "Init pos: (" << init.x() << ", " << init.y() << ")\n" << "Init theta: " << initTheta*180/M_PI << "\n" << "XY inflation: " << inflate_h << "\n" << "Z inflation: " << inflate_z << "\n" << "Max depth: " << maxDepth << "\n" << "View depth: " << viewDepth << "\n" << "Auto-plan: " << (auto_plan ? "on" : "off") << "\n" << "\n" << "Plan cost: " << (searchResult ? searchResult->costToCome : 0) << "\n" << "Plan time: " << (searchResult ? planTime : 0) << "\n"; if (show_help) { ostr << "\n\n" << " Left-click init pos\n" << "Shift+click init theta\n" << "Right-click goal pos\n\n" << " 1/2 max depth\n" << " 3/4 view depth\n" << " -/+ XY inflation\n" << " A auto-plan\n" << " Enter re-plan\n" << " ESC quit\n" << " ? hide help"; } else { ostr << "\nPress ? to toggle help."; } std::string str = ostr.str(); void* font = GLUT_BITMAP_8_BY_13; const int tx = 8; const int ty = 13; const int th = ty+2; int maxwidth = 0; int linewidth = 0; int rh = th; for (size_t i=0; i<str.length(); ++i) { char c = str[i]; if (c == '\n') { maxwidth = std::max(maxwidth, linewidth); linewidth = 0; rh += th; } else { linewidth += tx; } } maxwidth = std::max(maxwidth, linewidth); int rw = maxwidth + 20; rh += 10; glColor4ub(191,191,255,225); glEnable(GL_BLEND); glBegin(GL_QUADS); glVertex2f( 0, height); glVertex2f(rw, height); glVertex2f(rw, height-rh); glVertex2f( 0, height-rh); glEnd(); glDisable(GL_BLEND); int rx = 10; int ry = height-th; glColor3ub(0,0,0); glRasterPos2i(rx, ry); for (size_t i=0; i<str.length(); ++i) { char c = str[i]; if (c == '\n') { ry -= th; glRasterPos2i(rx, ry); } else { glutBitmapCharacter(font, str[i]); } } glPopMatrix(); glutSwapBuffers(); }
void MainController::applicationDidLoad(et::RenderContext* rc) { #if (ET_PLATFORM_WIN) application().pushSearchPath("..\\Data"); application().pushSearchPath("Q:\\SDK\\"); application().pushSearchPath("Q:\\SDK\\Models"); application().pushSearchPath("Q:\\SDK\\Textures"); #elif (ET_PLATFORM_MAC) application().pushSearchPath("/Volumes/Development/SDK"); application().pushSearchPath("/Volumes/Development/SDK/Models"); application().pushSearchPath("/Volumes/Development/SDK/Textures"); #endif _scene.options.bounces = _productionBounces; _scene.options.samples = _productionSamples; _scene.options.exposure = 1.0f; updateTitle(); rc->renderState().setDepthMask(false); rc->renderState().setDepthTest(false); _scene.load(rc); _textureData.resize(frameSize.square()); _textureData.fill(255); _outputFunction = [this](const vec2i& pixel, const vec4& color) mutable { size_t index = pixel.x + pixel.y * frameSize.x; _textureData[index].x = static_cast<unsigned char>(255.0f * clamp(color.x, 0.0f, 1.0f)); _textureData[index].y = static_cast<unsigned char>(255.0f * clamp(color.y, 0.0f, 1.0f)); _textureData[index].z = static_cast<unsigned char>(255.0f * clamp(color.z, 0.0f, 1.0f)); }; BinaryDataStorage textureData(reinterpret_cast<unsigned char*>(_textureData.binary()), _textureData.dataSize()); _result = rc->textureFactory().genTexture(TextureTarget::Texture_2D, TextureFormat::RGBA, frameSize, TextureFormat::RGBA, DataType::UnsignedChar, textureData, "result-texture"); _cameraAngles.setValue(cameraInitialAngles); _cameraAngles.setTargetValue(cameraInitialAngles); _cameraAngles.finishInterpolation(); _cameraAngles.updated.connect([this]() { updateCamera(); startRender(true); }); updateCamera(); _cameraAngles.run(); _gestures.pointerPressed.connect([this](et::PointerInputInfo) { startRender(true); }); _gestures.pointerReleased.connect([this](et::PointerInputInfo) { startRender(false); }); _gestures.drag.connect([this](et::vector2<float> d, unsigned long) { _cameraAngles.addTargetValue(0.1f * vec2(d.x, -d.y)); _cameraAngles.finishInterpolation(); }); for (size_t i = 0; i < numThreads; ++i) _threads.push_back(sharedObjectFactory().createObject<RaytraceThread>(this)); Invocation([this](){ startRender(false); }).invokeInMainRunLoop(); }
void Camera::setViewport(vec2i dim) { vpWidth = dim.x(); vpHeight = dim.y(); hasProjChanged = true; }