1

First of all, we (my friends and I) are working on a Computer Graphics project where we are supposed to simulate particles (in our case, small cubes (mesh3d)) that would interact with each other in a space context. This is, hundreds of particles exploding and attracting each others using physics laws.

We based our implementation on a framework provided from our school, so we have to "adapt ourselves" to it.

While creating the cube (mesh3d), we provide a material containing a texture.

Mesh3D* 
SolarViewer::
createCube()
{
// initialize Mesh3D
Mesh3D *cube = new Mesh3D();
MeshMaterial* mat = new MeshMaterial; //we create the material for the cube
mat->m_diffuseTexture.create("particle.tga"); //we create the diffuse texture of the material to be particle.tga

// setup uniform cube with side length 0.5 and center of cube being (0,0,0)
std::vector< Vector3 > cubeVertices;
std::vector< Vector3 > cubeNormals;
std::vector< Vector3 > cubeColors;
std::vector< unsigned int > cubeIndices;
float d = 1.0;


// front
cubeVertices.push_back(Vector3(-d,-d, d));
cubeVertices.push_back(Vector3( d,-d, d));
cubeVertices.push_back(Vector3( d, d, d));
cubeVertices.push_back(Vector3(-d, d, d));
for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(0,0,1));
for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.8,0.3,0.3));
cubeIndices.push_back(0);
cubeIndices.push_back(1);
cubeIndices.push_back(2);
cubeIndices.push_back(0);
cubeIndices.push_back(2);
cubeIndices.push_back(3);


// right
cubeVertices.push_back(Vector3( d,-d,-d));
cubeVertices.push_back(Vector3( d,-d, d));
cubeVertices.push_back(Vector3( d, d, d));
cubeVertices.push_back(Vector3( d, d,-d));
for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(1,0,0));
for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.3,0.8,0.3));
cubeIndices.push_back(4);
cubeIndices.push_back(5);
cubeIndices.push_back(6);
cubeIndices.push_back(4);
cubeIndices.push_back(6);
cubeIndices.push_back(7);


// back
cubeVertices.push_back(Vector3( d,-d,-d));
cubeVertices.push_back(Vector3(-d,-d,-d));
cubeVertices.push_back(Vector3(-d, d,-d));
cubeVertices.push_back(Vector3( d, d,-d));
for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(0,0,-1));
for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.3,0.3,0.8));
cubeIndices.push_back(8);
cubeIndices.push_back(9);
cubeIndices.push_back(10);
cubeIndices.push_back(8);
cubeIndices.push_back(10);
cubeIndices.push_back(11);


// left
cubeVertices.push_back(Vector3(-d,-d, d));
cubeVertices.push_back(Vector3(-d,-d,-d));
cubeVertices.push_back(Vector3(-d, d,-d));
cubeVertices.push_back(Vector3(-d, d, d));
for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(-1,0,0));
for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.8,0.8,0.3));
cubeIndices.push_back(12);
cubeIndices.push_back(13);
cubeIndices.push_back(14);
cubeIndices.push_back(12);
cubeIndices.push_back(14);
cubeIndices.push_back(15);


// top
cubeVertices.push_back(Vector3(-d, d,-d));
cubeVertices.push_back(Vector3( d, d,-d));
cubeVertices.push_back(Vector3( d, d, d));
cubeVertices.push_back(Vector3(-d, d, d));
for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(0,1,0));
for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.8,0.3,0.8));
cubeIndices.push_back(16);
cubeIndices.push_back(17);
cubeIndices.push_back(18);
cubeIndices.push_back(16);
cubeIndices.push_back(18);
cubeIndices.push_back(19);


// bottom
cubeVertices.push_back(Vector3( d,-d,-d));
cubeVertices.push_back(Vector3(-d,-d,-d));
cubeVertices.push_back(Vector3(-d,-d, d));
cubeVertices.push_back(Vector3( d,-d, d));
for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(0,-1,0));
for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.3,0.8,0.8));
cubeIndices.push_back(20);
cubeIndices.push_back(21);
cubeIndices.push_back(22);
cubeIndices.push_back(20);
cubeIndices.push_back(22);
cubeIndices.push_back(23);


cube->setIndices(cubeIndices, mat); //we set the cubeIndices and the material for the cube
cube->setVertexPositions(cubeVertices);
cube->setVertexNormals(cubeNormals);
cube->setVertexColors(cubeColors);

    return cube;
}

and then we iterate through all of our cubes in the draw scene method in order to draw them

for (std::vector<Mesh3D*>::iterator mIt = m_meshes.begin(); mIt != m_meshes.end(); ++mIt)
                {
                    Mesh3D* cube =  *mIt;           
                    m_meshShaderTexture.setMatrix4x4Uniform("modelworld", cube->getTransformation());
                   cube->getMaterial(0).m_diffuseTexture.bind();
                   m_meshShaderTexture.setIntUniform("texture", cube->getMaterial(0).m_diffuseTexture.getLayer());
                   draw_object(m_meshShaderTexture, cube);// method called to draw the cube

                }

We called this line in the method "drawObject" which has for purpose to draw the mesh3d objects :

    void SolarViewer::draw_object(Shader& sh, Mesh3D *mesh)
{

sh.setMatrix4x4Uniform("modelworld", mesh->getTransformation() );

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer( 3, GL_DOUBLE, 0, mesh->getVertexPointer() );
glNormalPointer( GL_DOUBLE, 0, mesh->getNormalPointer() );
glTexCoordPointer( 2, GL_DOUBLE, 0, mesh->getUvTextureCoordPointer() );


for(unsigned int i = 0; i < mesh->getNumberOfParts(); i++)
{   
    glDrawElements( GL_TRIANGLES, mesh->getNumberOfFaces()*3, GL_UNSIGNED_INT, mesh->getVertexIndicesPointer() );

}

glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

}

But at the end, while trying to compile, the following line gives us a BAD_ACCESS :(

glDrawElements( GL_TRIANGLES, mesh->getNumberOfFaces()*3, GL_UNSIGNED_INT, mesh->getVertexIndicesPointer() );

What are we doing wrong ? Is it enough to bind the following texture (.tga) to have the glowing effect ? : http://i166.photobucket.com/albums/u83/j1m68/star.jpg

genpfault
  • 51,148
  • 11
  • 85
  • 139
Floran Gmehlin
  • 824
  • 1
  • 11
  • 34
  • Looks right to me (although you could use a number of techniques to speed things up). What does mesh->getNumberOfParts(), mesh->getNumberOfFaces(), etc return? Looks like they might be the cause of your problem. – Ani Dec 18 '12 at 17:06
  • In the actual state before the bad_access, we have the following output : `mesh number of parts : 1` `mesh number of faces : 12` These are, the number of faces of the mesh (although i do not get it why it is 12 instead of 6, for a cube) and number of part, I don0t really know :/ As I said, we used a framework that was provided, so there are some things we have no idea about -.- ... – Floran Gmehlin Dec 18 '12 at 17:32
  • 1
    Well - there you have it. If mesh->getNumberOfFaces() is already 12, mesh->getNumberOfFaces() * 3 = 36 which means OpenGL expects many more indices and vertices in the arrays you specified and therefore you're getting an access violation. If you replace "mesh->getNumberOfFaces() * 3" with "mesh->getNumberOfFaces()", it ought to be ok. Remember - that argument is number of primitives (triangles). If that fixes it - I'll suggest it as an answer you can accept. – Ani Dec 18 '12 at 17:42

0 Answers0