[osg-users] Textuiring a simple quad

Jochen Maier franke.jochen at gmx.de
Tue Oct 10 05:21:14 PDT 2017


Hi, all :)

I have so a simple quastion but I can solve the problem.

I want to draw a quad attach a simple Shader with a simple texture operation.
But I only get a black quad. The shader works when a color each fragmnent with the color red, but wen I want to get texture-colors... its only got black.

Some Code:

Code:

osg::Vec2Array* textureCoordinates = new osg::Vec2Array;
	osg::Vec3Array* quadVertices = new osg::Vec3Array;
	osg::Geometry* quadGeometry = new osg::Geometry();

	// *********** Vertices ***********
	quadVertices->push_back(osg::Vec3(x - rad, y - rad, 0.0));
	quadVertices->push_back(osg::Vec3(x + rad, y - rad, 0.0));
	quadVertices->push_back(osg::Vec3(x + rad, y + rad, 0.0));
	quadVertices->push_back(osg::Vec3(x - rad, y + rad, 0.0));

	quadGeometry->setVertexArray(quadVertices);
	// *********** Vertices - Ende ***********

	// *********** Texturkorrdinaten ***********
	textureCoordinates->push_back(osg::Vec2(0, 0));
	textureCoordinates->push_back(osg::Vec2(1, 0));
	textureCoordinates->push_back(osg::Vec2(1, 1));
	textureCoordinates->push_back(osg::Vec2(0, 1));

	quadGeometry->setTexCoordArray(0, textureCoordinates, osg::Array::BIND_PER_VERTEX);
	// *********** Texturkorrdinaten-Ende ***********


	// *********** Faces-Idizes ***********
	osg::DrawElementsUInt* gridFace = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
	gridFace->push_back(0);
	gridFace->push_back(1);
	gridFace->push_back(2);
	gridFace->push_back(3);

	quadGeometry->addPrimitiveSet(gridFace);
	// *********** Faces-Idizes - Ende ***********

	// **** Logo - Texture ****
	{
		std::string path("Grafik\\");
		std::string fileName = path + std::string("Skull.tga");

		osg::Image *image = osgDB::readImageFile(fileName);
		if (!image)
		{
			exit;
		}

		osg::TextureRectangle* texture = new osg::TextureRectangle(image);

		osg::TexMat* texmat = new osg::TexMat;
		texmat->setScaleByTextureRectangleSize(true);

		// setup state
		osg::StateSet* state = new osg::StateSet();
		state->addUniform(new osg::Uniform("skull_Texture", SKULL_TEXTURE));
		state->setTextureAttributeAndModes(SKULL_TEXTURE, texture, osg::StateAttribute::ON);
		state->setTextureAttributeAndModes(SKULL_TEXTURE, texmat, osg::StateAttribute::ON);

		state->setMode(GL_BLEND, osg::StateAttribute::ON);
	
	
		quadGeometry->setStateSet(state);
	}




Here I attech the shader

Code:

	// Shader-Logo
	{
		osg::StateSet* stateSet = quadGeometry->getOrCreateStateSet();

		mShaderProgramLogo = new osg::Program;
		mShaderProgramLogo->setName("ProgramLogo");
		mVertexShaderLogo = new osg::Shader(osg::Shader::VERTEX);
		mFragmentShaderLogo = new osg::Shader(osg::Shader::FRAGMENT);

		mShaderProgramLogo->addShader(mFragmentShaderLogo);
		mShaderProgramLogo->addShader(mVertexShaderLogo);

		mVertexShaderLogo->loadShaderSourceFromFile("Shader\\vertLogo.txt");
		mFragmentShaderLogo->loadShaderSourceFromFile("Shader\\fragmentLogo.txt");
		stateSet->setAttributeAndModes(mShaderProgramLogo, osg::StateAttribute::ON);
		
		quadGeometry->setStateSet(stateSet);
	}





and here the fragment-shader code:

Code:

#version 330

uniform sampler2D skull_Texture;

varying vec2 textCoord;

void main(void)
{
    vec4 colSkull = texture2D(skull_Texture, textCoord);

	//gl_FragColor = vec4(1.0,0,0,1.0); // => this works fine
	gl_FragColor = colSkull;	// => every fragment is black!!!
}




Can anybody help my.
OSG is such a nice library...but why it doesn't work? :(

Thank you!

Cheers,
JoseMan[/code]

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







More information about the osg-users mailing list