Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
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  
  Rendering Trouble  (Read 1277 times)
0 Members and 1 Guest are viewing this topic.
Offline Harris6310

Senior Newbie





« Posted 2013-01-24 11:19:32 »

Hello, I am trying to learn the basics of LWJGL. I can't seem to get a texture to render in 2D. Could someone take a look at my code and tell me what I am doing wrong?

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  
package com.game;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class Game
{
   Texture tileset;
   
   public static void main(String[] args)
   {
      Game game = new Game();
      game.initialise();
      game.run();
   }
   
   public void initialise()
   {
      try
      {
         Display.setDisplayMode(new DisplayMode(640, 480));
         Display.setTitle("Game");
         Display.create();
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
     
      GL11.glEnable(GL11.GL_TEXTURE_2D);
      GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      GL11.glEnable(GL11.GL_BLEND);
      GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
      GL11.glViewport(0, 0, 640, 480);
      GL11.glMatrixMode(GL11.GL_MODELVIEW);
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glLoadIdentity();
      GL11.glOrtho(0, 640, 0, 480, 1, -1);
      GL11.glMatrixMode(GL11.GL_MODELVIEW);
     
      try
      {
         tileset = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("/res/tilesets/001-Grassland01.png"));
         System.out.println("Texture loaded. " + tileset);
      }
      catch(Exception e)
      {
         
      }
   }
   
   public void run()
   {
      while(!Display.isCloseRequested())
      {
         this.render();
         
         Display.update();
         Display.sync(60);
      }
     
      Display.destroy();
   }
   
   public void render()
   {
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
     
      Color.white.bind();
      tileset.bind();
     
      GL11.glBegin(GL11.GL_QUADS);
         GL11.glTexCoord2f(0, 0);
         GL11.glVertex2f(0, 0);
         GL11.glTexCoord2f(1, 0);
         GL11.glVertex2f(tileset.getWidth(), 0);
         GL11.glTexCoord2f(1, 1);
         GL11.glVertex2f(tileset.getWidth(), tileset.getHeight());
         GL11.glTexCoord2f(0, 1);
         GL11.glVertex2f(0, tileset.getHeight());
      GL11.glEnd();
   }
}


The display is shown and the texture is loaded but nothing is rendered. Here is a screenshot of the display.



I know I may be using bad techniques, but I would just like to get it working first.
Offline theagentd

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #1 - Posted 2013-01-24 12:59:46 »

Maybe you should print out the error instead of silently ignoring it?

1  
2  
3  
4  
catch(Exception e)
{
         
}

Myomyomyo.
Offline StrideColossus
« Reply #2 - Posted 2013-01-24 13:04:59 »

Are you certain that the texture is actually being loaded?  The exception handler silently ignores an exceptions thrown.

Edit: @theagent, beat me to it Wink
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Harris6310

Senior Newbie





« Reply #3 - Posted 2013-01-24 22:05:16 »

-snip-

I have added it in and no error is shown.

-snip-

The output line shows this:

1  
Texture loaded: org.newdawn.slick.opengl.TextureImpl@1ded246d
Offline HeroesGraveDev

JGO Kernel


Medals: 310
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #4 - Posted 2013-01-24 22:06:50 »

Call glViewport() directly before glOrtho()

Offline Harris6310

Senior Newbie





« Reply #5 - Posted 2013-01-24 22:09:29 »

-snip-

I have tried that and still nothing is displayed.
Offline theagentd

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #6 - Posted 2013-01-24 22:12:16 »

1  
GL11.glOrtho(0, 640, 0, 480, 1, -1);

should be
1  
GL11.glOrtho(0, 640, 480, 0, 1, -1);

Myomyomyo.
Offline Harris6310

Senior Newbie





« Reply #7 - Posted 2013-01-24 22:14:41 »

-snip-

Well that was because I wanted the bottom left to be the origin. Though it doesn't seem to work both ways.
Offline Sparky83

Senior Devvie


Medals: 6
Projects: 1



« Reply #8 - Posted 2013-01-24 22:49:29 »

OK, i got it!

You mixed up tileset.getWidth() and tileset.getImageWidth(). First one gets you value from 0 to 1, second one in pixels...
Correct code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
      GL11.glBegin(GL11.GL_QUADS);
         GL11.glTexCoord2f(0, 0);
         GL11.glVertex2f(0, 0);
         GL11.glTexCoord2f(1, 0);
         GL11.glVertex2f(tileset.getImageWidth(), 0);
         GL11.glTexCoord2f(1, 1);
         GL11.glVertex2f(tileset.getImageWidth(), tileset.getImageHeight());
         GL11.glTexCoord2f(0, 1);
         GL11.glVertex2f(0, tileset.getImageHeight());
      GL11.glEnd();
Offline davedes
« Reply #9 - Posted 2013-01-25 00:51:42 »

IMHO you should write your own texture loader rather than relying on the old and deprecated SlickUtil; it's really the "first step" to understanding how to use LWJGL and OpenGL.

https://github.com/mattdesl/lwjgl-basics/wiki/Textures

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Harris6310

Senior Newbie





« Reply #10 - Posted 2013-01-25 07:50:54 »

-snip-

This has fixed the problem, thanks for your help.

-snip-

Yeah, I am now going to learn more about the proper use of OpenGL. I just wanted to get something to work first.
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.

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

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

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

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

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

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

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

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

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

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