Why not show textures when rendering geometry with PrimitiveSet::TRIANGLES using Shader330 version?

19 views
Skip to first unread message

mirr...@gmail.com

unread,
Dec 4, 2020, 3:42:44 AM12/4/20
to OpenSceneGraph Users
system Win10& GTX1660 ,osg 3.6.4, osgearth 2.10.0 


QQ图片20201204163639.jpg    
static const char* gl3_VertexShader = {
    "#version 330 core\n"
    "// gl3_VertexShader\n"
    "#ifdef GL_ES\n"
    "    precision highp float;\n"
    "#endif\n"
    "in vec4 osg_Vertex;\n"
    "in vec4 osg_MultiTexCoord0;\n"
    "uniform mat4 osg_ModelViewProjectionMatrix;\n"
    "out vec2 texCoord;\n"
    "void main(void)\n"
    "{\n"
    "    gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex;\n"
    "    texCoord = osg_MultiTexCoord0.xy;\n"
    "}\n"
};

static const char* gl3_FragmentShader = {
    "# version 330 core "\n"
    "// gl3_FragmentShader\n"
    "#ifdef GL_ES\n"
    "    precision highp float;\n"
    "#endif\n"
    "uniform sampler2D baseTexture;\n"
    "in vec2 texCoord;\n"
    "out vec4 color;\n"
    "void main(void)\n"
    "{\n"
    "    color = texture(baseTexture, texCoord);\n"
    "}\n"
};


    Geometry* geom = new Geometry;

    Vec3Array* coords = new Vec3Array(4);
    (*coords)[0] = corner+heightVec;
    (*coords)[1] = corner;
    (*coords)[2] = corner+widthVec;
    (*coords)[3] = corner+widthVec+heightVec;
    geom->setVertexArray(coords);

    Vec2Array* tcoords = new Vec2Array(4);
    (*tcoords)[0].set(l,t);
    (*tcoords)[1].set(l,b);
    (*tcoords)[2].set(r,b);
    (*tcoords)[3].set(r,t);
    geom->setTexCoordArray(0,tcoords);

    osg::Vec4Array* colours = new osg::Vec4Array(1);
    (*colours)[0].set(1.0f,1.0f,1.0,1.0f);
    geom->setColorArray(colours, osg::Array::BIND_OVERALL);

    osg::Vec3Array* normals = new osg::Vec3Array(1);
    (*normals)[0] = widthVec^heightVec;
    (*normals)[0].normalize();
    geom->setNormalArray(normals, osg::Array::BIND_OVERALL);

    DrawElementsUByte* elems = new DrawElementsUByte(PrimitiveSet::TRIANGLES);
    elems->push_back(0);
    elems->push_back(1);
    elems->push_back(2);

    elems->push_back(2);
    elems->push_back(3);
    elems->push_back(0);
    geom->addPrimitiveSet(elems);

    osg::ref_ptr<osg::Image> image = new osg::Image;
    image->allocateImage(1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE);
    image->setColor(osg::Vec4(1.0,1.0,1.0,1.0), 0, 0, 0);

    osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D(image.get());

    osg::ref_ptr<osg::Program> program = new osg::Program;
    program->addShader(new osg::Shader(osg::Shader::VERTEX, gl3_VertexShader));
    program->addShader(new osg::Shader(osg::Shader::FRAGMENT,          gl3_FragmentShader));
    setAttributeAndModes(program.get());
    setTextureAttribute(0,  texture);
    addUniform(new osg::Uniform("baseTexture", 0));

OpenSceneGraph Users

unread,
Dec 4, 2020, 6:37:28 PM12/4/20
to OpenSceneGraph Users
I doubt anyone will be able to help given the information.  You are diving into providing snippet of code and a screenshot with almost no context.

It's far better to take a step back and explain what you are doing with your application from a high level to give the context, then explain what you are trying to do and what looks to be going wrong.  Describe what you've looked so far.  Providing some background on your level of experience with the OSG/osgEarth/OpenGL etc. would be useful for.  

Once others know a bit more about the issue and what type of support you might need we'll be to start providing directions.  However, if the issues you are seeing are presented in a way that is next to impossible to know how to answer you shouldn;t expect answers as we don't have magical powers.

mirr...@gmail.com

unread,
Dec 4, 2020, 10:50:05 PM12/4/20
to OpenSceneGraph Users

My working environment is closed and I cannot provide complete code.

Does the geometry of triangles primitives rendered in Shader 330 version lack any properties?

OpenSceneGraph Users

unread,
Dec 5, 2020, 4:22:22 AM12/5/20
to OpenSceneGraph Users
On Sat, 5 Dec 2020 at 04:31, OpenSceneGraph Users <osg-...@lists.openscenegraph.org> wrote:

My working environment is closed and I cannot provide complete code.

I'm not asking for code or data, just a high level explanation as outline in my previous email.

Does the geometry of triangles primitives rendered in Shader 330 version lack any properties?

No. 

The problem is unlikely to be anything to do with triangles, but some higher level issue about how the scene is constructed and and what features you are attempting to use,

You've said nothing about the wider context.

mirr...@gmail.com

unread,
Dec 9, 2020, 8:55:59 PM12/9/20
to OpenSceneGraph Users
   i find this setUseVertexAttributeAliasing  must be set in Osg when using higher versions of Shader.  Otherwise it won't work

  gc->getState()->setUseVertexAttributeAliasing(true);
Reply all
Reply to author
Forward
0 new messages