Sign in to follow this  
Giacomo83

draw two object

Recommended Posts

I've another question: how can I draw 2 differents objects like a sphere and a quad?I triyed to define vertices,indices,texture for each objects and I call two time the functions glVertexPointer and glTexCoordPointer, than in my AppCycle I call two time glBindTexture and glDrawElements...but don't run? Any suggestion? Thanks

Share this post


Link to post
Share on other sites
Define "don't run"

Nothing appears on screen? One object appears? Both appear but not textured? Application crashes?

You are going to have to post the relevant source code if you want people to be able to help you with this.

Alan

Share this post


Link to post
Share on other sites
Sorry you're right!
I try to draw two object in two projects: one with texture and one without texture.
In the one with texture, nothing appears; in the project without texture it's draw only the first object(a cube!)
This is my code im AppInit:

glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_NORMAL_ARRAY );

glVertexPointer( 3, GL_SHORT, 0, objVertexdata);
glNormalPointer( GL_BYTE, 0, objNormal);

glVertexPointer( 3, GL_SHORT, 0, objVertexdata2);
glNormalPointer( GL_BYTE, 0, objNormal2)




And this in AppCycle:

glScalex( 3 << 17, 3 <<17, 3 << 17);
glDrawElements( GL_TRIANGLES, objFaces * 3,
GL_UNSIGNED_BYTE, objFacedataDuck );
glTranslatef( 24.0 , 9.0, -300.0 );
glDrawElements( GL_TRIANGLES, objFaces * 3,
GL_UNSIGNED_BYTE, objFacedataDuck2 );


The code is taken from the project without texture!

Share this post


Link to post
Share on other sites
There is a lot of setup involved with textures, especially if you want more than one.

Are you setting up your textures in your AppUi class and then passing them to you appInit method?

The in AppInit are you doing more setup such as this?

glBindTexture( GL_TEXTURE_2D, iNokTexObjects[0] );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, TEX_WIDTH, TEX_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, texPtr );
glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
glTexParameterx(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);


Then your appCycle code should do something like this:

glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, iNokTexObjects[0] );
//drawing code
glDisable( GL_TEXTURE_2D );


there is more as well but it's quite long, so see if this helps first.

Good luck, textures gave me quite a headache when I was first trying to figure them out. It took me a LONG time to get them working correctly.

Share this post


Link to post
Share on other sites
Thanks for your help!
I try to add the lines you write, but when I start my application nothing is drawed.
This is my complete code of AppInit and AppCycle:


void CTexture::AppInit( TUint8 *tex1,TUint8 *tex2 )
{
/* Create a texture pointer for glTexImage2D
and assign the first texture to it. */

const GLvoid *texPtr = tex1;

/* Set the screen background color. */
glClearColor( 0.f, 0.f, 0.f, 1.f );

/* Enable back face culling, texturing, and normalization. */
glEnable( GL_CULL_FACE );
glEnable( GL_TEXTURE_2D );
glEnable( GL_NORMALIZE );

/* Initialize viewport, texture matrix scaling, and projection. */
glViewport( 0, 0, iScreenWidth, iScreenHeight );
glMatrixMode( GL_PROJECTION );
glFrustumf(-1.f,1.f,-1.f,1.f,3.f,1000.f);
/* Initialize appropriate texture matrix. First we have to translate the
input texture coordinate values to be within a range of [0,255]. Hence
we translate the x- and y-coordinate values by 128. Recall that the
values in nokTexCoords are between [-128,127], now they are in a range
of [0,255]. After that we scale the values by 1/255 to make the values
to be in range [0,1]. */

glMatrixMode( GL_TEXTURE );
glLoadIdentity();

/* Remember to change the matrix mode to modelview. */
glMatrixMode( GL_MODELVIEW );

/* Enable vertex and texture arrays. */
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glEnableClientState( GL_NORMAL_ARRAY );
glEnableClientState(GL_COLOR_ARRAY);
/* Set array pointers. */
glVertexPointer( 3, GL_FLOAT, 0, vertices);
glTexCoordPointer( 2, GL_FLOAT , 0, nokTexCoords );
glNormalPointer( GL_BYTE, 0, normals);
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );

glVertexPointer( 3, GL_BYTE, 0, vertices2);
glTexCoordPointer( 2, GL_BYTE , 0, nokTexCoords2);

/* Set up global ambient light. */
glLightModelxv( GL_LIGHT_MODEL_AMBIENT, globalAmbient );

