Question

How to move object in world via glUniform**?

I tried glUniform3f(_positionSlot, 6.0f, 6.0f, -2.0f);

But my object not moving.

shader.vs:

attribute vec4 Position; // 1
attribute vec4 SourceColor; // 2
varying vec4 DestinationColor; // 3
uniform mat4 Projection;
uniform mat4 Modelview;

void main(void) { // 4
   DestinationColor = SourceColor; // 5
   gl_Position = Projection * Modelview *Position;

}

Reder:

- (void)render:(CADisplayLink*)displayLink {
    glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    CC3GLMatrix *projection = [CC3GLMatrix matrix];
    float h = 4.0f * self.frame.size.height / self.frame.size.width;
    [projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:10];
    glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);

    CC3GLMatrix *modelView = [CC3GLMatrix matrix];
    [modelView populateFromTranslation:CC3VectorMake(sin(CACurrentMediaTime()), 0, -7)];
    _currentRotation += displayLink.duration * 90;
    [modelView rotateBy:CC3VectorMake(_currentRotation, _currentRotation, 0)];
    glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);

    // 1
    glViewport(0, 0, self.frame.size.width, self.frame.size.height);

    // 2
    glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));

    glUniform4f(_colorSlot, 1.0f, 0.0f, 0.0f,1);
    glDrawElements(GL_TRIANGLE_STRIP, 14, GL_UNSIGNED_SHORT, indices);

    glUniform3f(_modelViewUniform, 6.0f, 6.0f, -2.0f);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer2);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    [_context presentRenderbuffer:GL_RENDERBUFFER];
}

EDIT:

_positionLocation=glGetAttribLocation(programHandle, "Translation");
glUniform3f(_positionLocation, -1.0f, 6.0f, -2.0f);
    glDrawElements(GL_TRIANGLE_STRIP, 14, GL_UNSIGNED_SHORT, indices);

    glUniform3f(_positionLocation, 1.0f, 6.0f, -2.0f);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer2);

If I draw only cube or only square all is ok. But seems that couple of it renders at same position and I see only cube. Seems that this method doesn't work. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

Was it helpful?

Solution

   gl_Position = Projection * Modelview * Position;

gl_Position is the vertex shader output that receives the transformed position of each vertex, not just the whole model. The "position slot" (varying/attribute) is used to pass vertex attribute data, namely each vertex position to the shader. You cannot apply a uniform to an attribute or varying. You need some additional uniform (let's call it "translation"):

attribute vec4 Position; // 1
attribute vec4 SourceColor; // 2
varying vec4 DestinationColor; // 3
uniform mat4 Projection;
uniform mat4 Modelview;
uniform vec4 Translation;

void main(void) { // 4
   DestinationColor = SourceColor; // 5
   gl_Position = Projection * Modelview * (Position + Translation);
}

Which you can set using with something like

glUniform3f(positionLocation, 6.0f, 6.0f, -2.0f);

BTW: Those are not called "slots", they're called "Locations".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top