Hi,

i have a small problem. I want to access gl_TextureMatrix[8] within my Shader.

As you can see i used a loop to setup 10 texture matrices:

Code:

        for (int i = 0; i < 10; i++) {
                stateSet->setTextureAttributeAndModes(i, texMat,
                                osg::StateAttribute::ON);
        }




But when i try to access gl_TextureMatrix[8] not the matrix i was hoping.

I checked some build in limits for example:
GL_MAX_TEXTURE_UNITS is 34018.

Just change the used  gl_TextureMatrix[n] to n=8 to see that no texture matrix 
is returned.



Code:

#include <osg/Group>
#include <osg/Geode>
#include <osgViewer/Viewer>
#include <osgGA/TrackballManipulator>
#include <osgViewer/CompositeViewer>
#include <osg/ShapeDrawable>
#include <osg/Node>
#include <osg/MatrixTransform>
#include <osg/TexMat>
#include <osgViewer/Viewer>
#include <osg/Texture2DArray>
#include <osgDB/ReadFile>
#include <string>
#include <stdio.h>
#include <iostream>

using namespace osg;

osg::TexMat* texMat = new osg::TexMat;
osgViewer::View* viewB = new osgViewer::View;


void applyShader(Geode* geode) {
        ref_ptr<Program> program = new Program;
        program->setName("TexturesTest");
        ref_ptr<Shader> vertObj = new Shader(Shader::VERTEX);
        ref_ptr<Shader> fragObj = new Shader(Shader::FRAGMENT);

        program->addShader(vertObj.get());
        program->addShader(fragObj.get());

        ref_ptr<StateSet> stateSet = new osg::StateSet;
        ref_ptr<Uniform> texturesUniform = new 
Uniform(Uniform::SAMPLER_2D_ARRAY,
                        "textures", 1);
        stateSet->addUniform(texturesUniform.get());
        stateSet->setAttributeAndModes(program.get(), StateAttribute::ON);

        ref_ptr<Texture2DArray> textureArray = new Texture2DArray;
        //textureArray->setInternalFormat(GL_RGBA16F_ARB);
        //textureArray->setSourceFormat(GL_RGBA);
        //textureArray->setSourceType(GL_FLOAT);


        textureArray->setFilter(osg::Texture2DArray::MIN_FILTER,
                        osg::Texture2DArray::NEAREST);
        textureArray->setFilter(osg::Texture2DArray::MAG_FILTER,
                        osg::Texture2DArray::NEAREST);
        textureArray->setWrap(osg::Texture::WRAP_S, 
osg::Texture::CLAMP_TO_BORDER);
        textureArray->setWrap(osg::Texture::WRAP_T, 
osg::Texture::CLAMP_TO_BORDER);
        textureArray->setWrap(osg::Texture::WRAP_R, 
osg::Texture::CLAMP_TO_BORDER);

        textureArray->setTextureDepth(2);

        textureArray->setImage(0, osgDB::readImageFile("src/foo2.jpg"));
        textureArray->setImage(1, osgDB::readImageFile("src/foo2.jpg"));

        stateSet->setTextureAttribute(0, textureArray.get(),
                        osg::StateAttribute::ON);
        for (int i = 0; i < 10; i++) {
                stateSet->setTextureAttributeAndModes(i, texMat,
                                osg::StateAttribute::ON);
        }

        std::string vertSource = "varying vec4 vPos;"
                "void main()"
                "{"
                "vPos = gl_Vertex;"
                "gl_Position = ftransform();"
                "}";
        vertObj->setShaderSource(vertSource);

        std::string fragSource = "#version 120\n"
                "#extension GL_EXT_gpu_shader4 : enable\n"
                "#extension GL_EXT_texture_array : enable\n"
                "varying vec4 vPos;"
                "uniform sampler2DArray textures;"
                ""
                "void main()"
                "{"
                "vec4 coords = gl_TextureMatrix[7] * vPos;"
                "gl_FragColor = texture2DArray(textures, vec3(coords.st, 0));"
                "}";
        fragObj->setShaderSource(fragSource);

        geode->setStateSet(stateSet.get());
}

int main(int argc, char** argv) {
        //      setNotifyLevel(osg::DEBUG_INFO);

        // Create a cube
        ref_ptr<Group> rootNode = new Group;
        Box* cube = new Box(Vec3(0, 0, 0), 1.0f);
        ShapeDrawable* drawable = new ShapeDrawable(cube);
        Geode* geode = new Geode();
        geode->addDrawable(drawable);
        rootNode->addChild(geode);

        // Apply our shader to this cube
        applyShader(geode);

        osg::ArgumentParser arguments(&argc, argv);
        osgViewer::CompositeViewer viewer(arguments);

        viewer.addView(viewB);
        viewB->setUpViewInWindow(10, 650, 640, 480);
        viewB->setSceneData(rootNode.get());
        viewB->setCameraManipulator(new osgGA::TrackballManipulator);

        while (!viewer.done()) {

                osg::Matrix mat = viewB->getCamera()->getViewMatrix()
                                * viewB->getCamera()->getProjectionMatrix();
                mat = mat * osg::Matrix::scale(0.5, 0.5, 1) * 
osg::Matrix::translate(
                                0.5, 0.5, 0);
                texMat->setMatrix(mat);

                viewer.frame();
        }
        return 0;

}




Is there a buildin limit for texture matrices? Or am i doing something wrong 
about the setup of texture matrices? When iam using the 1-7 matrices everything 
works as expected but i need some more.

Thank you!

Cheers,
Johannes

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





_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to