/* Set up lamp. */
glEnable( GL_LIGHT0 );
glLightxv( GL_LIGHT0, GL_DIFFUSE, lightDiffuseLamp );
glLightxv( GL_LIGHT0, GL_AMBIENT, lightAmbientLamp );
glLightxv( GL_LIGHT0, GL_SPECULAR, lightSpecularLamp );
glLightxv( GL_LIGHT0, GL_POSITION, lightPositionLamp );

glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, objDiffuse);
glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, objAmbient);
glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, objSpecular);
glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, objEmission);
glMaterialx( GL_FRONT_AND_BACK, GL_SHININESS, 10 << 16);

/* Set shading mode*/
// glColorPointer(4,GL_UNSIGNED_BYTE,0,colors);
glShadeModel( GL_FLAT );


/* Textures are initialized in OpenGL ES API by this
function. Use glDeleteTextures() in destructor. */

glGenTextures( 2, iNokTexObjects );

/* Bind the Nasa Hubble texture to iNokTexObjects[0], set the texture
to the texPtr defined above, and set the texture environment. */

glBindTexture( GL_TEXTURE_2D, iNokTexObjects[0] );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, TEX_WIDTH, TEX_HEIGHT,
0, GL_RGB, GL_UNSIGNED_BYTE, texPtr );
glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
// glTexParameterx(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
/* Change the texture pointer to ogles.jpg texture and assosiate that with
the iNokTexObjects[1]. */

texPtr = tex2;
glBindTexture( GL_TEXTURE_2D, iNokTexObjects[1] );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, TEX_WIDTH, TEX_HEIGHT,
0, GL_RGB, GL_UNSIGNED_BYTE, texPtr );
glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );

/* Set the blending function for translucency. */
//glBlendFunc( GL_ONE, GL_ONE );
glBlendFunc(GL_ONE, GL_ONE);
/* Do not use perspective correction, can be enabled from the menu */
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );

/* Symbian menu item initialization. See CTextureAppUi.cpp for more. */
iPerspectiveMode = 0; // no texture perspective correction
iTextureMode = 0; // no texture wrapping
iLightingMode = 0; // no lighting
iBlending = 0; // no blending

}

void CTexture::AppCycle( TInt aFrame,TInt dirx,TInt diry )
{
#define cameraDistance 100

glClear( GL_COLOR_BUFFER_BIT );

/* Animate and draw the cube. */
glLoadIdentity();
glTranslatef(dirx,diry,-100.0);
glRotatex( aFrame << 16,0,0,1<<16);
glRotatex( aFrame << 16,1<<16,0,0);
/* Scale the quad coordinates to fit the screen. */
glScalex( 28 << 11, 28 << 11, 28 << 11 );

/* Draw the first 5 sides of the cube with the texture Nasa Hubble,
which is bound to the iNokTexObjects[0]. */

glBindTexture( GL_TEXTURE_2D, iNokTexObjects[0] );
glEnable(GL_TEXTURE_2D);
glDrawElements(GL_TRIANGLES,420*3,GL_UNSIGNED_BYTE,indices);
glDisable(GL_TEXTURE_2D);
// glScalex( 20 << 16, 20 << 16, 20 << 16 );
glBindTexture( GL_TEXTURE_2D, iNokTexObjects[1] );
glEnable(GL_TEXTURE_2D);
glDrawElements(GL_TRIANGLES,2*3,GL_UNSIGNED_BYTE,indices2);
glDisable(GL_TEXTURE_2D);
}




Vertices and indices are the data of the sphere and vertices2 and indices2 are the data of quad that I want to use as background!
Thanks for your important help!

Share this post


Link to post
Share on other sites
I modify my code...now I can display a background and a phere with texture on it!But if I try to put a texture on the quad of background, my application don't run!
This is my AppUi where I load the two textures:

void CTextureAppUi::LoadTexturesL()
{
// Construct the full path names for the textures
_LIT(KTex1Name, "earthmap.jpg" );
iTex1FullName.Append( TEXTUREPATH );
iTex1FullName.Append( KTex1Name );

_LIT(KTex2Name, "sfondo.jpg" );
iTex2FullName.Append( TEXTUREPATH );
iTex2FullName.Append( KTex2Name );

// Create bitmaps for textures
iTex1 = new(ELeave) CFbsBitmap();
iTex1->Create( TSize(256,256), EColor16M );
iTex2 = new(ELeave) CFbsBitmap();
iTex2->Create( TSize(256,256), EColor16M );

// Create a image converter library utility for loading the NASA texture
iFs.Connect();
iBmapUtil = CImageHandler::NewL( iTex1, iFs, *this );

// Set the status and start loading
iTexID = TEX;

iBmapUtil->LoadFileL( iTex1FullName, 0 );

}

