Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (777)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
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  
  2D label on objects in 3D (LWJGL)  (Read 1968 times)
0 Members and 1 Guest are viewing this topic.
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Posted 2014-01-26 07:55:08 »

In the end I want to be able to create labels like the player names in the below image,

however, I want them to remain the same size no matter how far away or close up the camera is. My first thought is that the labels would have to be rendered orthographically, but then how can I tell where to render them (in order to be above their player)? I figured maybe some kind of backwards ray-tracing, but I'm not sure how this could be done, let alone if this is even the answer.

My question is how can I render labels orthographically over objects rendered with perspective?
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 167
Projects: 5
Exp: 6 years



« Reply #1 - Posted 2014-01-26 07:57:02 »

What library are you using? In jMe you just add nodes to the guiNode. In general, you just render the gui separately (separate camera).
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Reply #2 - Posted 2014-01-26 07:58:05 »

LWJGL, sorry, I'll put that in the title.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nerb
« Reply #3 - Posted 2014-01-26 08:14:04 »

I'm pulling this out of thin air, but I think you'd need to run the label's position through the same projection as the character in order to get the label's position in screen space. Then you apply your own scale to it. If your using prog. pipeline, then it should be easy enough to implement in a shader. No idea how you'd accomplish it in fixed func.
Offline zFollette

Junior Devvie


Exp: 2 years


I like jokes


« Reply #4 - Posted 2014-01-26 08:17:03 »

If you want to mix 2d and 3d, you are going to have to switch between 2D and 3D (gluPerspective and glOrtho). The methods are VERY similar to basic 2d loading and 3d loading:

1  
2  
3  
4  
5  
6  
7  
private void make3D() {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(fov, width / height, zNear, zFar);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }


Just the basic setup with a call to loadIdentity at the end, same for 2D:

1  
2  
3  
4  
5  
6  
7  
private void make2D() {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, width, height, 0, -1, 1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }


Of course, you do have to enable/disable everything you need on every call to these methods.
Here is an example of implementation:

1  
2  
3  
4  
5  
6  
private void render() {
make3D();
//draw 3d stuff
make2D();
//draw2D stuff
}

Humor will keep you alive.
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Reply #5 - Posted 2014-01-26 08:37:20 »

to zFollette: thanks for the help, at first I though it was that simple, but then I realized If I'm rendering the labels on a 2D plane (the screen) how am I to tell where to render the label so that it's over top of the player in 3D?

to nerb: That was my guess too (pretty much reverse ray tracing), and now that I know I just have to find out how I could use the perspective matrix to cast the 3D coordinate to the a 2D coordinate on the screen.
Offline nerb
« Reply #6 - Posted 2014-01-26 08:47:43 »

I just have to find out how I could use the perspective matrix to cast the 3D coordinate to the a 2D coordinate on the screen.

You're 3/4 of the way there then. Just multiply your coordinate by the projection matrix. How are you with matrix math? A quick google should reveal all, if you are uncertain.

EDIT: This works if by 'perspective matrix' you mean the MVP matrix??? But that's essentially what you'll need to do. Send the point through all the same transformations as the rest of the model, and then bingo!
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Reply #7 - Posted 2014-01-26 09:12:04 »

Ah! I forgot vertices can be used in matrix math Tongue

Thanks for clearing things up, now it's just up to me to work out the grimy details. Thanks to everyone who replied so quickly! I'll post how I did it when I do Cheesy
Offline Opiop
« Reply #8 - Posted 2014-01-26 11:56:08 »

Matrices are used in OpenGL a ton so I would get used to them!
Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

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

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

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

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

nelsongames (1646 views)
2018-04-24 18:15:36

nelsongames (2297 views)
2018-04-24 18:14:32
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

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!