GL_DEPTH_TEST does not work for ofImage without thinking drawing order?

I put “.png” alpha images as ofImage instance in 3D world and I would like to apply those images “depth” order.
I’ve already knew that when enable GL_DEPTH_TEST to image with alpha channel, write ‘glDepthMask(false);’ in function draw().

http://forum.openframeworks.cc/t/billboard-particles-depth-testing/5458/0

But, I still does not succeed To apply the DEPTH to ofImage Instance without thinking drawing order.

[hr]

I create two images(actually instance is one), just each instance position and color is different.
The folloing code, we see the red image is my side, The red image’s z-position is smaller than the white one.

\#include "testApp.h"

// red image position
ofVec3f redPos;

// write image position
ofVec3f whitePos;

ofImage circleImage;

//--------------------------------------------------------------
void testApp::setup(){
ofBackground(120);
ofSetFrameRate(60);
ofEnableAlphaBlending();

/*
* set up position
*/
// [RED image] z axis is zero.
redPos.set(ofGetWidth()/2, ofGetHeight()/2, 0);

// [WHITE image] z axis is 100 that expected white image is near side( this side ).
whitePos.set(ofGetWidth()/2, ofGetHeight()/2, 100);

/**
* load image
*/
circleImage.loadImage(“particle32.png”);
}

//--------------------------------------------------------------
void testApp::update(){
}

//--------------------------------------------------------------
void testApp::draw(){
glDepthMask(false);
glEnable(GL_DEPTH_TEST);

// draw WHITE image.
ofSetColor(255,255,255);
ofPushMatrix();
ofTranslate(whitePos);
circleImage.draw(0, 20.0, 20.0);
ofPopMatrix();

// draw RED image.
ofSetColor(255,0,0);
ofPushMatrix();
ofTranslate(redPos);
circleImage.draw(0, 20.0, 20.0);
ofPopMatrix();

glDisable(GL_DEPTH_TEST);

}

However, I change the draw order of the each image, The white image is my side which is my WISH !
( Just change the order of each drawing code. Draw “red image” first, “white image” second. )

Do I need to think the order when write draw code for images to apply the depth ?

Please Any suggestion !

//-------------------------------------------------------------- void testApp::draw(){ glDepthMask(false); glEnable(GL\_DEPTH\_TEST);

// draw RED circle.
ofSetColor(255,0,0);
ofPushMatrix();
ofTranslate(redPos);
circleImage.draw(0, 20.0, 20.0);
ofPopMatrix();

// draw WHITE circle.
ofSetColor(255,255,255);
ofPushMatrix();
ofTranslate(whitePos);
circleImage.draw(0, 20.0, 20.0);
ofPopMatrix();

glDisable(GL_DEPTH_TEST);

}

Generally to do this properly, you need to use shaders, which is kind of a pain. A simple technique I’ve seen used for this is to disable GL_DEPTH and simply sort all the things you’re drawing so that each one is drawn in order, i.e. draw z=100, then z=50, then z=10, then z=0.

This article might kind of help: http://www.opengl.org/wiki/Transparency-Sorting

There’s some better articles out there but I can’t seem to find any that don’t refer to another toolkit.

hi people !

quite an old thread but i’m facing the same issue … basically i need to render a 3d simple scene.
i would like to be able to use alpha, so transparency and i also need to have dept testing to draw the scene properly …

so is there any combination of ofEnableAlphaBlending and ofEnableDepthTest that allows you to draw with “alpha” transparency and kind of “depth sorted” ?

is EnableAlphaBlending part of the ofStyle tag ? so each time i do a ofPushStyle, do i need to tell again if i want or not to ofEnableAlphaBlending ? same with ofEnableDepthTest … do i need to reset it at each ofPushStyle ?

joshua … which is the main idea behind using shaders for this ? could you give a small clue for this ?

thanks !

-> depth sorting without GL_DEPTH buffer

A quick question I am about to start adding this to my code after going crazy as to why my alpha got lost when I put my images in a 3d scene. Maybe this is a dumb question but if my images are inside easycam do I have to depth sort based on the cameras point of view? For example if I depth sort from the front to back and draw the images in that order, do I then have to alter the order I draw if I use easycam to navigate to the back view?

And same goes for the other angles, in my program images rotate around any axis and all have transparencies. Do I have to calculate the draw order for every viewpoint?