Example #1
0
//初始化代码基本不变,我们使用新的函数,加载*.raw纹理。并把纹理t2的一部分blit到t1中混合,接着按常规的方法设置2D纹理。
void MyGLWidget::initializeGL()
{
    m_t1 = allocateTextureBuffer(256, 256, 4);// 为图像t1分配内存
    if(readTextureData(":/data/Monitor.raw", m_t1) == 0)// 读取图像数据
    {
        QMessageBox::warning(this, tr("Warning"), tr("Read Monitor.raw error."));
    }
    m_t2 = allocateTextureBuffer(256, 256, 4);// 为图像t2分配内存
    if(readTextureData(":/data/GL.raw", m_t2) == 0)// 读取图像数据
    {
        QMessageBox::warning(this, tr("Warning"), tr("Read GL.raw error."));
    }
    //把图像t2的(127,127)-(256,256)部分和图像t1的(64,64,196,196)部分混合
    blit(m_t2, m_t1,127,127,128,128,64,64,1,127);
    //下面的代码和前面一样,释放分配的空间,创建纹理
    buildTexture(m_t1);//t1图像加载为纹理
    deallocateTexture(m_t1);// 释放图像数据
    deallocateTexture(m_t2);
    glEnable(GL_TEXTURE_2D);// 使用2D纹理
    glShadeModel(GL_SMOOTH);// 使用光滑着色
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);// 设置背景色为黑色
    glClearDepth(1.0);// 设置深度缓存清楚值为1
    glEnable(GL_DEPTH_TEST);// 使用深度缓存
    glDepthFunc(GL_LESS);// 设置深度测试函数
}
Example #2
0
int main( int argc, char *argv[] )
{
// Put the file paths back to a flatter system because I do not know if
// Windows will barf at the forward slashes and I do not have time to make this
// code check which OS it's using.
//	char temp[256];
//	string dataFileName(getcwd(temp, 255));
//	string colorFileName(getcwd(temp, 255));
//	dataFileName.append("Elevation.bin");
//	colorFileName.append("rainbow.pal");

//	string  dataFileName("Elevation.bin");
//	string  colorFileName("livingston.pal");
	string  dataFileName("Head.bin");
	string  colorFileName("rainbow.pal");
	cout << "Data file: " << dataFileName << endl;
	cout << "Color file: " << colorFileName << endl;
//	cout << "Setting up data reader" << endl;
	dr = new DataReader(dataFileName, colorFileName);
//	cout << "Building texture" << endl;
	texels = buildTexture();	// texels allocated here
//	cout << "Everything appears to work" << endl;
	setup(argc, argv);
	free(texels);
	free(dr);
	return 0;
}
Example #3
0
StbImage::StbImage(const QString& name, const QString& path, IResourceFactory* factory, stbi_uc* data, bool mipmap, bool filtering, bool wrap_s, bool wrap_t, int comp) :
    TextureData(name, path, factory),
    m_data(data),
    m_comp(comp),
    m_mipmap(mipmap),
    m_filtering(filtering),
    m_wrap_s(wrap_s),
    m_wrap_t(wrap_t)
{
    if(m_data != 0)
    {
        buildTexture();
        m_state = STATE_LOADED;
    }
    else
    {
        m_state = STATE_UNLOADED;
    }
}
Example #4
0
	Texture buildTexture(float top, float left, float width, float height, float textureWidth, float textureHeight, bool useHalfTexel) {
		Rect r(top, left, width, height);
		return buildTexture(r, textureWidth, textureHeight, useHalfTexel);
	}
