Matrix Palette, Drawing Issue when ModelView Matrix - android

Using OpenGL 1.1 and the Matrix Palette extension. The issue I'm having, is that not every model being loaded needs to be animated, so I don't think that I need to enable those client states nor provide weights or weight index arrays. For example, I'm trying something like this during my drawing code...
glMatrixMode(GL_MATRIX_PALETTE_OES);
glBindBuffer(GL_ARRAY_BUFFER, dataBuffers[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dataBuffers[1]);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_WEIGHT_ARRAY_OES);
glEnableClientState(GL_MATRIX_INDEX_ARRAY_OES);
//Code to modify the palettes... works fine...
for(i = 0; i < mech.boneCount; ++i){
glCurrentPaletteMatrixOES(i);
glLoadPaletteFromModelViewMatrixOES();
GenerateBoneMatrixPalette(bones, i);
}
glVertexPointer(3, GL_FLOAT, sizeof(VertexData), (char*)(NULL + 0));
glNormalPointer(GL_FLOAT, sizeof(VertexData), (char*)(NULL + 12));
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(VertexData), (char*)(NULL + 24));
glWeightPointerOES(1, GL_FLOAT, sizeof(VertexData), (char*)(NULL + 28));
glMatrixIndexPointerOES(1, GL_UNSIGNED_BYTE, sizeof(VertexData), (char*)(NULL + 32));
glDrawElements(GL_TRIANGLES, mech.indexsize, GL_UNSIGNED_SHORT, (char*)(NULL + 0));
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_WEIGHT_ARRAY_OES);
glDisableClientState(GL_MATRIX_INDEX_ARRAY_OES);
glBindBuffer(GL_ARRAY_BUFFER, dataBuffers[2]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dataBuffers[3]);
glMatrixMode(GL_MODELVIEW);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
//pardon the hard 28 value here, it's the correct offset for this test
glVertexPointer(3, GL_FLOAT, 28, (char*)(NULL + 0));
glNormalPointer(GL_FLOAT, 28, (char*)(NULL + 12));
glColorPointer(4, GL_UNSIGNED_BYTE, 28, (char*)(NULL + 24));
glDrawElements(GL_TRIANGLES, indexsize, GL_UNSIGNED_SHORT, (char*)(NULL + 0));
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
But it is not working. The first section displays correctly but the second does not display at all. If I add an, what feels to be, unnecessary weight and weight index element to the second section modifying the stride as needed and not setting the matrix to the modelview... then it displays what I expect.
The test that feels the strangest, is if I do not enable the Matrix Palette extension at all and only draw the second part, it works just fine. However, just enabling the Matrix Palette extension causes the second section to not work at all, making it seem that I can not draw simply while the mode is set to the ModelView Matrix... though this seems somewhat unusual as the ModelView still absolutely is affected by transformations.
So... is it possible to switch to and draw while the ModelView is the current matrix while using this extension? Or must I make use/reuse a single palette to make it work.

I haven't found an OES matrix palette extension in the extension registry, but an ARB extension and suppose it works similar. In this extension, you have to enable GL_MATRIX_PALETTE and or GL_VERTEX_BLEND (with glEnable) to use matrix palette skinning and disable it to not use it.
But the glMatrixMode does't have anything to do with enabling or disabling it. It just selects the matrix to which matrix modification functions apply (like glLoadIdentity, glTranslate, ...).
EDIT: After googling this extension (I have no ES experience) I found, that you definitely have to enable GL_MATRIX_PALETTE_OES to use it (via glEnable) and then disable it again for your second part to not use it. As I've written above, glMatrixMode doesn't do what you thought it to.

Related

OpenGL ES 2.0: Performance drop using bindbuffer frequently

