3

On my scene i have many objects that i want to rotate at the same time but on different angles.

I have a shader that computes position of each object and draw the whole scene (pass vertex array into the shader with array of vertexes).

    "uniform float uRotation;" +
    ...
    "   mat4 mz = mat4(1.0);" +
    "   mz[0][0] = cos(rotation);" +
    "   mz[0][1] = sin(rotation);" +
    "   mz[1][0] = -sin(rotation);" +
    "   mz[1][1] = cos(rotation);" +
    ...
    gl_Position = uMVPMatrix * (aPosition *mz);

i have all my vertexes, indexes, colors etc in arrays (not 1 array for each object, but 1 array with offset for vertexes, one for colors etc.).

I want to pass array of floats (angles for each "object") in one float[], but nothing is happinig.

    public void setRotations(float[] rotations)
    {
        GLES20.glUniform3fv(muRotation, rotations.length, rotations, 0);    
    }

if pass and use only one float everything is ok.

How can i pass specific float for each object (for example i want to draw 20 rectangles (i have a vetrex array: float[20*3*4]) as i understand i must have float for each vertex: rotations: float[20*4])?

UPDATED:

I try to pass array as a texture.

When i create resources for the scene - i try to create my array (called mask):

    mask = new float[512*512*4];
    for (int i = 0; i < mask.length; i++)
    {
        mask[i] = 2f;
    }

    GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 1029384756);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 1, GLES20.GL_RGBA, 512, 512, 0, GLES20.GL_RGBA, GLES20.GL_FLOAT, FloatBuffer.wrap(mask));
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

Then when i draw new item on scene i use shader and try to pass my array:

    setShaderProgram(ShaderProgram.getInstance());

    GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
    GLES20.glUniform1i(ShaderProgram.sUniformMask, GLES20.GL_TEXTURE1);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 1029384756);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

But when i try to check any value from the array if it >0.5 every computation gives me false

UPDATED

Fragment shader contains:

"uniform sampler2D " + ShaderProgramConstants.UNIFORM_TEXTURE_0 + ";\n" +
"uniform sampler2D " + ShaderProgramConstants.UNIFORM_TEXTURE_1 + ";\n" +
"varying mediump vec2 " + ShaderProgramConstants.VARYING_TEXTURECOORDINATES + ";\n" +

"void main() {\n" +
"vec4 color = texture2D(" + ShaderProgramConstants.UNIFORM_TEXTURE_0 + ", " + ShaderProgramConstants.VARYING_TEXTURECOORDINATES + ");\n" +
"vec4 mask = texture2D(" + ShaderProgramConstants.UNIFORM_TEXTURE_1 + ", " + ShaderProgramConstants.VARYING_TEXTURECOORDINATES + ");\n" +
"float maskVal = mask.r;\n" ...

color and mask textures have the same dimensions

6
  • You can pass your array as a one dimensional texture.You can also pass a block of uniforms as UBO but it won't help you much if your array is big.The best way is to fill a texture and send it over. Commented Jul 16, 2012 at 12:53
  • I've already have textures for each element, i can't understand what do you mean. Maybe you have some links for good tutorials ? Thx. Commented Jul 17, 2012 at 8:46
  • In fact you should check if ES supports one dimensional textures.You your array values into glTexImage1D​ and use target of type GL_TEXTURE_1D.Then you can read it in the shader.I write it based on OpenGL desktop API.Check if you can do it in ES. Commented Jul 17, 2012 at 9:11
  • What are you doing in your shader. Do you have a Sampler2D and a texture coordinate to sample the texture? Commented Aug 14, 2012 at 4:42
  • Shader is very simple (check edits) Commented Aug 14, 2012 at 8:06

1 Answer 1

3
+50

GLES20.glUniform1i(ShaderProgram.sUniformMask, GLES20.GL_TEXTURE1);

If this is your sampler uniform, it should read:

GLES20.glUniform1i(ShaderProgram.sUniformMask, 1); (not GL_TEXTURE1)

Similar for sampler0:

GLES20.glUniform1i(ShaderProgram.sUniformMask, 0);

Sign up to request clarification or add additional context in comments.

4 Comments

maybe smth like glGentexture is needed ?
There is an error 1282 (invalid operation) when i try to GLES20.glUniform1i(ShaderProgram.sUniformMask, 1);
@dilix Were you able to solve the error? Make sure your sUniformMask has a valid value, and that a valid shader is bound. There are several reasons why you can get an invalid operation, check the reasons at the bottom of this page
Yes, the trouble was i try to bid my unifore BEFORE i bin the fragment shader , thx =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.