void GLImageProcessor::setImage(const QImage& inputImage)
{
	image = inputImage;
	renderFbo.reset(new QOpenGLFramebufferObject(image.size(), renderFbo->format()));
	buildTexture();
}
Example #6
0
VkBool32 Example::buildResources( const vkts::IUpdateThreadContext& updateContext)
{
	VkResult result;

	//

	glm::uvec2 dimension = updateContext.getWindowDimension(windowIndex);
	VkExtent2D extent2D = { dimension.x, dimension.y };

	//

	auto lastSwapchain = swapchain;

	VkSwapchainKHR oldSwapchain = lastSwapchain.get() ? lastSwapchain->getSwapchain() : VK_NULL_HANDLE;

	swapchain = vkts::wsiSwapchainCreate(physicalDevice->getPhysicalDevice(), device->getDevice(), 0, surface->getSurface(), VKTS_NUMBER_BUFFERS, extent2D, 1, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_SHARING_MODE_EXCLUSIVE, 0, nullptr, VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, VK_TRUE, oldSwapchain);

	if (!swapchain.get())
	{
		vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not create swap chain.");

		return VK_FALSE;
	}

	//

    swapchainImagesCount = (uint32_t)swapchain->getAllSwapchainImages().size();

    if (swapchainImagesCount == 0)
    {
        vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not get swap chain images count.");

        return VK_FALSE;
    }

    swapchainImageView = vkts::SmartPointerVector<vkts::IImageViewSP>(swapchainImagesCount);
    framebuffer = vkts::SmartPointerVector<vkts::IFramebufferSP>(swapchainImagesCount);
    cmdBuffer = vkts::SmartPointerVector<vkts::ICommandBuffersSP>(swapchainImagesCount);

    //

	if (lastSwapchain.get())
	{
		lastSwapchain->destroy();
	}

	//

	if (!buildRenderPass())
	{
		return VK_FALSE;
	}

	if (!buildPipeline())
	{
		return VK_FALSE;
	}

	//

	vkts::IImageSP stageImage;
	vkts::IDeviceMemorySP stageDeviceMemoryImage;

	vkts::ICommandBuffersSP updateCmdBuffer = vkts::commandBuffersCreate(device->getDevice(), commandPool->getCmdPool(), VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1);

	if (!updateCmdBuffer.get())
	{
		vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not create command buffer.");

		return VK_FALSE;
	}

	result = updateCmdBuffer->beginCommandBuffer(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, VK_NULL_HANDLE, 0, VK_NULL_HANDLE, VK_FALSE, 0, 0);

	if (result != VK_SUCCESS)
	{
		vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not begin command buffer.");

		return VK_FALSE;
	}

    for (uint32_t index = 0; index < swapchain->getMinImageCount(); index++)
    {
        swapchain->cmdPipelineBarrier(updateCmdBuffer->getCommandBuffer(), 0, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, index);
    }

	VkBool32 doUpdateDescriptorSets = VK_FALSE;

	if (!image.get())
	{
		if (!buildTexture(updateCmdBuffer, stageImage, stageDeviceMemoryImage))
		{
			vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not build texture.");

			return VK_FALSE;
		}

		doUpdateDescriptorSets = VK_TRUE;
	}

	result = updateCmdBuffer->endCommandBuffer();

	if (result != VK_SUCCESS)
	{
		vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not end command buffer.");

		return VK_FALSE;
	}

	VkSubmitInfo submitInfo;

	memset(&submitInfo, 0, sizeof(VkSubmitInfo));

	submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;

	submitInfo.waitSemaphoreCount = 0;
	submitInfo.pWaitSemaphores = nullptr;
	submitInfo.commandBufferCount = updateCmdBuffer->getCommandBufferCount();
	submitInfo.pCommandBuffers = updateCmdBuffer->getCommandBuffers();
	submitInfo.signalSemaphoreCount = 0;
	submitInfo.pSignalSemaphores = nullptr;

	result = queue->submit(1, &submitInfo, VK_NULL_HANDLE);

	if (result != VK_SUCCESS)
	{
		vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not submit queue.");

		return VK_FALSE;
	}

	result = queue->waitIdle();

	if (result != VK_SUCCESS)
	{
		vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not wait for idle queue.");

		return VK_FALSE;
	}

	updateCmdBuffer->destroy();

	if (stageImage.get() || stageDeviceMemoryImage.get())
	{
		destroyTexture(stageImage, stageDeviceMemoryImage);
	}

	//

	if (doUpdateDescriptorSets)
	{
		if (!updateDescriptorSets())
		{
			return VK_FALSE;
		}
	}

	for (int32_t i = 0; i < (int32_t)swapchainImagesCount; i++)
	{
		if (!buildSwapchainImageView(i))
		{
			return VK_FALSE;
		}

		if (!buildFramebuffer(i))
		{
			return VK_FALSE;
		}

		if (!buildCmdBuffer(i))
		{
			return VK_FALSE;
		}
	}

	return VK_TRUE;
}
Example #7
0
//
// Vulkan initialization.
//
VkBool32 Example::init(const vkts::IUpdateThreadContext& updateContext)
{
	fence = vkts::fenceCreate(device->getDevice(), 0);

	if (!fence.get())
	{
		vkts::logPrint(VKTS_LOG_ERROR, __FILE__, __LINE__, "Could not build fence.");

		return VK_FALSE;
	}

	if (!buildShader())
	{
		vkts::logPrint(VKTS_LOG_ERROR, __FILE__, __LINE__, "Could not build shader.");

		return VK_FALSE;
	}

	if (!buildCmdPool())
	{
		vkts::logPrint(VKTS_LOG_ERROR, __FILE__, __LINE__, "Could not build command pool.");

		return VK_FALSE;
	}

	//

	if (!buildTexture())
	{
		vkts::logPrint(VKTS_LOG_ERROR, __FILE__, __LINE__, "Could not build texture.");

		return VK_FALSE;
	}

	//

	if (!buildDescriptorSetLayout())
	{
		vkts::logPrint(VKTS_LOG_ERROR, __FILE__, __LINE__, "Could not build descriptor set layout.");

		return VK_FALSE;
	}

	if (!buildDescriptorSetPool())
	{
		vkts::logPrint(VKTS_LOG_ERROR, __FILE__, __LINE__, "Could not build descriptor set pool.");

		return VK_FALSE;
	}

	if (!buildDescriptorSets())
	{
		vkts::logPrint(VKTS_LOG_ERROR, __FILE__, __LINE__, "Could not build descriptor sets.");

		return VK_FALSE;
	}

	if (!buildPipelineLayout())
	{
		vkts::logPrint(VKTS_LOG_ERROR, __FILE__, __LINE__, "Could not build pipeline layout.");

		return VK_FALSE;
	}

	if (!buildPipeline())
	{
		vkts::logPrint(VKTS_LOG_ERROR, __FILE__, __LINE__, "Could not build pipeline.");

		return VK_FALSE;
	}

	if (!buildCmdBuffer())
	{
		vkts::logPrint(VKTS_LOG_ERROR, __FILE__, __LINE__, "Could not build command buffer.");

		return VK_FALSE;
	}

	return VK_TRUE;
}