Log in or register to post comments

Dynamic data set and model switching

March 27, 2012 - 3:53am #1

I am trying to put every thing on the internet and download it on the go, including the xml , dat and the model data & texture. Walk through a long road, I managed to download all of them now. Switching dataset is a no problem, but when it comes to the model. I run into something I have no idea of.

My model data is coming from an XML and I first store it in a NSString. It looks like something like this :
11.222200,0.110300,20.030291,10.302300,-4.461498,20.030291,10.152300,-4.397198,20.644890.........
The normal data and texture coordinate data are obtained in the same way too.
P.S. I use all the data from the example teapot, just to make sure if I did it all right, I can see the teapot.

Then in the setup3dObjects function, I modify thing like this:

float *modelVertices = (float *) malloc(sizeof(float) * ((modelObject.numVerts*3)));
//separate the values by comma
            NSArray * array  = [modelObject.vertData componentsSeparatedByString:@","];
            for (int i =0 ; i < (modelObject.numVerts*3); i++) {
//get the string and convert to float number
                modelVertices[i] = [[array objectAtIndex:i] floatValue];
            }
            obj3D.vertices = modelVertices;     

But finally, I got this when the image target is detected:

The teapot is now many pieces of triangles roughly assembling in a teapot shape. Checked that the float values converted from the strings in the above code is the same as the one in the example teapot.h.

Any ideas on what I did wrong?
Please help, I am so frustrated on this. I am very new to 3D/OpenGL things:(

Dynamic data set and model switching

August 12, 2012 - 11:51am #6

Hi Patrick,

I'm walking on the similar road you walked through. I'm dowloading the datasets at runtime. My problem is that I don't know how to switch datasets runtime. Actually in a dataset I have the data for only one trackable. When I start the Vuforia's AR view fisrt time in the app everything is ok. After that I quit the Vuforia's AR view. When the AR view starts again the new dataset should be loaded. I don't know how to switch the datasets. Could you help me, please?

Re: Dynamic data set and model switching

March 28, 2012 - 7:26pm #5

MoSR,

Thank you for pointing me to the right direction and your suggestions!

Re: Dynamic data set and model switching

March 28, 2012 - 2:55am #4

Hi Patrick,

Good call. :-)

To handle 3D model files with different format of content, you could use the lack of an indices array to switch between using glDrawElements and glDrawArrays.

It's generally Good Practice to switch on and off all the array pointers based on availability of the appropriate data (normals, texture coordinates, etc).

Later, if you store the texture names and paths with your objects as well, then you begin to have a self-contained object description.

Re: Dynamic data set and model switching

March 27, 2012 - 10:25pm #3

MoSR, I have found the problem, it is about the OpenGL code!

In the renderFrameQCAR function, we originally have this

            glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)obj3D.vertices);
            glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)obj3D.normals);
            glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)obj3D.texCoords);
            
            glEnableVertexAttribArray(vertexHandle);
            glEnableVertexAttribArray(normalHandle);
            glEnableVertexAttribArray(textureCoordHandle);
            
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, [obj3D.texture textureID]);
            glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (const GLfloat*)&modelViewProjection.data[0]);
            glDrawElements(GL_TRIANGLES, obj3D.numIndices, GL_UNSIGNED_SHORT, (const GLvoid*)obj3D.indices);

It turns out the problem is that I replaced the last line with this
glDrawArrays(GL_TRIANGLES, 0, obj3D.numVertices);

Cause of this is because I was looking for replacing the model in the example. And searched the forum, I found someone providing an example to replace the teapot with a banana. Stated at the example, that the banana model do not have the teapotIndices as in the teapot.h (the perl script mentioned in that thread doesn't generate this information from an .obj file). So glDrawElements should be replaced with glDrawArrays. This is where the teapot explode. When I change back to glDrawElements, the teapot is back to normal.

Problem now is, I have some other model in .obj files, and that perl script to generate the C data, but I don't have the indices information. How could I still display the model correctly?
Sorry for my ignorance on 3D stuff, can the original teapot be displayed properly without the indices?

Re: Dynamic data set and model switching

March 27, 2012 - 4:09am #2

Wow - an exploding teapot! :-)

Patrick, it looks like the model is there, but the texture is being incorrectly painted onto it. So it could be that the teapot coordinates are mismatched to the texture coordinates.

Try creating your teapot as a second Object3D in the setup3dObjects and compare it's data with all the elements in the hard coded teapot (in the first object).

Log in or register to post comments