Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (753)
Games in Android Showcase (228)
games submitted by our members
Games in WIP (842)
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  
  Textures are not displayed properly  (Read 1914 times)
0 Members and 1 Guest are viewing this topic.
Offline userek

Junior Devvie


Projects: 1



« Posted 2008-08-16 11:48:26 »

Hello,
I want to write a simple 2D game using jogl. I'm trying to put textures on the quads, but something doesn't work. Instead of the actual textures quads are filled with a single color:

There is probably something wrong in the reshape method because textures are displayed properly when this method is commented:

My code looks like this:
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  
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLException;
import javax.media.opengl.GLJPanel;
import javax.swing.*;

import com.sun.opengl.util.Animator;
import com.sun.opengl.util.FPSAnimator;
import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;

import java.awt.BorderLayout;
import java.io.IOException;
import java.util.*;

public class GameWindow extends JFrame implements GLEventListener{
   private int width = 800;
   private int height = 600;
   private GLJPanel panel;
   private Animator animator;
   
   private List<Sprite> sprites = new ArrayList<Sprite>();
   
   public GameWindow(){
      initComponents();
   }
   
   public void initComponents(){
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(new BorderLayout());
      setSize(width,height);
      setLocationRelativeTo(null);
     
      panel = new GLJPanel();      
      panel.addGLEventListener(this);
      add(panel,BorderLayout.CENTER);
     
      setVisible(true);
   }

   @Override
   public void display(GLAutoDrawable glDrawable) {
      GL gl=glDrawable.getGL();
     
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glLoadIdentity();  

      for(int i=0,x=0;i<sprites.size();i++){
         sprites.get(i).draw(gl, x, 0);
         x+=sprites.get(i).getWidth();
      }

      gl.glFlush();
     
   }

   @Override
   public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
      // TODO Auto-generated method stub
     
   }

   @Override
   public void init(GLAutoDrawable glDrawable) {  
      GL gl = glDrawable.getGL();

      gl.glEnable(GL.GL_TEXTURE_2D);

      gl.glClearColor(255, 255, 255, 0);

      gl.glViewport(0, 0, width, height);

      gl.glDisable(GL.GL_DEPTH_TEST);

      sprites.add(new Sprite("ground.png"));
      sprites.add(new Sprite("grass.png"));
      sprites.add(new Sprite("attack e0000.png"));
     
     
      animator = new FPSAnimator(glDrawable,60);
      animator.start();
   }
     
   

   @Override
   public void reshape(GLAutoDrawable glDrawable, int x, int y, int width, int height)
   {
      GL gl = glDrawable.getGL();
      gl.glViewport(x, y, width, height);
      //gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glLoadIdentity();

      gl.glOrtho(0, panel.getWidth(), panel.getHeight(), 0, -1, 1);
   }
   
   public static void main(String args[]){
      new GameWindow();
   }
}

class Sprite{
   private Texture texture;
   private int width;
   private int height;  

   public Sprite(String ref) {
      try {
         texture = TextureIO.newTexture(getClass().getResource(ref),false,TextureIO.PNG);
         width = texture.getImageWidth();
         height = texture.getImageHeight();
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(0);
      }
   }
   public void draw(GL gl , int x, int y) {

      gl.glPushMatrix();
      texture.enable();
      texture.bind();  
      gl.glTranslatef(x, y, 0);      
      gl.glColor3f(1,1,1);
      gl.glBegin(GL.GL_QUADS);
      {  
         gl.glTexCoord2f(0, 0);gl.glVertex2f(0, 0);
         gl.glTexCoord2f(width,0);   gl.glVertex2f(width,0);
         gl.glTexCoord2f(width,height);gl.glVertex2f(width,height);
         gl.glTexCoord2f( 0,height );gl.glVertex2f(0, height);
      }
      texture.disable();
      gl.glEnd();
      gl.glPopMatrix();
   }
   public int getWidth() {
      return width;
   }
   public int getHeight() {
      return height;
   }
}


Any ideas?
Thanks
Offline kollerma

Senior Newbie





« Reply #1 - Posted 2008-08-16 13:36:09 »

What I think is clearly wrong is how you set the texture coordinates.
Texture coordinates are float values between 0 and 1, but you seem to use the width and height in pixels.
Like this the textures are displayed very, very small and get repeated or whatever you set as behaviour.

