Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
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  
  Drawing text in a 2d fashion on a 3d scene?  (Read 910 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2011-06-04 04:08:19 »

Alright, so I followed this tutorial: http://ninjacave.com/slickutil3

and I got my text to display, but because of the way it's set up (using glulookat and text using ortho) I can only display my scene or my text. Any idea how to fix this?

When I try to display text on my 3d scene it doesn't display the rest of the scene. What do I have to do to fix this? I tried the answer here: Opengl drawing a 2d overlay on a 3d scene problem but it didn't work for me. Here is my code:

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  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
public class GunCraft {
    private boolean done = false;
    Texture texture;
    Camera cam;
    private float yspeed;
    float legAngle;
    float playerX = 0;
    float playerZ = 0;
    float playerSpeed = 0.01f;
    float enemyX = 0;
    float enemyZ = 0;
    private TrueTypeFont font2;


    public static void main(String args[]) {
        GunCraft gc = new GunCraft();
        gc.run();
    }

    public void run() {
        try {
            init();
            while (!done) {
                mainloop();
                render();
                Display.update();
            }
            cleanup();
        }
        catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }

    /**
     * Very basic for first lesson.  Just check for escape key or user
     * clicking to close the window.
     */

    private void mainloop() {
        if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {       // Exit if Escape is pressed
            done = true;
        }
        if(Display.isCloseRequested()) {                     // Exit if window is closed
            done = true;
        }
    }

    /**
     * For rendering all objects to the screen
     * @return boolean for success or not
     */

    private void render() {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);          // Clear The Screen And The Depth Buffer
        font2.drawString(100, 100, "NICE LOOKING FONTS!", Color.green);

        loadIdentity();
        //GL11.glRotatef(yspeed,0.0f,1.0f,0.0f);
        drawRect(texture, 1f, 1f, 1f);


    }

    public void loadIdentity(){
        GL11.glLoadIdentity();
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        float camAngle = (float) Math.atan2(playerX, playerZ);
        float radius = 5;
        GLU.gluLookAt(playerX + (float)(Math.sin(camAngle) * radius), 0, playerZ + (float)(Math.cos(camAngle) * radius), 0, 0, 0, 0, 1, 0);
        //GL11.glGetMatrix();
    }

    public void drawRect(Texture texture, float width, float height, float depth){
        texture.bind();
        GL11.glBegin(GL11.GL_QUADS);                            // Start Drawing Quads
            // Front Face
            GL11.glNormal3f( 0.0f, 0.0f, 1.0f);                 // Normal Pointing Towards Viewer
            GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-width, -height, depth);    // Point 1 (Front)
            GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( width, -height,  depth);    // Point 2 (Front)
            GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( width,  height,  depth);    // Point 3 (Front)
            GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-width,  height,  depth);    // Point 4 (Front)
            // Back Face
            GL11.glNormal3f( 0.0f, 0.0f,-1.0f);                 // Normal Pointing Away From Viewer
            GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-width, -height, -depth);    // Point 1 (Back)
            GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-width,  height, -depth);    // Point 2 (Back)
            GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( width,  height, -depth);    // Point 3 (Back)
            GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( width, -height, -depth);    // Point 4 (Back)
            // Top Face
            GL11.glNormal3f( 0.0f, 1.0f, 0.0f);                 // Normal Pointing Up
            GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-width,  height, -depth);    // Point 1 (Top)
            GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-width,  height,  depth);    // Point 2 (Top)
            GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( width,  height,  depth);    // Point 3 (Top)
            GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( width,  height, -depth);    // Point 4 (Top)
            // Bottom Face
            GL11.glNormal3f( 0.0f,-1.0f, 0.0f);                 // Normal Pointing Down
            GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-width, -height, -depth);    // Point 1 (Bottom)
            GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( width, -height, -depth);    // Point 2 (Bottom)
            GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( width, -height,  depth);    // Point 3 (Bottom)
            GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-width, -height,  depth);    // Point 4 (Bottom)
            // Right face
            GL11.glNormal3f( 1.0f, 0.0f, 0.0f);                 // Normal Pointing Right
            GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( width, -height, -depth);    // Point 1 (Right)
            GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( width,  height, -depth);    // Point 2 (Right)
            GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( width,  height,  depth);    // Point 3 (Right)
            GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( width, -height,  depth);    // Point 4 (Right)
            // Left Face
            GL11.glNormal3f(-1.0f, 0.0f, 0.0f);                 // Normal Pointing Left
            GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-width, -height, -depth);    // Point 1 (Left)
            GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-width, -height,  depth);    // Point 2 (Left)
            GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-width,  height,  depth);    // Point 3 (Left)
            GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-width,  height, -depth);    // Point 4 (Left)
        GL11.glEnd();                                      // Done Drawing The Quad
    }

    /**
     * Create a window for all display
     * @throws Exception
     */

    private void createWindow() throws Exception {
        Display.setFullscreen(false);
        Display.setDisplayMode(new DisplayMode(640, 480));
        Display.setTitle("GunCraft - By William Starkovich");
        Display.create();
    }

    /**
     * Initialize the environment
     * @throws Exception
     */

    private void init() throws Exception {
        createWindow();
        try{
            FileInputStream fis;
            fis = new FileInputStream(new File("font.ttf"));

            Font awtFont2 = Font.createFont(Font.TRUETYPE_FONT, fis);
            awtFont2 = awtFont2.deriveFont(24f); // set font size
            font2 = new TrueTypeFont(awtFont2, false);

        } catch (Exception e) {
            e.printStackTrace();
        }

        initGL(640,480);
    }

    /**
     * Initialize OpenGL
     *
     */

    private void initGL(int width, int height) {
        GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
        GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
        GL11.glClearDepth(1.0); // Depth Buffer Setup
        GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
        GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do
        GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
        GL11.glLoadIdentity(); // Reset The Projection Matrix

        // Calculate The Aspect Ratio Of The Window
        GLU.gluPerspective(
          45.0f,
          (float) Display.getDisplayMode().getWidth() / (float) Display.getDisplayMode().getHeight(),
          0.1f,
          100.0f);

        //GLU.gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz)

        GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix

        // Really Nice Perspective Calculations
        GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);                                    

        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, width, height, 0, -1000, 1000);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glDisable(GL11.GL_CULL_FACE);

        try {
            texture  = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("tex.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }  
    }

    /**
     * Cleanup all the resources.
     *
     */

    private void cleanup() {
        Display.destroy();
    }

}
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.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

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

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

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

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

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

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

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

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

toopeicgaming1999 (163 views)
2014-11-26 15:20:36
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!