Log in or register to post comments

Error when I use pointers instead of static array

December 10, 2010 - 11:58am #1

Hello.

I'm using ImageTargets to make my own code. I'm also using Teapot.h data to make my tests.

I'm using the same vertices coordinates, normals and texture coordinates as statics array and as pointers.

When I use statics arrays (take from Teapot.h), model is drawn correctly. But, when I use pointers, model is not drawn well.

Here is rendering code using static arrays:

JNIEXPORT void JNICALL Java_com_xxx_xxxxxx_ActivityRenderer_renderFrame
  (JNIEnv *env, jobject obj)
{
        // modelViewProjection initialization code HERE

        glUseProgram(shaderProgramID);
         
        glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
                              (const GLvoid*) &teapotVertices[0]);
        glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
                              (const GLvoid*) &teapotNormals[0]);
        glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
                              (const GLvoid*) &teapotTexCoords[0]);
        
        glEnableVertexAttribArray(vertexHandle);
        glEnableVertexAttribArray(normalHandle);
        glEnableVertexAttribArray(textureCoordHandle);
        
        glBindTexture(GL_TEXTURE_2D, thisTexture->mTextureID);
        glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
                           (GLfloat*)&modelViewProjection.data[0] );
        glDrawElements(GL_TRIANGLES, NUM_TEAPOT_OBJECT_INDEX, GL_UNSIGNED_SHORT,
                       (const GLvoid*) &teapotIndices[0]);
}

teapotVertices, teapotNormals and teapotTexCoords are static const float teapot...[...]
teapotIndices is static const unsigned short teapotIndices[NUM_TEAPOT_OBJECT_INDEX]

And here when I use pointers (all vertices data from Teapot.h, passed from Java to C++ using JNI).

JNIEXPORT void JNICALL Java_com_xxx_xxxxxx_ActivityRenderer_renderFrame
  (JNIEnv *env, jobject obj,
		  jfloatArray vertices, jfloatArray normals,
		  jfloatArray texCoord, jintArray indices)
{
	jfloat *vertPos, *vertNorm, *vertTexC;
	jint *indicesArray;

	// Get array length
	jsize numNormals = env->GetArrayLength(normals);
	jsize numTexCoords = env->GetArrayLength(texCoord);

	// Get indices' number
	GLsizei numIndices = (GLsizei) env->GetArrayLength(indices);

	// Get a pointer to array's elements pass as arguments
	vertPos = env->GetFloatArrayElements(vertices, 0);
	indicesArray = env->GetIntArrayElements(indices, 0);

	if (numNormals > 0)
		vertNorm = env->GetFloatArrayElements(normals, 0);
	if (numTexCoords > 0)
		vertTexC = env->GetFloatArrayElements(texCoord, 0);

        // modelViewProjection initialization code HERE

        glUseProgram(shaderProgramID);

        glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
	                              (const GLvoid*) vertPos);
        glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
	                              (const GLvoid*) vertNorm);
        glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
	                              (const GLvoid*) vertTexC);

        glEnableVertexAttribArray(vertexHandle);
        glEnableVertexAttribArray(normalHandle);
        glEnableVertexAttribArray(textureCoordHandle);

        glBindTexture(GL_TEXTURE_2D, thisTexture->mTextureID);
        glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
	                           (GLfloat*)&modelViewProjection.data[0] );
        glDrawElements(GL_TRIANGLES, NUM_TEAPOT_OBJECT_INDEX, GL_UNSIGNED_SHORT,
	                       (const GLvoid*) indicesArray);

	glDisableVertexAttribArray(vertexHandle);
        glDisableVertexAttribArray(normalHandle);
	glDisableVertexAttribArray(textureCoordHandle);

        // I'm not sure but my problems come from here:
	env->ReleaseFloatArrayElements(vertices, vertPos, 0);
	if (numNormals > 0)
	    env->ReleaseFloatArrayElements(normals, vertNorm, 0);
	if (numTexCoords > 0)
	    env->ReleaseFloatArrayElements(texCoord, vertTexC, 0);
	env->ReleaseIntArrayElements(indices, indicesArray, 0);
}

As you can see, I've only changed vertices coordinates, normals and texture coordinates from static to dynamic array.

I have also checked values and are the same when using static arrays and pointers.

May I need to use buffers? (glGenBuffers, glBindBuffer and glBufferData)

Any advice?

Re: Error when I use pointers instead of static array

December 11, 2010 - 8:48am #2

The problem is that you are storing your indices as an array of integers, but are calling glDrawElements with the GL_UNSIGNED_SHORT flag. You should use the GL_UNSIGNED_INT flag instead.

- Kim

Log in or register to post comments