Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
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  
  Texture always facing the camera  (Read 2105 times)
0 Members and 1 Guest are viewing this topic.
Offline Blunderchips
« Posted 2015-01-18 17:06:38 »

How would you get a texture to always face the camera in LWJGL?
-siD-
Offline quew8

JGO Knight


Medals: 53



« Reply #1 - Posted 2015-01-18 17:39:59 »

This is called billboarding. And there are essentially two methods or doing it.
1) Don't use the camera rotation as part of the view transform when you are billboarding. The translation part yes but not the rotation method.

2) You can create the geometry you're rendering using the camera's left and up axis for the geometry's left and up axes. Example:
To render a quad of width "w" and height "h" at position "P" with a camera left axis "L" and up axis "U", then the 4 points of the quad would be:
P,
P + w*L,
P + w*L + h*U,
p + h*U

For more help, ask a more specific question and give some information about your current setup.
Offline Blunderchips
« Reply #2 - Posted 2015-01-18 18:45:31 »

I will be using it for a 2D sun texture in my world, for my setup I have a world class that stores the position of both the player position and the sun position and handles where the sun should in the world be based on the current time of day and the current location of the player in the world.

With the points P, P + w*L, P + w*L + h*U, p + h*U how would I plot them to get the texture to billboard?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mike

« JGO Spiffy Duke »


Medals: 149
Projects: 1
Exp: 6 years


Java guru wannabe


« Reply #3 - Posted 2015-01-18 18:56:08 »

Something like this (You should use a VBO or something though instead of direct mode, but at least it gives you a point of reference) Smiley
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  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
private void drawSun(final Point3DFloat upVector, final Point3DFloat rightVector) {
        float sunSize = 7;
        float sunDistance = Config.getRenderDistance() / 2;
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, TextureContainer.getTexture(PictureContainer.SUN[0]));
        GL11.glBegin(GL11.GL_TRIANGLES);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(
            Camera.getPosition().getX()
                + LIGHTPOSITION.get(0)
                * sunDistance
                - (rightVector.getX() + upVector.getX())
                * sunDistance
                / sunSize, Camera.getPosition().getY()
                + LIGHTPOSITION.get(1)
                * sunDistance
                - (rightVector.getY() + upVector.getY())
                * sunDistance
                / sunSize, Camera.getPosition().getZ()
                + LIGHTPOSITION.get(2)
                * sunDistance
                - (rightVector.getZ() + upVector.getZ())
                * sunDistance
                / sunSize);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex3f(
            Camera.getPosition().getX()
                + LIGHTPOSITION.get(0)
                * sunDistance
                + (rightVector.getX() - upVector.getX())
                * sunDistance
                / sunSize, Camera.getPosition().getY()
                + LIGHTPOSITION.get(1)
                * sunDistance
                + (rightVector.getY() - upVector.getY())
                * sunDistance
                / sunSize, Camera.getPosition().getZ()
                + LIGHTPOSITION.get(2)
                * sunDistance
                + (rightVector.getZ() - upVector.getZ())
                * sunDistance
                / sunSize);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(
            Camera.getPosition().getX()
                + LIGHTPOSITION.get(0)
                * sunDistance
                + (rightVector.getX() + upVector.getX())
                * sunDistance
                / sunSize, Camera.getPosition().getY()
                + LIGHTPOSITION.get(1)
                * sunDistance
                + (rightVector.getY() + upVector.getY())
                * sunDistance
                / sunSize, Camera.getPosition().getZ()
                + LIGHTPOSITION.get(2)
                * sunDistance
                + (rightVector.getZ() + upVector.getZ())
                * sunDistance
                / sunSize);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(
            Camera.getPosition().getX()
                + LIGHTPOSITION.get(0)
                * sunDistance
                + (rightVector.getX() + upVector.getX())
                * sunDistance
                / sunSize, Camera.getPosition().getY()
                + LIGHTPOSITION.get(1)
                * sunDistance
                + (rightVector.getY() + upVector.getY())
                * sunDistance
                / sunSize, Camera.getPosition().getZ()
                + LIGHTPOSITION.get(2)
                * sunDistance
                + (rightVector.getZ() + upVector.getZ())
                * sunDistance
                / sunSize);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex3f(
            Camera.getPosition().getX()
                + LIGHTPOSITION.get(0)
                * sunDistance
                - (rightVector.getX() - upVector.getX())
                * sunDistance
                / sunSize, Camera.getPosition().getY()
                + LIGHTPOSITION.get(1)
                * sunDistance
                - (rightVector.getY() - upVector.getY())
                * sunDistance
                / sunSize, Camera.getPosition().getZ()
                + LIGHTPOSITION.get(2)
                * sunDistance
                - (rightVector.getZ() - upVector.getZ())
                * sunDistance
                / sunSize);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(
            Camera.getPosition().getX()
                + LIGHTPOSITION.get(0)
                * sunDistance
                - (rightVector.getX() + upVector.getX())
                * sunDistance
                / sunSize, Camera.getPosition().getY()
                + LIGHTPOSITION.get(1)
                * sunDistance
                - (rightVector.getY() + upVector.getY())
                * sunDistance
                / sunSize, Camera.getPosition().getZ()
                + LIGHTPOSITION.get(2)
                * sunDistance
                - (rightVector.getZ() + upVector.getZ())
                * sunDistance
                / sunSize);
        GL11.glEnd();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
    }


Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Blunderchips
« Reply #4 - Posted 2015-01-19 15:49:51 »

For rendering, I am currently using display list for everything.
where would I get upVector and rightVector from and what is Config.getRenderDistance()?
Offline Mike

« JGO Spiffy Duke »


Medals: 149
Projects: 1
Exp: 6 years


Java guru wannabe


« Reply #5 - Posted 2015-01-19 15:54:24 »

Config.getRenderDistance() in the code above is the far perspective. You can just try out values that works out well for you.

The up and the right vector is calculated as such (you won't have the crossproduct and such functions but you can find that very easily by googling).
        Point3DFloat normalizedLookVector =
            normalizeVector(new Point3DFloat(Camera.getPosition().getX() - Camera.getLookingAt().getX(), Camera.getPosition().getY()
                - Camera.getLookingAt().getY(), Camera.getPosition().getZ() - Camera.getLookingAt().getZ()));
        rightVector.setLocation(crossProduct(upVector, normalizedLookVector));
        normalizeVector(rightVector);
        upVector.setLocation(crossProduct(rightVector, normalizedLookVector));
        normalizeVector(upVector);

Good luck!

Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Blunderchips
« Reply #6 - Posted 2015-01-20 08:43:57 »

Thanks  Grin
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (149 views)
2017-09-30 02:57:34

theagentd (222 views)
2017-09-26 18:23:31

cybrmynd (301 views)
2017-08-02 12:28:51

cybrmynd (288 views)
2017-08-02 12:19:43

cybrmynd (297 views)
2017-08-02 12:18:09

Sralse (291 views)
2017-07-25 17:13:48

Archive (978 views)
2017-04-27 17:45:51

buddyBro (1102 views)
2017-04-05 03:38:00

CopyableCougar4 (1678 views)
2017-03-24 15:39:42

theagentd (1430 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!