[osg-users] Rendering Textured Quad no longer working after moving to different View

Philipp Meyer philipp.meyer at fh-bielefeld.de
Mon Sep 12 02:51:09 PDT 2016


Hi,

I have a little annoying issue here and fail to understand why it happens or what goes wrong.

Im using a shader to compute various output Textures, some for direct display, others for post processing.
To be able to easily display the rendered Textures, I created a little helper class which derives from osg::Camera and internally used a orthographic projection and a Quad to render the texture to the screen. 
(Source code below) That way, I can simply add the TextureDisplay to any scene graph to view my rendered Texture.

Code:

/*
 * TextureView.cpp
 *
 *  Created on: Jun 8, 2016
 *      Author: ubuntu
 */

#include "TextureView.h"

#include "osgHelper.h"
#include "MDRTErrorHandling.h"

#include <cassert>

namespace MDRT {

TextureView::TextureView() {
	setViewMatrix(osg::Matrix::identity());
	setProjectionMatrix(osg::Matrix::ortho2D(0, 1, 0, 1));
	setClearColor(osg::Vec4(1, 0, 0, 1));

//	auto mt = osgHelper::make_osgref<osg::MatrixTransform>();
//	mt->setMatrix(osg::Matrix::identity());
//	mt->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
//	addChild(mt);

	auto geode = osgHelper::make_osgref<osg::Geode>();
	geometry = osgHelper::make_osgref<osg::Geometry>();
	geode->addDrawable(geometry);
	addChild(geode);

	auto quadVertices = osgHelper::make_osgref<osg::Vec3Array>();
	quadVertices->push_back(osg::Vec3(0, 0, 0));
	quadVertices->push_back(osg::Vec3(1, 0, 0));
	quadVertices->push_back(osg::Vec3(1, 1, 0));
	quadVertices->push_back(osg::Vec3(0, 1, 0));
	geometry->setVertexArray(quadVertices);

	auto quadPrimitiveSet = osgHelper::make_osgref<osg::DrawElementsUInt>(
			osg::PrimitiveSet::QUADS, 0);
	quadPrimitiveSet->push_back(0);
	quadPrimitiveSet->push_back(1);
	quadPrimitiveSet->push_back(2);
	quadPrimitiveSet->push_back(3);
	geometry->addPrimitiveSet(quadPrimitiveSet);

	auto texCoords = osgHelper::make_osgref<osg::Vec2Array>();
	texCoords->push_back(osg::Vec2(0, 0));
	texCoords->push_back(osg::Vec2(1, 0));
	texCoords->push_back(osg::Vec2(1, 1));
	texCoords->push_back(osg::Vec2(0, 1));
	geometry->setTexCoordArray(0, texCoords, osg::Array::BIND_PER_VERTEX);

	auto ss = geode->getOrCreateStateSet();

	auto program = osgHelper::make_osgref<osg::Program>();
	auto vertShader = osg::Shader::readShaderFile(osg::Shader::Type::VERTEX,
			"res/CustomShaders/FrameHeader.vert");
	auto fragShader = osg::Shader::readShaderFile(osg::Shader::Type::FRAGMENT,
			"res/CustomShaders/FrameHeader.frag");

	if (!vertShader || !fragShader) {
		throw MDRTExceptionBase("error loading textureview shaders");
	}

	bool ok = true;

	ok = ok && program->addShader(vertShader);
	ok = ok && program->addShader(fragShader);

	if (!ok) {
		throw MDRTExceptionBase(
				"error adding textureview shaders to program obj");
	}

	ss->setAttributeAndModes(program);
	ss->getOrCreateUniform("tex", osg::Uniform::Type::SAMPLER_2D_RECT, 0);

}

TextureView::~TextureView() {
	// TODO Auto-generated destructor stub
}

void TextureView::setTexture(osg::Texture* tex) {
	auto texRec = dynamic_cast<osg::TextureRectangle*>(tex);
	assert(texRec);

	float texWidth = static_cast<float>(texRec->getImage()->s());
	float texHeight = static_cast<float>(texRec->getImage()->t());

	auto texCoords = osgHelper::make_osgref<osg::Vec2Array>();
	texCoords->push_back(osg::Vec2(0, 0));
	texCoords->push_back(osg::Vec2(1 * texWidth, 0));
	texCoords->push_back(osg::Vec2(1 * texWidth, 1 * texHeight));
	texCoords->push_back(osg::Vec2(0, 1 * texHeight));
	geometry->setTexCoordArray(0, texCoords, osg::Array::BIND_PER_VERTEX);
	geometry->getOrCreateStateSet()->setTextureAttributeAndModes(0, tex);
	geometry->dirtyDisplayList();

}

} /* namespace MDRT */




This works fine and I have used it since several months. Today, I decided that it would be nicer to view the Textures in a separate window, so I switched to a CompositeViewer and created a new View and window for the textures. After that, I add my TextureViews to the views main camera.



Code:
//create texture views for radar shader output
		constexpr int radarCameraTextureViewWidth = 800;
		constexpr int radarCameraTextureViewHeight = radarCameraTextureViewWidth
				/ 4;
		constexpr int radarTextureViewSize = radarCameraTextureViewHeight;

		auto radarCameraTextureView = osgHelper::make_osgref<osgViewer::View>();
		radarCameraTextureView->setCameraManipulator(nullptr);

		radarCameraTextureView->getCamera()->setGraphicsContext(
				masterCamera->getGraphicsContext());
		radarCameraTextureView->getCamera()->setViewMatrix(
				osg::Matrix::identity());
		radarCameraTextureView->getCamera()->setProjectionMatrix(
				osg::Matrix::identity());
		viewer->addView(radarCameraTextureView);

		auto radarCameraTextureViewRoot = radarCameraTextureView->getCamera();

		auto radarShaderOutputTextureView =
				osgHelper::make_osgref<TextureView>();
		radarShaderOutputTextureView->setViewport(0, 0, radarTextureViewSize,
				radarTextureViewSize);
		radarShaderOutputTextureView->setClearColor(osg::Vec4(0, 1, 0, 1));
		radarShaderOutputTextureView->setTexture(rs.radarShaderOutputTexture);
		radarCameraTextureViewRoot->addChild(radarShaderOutputTextureView);




However, to my surprise I cannot see any textures. Instead, I only see the clear color of the TextureView cameras. I can tell that the cameras are added properly to the view and I can change their clear color, it just seems like the Quad is not there at all. The TextureViews still work normally when adding them to my mainView (Even after switching to CompositeViewer)

First I suspected that it may have something to do with the camera settings of my main view vs the new view (As I know that for example ViewMatrices are multiplied in child cameras), but the view and projection matrices of both master cameras are identical (set to identity).

Does anyone have an idea what the problem could be? I'm having issues debugging this, as I don't really know where to start.

Thank you!

Cheers,
Philipp

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=68579#68579








More information about the osg-users mailing list