Use two Uniforms in Vertex Shader

243 views Asked by At

was following the OpenGL RedBook and was able to use a Uniform for a frustrum or translate matrix. I have become a little lost trying to use both at the same time.

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

And then I use it in 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);

Immediately afterwards, I do the same thing for the translate one but change some of the values, such as: 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);

Vertex shader:

#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;
}

It compiles, but the second Uniform no longer works. Any ideas?

0

There are 0 answers