You should be using the getImageTexCoords() function from Texture to get the values for top, bottom, left and right as mentioned in the  javadoc of getImageHeight() and getImageWidth().
This is also important because the Texture class uses the GL_TEXTURE_RECTANGLE_ARB extension if possible which has an influence on the texture coordinates.
Offline userek

Junior Devvie


Projects: 1



« Reply #2 - Posted 2008-08-16 14:45:21 »

What I think is clearly wrong is how you set the texture coordinates.
Texture coordinates are float values between 0 and 1, but you seem to use the width and height in pixels.
Like this the textures are displayed very, very small and get repeated or whatever you set as behaviour.

You should be using the getImageTexCoords() function from Texture to get the values for top, bottom, left and right as mentioned in the  javadoc of getImageHeight() and getImageWidth().
This is also important because the Texture class uses the GL_TEXTURE_RECTANGLE_ARB extension if possible which has an influence on the texture coordinates.
Thank you!  Obviously that was the problem. I can't image how long it would take me to figure that out Wink.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline userek

Junior Devvie


Projects: 1



« Reply #3 - Posted 2008-08-17 12:42:17 »

Hello again. I thought, the problem is solved, however not quite. I changed my Sprite class a littlebit:
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  
   
public class Sprite{
   private Texture texture;
   private int width;
   private int height;  

   public Sprite(String path, int width, int height){
      texture = DataManager.getInstance().getTexture(path);
      this.width=width;
      this.height=height;
   }
   
   public Sprite(String path) {
      texture = DataManager.getInstance().getTexture(path);
      width = texture.getImageWidth();
      height = texture.getImageHeight();
   }
   
   public void draw(GL gl , int x, int y) {

      gl.glPushMatrix();
      texture.enable();
      texture.bind();  
      gl.glTranslatef(x, y, 0);      
      gl.glColor3f(1,1,1);
      gl.glBegin(GL.GL_QUADS);
      {  
         gl.glTexCoord2f(0, 0);gl.glVertex2f(0, 0);
         gl.glTexCoord2f(1,0);   gl.glVertex2f(width,0);
         gl.glTexCoord2f(1,1);gl.glVertex2f(width,height);
         gl.glTexCoord2f( 0,1 );gl.glVertex2f(0, height);
      }
      texture.disable();
      gl.glEnd();
      gl.glPopMatrix();
   }
   
   public int getWidth() {
      return width;
   }
   public int getHeight() {
      return height;
   }
}

Everything worked fine for the images i used yesterday. Today however i tried with different image, which seems to break everything. Not only the sprite isn't displayed properly but also all other sprites use the same broken texture instead of their own. Below i post two screens and this image. The only difference is that i used different image for the grass.
Thanks


Offline userek

Junior Devvie


Projects: 1



« Reply #4 - Posted 2008-08-17 12:51:31 »

Hmm, Got it solved. I can't say i know why it works that way though Wink. I only changed the way i load textures:
from:
1  
texture = TextureIO.newTexture(getClass().getResource(path),false,TextureIO.PNG);

to:
1  
texture = TextureIO.newTexture(getClass().getResource(path),true,TextureIO.PNG);
Offline kollerma

Senior Newbie





« Reply #5 - Posted 2008-08-17 13:23:48 »

You have 0 and 1 hardcoded, you really shouldn't be doing that.
Use getImageTexCoords() instead.

The problem is, that as soon as the Texture internally uses the GL_TEXTURE_RECTANGLE_ARB extension
the texture coordinates are in pixels again.

So you should really use the getImageTexCoords() function if you use the Texture class!
Maybe that also solves you're problem.
Pages: [1]
  ignore  |  Print  
 
 

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

nelsongames (17 views)
2018-04-24 18:14:32

ivj94 (608 views)
2018-03-24 14:47:39

ivj94 (54 views)
2018-03-24 14:46:31

ivj94 (402 views)
2018-03-24 14:43:53

Solater (66 views)
2018-03-17 05:04:08

nelsongames (111 views)
2018-03-05 17:56:34

Gornova (177 views)
2018-03-02 22:15:33

buddyBro (747 views)
2018-02-28 16:59:18

buddyBro (94 views)
2018-02-28 16:45:17
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!