I have a performance problem in my opengl 2.0 game. Framerate was good until I made a change in the game. Its some sort of outbreak game with 64 shapes (bricks). What I now want is when the ball hits a brick its not immediately removed - it changes status and that includes changing the texture or more correctly - the uv-coord of the atlas. I have a textureatlas and what I do is just to call GLES20.bindBuffer() for every texture in the loop, instead of calling the outside the loop. Earlier I had the same uv-coord for all shapes but now I change it depending on the bricks status and thats why I need to use binding inside the loop
private void drawShape() {
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboDataListLevelSprites.get(iName).getBuff_id_vertices());
GLES20.glEnableVertexAttribArray(mPositionHandle);
GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, 0, 0);
//used this snipped before when using the same image (uv-coords) for all bricks
/*GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboDataListLevelSprites.get(iName).getBuff_id_uvs());
GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);
GLES20.glVertexAttribPointer(mTextureCoordinateHandle, 2, GLES20.GL_FLOAT, false, 0, 0);
*/
for (Iterator<BrickProperties> it = arrayListBricks.iterator(); it.hasNext(); ) {
BrickProperties bp = it.next();
//now bindbuffer inside loop just too switch uv-coords of the atlas when its time to use another image
int buffIndexVal = bp.get_status_diff();
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, BrickProperties.get_buff_id_uvs()[buffIndexVal]);
GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);
GLES20.glVertexAttribPointer(mTextureCoordinateHandle, 2, GLES20.GL_FLOAT, false, 0, 0);
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, bp.getTranslateData()[0], bp.getTranslateData()[1], bp.getTranslateData()[2]);
if (bp.get_status() == 0) {
it.remove();
}
render();
}
}
private void render() {
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);
}
I understand the reason to the performance drop is all the bindbuffer calls to the GPU but how could I possibly get around this problem?
It is one thing that you are binding a buffer for every object but then there is another thing you are using 2 buffers to draw a single object. You also have redundant call to unbind the buffer at the start of your render method, simply remove that.
In most cases (may be all cases) you want interleaved vertex data for increased performance. So use
{
position,
textureCoordinates
}
in a single buffer.
I see in your case you have 2 states of the same object where the second one will change vertex coordinates but not position coordinates. It might make sense to share the position data between the two if the buffer is relatively large (which I assume is not). Anyway for such sharing I would suggest you rather use your buffer structure as
{
position,
textureCoordinates,
secondaryTextureCoordinates
}
then use a separate buffer or even to put the secondary texture coordinates to another part of the same buffer.
So if the vertex buffers are relatively small then I suggest you to use "atlas" procedure. For your case that would mean creating twice the size of the buffer and put all the coordinates (having position duplicated) and put this vertex data so that there is one part after another.
I assume you can easily do that for your current drawing and effectively reduce the number of bound buffers to 0 per draw call (you only ned to bind it at some initialization). Now you will have the second part where you will set the attribute pointers for each of the drawn element just so that you may control which texture coordinates are used. This will again present redundant calls which may be avoided in your case:
Since the data structure is consistent in your buffer there is really no reason to set the pointers more the once. Simply set them once to the beginning when buffer is bound and then use the offset in draw call to control what part of the buffer is actually used GLES20.glDrawArrays(GLES20.GL_TRIANGLES, offset, 6).
If done properly your draw method should looks something like:
for (Iterator<BrickProperties> it = arrayListBricks.iterator(); it.hasNext(); ) {
BrickProperties bp = it.next();
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.translateM(mModelMatrix, 0, bp.getTranslateData()[0], bp.getTranslateData()[1], bp.getTranslateData()[2]);
if (bp.get_status() == 0) {
it.remove();
}
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, bp.vertexOffset, 6);
}
This removes all the bindings and preserves only matrix operations and draw calls. If there are other drawings in the pipeline you need the buffer binding before the loop otherwise you may put it as a part of the initialization. In both cases the calls should be reduced significantly.
To add a note here it is a common practice to have another object that tracks the openGL states to avoid redundant calls. Instead of calling GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, bufferID.. you would rather call something like contextObject.bindVertexBuffer(bufferID) which would check if the bufferID is the same as in previous call. And if it is then no actual binding would be done. If you create and use such system then it makes little difference on where you call the buffer binding and rest of the object setup since redundant calls will have no effect. Still this procedure alone will not make your situation optimal so you still need both.
You could bind buffer and draw all the objects with the same UV-s at once. Instead of iterating through each brick, iterate through all objects that use the same UV-s.
Also, try batching the objects so that you draw them all at once. Using Index Buffer Objects may help in this.

