/*! \internal */ QCLImage2D QCLContextGL::createTexture2D (QMacCompatGLenum type, QMacCompatGLuint texture, QMacCompatGLint mipmapLevel, QCLMemoryObject::Access access) { return createTexture2D(GLenum(type), GLuint(texture), GLint(mipmapLevel), access); }
ID3D11Texture2D* D3D11Context::createTexture2DFromAppData (const char* id) { int dataSize; const char* data = demo_->appDataGet (id, dataSize); if (nullptr == data) return nullptr; juce::Image img = ImageCache::getFromMemory (data, dataSize); if (!img.isValid()) return nullptr; int w = img.getWidth(); int h = img.getHeight(); if ((w % 2) > 0 || (h % 2) > 0) { w = (w % 2) > 0 ? (w + 1) : w; h = (h % 2) > 0 ? (h + 1) : h; img = img.rescaled (w, h); } img = img.convertedToFormat (juce::Image::ARGB); HeapBlock<uint8> mem (w * h * 4); memset (mem.getData(), 0xff, w * h * 4); juce::Image::BitmapData imgData (img, juce::Image::BitmapData::readOnly); uint8* src = imgData.data; uint8* dst = mem.getData(); size_t rowPitch = (size_t)w * 4; for (int r = 0; r < h; ++r) { uint8* s = src; uint8* d = dst; for (int c = 0; c < w; ++c) { d[0] = s[2]; d[1] = s[1]; d[2] = s[0]; d[3] = s[3]; s += 4; d += 4; } src += imgData.lineStride; dst += rowPitch; } Hold<ID3D11Texture2D> texture; if (texture.set (createTexture2D (w, h, 1, DXGI_FORMAT_R8G8B8A8_UNORM, mem.getData(), rowPitch, h * rowPitch)).isNull()) return nullptr; return texture.drop(); }
Uptr<GLFramebuffer> createFramebuffer(const ImageSize& size, GLenum color_format, int color_attachments_count, GLenum depth_format) { GLFramebufferDesc desc; for (int i = 0; i < color_attachments_count; ++i) { auto color = createTexture2D(size.width, size.height, color_format); desc.attachments.push_back(GLFramebufferAttachmentDesc{ std::move(color), GLenum(GL_COLOR_ATTACHMENT0+i), 0 }); } if (depth_format != GL_NONE) { auto depth = createTexture2D(size.width, size.height, depth_format); desc.attachments.push_back(GLFramebufferAttachmentDesc{ std::move(depth), GL_DEPTH_ATTACHMENT, 0 }); } return std::make_unique<GLFramebuffer>(desc); }
DepthStencilTexture RenderContext::createDepthStencilTexture(u32 width, u32 height, int sample_count) { DepthStencilTexture result = {}; result.texture = createTexture2D(0, width, height, Format::Depth24Stencil8, false, true, sample_count); ID3D11Texture2D *texture = (ID3D11Texture2D *)result.texture.texture_handle; ID3D11DepthStencilView *dsv = 0; pp->device->CreateDepthStencilView(texture, 0, &dsv); result.depth_stencil = (void *)dsv; return result; }
int main( int argc, char** argv ) { osg::ref_ptr<osg::Geode> quad1 = new osg::Geode; quad1->addDrawable( osg::createTexturedQuadGeometry( osg::Vec3(0.0,0.0,0.0), osg::Vec3(1.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0)) ); createTexture2D( *(quad1->getOrCreateStateSet()), true ); osg::ref_ptr<osg::Geode> quad2 = new osg::Geode; quad2->addDrawable( osg::createTexturedQuadGeometry( osg::Vec3(2.0,0.0,0.0), osg::Vec3(1.0,0.0,0.0), osg::Vec3(0.0,0.0,1.0)) ); createTexture2D( *(quad2->getOrCreateStateSet()), false ); osg::ref_ptr<osg::Group> root = new osg::Group; root->addChild( quad1.get() ); root->addChild( quad2.get() ); osgViewer::Viewer viewer; viewer.setSceneData( root.get() ); return viewer.run(); }
RenderTexture RenderContext::createRenderTexture(u32 width, u32 height, Format format, int sample_count) { RenderTexture result = {}; result.texture = createTexture2D(0, width, height, format, true, false, sample_count); ID3D11RenderTargetView *rtv = 0; ID3D11Texture2D *texture = (ID3D11Texture2D *)result.texture.texture_handle; D3D11_RENDER_TARGET_VIEW_DESC rtv_desc = {}; rtv_desc.Format = (DXGI_FORMAT)format_values[(u32)format]; rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; rtv_desc.Texture2D.MipSlice = 0; HRESULT error = pp->device->CreateRenderTargetView(texture, &rtv_desc, &rtv); result.render_texture = (void *)rtv; return result; }
void gep::Renderer::initialize() { m_pDebugRenderer = new DebugRenderer(); createWindow(); initD3DDevice(); g_globalManager.getLogging()->logMessage("Using DirectX Version: %d.%d sdk %d", D3D11_MAJOR_VERSION, D3D11_MINOR_VERSION, D3D11_SDK_VERSION); // Create the dummy 2d texture { m_pDummyTexture = createTexture2D("dummy texture 2d", new DummyTexture2DLoader(), TextureMode::Static); m_pDummyTexture->createEmpty(4, 4, ImageFormat::RGBA8); auto& dummyData = m_pDummyTexture->getImageData().getData()[0]; for(uint32 i = 0; i < 4 * 4 * 4; i+=4) { dummyData[i] = 0xFF; //R dummyData[i+1] = 0; //G dummyData[i+2] = 0xFF; //B dummyData[i+3] = 0xFF; //A } m_pDummyTexture->setHasData(true); m_pDummyTexture->finalize(); g_globalManager.getResourceManager()->registerResourceType("Texture2D", m_pDummyTexture); } // Creates the dummy shader { m_pDummyShader = createShader(); m_pDummyShader->setLoader(new ShaderFileLoader("data/base/dummy.fx")); m_pDummyShader->getLoader()->loadResource(m_pDummyShader); m_pDummyShader->finalize(); g_globalManager.getResourceManager()->registerResourceType("Shader", m_pDummyShader); } // Register the font resource type (no fallback) g_globalManager.getResourceManager()->registerResourceType("Font", nullptr); // load engine resources m_pDefaultFont = g_globalManager.getResourceManager()->loadResource<Font>(FontFileLoader("data/base/dejavusans.ttf", 11), LoadAsync::No); m_pFontShader = g_globalManager.getResourceManager()->loadResource<Shader>(ShaderFileLoader("data/base/font.fx"), LoadAsync::No); g_globalManager.getResourceManager()->finalizeResourcesWithFlags(ResourceFinalize::FromRenderer); m_fontPosition = ShaderConstant<vec2>("position", m_pFontShader); m_fontColor = ShaderConstant<Color>("color", m_pFontShader); m_fontScreenSize = ShaderConstant<vec2>("targetSize", m_pFontShader); m_fontTexture = ShaderConstant<Texture2D>("diffuse", m_pFontShader); m_fontScreenSize.set(vec2((float)m_settings.screenResolution.x, (float)m_settings.screenResolution.y)); // text billboard m_pTextBillboardShader = g_globalManager.getResourceManager()->loadResource<Shader>(ShaderFileLoader("data/base/fontBillboard.fx"), LoadAsync::No); m_textBillboardView = ShaderConstant<mat4>("View", m_pTextBillboardShader); m_textBillboardProjection = ShaderConstant<mat4>("Projection", m_pTextBillboardShader); m_textBillboardPosition = ShaderConstant<vec3>("position", m_pTextBillboardShader); m_textBillboardColor = ShaderConstant<Color>("color", m_pTextBillboardShader); m_textBillboardTexture = ShaderConstant<Texture2D>("diffuse", m_pTextBillboardShader); m_textBillboardScreenSize = ShaderConstant<vec2>("targetSize", m_pTextBillboardShader); m_textBillboardScreenSize.set(vec2((float)m_settings.screenResolution.x, (float)m_settings.screenResolution.y)); { auto aspectRatio = float(m_settings.screenResolution.x) / float(m_settings.screenResolution.y); m_projection = mat4::projectionMatrix(60.0f, aspectRatio, 0.1f, 10000.0f); } m_view = mat4::lookAtMatrix(vec3(300, 0, 205), vec3(0, 0, 150), vec3(0,0,1)); { Vertexbuffer::DataChannel dataChannels[] = { Vertexbuffer::DataChannel::POSITION_2D, Vertexbuffer::DataChannel::TEXCOORD0 }; m_pFontBuffer = new Vertexbuffer(m_pd3dDevice, dataChannels, Vertexbuffer::Primitive::Triangle, Vertexbuffer::Usage::Dynamic); } { Vertexbuffer::DataChannel dataChannels[] = { Vertexbuffer::DataChannel::POSITION }; m_pLinesBuffer = new Vertexbuffer(m_pd3dDevice, dataChannels, Vertexbuffer::Primitive::Line, Vertexbuffer::Usage::Dynamic); } { Vertexbuffer::DataChannel dataChannels[] = { Vertexbuffer::DataChannel::POSITION_2D }; m_pLines2DBuffer = new Vertexbuffer(m_pd3dDevice, dataChannels, Vertexbuffer::Primitive::Line, Vertexbuffer::Usage::Dynamic); } { m_pDummyModel = createModel(); m_pDummyModel->loadFile("data/base/dummy.thModel"); m_pDummyModel->setLoader(ModelDummyLoader().moveToHeap()); m_pDummyModel->getLoader()->loadResource(m_pDummyModel); m_pDummyModel->getMaterial(0).setShader(m_pDummyShader->makeResourcePtrFromThis<Shader>()); m_pDummyModel->finalize(); g_globalManager.getResourceManager()->registerResourceType("Model", m_pDummyModel); } //Loading additional resources m_pLightingShader = g_globalManager.getResourceManager()->loadResource<Shader>(ShaderFileLoader("data/shaders/lighting.fx")); m_pLightingAnimatedShader = g_globalManager.getResourceManager()->loadResource<Shader>(ShaderFileLoader("data/shaders/lightingAnimated.fx")); m_pLinesShader = g_globalManager.getResourceManager()->loadResource<Shader>(ShaderFileLoader("data/base/lines.fx")); m_pWireframeShader = g_globalManager.getResourceManager()->loadResource<Shader>(ShaderFileLoader("data/shaders/wireframe.fx")); m_lineColor = ShaderConstant<Color>("Color", m_pLinesShader); m_lineView = ShaderConstant<mat4>("View", m_pLinesShader); m_lineProjection = ShaderConstant<mat4>("Projection", m_pLinesShader); m_pLines2DShader = g_globalManager.getResourceManager()->loadResource<Shader>(ShaderFileLoader("data/base/lines2D.fx")); m_line2DColor = ShaderConstant<Color>("Color", m_pLines2DShader); m_lines2DScreenSize = ShaderConstant<vec2>("targetSize", m_pLines2DShader); m_lines2DScreenSize.set(vec2((float)m_settings.screenResolution.x, (float)m_settings.screenResolution.y)); #ifdef _DEBUG const GUID ID_ID3DUserDefinedAnnotation = { 0xb2daad8b, 0x03d4, 0x4dbf, { 0x95, 0xeb, 0x32, 0xab, 0x4b, 0x63, 0xd0, 0xab } }; m_pDeviceContext->QueryInterface(ID_ID3DUserDefinedAnnotation, (void**)&m_pUserDefinedAnnotation); if(m_pUserDefinedAnnotation == nullptr || !m_pUserDefinedAnnotation->GetStatus()) { GEP_RELEASE_AND_NULL(m_pUserDefinedAnnotation); HMODULE pModule = LoadLibraryA("d3d9.dll"); D3DPREF_BeginEvent = (D3DPERF_BeginEvent_Func)GetProcAddress(pModule, "D3DPERF_BeginEvent"); D3DPREF_EndEvent = (D3DPERF_EndEvent_Func)GetProcAddress(pModule, "D3DPERF_EndEvent"); } #endif }
int main(int argc, char* argv[]) { Window win(g_window_res); // initialize the transfer function Transfer_function transfer_fun; // first clear possible old values transfer_fun.reset(); // the add_stop method takes: // - unsigned char or float - data value (0.0 .. 1.0) or (0..255) // - vec4f - color and alpha value (0.0 .. 1.0) per channel //transfer fuer aufgabe 31 und 32 transfer_fun.add(0.0f, glm::vec4(0.0, 0.0, 0.0, 0.0)); transfer_fun.add(1.0f, glm::vec4(1.0, 1.0, 1.0, 1.0)); // transfer fuer aufgabe 33 /*transfer_fun.add(0.0f, glm::vec4(0.0, 0.0, 0.0, 0.0)); transfer_fun.add(0.2f, glm::vec4(0.0, 0.0, 0.0, 0.0)); transfer_fun.add(0.25f, glm::vec4(0.5, 0.5, 0.5, 0.1)); transfer_fun.add(0.3f, glm::vec4(0.0, 0.0, 0.0, 0.0)); transfer_fun.add(0.35f, glm::vec4(0.0, 0.0, 0.0, 0.0)); transfer_fun.add(0.55f, glm::vec4(1.0, 1.0, 1.0, 0.2)); transfer_fun.add(0.75f, glm::vec4(0.0, 0.0, 0.0, 0.0)); transfer_fun.add(1.0f, glm::vec4(0.0, 0.0, 0.0, 0.0));*/ // transfer fuer aufgabe 4 /*transfer_fun.add(0.0f, glm::vec4(0.0, 0.0, 0.0, 0.0)); transfer_fun.add(0.2f, glm::vec4(0.0, 0.0, 0.0, 0.0)); transfer_fun.add(0.25f, glm::vec4(0.5, 0.5, 0.5, 1.0)); transfer_fun.add(0.3f, glm::vec4(0.0, 0.0, 0.0, 0.0)); transfer_fun.add(1.0f, glm::vec4(0.0, 0.0, 0.0, 0.0));*/ ///NOTHING TODO UNTIL HERE------------------------------------------------------------------------------- //init volume loader Volume_loader_raw loader; //read volume dimensions glm::ivec3 vol_dimensions = loader.get_dimensions(g_file_string); unsigned max_dim = std::max(std::max(vol_dimensions.x, vol_dimensions.y), vol_dimensions.z); // calculating max volume bounds of volume (0.0 .. 1.0) glm::vec3 max_volume_bounds = glm::vec3(vol_dimensions) / glm::vec3((float)max_dim); // loading volume file data auto volume_data = loader.load_volume(g_file_string); auto channel_size = loader.get_bit_per_channel(g_file_string) / 8; auto channel_count = loader.get_channel_count(g_file_string); // init and upload volume texture glActiveTexture(GL_TEXTURE0); createTexture3D(vol_dimensions.x, vol_dimensions.y, vol_dimensions.z, channel_size, channel_count, (char*)&volume_data[0]); // init and upload transfer function texture glActiveTexture(GL_TEXTURE1); createTexture2D(255u, 1u, (char*)&transfer_fun.get_RGBA_transfer_function_buffer()[0]); // setting up proxy geometry Cube cube(glm::vec3(0.0, 0.0, 0.0), max_volume_bounds); // loading actual raytracing shader code (volume.vert, volume.frag) // edit volume.frag to define the result of our volume raycaster GLuint program(0); try { program = loadShaders(g_file_vertex_shader, g_file_fragment_shader); } catch (std::logic_error& e) { std::cerr << e.what() << std::endl; } // init object manipulator (turntable) Manipulator manipulator; // manage keys here // add new input if neccessary (ie changing sampling distance, isovalues, ...) while (!win.shouldClose()) { // exit window with escape if (win.isKeyPressed(GLFW_KEY_ESCAPE)) { win.stop(); } if (win.isKeyPressed(GLFW_KEY_LEFT)) { g_light_pos.x -= 0.5f; } if (win.isKeyPressed(GLFW_KEY_RIGHT)) { g_light_pos.x += 0.5f; } if (win.isKeyPressed(GLFW_KEY_UP)) { g_light_pos.z -= 0.5f; } if (win.isKeyPressed(GLFW_KEY_DOWN)) { g_light_pos.z += 0.5f; } if (win.isKeyPressed(GLFW_KEY_MINUS)) { g_iso_value -= 0.002f; g_iso_value = std::max(g_iso_value, 0.0f); } if (win.isKeyPressed(GLFW_KEY_EQUAL) || win.isKeyPressed(GLFW_KEY_KP_ADD)) { g_iso_value += 0.002f; g_iso_value = std::min(g_iso_value, 1.0f); } if (win.isKeyPressed(GLFW_KEY_D)) { g_sampling_distance -= 0.0001f; g_sampling_distance = std::max(g_sampling_distance, 0.0001f); } if (win.isKeyPressed(GLFW_KEY_S)) { g_sampling_distance += 0.0001f; g_sampling_distance = std::min(g_sampling_distance, 0.2f); } //pre-classification if (win.isKeyPressed(GLFW_KEY_O)){ g_switch_classification = 0; } //post-classification if (win.isKeyPressed(GLFW_KEY_L)) { g_switch_classification = 1; } // to add key inputs: // check win.isKeyPressed(KEY_NAME) // - KEY_NAME - key name (GLFW_KEY_A ... GLFW_KEY_Z) //if (win.isKeyPressed(GLFW_KEY_X)){ // // ... do something // //} /// reload shader if key R ist pressed if (win.isKeyPressed(GLFW_KEY_R)) { if (g_reload_shader_pressed != true) { GLuint newProgram(0); try { std::cout << "Reload shaders" << std::endl; newProgram = loadShaders(g_file_vertex_shader, g_file_fragment_shader); } catch (std::logic_error& e) { std::cerr << e.what() << std::endl; newProgram = 0; } if (0 != newProgram) { glDeleteProgram(program); program = newProgram; } g_reload_shader_pressed = true; } } else { g_reload_shader_pressed = false; } /// show transfer function if T is pressed if (win.isKeyPressed(GLFW_KEY_T)){ if (!g_show_transfer_function_pressed){ g_show_transfer_function = !g_show_transfer_function; } g_show_transfer_function_pressed = true; } else { g_show_transfer_function_pressed = false; } auto size = win.windowSize(); glViewport(0, 0, size.x, size.y); glClearColor(g_background_color.x, g_background_color.y, g_background_color.z, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); float fovy = 45.0f; float aspect = (float)size.x / (float)size.y; float zNear = 0.025f, zFar = 10.0f; glm::mat4 projection = glm::perspective(fovy, aspect, zNear, zFar); glm::vec3 translate = max_volume_bounds * glm::vec3(-0.5f); glm::vec3 eye = glm::vec3(0.0f, 0.0f, 1.5f); glm::vec3 target = glm::vec3(0.0f); glm::vec3 up(0.0f, 1.0f, 0.0f); auto view = glm::lookAt(eye, target, up); auto model_view = view * manipulator.matrix(win) // rotate head upright * glm::rotate(0.5f*float(M_PI), glm::vec3(0.0f,1.0f,0.0f)) * glm::rotate(0.5f*float(M_PI), glm::vec3(1.0f,0.0f,0.0f)) * glm::translate(translate) ; glm::vec4 camera_translate = glm::column(glm::inverse(model_view), 3); glm::vec3 camera_location = glm::vec3(camera_translate.x, camera_translate.y, camera_translate.z); camera_location /= glm::vec3(camera_translate.w); glm::vec4 light_location = glm::vec4(g_light_pos, 1.0f) * model_view; glUseProgram(program); glUniform1i(glGetUniformLocation(program, "volume_texture"), 0); glUniform1i(glGetUniformLocation(program, "transfer_texture"), 1); glUniform3fv(glGetUniformLocation(program, "camera_location"), 1, glm::value_ptr(camera_location)); glUniform1f(glGetUniformLocation(program, "sampling_distance"), g_sampling_distance); glUniform1i(glGetUniformLocation(program, "switch_classification"), g_switch_classification); glUniform1f(glGetUniformLocation(program, "iso_value"), g_iso_value); glUniform3fv(glGetUniformLocation(program, "max_bounds"), 1, glm::value_ptr(max_volume_bounds)); glUniform3iv(glGetUniformLocation(program, "volume_dimensions"), 1, glm::value_ptr(vol_dimensions)); glUniform3fv(glGetUniformLocation(program, "light_position"), 1, //glm::value_ptr(glm::vec3(light_location.x, light_location.y, light_location.z))); glm::value_ptr(g_light_pos)); glUniform3fv(glGetUniformLocation(program, "light_color"), 1, glm::value_ptr(g_light_color)); glUniform3fv(glGetUniformLocation(program, "light_color"), 1, glm::value_ptr(g_light_color)); glUniformMatrix4fv(glGetUniformLocation(program, "Projection"), 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(glGetUniformLocation(program, "Modelview"), 1, GL_FALSE, glm::value_ptr(model_view)); cube.draw(); glUseProgram(0); if (g_show_transfer_function) transfer_fun.update_and_draw(); win.update(); } return 0; }
/*! \internal */ QCLImage2D QCLContextGL::createTexture2D (QMacCompatGLuint texture, QCLMemoryObject::Access access) { return createTexture2D(GLenum(GL_TEXTURE_2D), GLuint(texture), GLint(0), access); }
/*! \overload Creates a 2D OpenCL image object from the specified OpenGL \a texture object, and the \a access mode. If texture type is assumed to be \c{GL_TEXTURE_2D} and the mipmap level is assumed to be 0. This function will only work if supportsObjectSharing() is true. */ QCLImage2D QCLContextGL::createTexture2D(GLuint texture, QCLMemoryObject::Access access) { return createTexture2D(GL_TEXTURE_2D, texture, 0, access); }