void cv::render(const GlTexture& tex, Rect_<double> wndRect, Rect_<double> texRect) { #ifndef HAVE_OPENGL (void)tex; (void)wndRect; (void)texRect; throw_nogl; #else if (!tex.empty()) { tex.bind(); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glBegin(GL_QUADS); glTexCoord2d(texRect.x, texRect.y); glVertex2d(wndRect.x, wndRect.y); glTexCoord2d(texRect.x, texRect.y + texRect.height); glVertex2d(wndRect.x, (wndRect.y + wndRect.height)); glTexCoord2d(texRect.x + texRect.width, texRect.y + texRect.height); glVertex2d(wndRect.x + wndRect.width, (wndRect.y + wndRect.height)); glTexCoord2d(texRect.x + texRect.width, texRect.y); glVertex2d(wndRect.x + wndRect.width, wndRect.y); glEnd(); CV_CheckGlError(); tex.unbind(); } #endif }
/** * render skybox */ void renderWithProgram(GLSL::mat4 &p_pvm_matrix) { // CGlStateDisable depth_test(GL_DEPTH_TEST); CGlErrorCheck(); program.use(); pvm_matrix_uniform.set(p_pvm_matrix); vertex_buffer.bind(); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr); glEnableVertexAttribArray(0); texture_cube_map.bind(); #if 0 CGlErrorCheck(); glDrawArrays(GL_TRIANGLES, 0, 3); CGlErrorCheck(); #else index_buffer.bind(); CGlErrorCheck(); glDrawElements(GL_TRIANGLE_STRIP, 20, GL_UNSIGNED_BYTE, 0); index_buffer.unbind(); #endif texture_cube_map.unbind(); glDisableVertexAttribArray(0); vertex_buffer.unbind(); program.disable(); CGlErrorCheck(); }
inline GlTexture load_cubemap() { GlTexture tex; glBindTexture(GL_TEXTURE_CUBE_MAP, tex.get_gl_handle()); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, 0); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); int size; glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, 2048, 2048, 0, GL_RGB, GL_UNSIGNED_BYTE, load_image_data("assets/images/cubemap/positive_x.jpg", size).data()); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, 2048, 2048, 0, GL_RGB, GL_UNSIGNED_BYTE, load_image_data("assets/images/cubemap/negative_x.jpg", size).data()); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, 2048, 2048, 0, GL_RGB, GL_UNSIGNED_BYTE, load_image_data("assets/images/cubemap/positive_y.jpg", size).data()); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB, 2048, 2048, 0, GL_RGB, GL_UNSIGNED_BYTE, load_image_data("assets/images/cubemap/negative_y.jpg", size).data()); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, 2048, 2048, 0, GL_RGB, GL_UNSIGNED_BYTE, load_image_data("assets/images/cubemap/positive_z.jpg", size).data()); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, 2048, 2048, 0, GL_RGB, GL_UNSIGNED_BYTE, load_image_data("assets/images/cubemap/negative_z.jpg", size).data()); glBindTexture(GL_TEXTURE_CUBE_MAP, 0); return tex; }
GlTexture::GlTexture(const GlTexture& gl_texture) { m_texture_data = gl_texture.getTextureData(); m_texture_width = gl_texture.getTextureWidth(); m_texture_height = gl_texture.getTextureHeight(); m_texture_id = gl_texture.getTextureId(); }
void loadNormal0(const std::string &file) { if (normal0) delete normal0; normal0 = new GlTexture; normal0->loadFromFile(file); }
void loadTexture0(const std::string &file) { if (texture0) delete texture0; texture0 = new GlTexture; texture0->loadFromFile(file); }
/** * initialize skybox texture with files give as parameters */ void initWithTextures( const char *file_pos_x, const char *file_neg_x, const char *file_pos_y, const char *file_neg_y, const char *file_pos_z, const char *file_neg_z ) { texture_cube_map.bind(); texture_cube_map.loadFromFile(file_pos_x, GL_TEXTURE_CUBE_MAP_POSITIVE_X); texture_cube_map.loadFromFile(file_neg_x, GL_TEXTURE_CUBE_MAP_NEGATIVE_X); texture_cube_map.loadFromFile(file_pos_y, GL_TEXTURE_CUBE_MAP_POSITIVE_Y); texture_cube_map.loadFromFile(file_neg_y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y); texture_cube_map.loadFromFile(file_pos_z, GL_TEXTURE_CUBE_MAP_POSITIVE_Z); texture_cube_map.loadFromFile(file_neg_z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z); texture_cube_map.unbind(); CError_Append(texture_cube_map); }
void LoadGlImage(GlTexture& tex, const std::string& filename, bool sampling_linear) { const GLenum chtypes[] = { GL_ALPHA, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA }; TypedImage img = LoadImage(filename); const GLint format = chtypes[img.fmt.channels]; const GLint imgtype = GL_UNSIGNED_BYTE; tex.Reinitialise(img.w, img.h, format, sampling_linear, 0, format, imgtype, img.ptr ); img.Dealloc(); }
//============================================================================== void GlFramebuffer::attachTextureInternal( GLenum attachment, const GlTexture& tex, const U32 layer) { const GLenum target = GL_FRAMEBUFFER; switch(tex.getTarget()) { case GL_TEXTURE_2D: #if ANKI_GL == ANKI_GL_DESKTOP case GL_TEXTURE_2D_MULTISAMPLE: #endif ANKI_ASSERT(layer == 0); glFramebufferTexture2D(target, attachment, tex.getTarget(), tex.getGlName(), 0); break; case GL_TEXTURE_CUBE_MAP: ANKI_ASSERT(layer < 6); glFramebufferTexture2D(target, attachment, GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer, tex.getGlName(), 0); break; case GL_TEXTURE_2D_ARRAY: case GL_TEXTURE_3D: ANKI_ASSERT((GLuint)layer < tex.getDepth()); glFramebufferTextureLayer(target, attachment, tex.getGlName(), 0, layer); break; default: ANKI_ASSERT(0); break; } }
void create(float2 resolution) { depthBuffer.load_data(resolution.x, resolution.y, GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); framebuffer.attach(GL_DEPTH_ATTACHMENT, depthBuffer); if (!framebuffer.check_complete()) throw std::runtime_error("incomplete framebuffer"); glGenTextures(1, &cubeMapHandle); glBindTexture(GL_TEXTURE_CUBE_MAP, cubeMapHandle); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, 0); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); for (int i = 0; i < 6; ++i) { glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_R16F, resolution.x, resolution.y, 0, GL_RED, GL_UNSIGNED_SHORT, nullptr); } glBindTexture(GL_TEXTURE_CUBE_MAP, 0); struct CameraInfo { float3 position; float3 target; float3 up; CameraInfo(float3 p, float3 t, float3 u) : position(p), target(t), up(u) {} }; std::vector<CameraInfo> info = { {{0, 0, 0}, { 1, 0, 0}, {0, -1, 0}}, {{0, 0, 0}, {-1, 0, 0}, {0, -1, 0}}, {{0, 0, 0}, { 0, 1, 0}, {0, 0, 1}}, {{0, 0, 0}, { 0, -1, 0}, {0, 0, -1}}, {{0, 0, 0}, { 0, 0, 1}, {0, -1, 0}}, {{0, 0, 0}, { 0, 0, -1}, {0, -1, 0}}, }; for (int i = 0; i < 6; ++i) { CubemapCamera cc; cc.face = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i; cc.faceCamera.look_at(info[i].position, info[i].target, info[i].up); faces.push_back(cc); } gl_check_error(__FILE__, __LINE__); }
void create(float resolution) { shadowDepthTexture.load_data(resolution, resolution, GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); shadowFramebuffer.attach(GL_DEPTH_ATTACHMENT, shadowDepthTexture); if (!shadowFramebuffer.check_complete()) throw std::runtime_error("incomplete shadow framebuffer"); }
ExperimentalApp() : GLFWApp(1280, 720, "Shadow App") { glfwSwapInterval(0); gen = std::mt19937(rd()); igm.reset(new gui::ImGuiManager(window)); gui::make_dark_theme(); int width, height; glfwGetWindowSize(window, &width, &height); glViewport(0, 0, width, height); cameraController.set_camera(&camera); camera.farClip = 55.f; camera.look_at({0, 0, +15}, {0, 0, 0}); // Debugging views uiSurface.bounds = {0, 0, (float) width, (float) height}; uiSurface.add_child( {{0.0000f, +10},{0, +10},{0.1667f, -10},{0.133f, +10}}); uiSurface.add_child( {{0.1667f, +10},{0, +10},{0.3334f, -10},{0.133f, +10}}); uiSurface.add_child( {{0.3334f, +10},{0, +10},{0.5009f, -10},{0.133f, +10}}); uiSurface.add_child( {{0.5000f, +10},{0, +10},{0.6668f, -10},{0.133f, +10}}); uiSurface.add_child( {{0.6668f, +10},{0, +10},{0.8335f, -10},{0.133f, +10}}); uiSurface.add_child( {{0.8335f, +10},{0, +10},{1.0000f, -10},{0.133f, +10}}); uiSurface.layout(); fullscreen_post_quad = make_fullscreen_quad(); sceneShader = make_watched_shader(shaderMonitor, "assets/shaders/shadow/scene_vert.glsl", "assets/shaders/shadow/scene_frag.glsl"); shadowmapShader = make_watched_shader(shaderMonitor, "assets/shaders/shadow/shadowmap_vert.glsl", "assets/shaders/shadow/shadowmap_frag.glsl"); pointLightShader = make_watched_shader(shaderMonitor, "assets/shaders/shadow/point_light_vert.glsl", "assets/shaders/shadow/point_light_frag.glsl"); gaussianBlurShader = make_watched_shader(shaderMonitor, "assets/shaders/gaussian_blur_vert.glsl", "assets/shaders/gaussian_blur_frag.glsl"); skydome.recompute(2, 10.f, 1.15f); auto lightDir = skydome.get_light_direction(); sunLight = std::make_shared<DirectionalLight>(lightDir, float3(.50f, .75f, .825f), 64.f); // todo spotLightB shadowDepthTexture.load_data(shadowmapResolution, shadowmapResolution, GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); shadowFramebuffer.attach(GL_DEPTH_ATTACHMENT, shadowDepthTexture); if (!shadowFramebuffer.check_complete()) throw std::runtime_error("incomplete shadow framebuffer"); shadowBlurTexture.load_data(shadowmapResolution, shadowmapResolution, GL_R32F, GL_RGBA, GL_FLOAT, nullptr); shadowBlurFramebuffer.attach(GL_COLOR_ATTACHMENT0, shadowBlurTexture); if (!shadowBlurFramebuffer.check_complete()) throw std::runtime_error("incomplete blur framebuffer"); auto spotLightA = std::make_shared<SpotLight>(float3(0.f, 10.f, 0.f), float3(0.f, -1.f, 0.f), float3(0.766f, 0.766f, 0.005f), 30.0f, float3(1.0f, 0.0f, 0.0001f)); spotLights.push_back(spotLightA); // Single spotlight fbo for (int i = 0; i < 1; ++i) { auto buffer = std::make_shared<SpotLightFramebuffer>(); buffer->create(shadowmapResolution); spotLightFramebuffers.push_back(buffer); } // Point light init { pointLight.reset(new PointLight(float3(0.f, 0.f, 0.f), float3(0, 1, 1), float3(1.0f, 0.05f, 0.0002f))); pointLightFramebuffer.reset(new PointLightFramebuffer()); pointLightFramebuffer->create(float2(shadowmapResolution)); pointLightSphere = std::make_shared<Renderable>(make_sphere(0.5f)); sceneObjects.push_back(pointLightSphere); } viewA.reset(new GLTextureView(shadowDepthTexture.get_gl_handle())); viewB.reset(new GLTextureView(shadowBlurTexture.get_gl_handle())); viewC.reset(new GLTextureView(spotLightFramebuffers[0]->shadowDepthTexture.get_gl_handle())); viewD.reset(new GLTextureView(pointLightFramebuffer->depthBuffer.get_gl_handle())); auto lucy = load_geometry_from_ply("assets/models/stanford/lucy.ply"); rescale_geometry(lucy, 8.0f); auto lucyBounds = lucy.compute_bounds(); auto statue = std::make_shared<Renderable>(lucy); statue->pose.position = {0, 0, 0}; //sceneObjects.push_back(statue); auto hollowCube = load_geometry_from_ply("assets/models/geometry/CubeHollowOpen.ply"); for (auto & v : hollowCube.vertices) v *= 0.20f; auto hCube = std::make_shared<Renderable>(hollowCube); hCube->pose.position = float3(0, 0, 0); hCube->pose.orientation = make_rotation_quat_around_x(ANVIL_PI / 2); //sceneObjects.push_back(hCube); auto curvedMesh = make_curved_plane(2, 1, 8, 8); sceneObjects.push_back(std::make_shared<Renderable>(curvedMesh)); //floor = std::make_shared<Renderable>(make_plane(32.f, 32.f, 64, 64), false); //floor->pose.orientation = make_rotation_quat_axis_angle({1, 0, 0}, -ANVIL_PI / 2); //floor->pose.position = {0, lucyBounds.min().y, 0}; //sceneObjects.push_back(floor); gl_check_error(__FILE__, __LINE__); }
void vis_render() { const PlaneData *ro_visData; double aspect_ratio = 0; simulation->vis_get_vis_data_array(&ro_visData, &aspect_ratio); PlaneData &visData = (PlaneData&)*ro_visData; if (glTexture == nullptr) { glTexture = new GlTexture(GL_TEXTURE_2D, GL_RED, GL_RED, GL_UNSIGNED_BYTE); glTexture->bind(); glTexture->resize(visData.planeDataConfig->physical_data_size[0], visData.planeDataConfig->physical_data_size[1]); glTexture->unbind(); texture_data = new unsigned char[visData.planeDataConfig->physical_array_data_number_of_elements]; } visData.request_data_physical(); vis_min = visData.reduce_min(); vis_max = visData.reduce_max(); vis_max = std::max(vis_max, vis_min+1e-20); //< avoid numerical issues if min == max double real_delta = vis_max-vis_min; // vis_min -= real_delta*0.03; // vis_max += real_delta*0.03; double inv_delta = 1.0/real_delta; #pragma omp parallel for OPENMP_PAR_SIMD for (std::size_t i = 0; i < visData.planeDataConfig->physical_array_data_number_of_elements; i++) { double value = (visData.physical_space_data[i]-vis_min)*inv_delta; value *= 255.0; texture_data[i] = (unsigned char)std::min(255.0, std::max(0.0, value)); } glTexture->bind(); glTexture->setData(texture_data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); double scale_x = 1.0; double scale_y = aspect_ratio; if (aspect_ratio > 1.0) { scale_x /= aspect_ratio; scale_y = 1.0; } visualizationEngine->engineState->commonShaderPrograms.shaderTexturizeRainbowmap.use(); visualizationEngine->engineState->commonShaderPrograms.shaderTexturizeRainbowmap.pvm_matrix_uniform.set( visualizationEngine->engineState->matrices.pvm*GLSL::scale((float)scale_x, (float)scale_y, (float)1.0) ); glDrawQuad->render(); visualizationEngine->engineState->commonShaderPrograms.shaderTexturizeRainbowmap.disable(); glTexture->unbind(); if (hud_visible) { glFreeType->viewportChanged(visualizationEngine->renderWindow->window_width, visualizationEngine->renderWindow->window_height); std::string status_string = simulation->vis_get_status_string(); std::replace(status_string.begin(), status_string.end(), ',', '\n'); glFreeType->setPosition(10, visualizationEngine->renderWindow->window_height-16); glFreeType->renderString(status_string.c_str()); visSweetHUD->render(); } // execute simulation time step simulation->vis_post_frame_processing(sim_runs_per_frame); }
void ScreenFlow::display(GlContext *ctx) { glPushMatrix(); glTranslated(-1.85, 0.65, 0.0); GlTextRenderer::instance()->useFont("assets/fonts/corbel.ttf", 192); GlTextRenderer::instance()->setAlignment(GlTextRenderer::Alignment::RIGHT); GlTextRenderer::instance()->write(0.0f, 0.0f, 1.0 / 960.0f, 1.0 / 960.0f, breadcrumb(), 0.9f); glPopMatrix(); // int32_t leftLimit = position_ - 4; leftLimit = (leftLimit < 0) ? 0 : leftLimit; int32_t rightLimit = position_ + 5; rightLimit = (rightLimit >= items_.size()) ? items_.size() : rightLimit; double mu = -cos(animMu_ * 3.1415926) * 0.5 + 0.5; double animPosition = (1.0 - mu) * double(animCurrent_) + mu * double(animNext_); for (int32_t i = leftLimit; i < rightLimit; i++) { glPushMatrix(); double animDelta = double(i) - double(animPosition); glTranslated(animDelta, 0.0, 0.0); double rotMax = fmax(-1.0, fmin(1.0, animDelta)); { glTranslated(animDelta * 0.25, 0.0, -abs(animDelta)); glRotatef(-rotMax * 45.0f, 0.0f, 1.0f, 0.0f); } glColor4f(1.0f, 1.0f, 1.0f, 1.0f); GlTexture* demoScreenshot = items_[i]->screenshot(); if (demoScreenshot) demoScreenshot->bind(0); glBegin(GL_QUADS); glColor4f(1.0f, 1.0f, 1.0f, 1.0f / (1.0 + sqrtf(fabs(float(animDelta) * 5.0f)))); glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.96f, -0.6f); glTexCoord2f(1.0f, 1.0f); glVertex2f(0.96f, -0.6f); glTexCoord2f(1.0f, 0.0f); glVertex2f(0.96f, 0.6f); glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.96f, 0.6f); glEnd(); if (demoScreenshot) demoScreenshot->release(); glPopMatrix(); } // glTranslated(-1.1, -0.8, 0.0); GlTextRenderer::instance()->useFont("assets/fonts/corbel.ttf", 96); GlTextRenderer::instance()->setAlignment(GlTextRenderer::Alignment::RIGHT); GlTextRenderer::instance()->write(0.0f, 0.0f, 1.0f / 960.0f, 1.0f / 960.0f, std::string("Title: ").append(items_[position_]->title()), 0.9f); glTranslated(0.0, -0.15, 0.0); GlTextRenderer::instance()->write(0.0f, 0.0f, 1.0f / 960.0f, 1.0f / 960.0f, std::string("Author: ").append(items_[position_]->author()), 0.9f); animMu_ += ctx->delta() * 5.0; if (animMu_ > 1.0) animMu_ = 1.0; }
// copied from the orange book GlTexture* NoiseGenerator::make3Dnoise(int size, float ampStart, float ampDiv, int startFrequency) { int f, i, j, k, inc; //int startFrequency = 4; int numOctaves = NUM_OCT; //4; double ni[3]; double inci, incj, inck; int frequency = startFrequency; double amp = ampStart; double minn = 100.0, maxn = -100.0; // uint bufsize = noise3DTexSize * noise3DTexSize * noise3DTexSize * 4; // if ((noise3DTexPtr = (GLubyte *) malloc(bufsize)) == nullptr) // { // printf("ERROR: Could not allocate 3D noise texture - %d bytes\n", bufsize); // return nullptr; // } Space3D<Vec4b> noise3DTexPtr(size, size, size); byte *ptr = noise3DTexPtr.ptr()->v; for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= ampDiv) { setNoiseFrequency(frequency); ni[0] = ni[1] = ni[2] = 0; inci = 1.0 / (size / frequency); for (i = 0; i < size; ++i, ni[0] += inci) { incj = 1.0 / (size / frequency); for (j = 0; j < size; ++j, ni[1] += incj) { inck = 1.0 / (size / frequency); for (k = 0; k < size; ++k, ni[2] += inck, ptr+= 4) { double ns = noise3(ni) * N_FACT; *(ptr+inc) = (GLubyte)(((ns+1.0) * amp)*255.0); if (ns > maxn) maxn = ns; if (ns < minn) minn = ns; } } } } //GlTexture *tex = new GlTexture(); //tex->init(GL_TEXTURE_3D, QSize(size, size), size, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, // noise3DTexPtr, GL_LINEAR, GL_LINEAR, GL_REPEAT); //GL_NEAREST printf("%lf - %lf = %lf\n", maxn, minn, maxn - minn); Vec2i sz2d; uchar* buf2d = saveto2D(noise3DTexPtr, size, size/4, 8, &sz2d); GlTexture *tex = new GlTexture(); tex->init(GL_TEXTURE_2D, Vec2i(sz2d.x, sz2d.y), 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, buf2d, GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); /* QImage img(buf2d, sz2d.x, sz2d.y, QImage::Format_ARGB32); img.save("c:/temp/cubeTex.jpg"); */ delete[] buf2d; return tex; }