Starfield optimization in libgdx

I want to create a static starfield in libgdx.
My first way was: create a Decal and a DecalBatch over it.
When I draw the Decal I use a Billboarding technic on the Decal
star.decal.setRotation(camera.direction, camera.up);
next: I wanted to animate the alphas on the decals, so I created on a random way some time:
star.decal.setColor(1, 1, 1, 0.6f+((float) Math.random()*0.4f) );
It is working, but my FPS went down from 55 FPS to 25 FPS (because of my 500-1000 stars)
Can I use only one batch call in any way? Maybe a particleMaterial with only one Vertex list and with a GL_POINT mode that is always face to front of my camera?
How can I do this in libgdx?
The Batch is way to complex than what you need , on every frame it needs to copy all the vertices of the sprites in another array and do calculations on them to find the scale rotation etc..
As you suspect GL_POINT sprites will be way faster and in a medium range device it should be able to render in 60 fps like 2000 points that have different position and color
here is some old code of mine ,its in c and it uses opengl es 1.1 and propably there will be a more simple way to do it in libgdx
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnable (GL_POINT_SPRITE_OES);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, TXTparticle);
glTexEnvi(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);
glPointSize(30);
glColorPointer(4, GL_FLOAT, 32, particlesC);//particlesC the vertices color
glVertexPointer(3, GL_FLOAT, 24, particlesV);//particlesV the vertices
glDrawArrays(GL_POINTS, 0, vertvitLenght/6);
glDisable( GL_POINT_SPRITE_OES );
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

OpenGL es 2.0 glDrawElements index pointer error

Im having trouble with texturing a cube with different textures per face. I can draw the cube with one texture on all the faces, but when I try use multiple textures it fails. The way im trying to do it is like so:
//my indexing array located in a header file
#define NUM_IMAGE_OBJECT_INDEX 36
static const unsigned short cubeIndices[NUM_IMAGE_OBJECT_INDEX] =
{
0, 1, 2, 2, 3, 0, // front
4, 5, 6, 6, 7, 4, // right
8, 9,10, 10,11, 8, // top
12,13,14, 14,15,12, // left
16,17,18, 18,19,16, // bottom
20,21,22, 22,23,20 // back
};
now in my rendering function, this currently works for drawing the cube with a single texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, iconTextureID);
glDrawElements(GL_TRIANGLES, NUM_IMAGE_OBJECT_INDEX, GL_UNSIGNED_SHORT, 0);
this does not work
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, iconTextureID);
glDrawElements(GL_TRIANGLES, NUM_IMAGE_OBJECT_INDEX, GL_UNSIGNED_SHORT, (const GLvoid*)&cubeIndices[0]);
which should equate to the same thing, from looking at some other examples. Ultimately I would like to be doing this something like this:
for(int i = 0; i < 6; i++){
iconTextureID = textureID[i];
glBindTexture(GL_TEXTURE_2D, iconTextureID);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (const GLvoid*)&cubeIndices[i*6]); //index 0-5 use texture 1, 6-11 use texture 2, etc
}
does anyone know what could be wrong with this indexing? ive basically copy pasted this code from an android project (which works), currently trying to do this on ios.
In OpenGL ES 2.0, index data can come from either buffer objects or pointers to client memory. Your code is obviously using a buffer object. Though you don't show the creation of this buffer object, where you upload your client array of pointers, or where you call glBindBuffer(GL_ELEMENT_ARRAY_BUFFER) before rendering with it. It must be there or your code would have crashed. When a buffer is bound to GL_ELEMENT_ARRAY_BUFFER, OpenGL expects the "pointer" given to glDrawElements to be a byte offset into the buffer object, not a client-memory pointer.
This is why copy-and-paste coding is a bad idea. Where you copied from was probably using client memory; you are not.
If you want your looping code to work, you need to do the pointer arithmetic yourself:
for(int i = 0; i < 6; i++)
{
iconTextureID = textureID[i];
glBindTexture(GL_TEXTURE_2D, iconTextureID);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, reinterpret_cast<void*>(i * 6 * sizeof(GLushort)));
}

