PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

3D Textures

Hello,

I'm trying to play a little bit with the 3D example (the one with the jumping turtle).

When drawing the block, it uses the texture for each face. How can I define a different texture for each face of the cube?
Hi Gilad,

To use multiple textures you need to seperate each face of the cube into a separate model.

Because this isn't very efficient there is another technique. This involves having multiple pictures on one texture and map the pictures to each face.

I've attached a sample texture file. The black spaces are unused and each colored square refers to a face of the cube model.
Attachments:
cubetex.zip (3 KB)
gilad_no · 28 Jun 2007, 15:10 UTC — But how do I map the faces?
I've used the same code from the sample in Edge, to draw the cube.

Then, in order to render it, I use:

pDisplay->BindTexture(&m;_BoardTexture);
pDisplay->SetShading(E3D_GOURAUD|E3D_TEXTURE);
pDisplay->Render(&m;_Board);

But if I'm using your sample texture, I see it on every face, instead of mapping it as you suggested.

Additional question that I have (related to 3D): How do I write selection code? I want to draw a 3D board with some pieces on it, and allow the user to select a piece by clicking on it with the mouse. I saw a code in the sample (View3D) for the mouse selection, but from what I understood it is to determine the change in the mouse position - rather then selection the model.

BTW, I think you should really put up a section on the website where people can upload and share code samples for EdgeLib.

- Gilad
Hi Gilad,

You should map every face manually using the texture coordinates. You can do this with E3DSurface::SetTexCoord (or directly using 3D modeling software). This method is used in the turtle model included in the SDK.

Your second question is not really related to EDGELIB and it's pretty hard to give a short answer, as you probably want to be able to rotate and tilt the game board as well. You could try to give the game board a fixed position (for example from above, 45 degrees rotated to make the calculation less complicated.

We all like the idea here of putting up a website where EDGELIB developers can post, share and comment on sample code, snippets and tutorials. Do you have additional ideas on this? Please contact us at info [at] edgelib [dot] com to discuss this in more detail.

Thanks and regards,
I missed the SetTexCoord function. I now understand it better. Thanks for that.

Regarding the object selection:
I have 5 objects on the screen (E3DSurface), and 5 positions (EVECTOR3D)

Here is how I draw the scene:
Code:

ERESULT OnNextFrame(ClassEDisplay* pDisplay, unsigned long nDelta)
{
if (ecd.input->KeyHold(DIAM_MENUUP))
m_nZoom += (long)(nDelta * 8);
else if (ecd.input->KeyHold(DIAM_MENUDOWN))
m_nZoom -= (long)(nDelta * 8);
if (ecd.input->KeyHold(DIAM_MENULEFT))
m_nRotation -= (short)nDelta*2;
else if (ecd.input->KeyHold(DIAM_MENURIGHT))
m_nRotation += (short)nDelta*2;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
pDisplay->Perspective2D(256, 256, false);
pDisplay->Perspective3D(pDisplay->GetWidth(), pDisplay->GetHeight());
pDisplay->WorldReset();
pDisplay->WorldTranslate(0, 0, m_nZoom);
pDisplay->WorldRotate(256, m_nRotation, 0);

pDisplay->BindTexture(&m_Texture);
pDisplay->SetShading(E3D_GOURAUD | E3D_TEXTURE);

for (int nIndex=0;nIndex<5;nIndex++)
{
m_Item[nIndex].Reset();
m_Item[nIndex].Translate(m_Position[nIndex].x,m_Position[nIndex].y,m_Position[nIndex].z);
pDisplay->Render(&m_Item[nIndex]);
}

return E_OK;
}


I've added a handler for OnStylusDown. From what I understood, I need to use a matrix to translate the 2D point into a 3D point or a 3D vector. My items will never overlap, so I can use a cube for testing of collision with the vector, but I have no clue on how to create/use the matrix.

BTW: This is one of the examples I think should be posted into the website as a code snippet ("adding 3D selection to your code").

P.S.
I've sent you an email.


- Gilad
Hi Gilad,

You can create a new matrix as an array of 16 elements using the long datatype:

Code:

long matrix[16];
ecd.math->MatrixLoadIdentity(matrix);


The second line loads the identity matrix, if you transform your vector with it the result will be the same as the value before transformation (the matrix won't have any effect).

With MatrixMultiply you can apply rotation and translation to this matrix. First create a second (temporary) matrix, call MatrixSetRotate or MatrixSetTranslate on it and then call MatrixMultiply. You may need to call these several times (one for each seperate translation or rotation operation).