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

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.

Related

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

Android openGL attempt to increase FPS by using the same vertices for multiple meshes

I am developing a game where similar objects are rendered on the screen. The problem is that every time a new object is added, more vertices needs to be handled, so FPS decrease.
Since I am a beginner with openGl, I have some general ideas about improving performance, I just don't know what openGl allows me to do.
Since most of my objects are identical(same number of vertices, same colors, same uvs and same normals), but only the position and rotation of the objects are different, is it possible to send the vertices for similar objects only once, on each frame render , and then call drawElements for all objects and modify only the objects positions?
Is it possible to go even further, and upload those similar vertices, only once when the scene is 1st created, and on each frame render just call drawElements for each object?
Currently, I am sending the vertices for each object like this:
.......
gl.glNormalPointer(GL10.GL_FLOAT, 0, normals);
gl.glColorPointer(4, GL10.GL_UNSIGNED_BYTE, 0, colors);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices);
gl.glPushMatrix();
gl.glTranslatef(position);
gl.glRotatef(rotation);
gl.glScalef(scale);
gl.glDrawElements(renderType,nr,GL10.GL_UNSIGNED_SHORT, faces);
gl.glPopMatrix();
........
I am thinking I should do someting like this:
.......
gl.glNormalPointer(GL10.GL_FLOAT, 0, normals);
gl.glColorPointer(4, GL10.GL_UNSIGNED_BYTE, 0, colors);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices);
for(eachObject)
{
gl.glPushMatrix();
gl.glTranslatef(position);
gl.glRotatef(rotation);
gl.glScalef(scale);
gl.glDrawElements(renderType,nr,GL10.GL_UNSIGNED_SHORT, faces);
gl.glPopMatrix();
........
}
Yes
Yes (even better)
Your second code example looks exactly like what you should be doing.
There's no reason you'd need to upload the vertices more than once, doing it on initialization should be sufficient. After that just set the pointers any time you want to render. You can call glDrawElements as many times as you want with different translations/rotations.

Can't draw loaded models in OpenGL ES 1.x with C++

I load obj models and try to render them with OpenGL ES using Android NDK:
class ObjModel{
public:
ObjModel();
~ObjModel();
int numVertex, numNormal,numTexCoord, numTriange;
float *vertexArray;
float *normalArray;
float *texCoordArray;
unsigned short *indexArray;
void loadModel(string fileName);
};
model->loadModel(filename);
glVertexPointer(3, GL_FLOAT, 0, &(model->vertexArray[0]));
glNormalPointer(GL_FLOAT, 0, &(model->normalArray[0]));
glDrawElements(GL_TRIANGLES, model->numTriange, GL_UNSIGNED_SHORT,
&(model->indexArray[0]));
Model is not rendered fully, I see only part of it.
I checked the data in arrays and they are parsed properly. I think that the only issue might be with passing arguments. Am I doing it right?
Hope this helps! I think you are just missing the number 3!
glDrawElements(GL_TRIANGLES, 3 * model->numTriange, GL_UNSIGNED_SHORT,
&(model->indexArray[0]));

Matrix Palette, Drawing Issue when ModelView Matrix

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.

Categories

Resources