如何将变量(不统一)浮点值从 C++ 传递到顶点着色器 OpenGL ES 2.0

Posted

技术标签:

【中文标题】如何将变量(不统一)浮点值从 C++ 传递到顶点着色器 OpenGL ES 2.0【英文标题】:How pass a variable ( not uniform) float value from c++ to a vertex shader OpenGL ES 2.0 【发布时间】:2014-06-11 08:15:17 【问题描述】:

如何从c++中发送到pour shader的值,我知道我可以使用uniform类型,但这并不有效,因为需要修改每个顶点中的值并且uniform是常量,我有看到有一种“in”和“out”但不支持OpenGL ES 2.0,你给我发一个例子,说明如何将这些值传递给vertex shader float,我发给他们我的部分代码我正在使用。

attribute float cppValue;
varying float valueV;

void main ()

valueV= cppValue;


exampleValueCpp float = 1;
glVertexAttribPointer (0, 1, GL_FLOAT, GL_FALSE, 0, & exampleValueCpp);

更新

我已经看到有一个函数“glVertexAttrib1f”和“glVertexAttrib1fv”来修改属性,但我认为它只能与 glBegin 和 glEnd 一起使用,并且这些函数在 OpenGLES 2.0 或更高版本中已过时,我是对的吗? , 是不是 Asher 可以向顶点着色器发送一个不恒定的值?

【问题讨论】:

【参考方案1】:

如果您希望每个顶点的每个浮点数都不同,那么您应该有一个数组来收集它们:

float* exampleValueCpp = new float[numVertex];
for(int i = 0;i<numVertex;i++)
    //fill values

glBindBuffer(GL_ARRAY_BUFFER,0);//use application memory
glVertexAttribPointer (0, 1, GL_FLOAT, GL_FALSE, 0, exampleValueCpp);

但是您也可以将其上传到 VBO 并像这样使用它:

glGenBuffers(1, &cppBuffer);
glBindBuffer(GL_ARRAY_BUFFER, cppBuffer);

float* exampleValueCpp = new float[numVertex];
for(int i = 0;i<numVertex;i++)
    //fill values


glBufferData(GL_ARRAY_BUFFER, numVertex*sizeof(float), exampleValueCpp, GL_STATIC_DRAW);
delete[] exampleValueCpp;
glVertexAttribPointer (0, 1, GL_FLOAT, GL_FALSE, 0, 0);//last parameter is offset into the buffer

【讨论】:

以上是关于如何将变量(不统一)浮点值从 C++ 传递到顶点着色器 OpenGL ES 2.0的主要内容,如果未能解决你的问题,请参考以下文章

使用 SSE 将浮点值从 Assembler DLL 返回到 C++

是否可以通过 Cython 通过引用将值从 Python 传递到 C++?

如何将值从python传递给c++并返回?

如何将值从javascript变量传递给剃刀变量?

如何将变量的值从java类传递到jsp页面

在 Tensorflow C++ 中将浮点向量传递给张量