Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  LWJGL Orthographic projection render order based on Z  (Read 676 times)
0 Members and 1 Guest are viewing this topic.
Offline trollwarrior1
« Posted 2014-04-04 10:08:05 »

My problem is that everything renders in order: newest vertices appear on top of older ones. I want vertices with that are pointing out of the screen (Z negative?) to be rendered on top of vertices that are into the screen (Z positive?)

Atm I'm using glOrtho from GL11 class.
1  
2  
      glMatrixMode(GL_PROJECTION);
      glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);


Here is my vertex shader.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
#version 120

attribute vec3 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord;

varying vec4 v_color;
varying vec2 v_texCoords;


void main() {
   vec4 pos = vec4(a_position, 1);
   
   v_color = a_color;
   v_texCoords = a_texCoord;
   
   mat4 proj = gl_ProjectionMatrix;
   gl_Position = proj*pos;
}


Here is the image i'm getting.


Here is how I want it to look after changing Z values (I changed render order)



I think this is a problem with projection matrix.
Here is my spritebatcher, I hope I didn't make silly mistake when implementing Z axis..
http://www.java-gaming.org/?action=pastebin&id=892

It has few bugs, but they don't bother me. (Not flushing when spritesheet is different and some others maybe) and also it is not very efficient, but I don't really need efficiency here.
For now I just want to resolve the Z axis.
Offline trollwarrior1
« Reply #1 - Posted 2014-04-04 10:12:22 »

Hmmmmmmmmm. I think glEnable(GL_DEPTH_TEST) solved the problem...
Offline trollwarrior1
« Reply #2 - Posted 2014-04-07 10:11:52 »

I just added some alpha to my images.

Turns out that alpha blending doesn't work well with GL_DEPTH_TEST.

If anyone knows how to fix my problem in some other way, would be really nice, because I'm sick of drawing objects in correct order..
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Gibbo3771
« Reply #3 - Posted 2014-04-07 11:46:53 »

Have all renderable objects hold a Z-Index field.

Sort the array using that field, then happily draw.

No idea how much stuff you are having on the screen but this is always how I do it, if you have an array with 1000's of objects that can be rendered, then you might have some issues when sorting.

If your iterating through an array, it renders in order from 0 > size.

EDIT: Just re-read your post, my suggestion is probably useless.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline theagentd

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #4 - Posted 2014-04-07 12:39:43 »

You generally don't want to use depth testing with 2D due to the transparency problem. The most common solution for rendering transparent stuff with a depth buffer (regardless of 2D or 3D) is to disable depth writes and sort your objects. If you only need additive blending you don't need to sort your objects, but this is rarely the case.

Concerning sorting performance, I wouldn't worry that much. The order of your objects will not change much between frames, so I'd strongly recommend implementing a simple insertion sort. Insertion sort has the advantage of being O(n) if the list is already sorted and requires no temporary memory. Since we can assume that the order does not change much from frame to frame insertion sort will be extremely fast (except for the first frame when your objects are completely unsorted).

Myomyomyo.
Offline theagentd

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #5 - Posted 2014-04-07 13:51:48 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
import java.util.Comparator;

public class InsertionSort {
   
   public static <T> void sort(T[] array, int start, int end, Comparator<T> comparator){
     
      for(int i = start + 1; i < end; i++){

         T current = array[i];
         
         int j = i-1;
         while(j >= start && comparator.compare(array[j], current) < 0){
            array[j+1] = array[j];
            j--;
         }
         array[j+1] = current;
      }
     
   }
   
}


Sorting 1 000 objects
Quote
Insertion sort unsorted: 0.275 ms
Insertion sort sorted:   0.003 ms
Java sort unsorted: 0.083 ms
Java sort sorted:   0.002 ms
Java parallel sort unsorted: 0.084 ms
Java parallel sort sorted:   0.002 ms
First elements: 19369, 19369, 19369

Sorting 10 000 objects
Quote
Insertion sort unsorted: 56.627 ms
Insertion sort sorted:   0.156 ms
Java sort unsorted: 1.912 ms
Java sort sorted:   0.05 ms
Java parallel sort unsorted: 0.718 ms
Java parallel sort sorted:   0.11 ms
First elements: 15, 15, 15