// ----------------------------------------------------
// CTextureAppUi::ImageOperationCompleteL(TInt aError)
// Called when loading is finished. Handles the
// loading of the second texture, initializes OpenGL ES
// state when textures are loaded, and starts the
// iPeriodic in Container class.
// ----------------------------------------------------
//
void CTextureAppUi::ImageOperationCompleteL( TInt aError )
{
_LIT(KTexError, "Tex load failed");
User::LeaveIfError( aError );

// 1st texture is loaded, still need to load the 2nd one
if( iTexID == TEX )
{
// Change the texture ID
iTexID = TEXB;

// Change the bitmap pointer for image loader
if( !iBmapUtil->SetBitmapL( iTex2 ) )
{
User::Panic( KTexError, 222 );
}

// Load the second image
iBmapUtil->LoadFileL( iTex2FullName, 0 );

}
// Both textures loaded
if( iTexID == TEXB )
{
/*The data in the iTex1/2 are in RGB order but is read in BGR order.
So we have to switch the 1st or 3rd bytes. */

TUint8 *tex1 = (TUint8*)iTex1->DataAddress();
TUint8 *tex2 = (TUint8*)iTex2->DataAddress();
TUint8 t;

for( TInt i = 0; i < 256*256; i++ )
{
t = tex1[i*3];
tex1[i*3] = tex1[i*3+2];
tex1[i*3+2] = t;

t = tex2[i*3];
tex2[i*3] = tex2[i*3+2];
tex2[i*3+2] = t;
}

// Initialize OpenGL ES now that the textures are loaded
iAppContainer->iTexture->AppInit( tex1,tex2 );

// The original bitmaps can now be deleted because the textures
// are stored inside OpenGL ES now
delete iTex1;
iTex1 = NULL;

delete iTex2; User::Panic( KTexError, 111 );
iTex2 = NULL;

// Set the texture loaded flag
iTexLoaded = 1;

// Start the container's iPeriodic
iAppContainer->iPeriodic->Start( 100, 100, TCallBack( CTextureContainer::DrawCallBack, iAppContainer ) );
}
// Invalid texture ID defined
else
{

}
}



Here's the code of my main class where I draw the background and the sphere:


void CTexture::DrawSphere(TInt aFrame,TInt dirx,TInt diry)
{

glLoadIdentity();
glTranslatef(dirx,diry,-100.0);
glRotatex( aFrame << 16,0,0,1<<16);
glRotatex( aFrame << 16,1<<16,0,0);
/* Scale the quad coordinates to fit the screen. */
glScalex( 28 << 11, 28 << 11, 28 << 11 );

glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, iNokTexObjects[0]);
glVertexPointer( 3, GL_FLOAT, 0, vertices);
glTexCoordPointer( 2, GL_FLOAT , 0, nokTexCoords);
glNormalPointer( GL_BYTE, 0, normals);
glDrawElements(GL_TRIANGLES,420*3,GL_UNSIGNED_BYTE,indices);
glDisable(GL_TEXTURE_2D);
}

void CTexture::DrawBackground()
{
glLoadIdentity();
glTranslatef(0,0,-100.0);
glScalex( 28 << 11, 28 << 11, 28 << 10 );

glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, iNokTexObjects[1] );
glVertexPointer( 3, GL_FLOAT, 0, vertices2);
glTexCoordPointer( 2, GL_FLOAT , 0, nokTexCoords2);
glNormalPointer( GL_BYTE, 0, normals2);
glDrawElements(GL_TRIANGLES,32*3,GL_UNSIGNED_BYTE,indices2);
glDisable(GL_TEXTURE_2D);
}
// -----------------------------------------------------------------------------
// CTexture::AppCycle
// OpenGL ES main rendering loop.
// -----------------------------------------------------------------------------
//
void CTexture::AppCycle( TInt aFrame,TInt dirx,TInt diry )
{

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
DrawBackground();
DrawSphere(aFrame,dirx,diry);

}


What's wrong?
Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this