[osg-users] Why not show textures when rendering geometry with PrimitiveSet::TRIANGLES using Shader330 version?

OpenSceneGraph Users osg-users at lists.openscenegraph.org
Fri Dec 4 00:42:43 PST 2020


system Win10& GTX1660 ,osg 3.6.4, osgearth 2.10.0 


[image: 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));

-- 
You received this message because you are subscribed to the Google Groups "OpenSceneGraph Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to osg-users+unsubscribe at googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/osg-users/79f7c364-d90f-4462-890d-e7ec3fbfc7e5n%40googlegroups.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20201204/23f4194d/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: QQ??20201204163639.jpg
Type: image/jpeg
Size: 466768 bytes
Desc: not available
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20201204/23f4194d/attachment-0001.jpg>


More information about the osg-users mailing list