Anyone got the Voxel Fun tute by Dr Paul Thomas working?

Put your problem here if it does not fit any of the other categories.

Anyone got the Voxel Fun tute by Dr Paul Thomas working?

Postby edzillion » Tue Nov 17, 2009 6:11 pm

Hey

I have been trying to implement the voxel engine that is described in this guide:

Voxel Fun - How it works

I have worked my way though, and don't have any issues setting up the verts, the indexes, and the texture buffer, but I'm not seeing anything; I am unsure about whether my texture is bound correctly:

onSurfaceCreated (of class CubeRenderer implements GLSurfaceView.Renderer { )

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         gl.glEnable(GL10.GL_TEXTURE_2D);
  2.         gl.glGenTextures(1, tex, 0);
  3.         gl.glBindTexture(GL10.GL_TEXTURE_2D, tex[0]);
  4.         gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
  5.         gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
  6.  
  7.         bitmap = Bitmap.createBitmap(16 * 16 * 4, 16, Bitmap.Config.ARGB_8888);
  8.  
  9.         // Draw the texture in red/white hatched dots
  10.         for (int y = 0; y < 16; y += 2) {
  11.                 for (int x = 0; x < 1024; x += 2) {
  12.                         bitmap.setPixel(x, y, Color.WHITE);
  13.                         bitmap.setPixel(x + 1, y, Color.RED);
  14.                 }
  15.                 for (int x = 0; x < 1024; x += 2) {
  16.                         bitmap.setPixel(x, y + 1, Color.WHITE);
  17.                         bitmap.setPixel(x + 1, y + 1, Color.RED);
  18.                 }
  19.         }
  20.        
  21.         GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Then in the draw method of my cube (called from onDrawFrame)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  2.         gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  3.  
  4.         // Set the face rotation
  5.         gl.glFrontFace(GL10.GL_CCW);
  6.  
  7.         // Point to our buffers
  8.         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
  9.         gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
  10.  
  11.         // Draw the vertices as triangles, based on the Index Buffer
  12.         // information
  13.         gl.glDrawElements(GL10.GL_TRIANGLES, (mVerts * 3) / 2, GL10.GL_UNSIGNED_BYTE, indexBuffer);
  14.  
  15.         // Disable the client state before leaving
  16.         gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
  17.         gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Will the above code display my texture on the verts? Am I missing something?

Cheers
Ed
edzillion
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Oct 16, 2009 8:48 pm

Top

Postby PaulT » Tue Nov 17, 2009 6:48 pm

If you are worried that texturing might be a problem, just comment out

// gl.glEnable(GL10.GL_TEXTURE_2D);

and you should see polygons rendered in the base colour, (white by default).

Nothing obvious looks wrong in your texture code, though. If you are getting the OpenGL BSOD (black screen of death :-) ) then try setting your clipping planes very far apart, wide angle to your lens etc. in case you just have the wrong projection or are not looking in the right place.

Paul.
Paul
PaulT
Freshman
Freshman
 
Posts: 8
Joined: Mon Jul 13, 2009 12:00 pm

Postby edzillion » Tue Nov 17, 2009 9:16 pm

ok I think the problems are actually geometry related; turning off textures clarified this.

I can't understand it because I've gone through all the points and ran a few tests and it seems that the faces arent being made from the right verts or something; but I don't see anything wrong with the index buffer.

Image

This is the code for the above example; I have only added verts, indexes and texture points (although i have textures turned off here) for one axis of faces.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         ByteBuffer byteBuf = ByteBuffer.allocateDirect(mVerts * 3 * 4);
  2.         byteBuf.order(ByteOrder.nativeOrder());
  3.         vertexBuffer = byteBuf.asFloatBuffer();
  4.         for (int i = 0; i < mCubeSize + 1; i++) {
  5.                 vertexBuffer.put(2.0f * (float) i / (float) (mCubeSize + 1) - 1.0f); // x0.
  6.                 vertexBuffer.put(-1f); // y0.
  7.                 vertexBuffer.put(-1f); // z0.
  8.                 vertexBuffer.put(2.0f * (float) i / (float) (mCubeSize + 1) - 1.0f); // x1.
  9.                 vertexBuffer.put(1f); // y1.
  10.                 vertexBuffer.put(-1f); // z1.
  11.                 vertexBuffer.put(2.0f * (float) i / (float) (mCubeSize + 1) - 1.0f); // x2.
  12.                 vertexBuffer.put(1f); // y2.
  13.                 vertexBuffer.put(1f); // z2.
  14.                 vertexBuffer.put(2.0f * (float) i / (float) (mCubeSize + 1) - 1.0f); // x3.
  15.                 vertexBuffer.put(-1f); // y3.
  16.                 vertexBuffer.put(1f); // z3.
  17.         }
  18.         vertexBuffer.position(0);
  19.  
  20.         indexBuffer = ByteBuffer.allocateDirect((mVerts * 2 * 3) / 2 * 3);
  21.         indexBuffer.order(ByteOrder.nativeOrder());
  22.         iBuffer = indexBuffer.asShortBuffer();
  23.         for (int i = 0; i < mVerts; i += 4) {
  24.                 iBuffer.put((short) i);
  25.                 iBuffer.put((short) (i + 2));
  26.                 iBuffer.put((short) (i + 3));
  27.                 iBuffer.put((short) i);
  28.                 iBuffer.put((short) (i + 1));
  29.                 iBuffer.put((short) (i + 2));
  30.         }
  31.         iBuffer.position(0);
  32.  
  33.         byteBuf = ByteBuffer.allocateDirect((mVerts * 2 * 3) / 2 * 3);
  34.         byteBuf.order(ByteOrder.nativeOrder());
  35.         textureBuffer = byteBuf.asFloatBuffer();
  36.         float space = 0.015625f;
  37.         for (int i = 0; i < mVerts; i += 4) {
  38.                 textureBuffer.put((float) i * space);
  39.                 textureBuffer.put((float) 0);
  40.                 textureBuffer.put((float) (i * space) + space);
  41.                 textureBuffer.put((float) 0);
  42.                 textureBuffer.put((float) (i * space) + space);
  43.                 textureBuffer.put((float) 1);
  44.                 textureBuffer.put((float) i * space);
  45.                 textureBuffer.put((float) 1);
  46.         }
  47.         textureBuffer.position(0);
  48.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


