Yes ken i do have a little test case… it should work ( I use Texture and TextureLoader utils… )
Thanks a lot if you can have a look at it because i’m a little disappointed…
Moreover, on another hardware the code does not render black screen but garbage… I must have mis-done something but can’t find where
import com.sun.opengl.utils.Animator;
import com.sun.opengl.utils.BufferUtils;
import Texture;
import TextureLoader;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.nio.FloatBuffer;
import javax.media.opengl.DebugGL;
import static javax.media.opengl.GL.*;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
public class SymbolTests implements GLEventListener
{
// --------------------------------------------------
// STATIC ATTRIBUTES
// --------------------------------------------------
private static final int X_SYMB_COUNT = 30;
private static final int Y_SYMB_COUNT = 20;
// --------------------------------------------------
// ATTRIBUTES
// --------------------------------------------------
private GLU glu = new GLU ();
private Texture texture = null;
private FloatBuffer vertexArrayBuffer;
private FloatBuffer vertexArrayCoordBuffer;
// --------------------------------------------------
// CONSTRUCTOR
// --------------------------------------------------
/** Creates a new instance of SymbolTests */
public SymbolTests ()
{
}
// --------------------------------------------------
// -- GLEventListener IMPLEMENTATION --
// --------------------------------------------------
public void init (GLAutoDrawable drawable)
{
drawable.setGL ( new DebugGL(drawable.getGL () ) );
GL gl = drawable.getGL ();
gl.glEnable ( GL_TEXTURE_2D );
// Initialisation des textures
TextureLoader textLoad = new TextureLoader ( drawable );
try
{
texture = textLoad.getTexture ( "TexMap", "symbo.png" );
}
catch ( Exception e )
{
System.err.println ("ERROR READING symbo.png");
System.exit ( -12 );
}
gl.glDisable ( GL_TEXTURE_2D );
initVertexArrays ();
}
public void display (GLAutoDrawable drawable)
{
GL gl = drawable.getGL ();
texture.bind ( gl );
gl.glEnable ( GL_TEXTURE_2D );
// gl.glBegin ( GL_QUADS ); This one show the full tile and works
// {
// gl.glTexCoord2f ( 0.0f,1.0f );
// gl.glVertex2i ( 100, 100 );
// gl.glTexCoord2f ( 0.0f,0.0f );
// gl.glVertex2i ( 100, 200 );
// gl.glTexCoord2f ( 1.0f,0.0f );
// gl.glVertex2i ( 200, 200 );
// gl.glTexCoord2f ( 1.0f, 1.0f );
// gl.glVertex2i ( 200, 100 );
// }
// gl.glEnd ();
drawVertexArray ( gl );
gl.glDisable ( GL_TEXTURE_2D );
}
public void reshape (GLAutoDrawable drawable, int x, int y, int width, int height)
{
GL gl = drawable.getGL ();
gl.glMatrixMode (GL_PROJECTION);
gl.glLoadIdentity ();
glu.gluOrtho2D ( 0,width, 0, height );
gl.glMatrixMode (GL_MODELVIEW);
}
public void displayChanged (GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
{
// Not used
}
/**
* Entry point for this class
* @param args the command line arguments
*/
public static void main (String[] args)
{
GLCapabilities glcaps = new GLCapabilities ();
glcaps.setAlphaBits ( 8 );
GLCanvas canvas = GLDrawableFactory.getFactory ().createGLCanvas (glcaps);
canvas.addGLEventListener ( new SymbolTests () );
JFrame frame = new JFrame ( "Test Opengl : Symboles");
frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
frame.add ( canvas );
final Animator animator = new Animator (canvas);
frame.addWindowListener (new WindowAdapter ()
{
public void windowClosing (WindowEvent e)
{
// Run this on another thread than the AWT event queue to
// make sure the call to Animator.stop() completes before
// exiting
new Thread (new Runnable ()
{
public void run ()
{
animator.stop ();
System.exit (0);
}
}).start ();
}
});
frame.setBounds ( 0, 0, 640, 480 );
frame.setVisible ( true );
animator.start ();
}
// -------------------------------------------------
// --- PRIVATE METHODS ONLY BELOW THIS POINT ---
// -------------------------------------------------
private void initVertexArrays ()
{
int tile;
float xoffset, yoffset;
// Array 2 float for position, we draw quads so 4 float each time
vertexArrayBuffer = BufferUtils.newFloatBuffer ( 4 * 2 * X_SYMB_COUNT * Y_SYMB_COUNT );
vertexArrayCoordBuffer = BufferUtils.newFloatBuffer ( 4 * 2 * X_SYMB_COUNT * Y_SYMB_COUNT );
for( int i = 0; i < X_SYMB_COUNT; i++ )
{
for( int j = 0; j < Y_SYMB_COUNT; j++ )
{
tile = (int)Math.floor ( Math.random () * 4 ) + 1;
if( tile == 1 || tile == 3 )
{
xoffset = 0.0f;
}
else
{
xoffset = 0.50f;
}
if( tile < 3 )
{
yoffset = 0.0f;
}
else
{
yoffset = 0.50f;
}
vertexArrayCoordBuffer.put ( 0.0f + xoffset);
vertexArrayCoordBuffer.put ( 0.50f + yoffset );
vertexArrayBuffer.put ( 0.0f + i * 33.0f );
vertexArrayBuffer.put ( 0.0f + j * 33.0f );
vertexArrayCoordBuffer.put ( 0.0f + xoffset);
vertexArrayCoordBuffer.put ( 0.0f + yoffset );
vertexArrayBuffer.put ( 0.0f + i * 33.0f );
vertexArrayBuffer.put ( 32.0f + j * 33.0f );
vertexArrayCoordBuffer.put ( 0.50f + xoffset);
vertexArrayCoordBuffer.put ( 0.0f + yoffset );
vertexArrayBuffer.put ( 32.0f + i * 33.0f );
vertexArrayBuffer.put ( 32.0f + j * 33.0f );
vertexArrayCoordBuffer.put ( 0.50f + xoffset);
vertexArrayCoordBuffer.put ( 0.50f + yoffset );
vertexArrayBuffer.put ( 32.0f + i * 33.0f );
vertexArrayBuffer.put ( 0.0f + j * 33.0f );
vertexArrayBuffer.rewind ();
vertexArrayCoordBuffer.rewind ();
}
}
}
private void drawVertexArray ( GL gl )
{
gl.glEnableClientState ( GL_VERTEX_ARRAY );
gl.glEnableClientState ( GL_TEXTURE_COORD_ARRAY );
gl.glVertexPointer ( 2, GL_FLOAT, 0, vertexArrayBuffer );
gl.glTexCoordPointer ( 2, GL_FLOAT, 0, vertexArrayCoordBuffer );
gl.glDrawArrays ( GL_QUADS, 0, X_SYMB_COUNT * Y_SYMB_COUNT );
gl.glDisableClientState ( GL_VERTEX_ARRAY );
gl.glDisableClientState ( GL_TEXTURE_COORD_ARRAY );
}
}