Sorting 100 000 objects
Quote
Insertion sort unsorted: 5576.639 ms
Insertion sort sorted:   1.349 ms
Java sort unsorted: 15.842 ms
Java sort sorted:   0.459 ms
Java parallel sort unsorted: 4.505 ms
Java parallel sort sorted:   0.742 ms
First elements: 45, 45, 45

As you can see, insertion sort scales horribly with the number of objects when they are unsorted, but linearly when they are already sorted. Although Java's Arrays.sort() method is faster in all cases, it generates a large amount of garbage which may cause problems with garbage collection pauses.

Myomyomyo.
Offline trollwarrior1
« Reply #6 - Posted 2014-04-07 15:31:54 »

The performance isn't the issue for me.. Its the messiness of the code.. I just implemented something like you said (I had done in the past), but I'm not content with the results. Since I'm using shaders and sprite batch, I still need to keep the order of the drawing, even though I implemented this, since you sometimes need to flush more than once per frame, like changing projection/modelview matrix.
Offline matheus23

JGO Kernel


Medals: 114
Projects: 3


You think about my Avatar right now!


« Reply #7 - Posted 2014-04-07 15:45:42 »

You generally don't want to use depth testing with 2D due to the transparency problem. The most common solution for rendering transparent stuff with a depth buffer (regardless of 2D or 3D) is to disable depth writes and sort your objects. If you only need additive blending you don't need to sort your objects, but this is rarely the case.

What if I have two seperate lists:
1. Sprites that only have pixels with alpha 1 or alpha 0 or Sprites that are supposed to be rendered with additive blending,
2. Sprites that have pixels with other alpha values between 1 and 0.

Now, all sprites have some kind of "layer" variable, or in other words: A Z-position.
I don't need to sort list 1. I can simply render it non-sorted with additive blending, alpha test and depth testing enabled.
I sort list 2, then render it with normal blending and depth testing disabled.

Transparency should work and everything should be faster, no? Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #8 - Posted 2014-04-07 16:49:16 »

You generally don't want to use depth testing with 2D due to the transparency problem. The most common solution for rendering transparent stuff with a depth buffer (regardless of 2D or 3D) is to disable depth writes and sort your objects. If you only need additive blending you don't need to sort your objects, but this is rarely the case.

What if I have two seperate lists:
1. Sprites that only have pixels with alpha 1 or alpha 0 or Sprites that are supposed to be rendered with additive blending,
2. Sprites that have pixels with other alpha values between 1 and 0.

Now, all sprites have some kind of "layer" variable, or in other words: A Z-position.
I don't need to sort list 1. I can simply render it non-sorted with additive blending, alpha test and depth testing enabled.
I sort list 2, then render it with normal blending and depth testing disabled.

Transparency should work and everything should be faster, no? Smiley
The problem with this approach is that usually you'd want blending on all your sprites to get nice and smooth edges as alpha testing leads to clearly visible aliasing. You could enable multisampling and alpha-to-coverage to improve that, but that'd make the second pass do blending into a multisampled buffer which is significantly slower.

Myomyomyo.
Offline Slyth2727
« Reply #9 - Posted 2014-04-07 23:11:37 »

Funny enough, I was messing around with GLSL yesterday and I found an interesting little tweak. If you set gl_Position.z to equal 0.1f then you get all the vertices rendering, retaining perspetive. I would think that this would create some sort of flat shader, but now that i think of it, it makes perfect sense. This may or may not relate to what you were asking, I just thought I'd put it out there  Roll Eyes

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (35 views)
2014-12-15 09:26:44

Mr.CodeIt (26 views)
2014-12-14 19:50:38

BurntPizza (60 views)
2014-12-09 22:41:13

BurntPizza (95 views)
2014-12-08 04:46:31

JscottyBieshaar (55 views)
2014-12-05 12:39:02

SHC (71 views)
2014-12-03 16:27:13

CopyableCougar4 (73 views)
2014-11-29 21:32:03

toopeicgaming1999 (133 views)
2014-11-26 15:22:04

toopeicgaming1999 (124 views)
2014-11-26 15:20:36

toopeicgaming1999 (34 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!