Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

877826 Posts in 32885 Topics- by 24319 Members - Latest Member: NotoriousPyro

May 20, 2013, 02:05:41 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)OpenGL Draw 3D Quad in same X Y and Z location
Pages: [1]
Print
Author Topic: OpenGL Draw 3D Quad in same X Y and Z location  (Read 748 times)
Maud'Dib Atreides
Level 4
****


Obsessed with space


View Profile WWW Email
« on: April 19, 2012, 03:31:47 PM »

I'm sure anyone who's used OpenGL can solve this:

Hello, I need help. I'm drawing quads with GLBegin in 3D, then binding a texture.

What my code does:
I have 2 slightly transparent textures.
I draw 2 Quads of the same size, and same location.

Draw a quad in 3D with one texture (Quad A)
Then, draw another quad in 3D with one texture at the same position. (Quad B)

What's Wrong:

Whenever I draw the Quad B, it replaces the texture on Quad A.
Quad B's texture completely removes Quad A's texture and only the texture on Quad B shows.

What I want to do:
Have the texture on Quad B overlap the texture on Quad A, so I can see one drawn over the other at once, while having both Quad A and Quad B sharing the same X, Y, and Z locations.
That part is done.

Is there any way to draw 2 quads of the same size and same location with different textures, and have the second texture drawn simply overlap the first texture drawn, rather than replace it?

Thank you.   Wink
Logged

Guy: Give me all of your money.
Chap: You can't talk to me that way, I'M BRITISH!
Guy: Well, You can't talk to me that way, I'm brutish.
Chap: Somebody help me, I'm about to lose 300 pounds!
Guy: Why's that a bad thing?
Chap: I'M BRITISH.
nospoon
Level 1
*


View Profile Email
« Reply #1 on: April 19, 2012, 04:03:41 PM »

#1 . Do you have transparency turned ON ? ( GL_BLEND )
Code:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

#2 . If you are drawing them at the same Z coordinates, and you should have turned off the z-buffer
Code:
glDisable(GL_DEPTH_TEST);

So basically :
1.enable transparency (you can check if its working by drawing one quad w/ texture on a glClearColor(1,0,1) or something)
2.disable depth buffer
3.bind texture A
4.draw quad
5.bind tex B
6.draw quad

Should work Smiley
Logged
Maud'Dib Atreides
Level 4
****


Obsessed with space


View Profile WWW Email
« Reply #2 on: April 19, 2012, 05:50:15 PM »

Thank you, but it's important that the quads are drawn in 3D. I need to have the quads drawn in 3D, rather than 2d.

I'll later need to rotate the scene, so I'll need the quads to be drawn in 3D in the Depth Buffer.

Is there a way to draw without disabling the depth buffer?
Logged

Guy: Give me all of your money.
Chap: You can't talk to me that way, I'M BRITISH!
Guy: Well, You can't talk to me that way, I'm brutish.
Chap: Somebody help me, I'm about to lose 300 pounds!
Guy: Why's that a bad thing?
Chap: I'M BRITISH.
Richard Kain
Level 10
*****



View Profile WWW Email
« Reply #3 on: April 19, 2012, 10:56:59 PM »

Is there a way to draw without disabling the depth buffer?

Yes, actually, there is. Try this...

Code:
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glAlphaFunc(GL_GREATER, 0.1f);
glEnable(GL_ALPHA_TEST);

The first bit is important. Enabling depth testing is pretty standard, but the second line is where the magic happens. The default depth testing in OpenGL will make whatever you draw first, show up on top. This is the reverse of how a lot of other drawing solutions work. By setting your glDepthFunc to GL_LEQUAL, you reverse this effect. Now the last thing you draw will show up on top.

The rest of it is just to set up your blending so you'll get nice clean transparencies.
Logged
nospoon
Level 1
*


View Profile Email
« Reply #4 on: April 20, 2012, 12:00:32 AM »

Also you could try to do this using shaders which would probably be a better way.
Logged
Revk
Level 0
**



View Profile WWW Email
« Reply #5 on: April 20, 2012, 08:41:48 AM »

Why don't you just use multitexturing? Because two overlapping textures at the same exact position just call for that.
Logged
Maud'Dib Atreides
Level 4
****


Obsessed with space


View Profile WWW Email
« Reply #6 on: April 20, 2012, 02:41:38 PM »

Thank you everyone.

But Richard Kain, you were right on point. I wanted to avoid shaders seeing as how I'm working with the lowest supported tech of OpenGL and I want to also target devices that don't use OpenGL shading.

Your solution worked perfectly. I can't thank you enough, Richard Kain.

Best of luck in your life

~#Sharp
Logged

Guy: Give me all of your money.
Chap: You can't talk to me that way, I'M BRITISH!
Guy: Well, You can't talk to me that way, I'm brutish.
Chap: Somebody help me, I'm about to lose 300 pounds!
Guy: Why's that a bad thing?
Chap: I'M BRITISH.
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic