Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Why won’t my texture show up?  (Read 685 times)
0 Members and 2 Guests are viewing this topic.
Offline CyanPrime

JGO Ninja
***

Posts: 683
Medals: 7



« on: 2009-03-08 15:07:51 »

Okay, I'm using Java and JoGL. I'm trying to load and display a texture, but the texture isn't showing up at all. I'm not getting any errors at all, so I don't know what the problem could be.

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  
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;
import com.sun.opengl.util.j2d.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.nio.*;
import javax.imageio.*;
import javax.swing.*;


public class chartest extends JFrame implements GLEventListener, KeyListener
{
    private int texture;

    public void display(GLAutoDrawable gLDrawable)
    {
      final GL gl = gLDrawable.getGL();
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
      gl.glLoadIdentity();


      gl.glColor3f(1.0f, 1.0f, 1.0f);
      gl.glBindTexture(GL.GL_TEXTURE_2D, texture);

      gl.glBegin(GL.GL_QUADS);              // Draw A Quad
       gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-12.0f, -19.0f,  -15.0f);
        gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 12.0f, -19.0f,  -15.0f);
        gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 12.0f,  19.0f,  -15.0f);
        gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-12.0f,  19.0f,  -15.0f);
      gl.glEnd();                       // Done Drawing The Quad
     gl.glFlush();
    }

    public void displayChanged(GLAutoDrawable g, boolean b, boolean b2){}

    public void init(GLAutoDrawable gLDrawable)
    {
      final GL gl = gLDrawable.getGL();
      gl.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
      gl.glShadeModel(GL.GL_FLAT);
      gl.glEnable(GL.GL_TEXTURE_2D);

      texture = genTexture(gl);
      gl.glBindTexture(GL.GL_TEXTURE_2D, texture);
      URL url = this.getClass().getResource("test.png");

      try
      {
        BufferedImage img = ImageIO.read(url);
        makeRGBTexture(gl, new GLU(), img, GL.GL_TEXTURE_2D, false);
      }

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

      gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);
      gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);
    }

    public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height)
    {
      final GL gl = gLDrawable.getGL();
      final GLU glu = new GLU();

      if (height <= 0) // avoid a divide by zero error!
       height = 1;
      final float h = (float)width / (float)height;
      gl.glViewport(0, 0, width, height);
      gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glLoadIdentity();
      glu.gluPerspective(45.0f, h, 1.0, 20.0);
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glLoadIdentity();
    }

    public void keyPressed(KeyEvent e)
    {
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
        System.exit(0);
    }
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}

    public static void main(String[] args) {

        chartest c = new chartest();

        GLCanvas canvas = new GLCanvas(new GLCapabilities());
        canvas.addGLEventListener(c);
        c.add(canvas);
        c.setSize(640, 480);
        canvas.addKeyListener(c);
        c.addWindowListener(new WindowAdapter()
        {
          public void windowClosing(WindowEvent e)
          {
            System.exit(0);
          }
        });
        c.setVisible(true);
        canvas.requestFocus();
    }



    //-----------------------
   private void makeRGBTexture(GL gl, GLU glu, BufferedImage img, int target, boolean mipmapped)
    {
      ByteBuffer dest = null;
      switch (img.getType())
      {
        case BufferedImage.TYPE_3BYTE_BGR:
        case BufferedImage.TYPE_CUSTOM:
        {
          System.out.println("Custom");
          byte[] data = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
          dest = ByteBuffer.allocateDirect(99999);
          dest.order(ByteOrder.nativeOrder());
          dest.put(data, 0, data.length);
          break;
        }
        case BufferedImage.TYPE_INT_RGB:
        {
          int[] data = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
          dest = ByteBuffer.allocateDirect(data.length * BufferUtil.SIZEOF_INT);
          dest.order(ByteOrder.nativeOrder());
          dest.asIntBuffer().put(data, 0, data.length);
          break;
        }
        default:
          throw new RuntimeException("Unsupported image type " + img.getType());
      }

      if (mipmapped)
      {
        glu.gluBuild2DMipmaps(target, GL.GL_RGB8, img.getWidth(), img.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, dest);
      }
      else
      {
        gl.glTexImage2D(target, 0, GL.GL_RGB, img.getWidth(), img.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, dest);
      }
    }

    private int genTexture(GL gl)
    {
      final int[] tmp = new int[1];
      gl.glGenTextures(1, tmp, 0);
      return tmp[0];
    }
}
Offline Cakey

Full Member
**

Posts: 127



« Reply #1 on: 2009-03-08 17:09:59 »

I'm no expert but one thing immediately apparent to me is you don't have a gluPerspective line in there. I didn't run your code or anything though.

Offline CyanPrime

JGO Ninja
***

Posts: 683
Medals: 7



« Reply #2 on: 2009-03-08 17:35:45 »

I'm no expert but one thing immediately apparent to me is you don't have a gluPerspective line in there. I didn't run your code or anything though.
glu.gluPerspective(45.0f, h, 1.0, 20.0);

It's in the init method.
Games published by our own members! Go get 'em!
Offline gouessej

JGO Kernel
*****

Posts: 3560
Medals: 30


TUER


« Reply #3 on: 2009-03-08 19:07:50 »

glu.gluPerspective(45.0f, h, 1.0, 20.0);

It's in the init method.
No it is only in the reshape method

Julien Gouesse
Offline CyanPrime

JGO Ninja
***

Posts: 683
Medals: 7



« Reply #4 on: 2009-03-08 21:11:04 »

I got it to work by going here: http://forums.sun.com/thread.jspa?threadID=5197213
Offline Mickelukas

JGO Ninja
***

Posts: 731
Medals: 25


Java guru wanabee


« Reply #5 on: 2009-03-10 05:32:22 »

If anyone else has similar issues and you're testing it on a computer with OpenGL before 2.0:
Make sure that your textures are of a power of two. Mine weren't and I was wondering for really long why it worked on my computer and why it didn't work on my colleague's Wink

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.056 seconds with 20 queries.