How to draw/render a Bullet Physics collision body/shape?

I have implemented the Bullet Physics engine into my android program with the NDK (I am using Vuforia's imagetarget example for android), and it is set up and working correctly, however I would like to render/draw my collision boxes/planes to see my rigid bodies (btRigidBody)/collision shapes (btCollisionShape), I'm positive this is possible but I can't find any tutorials on how to do it!
I have taken the hello world Bullet physics tutorial on their wiki page and modified it to apply the transformations from the falling physics body to a 3d object I have in opengl es 2.0 to view the collision bodies, here is the code I am using to render to object:
void drawRigidBody(btRigidBody* body,QCAR::Matrix44F modelViewMatrix, unsigned int textureID)
{
btTransform trans;
body->getMotionState()->getWorldTransform(trans);
LOG("sphere pos: (x %f , y %f, z %f)",trans.getOrigin().getX(),trans.getOrigin().getY(),trans.getOrigin().getZ());
float physicsMatrix[16];
trans.getOpenGLMatrix(physicsMatrix);
SampleUtils::scalePoseMatrix(kObjectScale, kObjectScale, kObjectScale,
&modelViewMatrix.data[0]);
QCAR::Matrix44F modelViewProjection, objectMatrix;
SampleUtils::multiplyMatrix(&modelViewMatrix.data[0], physicsMatrix, &objectMatrix.data[0]);
SampleUtils::multiplyMatrix(&projectionMatrix.data[0], &objectMatrix.data[0], &modelViewProjection.data[0]);
glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
(const GLvoid*) &signVerts[0]);
glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
(const GLvoid*) &signNormals[0]);
glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
(const GLvoid*) &signTexCoords[0]);
glEnableVertexAttribArray(vertexHandle);
glEnableVertexAttribArray(normalHandle);
glEnableVertexAttribArray(textureCoordHandle);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
(GLfloat*)&modelViewProjection.data[0] );
glDrawArrays(GL_TRIANGLES, 0, signNumVerts);
}
EDIT: looking at the code for btBoxShape i noticed you can grab the box vertices and normals:
btVector3** vertices= wallShape->getVertices();
btVector3**normals = wallShape->getNormals();
but you can't grab a list of indices to draw the vertex points in a certain order!
If I recall correctly, this is not the proper way to draw debug shapes in Bullet. Did you read the user manual (PDF), page 16?
You are supposed to implement your own debug drawer class which implements btIDebugDraw, and in this class you implement the drawLine method.
You pass this debug drawer to bullet with setDebugDrawer, and then enable it with world->getDebugDrawer->setDebugMode(debugMode);
To draw the world, call world->debugDrawWorld();
This then calls drawLine on your custom function numerous times until a wireframe model of the physics world has been drawn.

glReadPixels get wrong data in android opengl es 2.0

I get the yuv data from camera , and send them to opengl, then I use fragment shader to convert the data to RGBA format and show it on the screen. Everything goes well but when I use glReadPixels to get the RGBA data from framebuffer to int array, I get wrong data.
// I use VBO to draw
glBindBuffer(GL_ARRAY_BUFFER, squareVerticesBufferID);
glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(gvPositionHandle);
glBindBuffer(GL_ARRAY_BUFFER, textureVerticesBuferID);
glVertexAttribPointer(gvTextureHandle, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(gvTextureHandle);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, squareVerticesIndexBufferID);
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, 0);
// Then I use glReadPixels to read the RGBA data
unsigned char *returnDataPointer = (unsigned char*) malloc(width * height * 4);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, returnDataPointer);
Unfortunately I get wrong data, the last thousands elements in the array are 0s,the same code works well on ios, did I miss something?
I work on Android 4.0.3 and use OpenGL ES 2 from the NDK.

Categories

Resources