osg传递着色器变量

> I tried using a different attribute index from 0, and the colours are now showing up. I had come across the link earlier that you posted, but didn't realise that was how OpenGL worked (I tested it out in an OpenGL app and got the same problem as in my OSG app, with the slightly deformed mesh and no colours).
>
> So if I use custom attributes and no built in ones, then I shouldn't have this problem of overwriting existing attributes, which I also tested in the OpenGL app, and it allowed me to use 0 for a vertex attrib, and 1 for a colour attrib. So that's what I want to do now in my OSG app. But I cannot get it to work, it just renders nothing. I have tried different attribute values (from '0 and 1', '10 and 11', to '0 and 11'), but to no avail.
>
> I've posted what I believe is the relevant code, maybe I didn't set the vertex data right in the geometry. The code here currently renders nothing.
>
>
> Code:
>
> GLuint vertLoc = 0;
> GLuint colLoc = 1;
>
> //
> osg::Geode* pyramidGeode = new osg::Geode();
> root->addChild(pyramidGeode);
>
> //geometry
> osg::Geometry* pyramidGeometry = new osg::Geometry();
> pyramidGeode->addDrawable(pyramidGeometry);
>
> //vertices
> osg::Vec3Array* pyramidVertices = new osg::Vec3Array;
>
> for(int i=0;i<model->verticesNum;i++) {
> float x = model->positions[i*3];
> float y = model->positions[i*3+1];
> float z = model->positions[i*3+2];
> pyramidVertices->push_back(osg::Vec3(x,y,z));
> }
>
>
> //pyramidGeometry->setVertexArray(pyramidVertices);
>
> program->addBindAttribLocation("a_vertex", vertLoc);
> pyramidGeometry->setVertexAttribArray(vertLoc,pyramidVertices);
> pyramidGeometry->setVertexAttribBinding(vertLoc, osg::Geometry::BIND_PER_VERTEX);
> }
> //colours
> osg::Vec4Array* colors = new osg::Vec4Array;
>
> for(int i=0;i<model->verticesNum;i++) {
> float r = model->cols[i*4];
> float g = model->cols[i*4+1];
> float b = model->cols[i*4+2];
> float a = model->cols[i*4+3];
> colors->push_back(osg::Vec4(r,g,b,a));
> }
>
> program->addBindAttribLocation("a_col", colLoc);
> pyramidGeometry->setVertexAttribArray(colLoc, colors);
> pyramidGeometry->setVertexAttribBinding(colLoc, osg::Geometry::BIND_PER_VERTEX);
> }
>
> //indices
> osg::DrawElementsUInt* pyramidBase = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES,model->indicesNum,model->indices, 0);
> pyramidGeometry->addPrimitiveSet(pyramidBase);
>
>
>
>
> and the shaders
>
> Code:
>
> //vertex shader
>
>
> attribute vec3 a_vertex;
> attribute vec4 a_col;
>
>
> varying vec4 col;
>
> void main() {
> col = a_col;
>
> //gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex
> gl_Position = gl_ModelViewProjectionMatrix * vec4(a_vertex, 1.0);
>
> }
>
> //fragment shader
>
> varying vec4 col;
>
> void main() {
> gl_FragColor =col;
> }

转载于:https://www.cnblogs.com/lizhengjin/archive/2010/10/02/1841353.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值