Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [OpenGL ES] NPOT Texture rendering (Z fighting) when using triangle fans to draw  (Read 484 times)
0 Members and 1 Guest are viewing this topic.
Offline tom_mai78101
« Posted 2014-01-12 06:23:53 »

I'm following along the book, OpenGL ES 2 for Android : A Quick Start Guide, in Chapter 8, texture rendering.

The codes I followed along were typed according to the book. The texture I used in my program is a custom 128x256 image, while the texture the book used is a 512x1024 image. I figured since both texture image ratios are the same, the textures shouldn't be causing any issues.

Below is an image of a screenshot I took in Android, concerning the Z fighting.



And here's the code, mostly the same as in the book.:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
{
   private static final int POSITION_COMPONENT_COUNT=2;
   private static final int TEXTURE_COORDS_COMPONENT_COUNT=2;
   private static final int STRIDE=(POSITION_COMPONENT_COUNT+TEXTURE_COORDS_COMPONENT_COUNT)*Constants.BYTES_PER_FLOAT;
   
   private static float[] VERTEX_DATA={
     
      //using a different texture.
     0f,0f,0.5f,0.5f,
      -0.5f, -0.8f, 0f,0f,
      0.5f,-0.8f,1f,0f,
      0.5f,0.8f,1f,1f,
      -0.5f,0.8f,0f,1f,
      -0.5f,-0.8f,0f,0f
   };
   private final VertexArray vertexArray;
   
   public Table(){
      vertexArray=new VertexArray(VERTEX_DATA);
   }
   
   public void bind(TextureShaderProgram program){
     
      vertexArray.setVertexAttribPointer(
         POSITION_COMPONENT_COUNT,
         program.getTextureCoordsAttributeLocation(),
         TEXTURE_COORDS_COMPONENT_COUNT,
         STRIDE
      );
      vertexArray.setVertexAttribPointer(
         0,
         program.getPositionAttributeLocation(),
         POSITION_COMPONENT_COUNT,
         STRIDE
      );
     
   }
   
   public void draw(){
      //GLES20.glFrontFace(GLES20.GL_CCW);
     GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN,0,6);
   }
}



The problem is in the center of the screenshot, the white link flickers through the blue portion of the texture rapidly. My guess would be that tje triangle fans were drawn incorrectly or something, but I couldn't think of any other ways of drawing triangle fans that doesn't work as it is supposed to do.

Any clues?
Offline opiop65

JGO Kernel


Medals: 123
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #1 - Posted 2014-01-12 06:25:27 »

I don't think I see an issue, can you post an example of what it should look like?
Shouldn't the book also include a site for all the resources you're supposed to use?

Offline tom_mai78101
« Reply #2 - Posted 2014-01-12 06:36:32 »

The white line shouldn't  be drawn going through the table, that's it. I don't have a good example available to show what it should look like, but here is the texture, and how it should look when it is laid flat, like in the screenshot.
Quote



The book's example and my project are the same, I just swapped out the textures.

Is it okay enough?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online theagentd
« Reply #3 - Posted 2014-01-12 19:32:24 »

Why are you rendering 6 vertices? A triangle fan only requires 4 vertices to form a quad. If the problem really is Z-fighting, it's because you're rendering two overlapping triangles at almost the same depth. The flickering is due to rounding differences between the two triangles.

Myomyomyo.
Offline Danny02
« Reply #4 - Posted 2014-01-12 20:17:42 »

I did check that already, but didn't see an error. Of course theagentd is right that one only needs 4 vertices for a quad, but his approach should be correct too. He starts in the middle and then goes around the four corners (first needs to be stored twice).

I don't get why the texture is so f**ked up either.
Online theagentd
« Reply #5 - Posted 2014-01-12 23:57:46 »

The texture is completely distorted, so his coordinates are incorrect in that case. Try rendering fewer vertices to narrow it down.

Myomyomyo.
Offline tom_mai78101
« Reply #6 - Posted 2014-01-13 00:27:34 »

Ok, I will try to cut down on the number of vertices.

More frustratingly, the rest of the Table-related codes are just initializing variables, loading textures, and binding them.

