Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (805)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
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  
  Textured Greedy Mesh - Solved  (Read 5174 times)
0 Members and 1 Guest are viewing this topic.
Offline Dev Crucio

Senior Devvie


Medals: 9
Projects: 1
Exp: 9 years


CaveGear


« Posted 2015-04-08 15:52:09 »

I'm working on a Sandbox game at the moment and wanted to improve the rendering of the Chunks.
I started to work myself into greedy meshing and not long after that I implemented it and it works fine now, the problem i have is: the faces don't all have the size of one Tile anymore, there's faces that have 2x2 Tiles meshed into one and if i put the texture on it, well obviously it stretches to fill the whole area. I want to display it so that it repeats the texture so it looks like it looked before. this wouldn't be a problem if the Tiles are all different textures ( i could just make the texcoord from 0 to 2 instead of from 0 to 1) but i have them all together in a spritesheet and no idea how i can repeat one part of it on a face.
I've read that i could write a fragment shader that uses modulo operations but i didn't want to implement shaders for now, so is there any other option for this?

Twitch - See me developing CaveGear Smiley
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2015-04-08 16:34:26 »

If you are willing to let go of your spritesheet, you can look at GL_TEXTURE_2D_ARRAY, where your texcoords have three dimensions (namely: u,v, layerIndex), where you pick your sprite with the layerIndex.

This allows repeating of sprites as per your demand. Limitation is that all sprites in the array must have the same dimensions, but with tile textures that's common. A workaround would be scaling-up-to-fit, or using multiple texture arrays, if you have distinct sets of dimensions.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline pitbuller
« Reply #2 - Posted 2015-04-08 19:43:28 »

Shaders should be first thing that you learn when you start to fiddle with graphics programming.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dev Crucio

Senior Devvie


Medals: 9
Projects: 1
Exp: 9 years


CaveGear


« Reply #3 - Posted 2015-04-09 13:31:15 »

i decided to go with the shaders (how difficult can that be?) since i don't know how to use texture2darsays or how to implement them in VBOs.

Frag Shader:
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  
varying vec3 vertNormal;
varying vec4 vertColor;
varying vec4 viewSpace;
uniform sampler2D tex;

void main(){
   if(viewSpace.z==0){
      vec4 texel = texture2D(tex,gl_TexCoord[0].st);
      texel.r=texel.r*vertColor.r;
      texel.g=texel.g*vertColor.g;
      texel.b=texel.b*vertColor.b;
      texel.a=texel.a*vertColor.a;
      gl_FragColor = texel;
   }else{
      float x = gl_TexCoord[0].s*32-mod(gl_TexCoord[0].s*32,1);
      float y = gl_TexCoord[0].t*32-mod(gl_TexCoord[0].t*32,1);
      float fx = gl_TexCoord[0].s*32-x;
      float fy = gl_TexCoord[0].t*32-y;
      float tx = mod(fx*viewSpace.z,1);
      if(tx<=0.001){tx=0.001;}
      if(tx>=0.999){tx=0.999;}
      float ty = mod(fy*viewSpace.z,1);
      if(ty<=0.001){ty=0.001;}
      if(ty>=0.999){ty=0.999;}
      float finalx = (x+tx)/32;
      float finaly = (y+ty)/32;
      vec2 modded = vec2(finalx,finaly);
      vec4 texel = texture2D(tex,modded);
      texel.r=texel.r*vertColor.r;
      texel.g=texel.g*vertColor.g;
      texel.b=texel.b*vertColor.b;
      texel.a=texel.a*vertColor.a;
      gl_FragColor = texel;
      }
   }  
}


Vert Shader:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
varying vec3 vertNormal;
varying vec4 vertColor;
varying vec4 viewSpace;

void main(){
   gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
   vertNormal = normalize(gl_NormalMatrix * gl_Normal);
   vertColor = gl_Color;
   viewSpace = gl_Vertex;
   gl_TexCoord[0] = gl_MultiTexCoord0;
}


the result:


