0

In my first libgdx 3D game i now switched from createBox to createRect, to create only the visible faces (if a wall is on the left side of another wall, its right face is not visible...). I am creating 4 models:

  1. frontFace
  2. backFace
  3. rightFace
  4. leftFace

They are almost drawn how they actually should. But there is one big issue: The side faces are only visible if i look in the positive z-Direction. If i look the other side (negative z-Direction), they don't draw. The front and back faces only draw, if i look to them in negative x-Direction. Has this something to do with the normals? I have set them to:

normal.x = 0;
normal.y = 1;
normal.z = 0;

Is that the error? How should i set the normals? What do they stand for? I have some basic idea about normal mapping for lighting, is that the same?

Important note: I have disabled backface culling, but it did not make any difference. View frustum culling is turned on. If any more informations are needed please post a comment and i will add them as soon as possible. Thanks

Robert P
  • 9,398
  • 10
  • 58
  • 100

2 Answers2

4

Perhaps not directly related, but still important to note: don't use createRect or createBox for anything other than debugging/testing. Instead combine multiple shapes into a single model/node/part. Or even better, use a modeling application where possible.

You didn't specify how you disabled backface culling. But keep in mind that you should not change the opengl state outside the shader/rendercontext (doing so will result in unpredicted behavior). To disable backface culling you can either specify it using the material attribute IntAttribute.CullFace (see: https://github.com/libgdx/libgdx/wiki/Material-and-environment#wiki-intattribute), the DefaultShader (or default ModelBatch) Config defaultCullFace member (see http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g3d/shaders/DefaultShader.Config.html#defaultCullFace) or the (deprecated) static DefaultShader#defaultCullFace member (see http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g3d/shaders/DefaultShader.html#defaultCullFace).

Whether a face is front or back is based on the vertex winding. Or in other words: the order in which you provide the corners of the rectangle is used to decide which side is front and which side is back. If you use one of the rect methods, you'll notice the arguments have either the 00, 01, 10 or 11 suffix. Here, when looking at the face, 00 is lower-left, 01 upper-left, 11 is upper-right and 10 is lower-right.

For a rectangle, it's normal is the perpendicular facing outwards the rectangle. For example if you have a rectangle on XZ plane with it front face on the top, then its normal is X=0,Y=1,Z=0. If its front face it facing the bottom, then its normal is X=0,Y=-1,Z=0. Likewise if you have a rectangle on XY plane, its normal is either X=0,Y=0,Z=1 or X=0,Y=0,Z=-1. Note that the normal is not used for face culling, it's most commonly used for lighting etc. Specifying an incorrect/opposite normal will not cause the face to be culled (it might cause incorrect/black lighting though).

Xoppa
  • 7,983
  • 1
  • 23
  • 34
  • Thanks for your answer. I only need to create blocks, which are 1x1x1. So how should i build them without createBox/creatRect? – Robert P Mar 01 '14 at 13:55
  • @Springrbua use Blender or another 3D editor for this purpose. If you are going to use Blender, just add mesh called `Cube` and resize it as appropriate. Then export it in `.obj` format file and load this mesh in libGDX. – Nolesh Jun 10 '14 at 09:41
  • @Nolesh why is this more efficient than creating rects at runtime? – Robert P Jun 11 '14 at 05:59
  • @Springrbua, as for me it's more easier than combine cube from 6 rects and as Xoppa said `cullface` won't draw back sides if you will set `GL_BACK` mode. – Nolesh Jun 11 '14 at 08:29
  • @Nolesh Yea i understood that, but if i draw the cubemodels, the front, left and right, top and button face will be drawn, even if they are not visible, for that i used drawRect, and i draw only the rects visible (if two walls are near each other 1 face of both is invisible) – Robert P Jun 11 '14 at 09:38
  • @Springrbua if I understood you correctly, it is not shown because of the normal orientation. You should check your normals. Try to use `GL_NONE` mode. Or try to rotate that side to 180 degrees. – Nolesh Jun 11 '14 at 11:15
  • @Nolesh it is allready working, but i did it with the rects, actually with the meshpartbuilder, cause this way all invisible faces (faces that are occupied by other walls) are not created and so they are not drawn, while backfaces are culled with back-face-culling. I am just wondering, if it is mor efficient to use models, even if the model always has 6 faces (a cube), even if only 2 are visible. – Robert P Jun 11 '14 at 12:00
  • @Springrbua seems to me you should use models. What if you will need to create something more complex than simple cube? Do you really think that it is possible to do this with planes created by `meshbuilder`? ))) – Nolesh Jun 11 '14 at 12:14
  • @Nolesh nah never :) I use the `Rect`s for the walls only. For the rest i use normal `Model`s ofc. – Robert P Jun 11 '14 at 12:26
  • @Springrbua ok) Then you should use plane model. Blender->Add->Mesh->Plane. Convert it to `.fbx` format and using `fbx-conv` convert it to `.g3db`. LibGDX likes this format. – Nolesh Jun 11 '14 at 12:44
  • @Nolesh but why should i creat a Model and load it in libgdx, when i can easily create it inside the code? I meand its just a `Rect`, or in my case 0-4 rects, which form a block (depending on the occupied faces only 0-4 rects are used cause top and bottom are always invisible). – Robert P Jun 11 '14 at 13:28
0

For your purpose I'd recommend you to use Decal class. Decals are bitmap sprites that exist in a 3D scene. This article is about using of decals in LibGDX. I hope it is what you wanted.

Nolesh
  • 6,848
  • 12
  • 75
  • 112
  • Thanks for your answer. Why do you think using `Decal`s would be better in this situation? Are they more efficient? The actual problem (backface culling) is allready solved, the solution was the order of the corners, which was wrong or some of the rects. – Robert P Jun 17 '14 at 08:22