TextureHelper.java
http://pastebin.java-gaming.org/d44b6608a7e

TextureShaderProgram
http://pastebin.java-gaming.org/44b607a8e7d
Offline tom_mai78101
« Reply #7 - Posted 2014-01-13 01:50:51 »

Some more pictures of the table flickering, taken within a second of each other: Clueless




Here is the updated code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
   private static float[] VERTEX_DATA={
     
      //using a different texture.
     -1f,    1f, 0f, 0f,
      -1f, -1f, 0f, 1f,    
      1f, -1f,   1f, 1f
      //0.5f,  0.8f,   1f, 0.1f,
       //-0.5f,  0.8f,   0f, 0.1f,
       //-0.5f, -0.8f,   0f, 0.9f
  };


That is all I changed. It looks like there is more to the perplexed texture distortion.

Online theagentd
« Reply #8 - Posted 2014-01-13 16:14:29 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
       vertexArray.setVertexAttribPointer(
         POSITION_COMPONENT_COUNT,
         program.getTextureCoordsAttributeLocation(),
         TEXTURE_COORDS_COMPONENT_COUNT,
         STRIDE
      );
      vertexArray.setVertexAttribPointer(
         0,
         program.getPositionAttributeLocation(),
         POSITION_COMPONENT_COUNT,
         STRIDE
      );

I assume the first argument is the offset into the buffer. That should be in bytes, not components. You need to multiply that by 4 since you use floats.

Myomyomyo.
Offline tom_mai78101
« Reply #9 - Posted 2014-01-14 10:01:22 »

It didn't change a thing when I multiplied the values by 4. I tried changing the GL_FLOAT to GL_UNSIGNED_BYTE, but it made the texture unrenderable.

Here's the code for the VertexArray object:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
package z.a.data;
import java.nio.FloatBuffer;
import java.nio.ByteBuffer;
import z.a.RenderView;
import java.nio.ByteOrder;
import android.opengl.GLES10;
import android.opengl.GLES20;
import z.a.Constants;

public class VertexArray
{
   private final FloatBuffer floatBuffer;
   
   public VertexArray(float[] vertexData){
      floatBuffer=ByteBuffer.allocateDirect(vertexData.length *Constants.BYTES_PER_FLOAT)
      .order(ByteOrder.nativeOrder())
      .asFloatBuffer()
      .put(vertexData, 0, vertexData.length);
   }
   
   public void setVertexAttribPointer(int dataOffset, int attributeLocation, int componentCount, int stride){
      floatBuffer.position(dataOffset);
      GLES20.glVertexAttribPointer(attributeLocation, componentCount,GLES20.GL_FLOAT, false, stride, floatBuffer);
      GLES20.glEnableVertexAttribArray(attributeLocation);
      //floatBuffer.position(0);
  }
}


I thought it is possible the buffer hasn't been rewinded, but that didn't work either.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online theagentd
« Reply #10 - Posted 2014-01-14 14:51:54 »

Since you're setting the position of a FloatBuffer, the offset should indeed be in the number of floats. I was under the impression that you were using vertex buffer objects.

There's a huge number of things that could be going wrong actually. Try changing the shader to not sample the texture. Instead just have it output the texture coordinates to the red and blue channels. You need to figure out exactly where it's going wrong.

Myomyomyo.
Offline tom_mai78101
« Reply #11 - Posted 2014-01-15 04:54:04 »

Okay, if I can't solve it, I may as well just call it unsolveable, and redo the whole thing over.
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (23 views)
2014-04-15 18:08:23

BurntPizza (18 views)
2014-04-15 03:46:01

UprightPath (32 views)
2014-04-14 17:39:50

UprightPath (16 views)
2014-04-14 17:35:47

Porlus (32 views)
2014-04-14 15:48:38

tom_mai78101 (58 views)
2014-04-10 04:04:31

BurntPizza (116 views)
2014-04-08 23:06:04

tom_mai78101 (216 views)
2014-04-05 13:34:39

trollwarrior1 (183 views)
2014-04-04 12:06:45

CJLetsGame (190 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!