Some blue lines between (I could understand that for rounding issues or something)
and some brown/yellowish gradient which makes no sense for me (the texture doesn't have this kind of gradient next to the grey texture)

in the vertex shader the 0.001 and 0.999 are there so the tex coordinates never belong to a nearby texture, i don't understand where the problem lies Undecided

Edit: I use the z coordinate to see how often the tile has to be repeated in the current face (1= normal, 2= it's a 2x2 quad, 4= it's a 4x4 quad)

Twitch - See me developing CaveGear Smiley
Offline theagentd
« Reply #4 - Posted 2015-04-09 14:59:31 »

Oh, god, the horror.

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  
44  
45  
46  
47  
48  
49  
#version 110

varying vec3 vertNormal;
varying vec4 vertColor;
varying vec4 viewSpace;
uniform sampler2D tex;

void main(){

   vec2 texCoords = gl_TexCoord[0].st;
   if(viewSpace.z==0){
      /*vec4 texel = texture2D(tex,gl_TexCoord[0].st);
      texel.r=texel.r*vertColor.r;
      texel.g=texel.g*vertColor.g;
      texel.b=texel.b*vertColor.b;
      texel.a=texel.a*vertColor.a;
      gl_FragColor = texel;*/

      gl_FragColor = vertColor * texture2D(tex, texCoords);

   }else{
      /*float x = gl_TexCoord[0].s*32-mod(gl_TexCoord[0].s*32,1);
      float y = gl_TexCoord[0].t*32-mod(gl_TexCoord[0].t*32,1);
      float fx = gl_TexCoord[0].s*32-x;
      float fy = gl_TexCoord[0].t*32-y;*/

      vec2 xy = floor(texCoords*32);
      vec2 fxy = texCoords*32 - xy;

      /*float tx = mod(fx*viewSpace.z,1);
      if(tx<=0.001){tx=0.001;}
      if(tx>=0.999){tx=0.999;}
      float ty = mod(fy*viewSpace.z,1);
      if(ty<=0.001){ty=0.001;}
      if(ty>=0.999){ty=0.999;}*/

      vec2 txy = clamp(fract(fxy * viewSpace.z), 0.001, 0.999);

      /*float finalx = (x+tx)/32;
      float finaly = (y+ty)/32;
      vec2 modded = vec2(finalx,finaly);*/

      vec2 modded = (xy+txy) / 32.0;

      /*vec4 texel = texture2D(tex, modded);
      texel.r=texel.r*vertColor.r;
      texel.g=texel.g*vertColor.g;
      texel.b=texel.b*vertColor.b;
      texel.a=texel.a*vertColor.a;
      gl_FragColor = texel;*/

      gl_FragColor = vertColor * texture2D(tex, modded);
   }  
}

Myomyomyo.
Offline Dev Crucio

Senior Devvie


Medals: 9
Projects: 1
Exp: 9 years


CaveGear


« Reply #5 - Posted 2015-04-09 15:02:23 »

well, my first try at GLSL i don't know that i can directly multiply vectors etc.  Shocked

Twitch - See me developing CaveGear Smiley
Offline theagentd
« Reply #6 - Posted 2015-04-09 15:06:39 »

Even better version as there is it gets rid of one branch:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
#version 110

varying vec3 vertNormal;
varying vec4 vertColor;
varying vec4 viewSpace;
uniform sampler2D tex;

void main(){

   vec2 texCoords = gl_TexCoord[0].st;
   if(viewSpace.z != 0.0){

      vec2 xy = floor(texCoords*32);
      vec2 fxy = texCoords*32 - xy;

      vec2 txy = clamp(fract(fxy * viewSpace.z), 0.001, 0.999);

      texCoords = (xy+txy) / 32.0;
   }
   
   gl_FragColor = vertColor * texture2D(tex, texCoords);
}

Myomyomyo.
Offline Dev Crucio

Senior Devvie


Medals: 9
Projects: 1
Exp: 9 years


CaveGear


« Reply #7 - Posted 2015-04-09 15:09:20 »

okey, but the problem with the lines is still there? :C

Twitch - See me developing CaveGear Smiley
Offline theagentd
« Reply #8 - Posted 2015-04-09 15:30:34 »

What kind of filtering do you use on the texture?

Myomyomyo.
Offline Dev Crucio

Senior Devvie


Medals: 9
Projects: 1
Exp: 9 years


CaveGear


« Reply #9 - Posted 2015-04-09 15:37:57 »

I use this in my texture init code:

      
1  
2  
3  
4  
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S,GL11.GL_REPEAT);
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T,GL11.GL_REPEAT);
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER,GL11.GL_NEAREST);
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,GL11.GL_LINEAR_MIPMAP_LINEAR);


edit: using png decoder

Twitch - See me developing CaveGear Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #10 - Posted 2015-04-09 17:14:17 »

Linear filtering can cause bleeding. Try to use GL_NEAREST for both.

Myomyomyo.
Offline Dev Crucio

Senior Devvie


Medals: 9
Projects: 1
Exp: 9 years


CaveGear


« Reply #11 - Posted 2015-04-09 17:50:05 »

that solved the problem, thx Smiley

Twitch - See me developing CaveGear Smiley
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (585 views)
2019-09-04 15:33:17

hadezbladez (5526 views)
2018-11-16 13:46:03

hadezbladez (2406 views)
2018-11-16 13:41:33

hadezbladez (5788 views)
2018-11-16 13:35:35

hadezbladez (1227 views)
2018-11-16 13:32:03

EgonOlsen (4666 views)
2018-06-10 19:43:48

EgonOlsen (5685 views)
2018-06-10 19:43:44

EgonOlsen (3200 views)
2018-06-10 19:43:20

DesertCoockie (4100 views)
2018-05-13 18:23:11

nelsongames (5117 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!