在顶点着色器中使用两个Uniforms

时间:2015-06-18 15:49:06

标签: opengl glsl buffer shader uniform

遵循OpenGL RedBook并且能够使用Uniform用于截头或转换矩阵。我试图同时使用两者都失败了。

//Declarations
GLfloat frustum[4][4] = {
    {((2.0*frusZNe //Etc...
GLfloat translate[4][4] = {
    {1.0, 0.0,     //Etc...

然后我在Init中使用它:

GLuint uboIndex;
GLint uboSize;
GLuint ubo;
GLvoid *buffer;

uboIndex = glGetUniformBlockIndex(program, "frustumUniform");
glGetActiveUniformBlockiv(program, uboIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &uboSize);
buffer = malloc(uboSize);

const char* frustumName[1] = {"frustum"};

GLuint indices[1];
GLint size[1];
GLint offset[1];
GLint type[1];
glGetUniformIndices(program, 1, frustumName, indices);
glGetActiveUniformsiv(program, 1, indices, GL_UNIFORM_OFFSET, offset);
glGetActiveUniformsiv(program, 1, indices, GL_UNIFORM_SIZE, size);
glGetActiveUniformsiv(program, 1, indices, GL_UNIFORM_TYPE, type);

//memcpy(&buffer + 0, &frustum, sizeof(frustum));

glGenBuffers(1, &ubo);
glBindBuffer(GL_UNIFORM_BUFFER, ubo);
glBufferData(GL_UNIFORM_BUFFER, uboSize, &frustum, GL_STATIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, uboIndex, ubo);

之后,我立即对翻译做了同样的事情,但更改了一些值,例如:glGetUniformBlockIndex(program,“translateUniform”);

///Translate Uniform, same 20 statements as above, with alterations
GLuint uboIndex2;
GLint uboSize2;
GLuint ubo2;
GLvoid *buffer2;



uboIndex2 = glGetUniformBlockIndex(program, "translateUniform");
glGetActiveUniformBlockiv(program, uboIndex2, GL_UNIFORM_BLOCK_DATA_SIZE, &uboSize2);
buffer2 = malloc(uboSize2);

string offsetString = iToS(uboIndex2);
debugMsg(offsetString + "Index2, 1:");

const char* translateName[1] = {"translate"};

GLuint indices2[1];
GLint size2[1];
GLint offset2[1];
GLint type2[1];

glGetUniformIndices(program, 1, translateName, indices2);
glGetActiveUniformsiv(program, 1, indices2, GL_UNIFORM_OFFSET, offset2);
glGetActiveUniformsiv(program, 1, indices2, GL_UNIFORM_SIZE, size2);
glGetActiveUniformsiv(program, 1, indices2, GL_UNIFORM_TYPE, type2);

//memcpy(&buffer + 0, &frustum, sizeof(frustum));



glGenBuffers(1, &ubo2);
glBindBuffer(GL_UNIFORM_BUFFER, ubo2);
glBufferData(GL_UNIFORM_BUFFER, uboSize2, &translate, GL_STATIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, uboIndex2, ubo2);

string uboIndex2S = iToS(uboIndex2);

debugMsg("Index 2: " + uboIndex2S);

string uboSizeS, uboSizeS2, uboIndexS, uboIndexS2;

uboSizeS = iToS(uboSize);
uboSizeS2 = iToS(uboSize2);
uboIndexS = iToS(uboIndex);
uboIndexS2 = iToS(uboIndex2);


debugMsg(uboSizeS + " ," + uboSizeS2 + " ," + uboIndexS + " , " + uboIndexS2);

顶点着色器:

#version 430 core



uniform frustumUniform {
    mat4 frustum;
};

uniform translateUniform {
    mat4 translate;
};

layout(location = 0) in vec4 vPosition;
void
main()
{

vec3 trans = vec3(.2, 0, 0);

mat4 view = mat4(1.0, 0.0, 0.0, 0.0,
        0.0, 1.0, 0.0, 0.0,
        0.0, 0.0, 1.0, 0.0,
        0.0, 0.0, -200.0, 1.0);

mat4 rotate = mat4(0.2, 0.0, 0.3, 0.0,
        0.0, 1.0, 0.0, 0.0,
        -0.3, 0.0, 0.2, 0.0,
        0.0, 0.0, 20.0, 1.0);


mat4 translateOld = mat4(1.0, 0.0, 0.0, 0.0,
        0.0, 1.0, 0.0, 0.0,
        0.0, 0.0, 1.0, 0.0,
        0.0, 0.0, 0.0, 1.0);



gl_Position = frustum * translate * view * vPosition;
}

它编译,但第二个Uniform不再有效。有什么想法吗?

0 个答案:

没有答案