35754

Trying to load an OSG model with textures allocated in textureUnit1

Question:

I want to render an Openscenegraph model with textures under two constraints:

1) Using shaders (opengles20) 2) Uploading the textures to the textureUnit1 on the GPU (NO the default textureUnit0)

I thought I was doing right but still I am getting non textured models (only the mesh).

Here are the shaders (notice that I use gl_MultiTexCoord1):

static const char gVertexShader1[] = { "varying vec2 texCoords;\n" "void main()\n" "{\n" " texCoords = gl_MultiTexCoord1.st;\n" " gl_Position = ftransform();\n" "}\n" }; static const char gFragmentShader1[] = { "varying vec2 texCoords;\n" "uniform sampler2D tex;\n" "void main()\n" "{\n" " gl_FragColor = texture2D(tex, texCoords);\n" "}\n" };

The loaded osg model also specifies texture unit 1 in all the texture images, for instance:

textureUnit 1 { GL_TEXTURE_2D ON Texture2D { UniqueID Texture2D_1 file "/storage/sdcard0/osg/textures/p51d-jw-05.png" wrap_s REPEAT wrap_t REPEAT wrap_r CLAMP min_filter LINEAR_MIPMAP_LINEAR mag_filter LINEAR maxAnisotropy 1 borderColor 0 0 0 0 borderWidth 0 useHardwareMipMapGeneration TRUE unRefImageDataAfterApply TRUE internalFormatMode USE_IMAGE_DATA_FORMAT resizeNonPowerOfTwo TRUE }

And finally the C++ code:

//Load model osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(newModel.filename); //Assign program osg::ref_ptr<osg::StateSet> ss = loadedModel->getOrCreateStateSet(); osg::Shader * vshader = new osg::Shader(osg::Shader::VERTEX, gVertexShader1 ); osg::Shader * fshader = new osg::Shader(osg::Shader::FRAGMENT, gFragmentShader1 ); osg::Program * prog = new osg::Program; prog->addShader ( vshader ); prog->addShader ( fshader ); ss->setAttributeAndModes(prog); //Uniforms osg::ref_ptr<osg::Texture2D> bodyTexture = new osg::Texture2D; bodyTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); bodyTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); bodyTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); bodyTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); ss->setTextureAttributeAndModes(1, bodyTexture.get()); ss->addUniform(new osg::Uniform("tex", 1));

Any idea?

Thanks,

JM

Answer1:

The way you set the texture to tex unit 1 is correct, are you sure the model you're loading contains texture coordinates at index 1 as well?

Just make sure there are such tex coords, otherwise try reusing a different tex coord index like gl_MultiTexCoord0

Recommend

  • Simple color GLSL shader doesn't output anything
  • No uniform with name in shader
  • Italicise text in latex figure caption produced by knitr
  • How does dot syntax work without explicit @property in Objective-C?
  • Package inputenc Error & Error: pandoc document conversion failed with error 43
  • how to add tex to octave graph
  • how to concatenate two strings in iphone
  • How to get screenshot saved path by Cocos2d-x on android
  • Save a pandas dataframe as table in Image or pdf document with nice multi index display
  • how to add tex to octave graph
  • SDL OpenGL in C++, Texture Shader missing the texture
  • Game development: OpenGlContext error issue
  • Error producing PDF from TeX source Error: pandoc document conversion failed with error 43 Execution
  • XNA.Texture2D to System.Drawing.Bitmap
  • Octave: LaTeX tics
  • GLSL textureCube and texture2D in same shader
  • PyQt: Create QPixmap with alpha channel and not premultiplied color channels
  • xtable output for a list of tables
  • Shader attribute mat4 not binding correctly (Opengl ES 2.0 Android)
  • iPhone OpenGL ES texture unit
  • Black Screen when rendering with Oculus SDK
  • Is the scope of a variable initialized in a for loop declaration actually more than just block scope
  • Android multuple camera preview
  • libgdx clickable image not working
  • Java Date object constructor for getting string is deprecated
  • LyX — Title, Abstract, Keywords Missing When Using Elsevier Class in View
  • LibGdx GLES2.0 cube texel stretching
  • Error in GGally: Error in unit(tic_pos.c, “mm”) : 'x' and 'units' must have leng
  • Using HTML/CSS for UI in XNA?
  • Git describe fails to return most recent annotated tag
  • HTTP/2 streams vs HTTP/1.1 connections
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Syntax for setting draggablecursor property in google maps api
  • Asynchronous UI Testing in Xcode With Swift
  • Highlight one bar in a series in highcharts?
  • Illegal mix of collations for operation for date/time comparison
  • GridView Sorting works once only
  • Calling of Constructors in a Java
  • PHP: When would you need the self:: keyword?
  • How to get icons for entities from eclipse?