Any thoughts? I did notice that the x values of each face range from -1 to .86667
shouldn't that be -1 to +1? but that wouldn't cause the above issue...
edzillion
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Oct 16, 2009 8:48 pm

Postby PaulT » Tue Nov 17, 2009 11:30 pm

It might be that your index buffer is declared as a short but your draw elements call says it is an unsigned byte. I think you should be using short for both.

Paul.
Paul
PaulT
Freshman
Freshman
 
Posts: 8
Joined: Mon Jul 13, 2009 12:00 pm

Postby edzillion » Wed Nov 18, 2009 5:38 pm

Yep, that was it, thanks.
I've got my texture mapped now, but it looks very blurry:

Image

This is a 6x6 cube; as you can see there are 6 pixels to a side, but they are not clear; from your description the line:

Code: Select all
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);


should sharpen it up, but this doesn't seem to be working.
edzillion
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Oct 16, 2009 8:48 pm

Postby PaulT » Wed Nov 18, 2009 9:26 pm

edzillion wrote:This is a 6x6 cube; as you can see there are 6 pixels to a side, but they are not clear; from your description the line:

Code: Select all
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);


should sharpen it up, but this doesn't seem to be working.

Yes, that should do it. Looks like it's in the right place too from your above code (after you have bound the texture). Are you only testing on the emulator or do you see the same thing on a device?

Paul.
Paul
PaulT
Freshman
Freshman
 
Posts: 8
Joined: Mon Jul 13, 2009 12:00 pm

Top

Postby edzillion » Thu Nov 19, 2009 4:52 pm

Ok set up my g1 and tested it on device, on that I get no textures at all. The device is running 1.6 which is my build target so no issue there; I was doing some reading and people were pointing to the glTexParameterf call as the source of the issue:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  2.  
  3.                 gl.glEnable(GL10.GL_DITHER);
  4.                 gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
  5.                 gl.glHint(GL10.GL_FOG_HINT, GL10.GL_NICEST);
  6.                 gl.glClearColor(0f, 0f, 0f, 1f);
  7.                 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  8.                 gl.glEnable(GL10.GL_DEPTH_TEST);
  9.                
  10.                 gl.glEnable(GL10.GL_TEXTURE_2D);               
  11.                 gl.glGenTextures(1, tex, 0);
  12.                 gl.glBindTexture(GL10.GL_TEXTURE_2D, tex[0]);
  13.                 gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
  14.                 gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
  15.  
  16.                 bitmap = Bitmap.createBitmap(16 * 16 * 4, 16, Bitmap.Config.ARGB_8888);
  17.                
  18.                 // Draw the texture in red/white hatched dots
  19.                 for (int y = 0; y < 16; y += 2) {
  20.                         for (int x = 0; x < 1024; x += 2) {
  21.                                 bitmap.setPixel(x, y, Color.WHITE);
  22.                                 bitmap.setPixel(x + 1, y, Color.RED);
  23.                         }
  24.                         for (int x = 0; x < 1024; x += 2) {
  25.                                 bitmap.setPixel(x, y + 1, Color.RED);
  26.                                 bitmap.setPixel(x + 1, y + 1, Color.WHITE);
  27.                         }
  28.                 }
  29.                 GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
  30.                 bitmap.recycle();
  31.                
  32.                 gl.glEnable(GL10.GL_ALPHA_TEST);
  33.                 gl.glAlphaFunc(GL10.GL_GREATER, 0.01f);
  34.  
  35.                 gl.glEnable(GL10.GL_FOG);
  36.                 gl.glFogx(GL10.GL_FOG_MODE, GL10.GL_LINEAR);
  37.                 gl.glFogf(GL10.GL_FOG_START, 2.5f);
  38.                 gl.glFogf(GL10.GL_FOG_END, 5f);
  39.                 float[] fogCol = new float[] { 0.0f, 0.0f, 0.0f, 1.0f };
  40.                 gl.glFogfv(GL10.GL_FOG_COLOR, fogCol, 0);
  41.  
  42.         }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


Also I noticed that there were some z-buffer issues when rotating with gl.glRotatef(angle, 0, 1.0f, 1.0f);

Image //wont show this image fsr
edzillion
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Oct 16, 2009 8:48 pm

Postby edzillion » Thu Nov 19, 2009 10:14 pm

Ok, got it. I needed to add the line:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
  2.                
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


here's the screen of that z-buffer issue:

Image
edzillion
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Oct 16, 2009 8:48 pm

Postby PaulT » Sat Dec 05, 2009 12:28 pm

Are you sure that is a z-buffer problem? Could it be that your facing polygons are being culled, or could it be a projection problem I wonder?

Paul.
Paul
PaulT
Freshman
Freshman
 
Posts: 8
Joined: Mon Jul 13, 2009 12:00 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests