LWJGL Forum

Programming => OpenGL => Topic started by: pdufranc on November 08, 2005, 23:34:46

Title: Bug display
Post by: pdufranc on November 08, 2005, 23:34:46
My code

//////////////////////////////////////////////
// create canvas in jframe with thread ( thread -> canvas.repaint();)

canvas = new AWTGLCanvas() {
               long startTime = 0;
               long fps = 0;
               public void paintGL() {

                   if (startTime == 0) {
                       setup();
                       startTime = System.currentTimeMillis() + 5000;
                   }
                   try {
                       makeCurrent();
                       myRender();
                       swapBuffers();
                   } catch (LWJGLException e) {
                       throw new RuntimeException(e);
                   }
               }
           };
       } catch (RuntimeException ex) {
       } catch (LWJGLException ex) {
       }
canvas.setBounds(0, 0, width, height);

//////////////////////////////////////////////
//Setup

byte[] pixels;
int cpt=0;
pixels = new byte[textureWidth * textureHeight * textureMode];
boolean state=true;
for (int i = 0; i < textureWidth; i++) {
   for (int j = 0; j < textureHeight; j++) {
//pixels[cpt] = (byte) 0xFF; ;
if (state){
   pixels[cpt] = (byte) 0;
   cpt++;
   pixels[cpt] = (byte) 1;
   cpt++;
   state=false;
}
else{
   pixels[cpt] = (byte) 1;
   cpt++;
   pixels[cpt] = (byte) 0;
   cpt++;
   state=true;

}
pixels[cpt] = 0;
cpt++;
pixels[cpt] = 0;
cpt++;

   }
}
GL11.glEnable(GL11.GL_TEXTURE_2D);
ByteBuffer scratch = ByteBuffer.wrap(pixels);
scratch = ByteBuffer.allocateDirect(textureWidth * textureHeight * textureMode);
scratch.position(0);
idTexture = BufferUtils.createIntBuffer(1);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glGenTextures(idTexture); // Create Texture In OpenGL

GL11.glBindTexture(GL11.GL_TEXTURE_2D, idTexture.get(0));
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,
    GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER,
    GL11.GL_NEAREST);
   GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, 4,
     textureWidth,
     textureHeight, 0, GL11.GL_RGBA,
     GL11.GL_UNSIGNED_BYTE, scratch);

GL11.glDisable(GL11.GL_TEXTURE_2D);


//////////////////////////////////////////////
// myRender

GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
float trans = 0;
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
// Origine en haut à gauche
GLU.gluOrtho2D(0, width, height, 0);
GL11.glViewport(0, 0, width, height);
GL11.glDisable(GL11.GL_BLEND);

GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, idTexture.get(0));
GL11.glBegin(GL11.GL_QUADS);
GL11.glNormal3f( 0.0f, 0.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(1.0f*rapWidth, 0.0f);
GL11.glVertex2f(width, 0);
GL11.glTexCoord2f(1.0f*rapWidth, 1.0f*rapHeight);
GL11.glVertex2f(width, height);
GL11.glTexCoord2f(0.0f, 1.0f*rapHeight);
GL11.glVertex2f(0, height);
GL11.glEnd();
GL11.glDisable(GL11.GL_TEXTURE_2D);



///
// GL11.glTexSubImage2D
the aim is change texture with GL11.glTexSubImage2D


My display is black. Why ?
Help Me
Title: Bug display
Post by: Mr EEK on November 09, 2005, 12:30:39
pdufranc,

I think (but am not 100% sure, being a bit of a noob myself) your Buffer usage may be wrong.  In the code

ByteBuffer scratch = ByteBuffer.wrap(pixels);
scratch = ByteBuffer.allocateDirect(textureWidth * textureHeight * textureMode);
scratch.position(0);


it looks like scratch (which starts by being an indirect buffer containing your pixels) is replaced with a new direct buffer (which doesn't contain your pixels i.e. is black).  Also, it's probably a good idea to use the LWJGL util class for creating the buffer, maybe something like this:

ByteBuffer scratch = BufferUtils.createByteBuffer(pixels.length);
for (int i = 0; i < pixels.length; i++)
{
   scratch.put(pixels[i]);
}
scratch.rewind();


I don't know if there is anything else causing your black display, but this might help.
Title: hmmmmm...
Post by: Fool Running on November 09, 2005, 16:47:00
Quotefor (int i = 0; i < pixels.length; i++)
{
   scratch.put(pixels);
}
Its significantly faster to just do:
scratch.put(pixels);
Title: Bug display
Post by: Mr EEK on November 10, 2005, 09:30:45
Is it faster?  I can't understand why it would be.

I checked the source (1.4.2_03), and all put(byte[]) does is call put(byte) in a for loop.  In fact, it does extra work because it does an additional bounds check.

If it is faster, I will eat a big slice of humble pie
:D
Title: Humble pie
Post by: Mr EEK on November 10, 2005, 09:57:02
Fool Running,

I am currently munching my way through a large slice of humble pie.

As you say, put(byte[]) is much faster.  I would be very interested to know why.  Maybe the implementation of ByteBuffer overrides that method to do something very cunning.  If you can explain it, I would be grateful.
Title: Bug display
Post by: tomb on November 10, 2005, 12:11:58
The vm know about the Buffer functions, just like they know about the java.lang.Math functions. So it ignores the actaul java code and replaces it with optemized native code when encountered. Atleast that is what it should do if it is any good.
Title: Bug display
Post by: Mr EEK on November 10, 2005, 12:28:08
Ah, I see, thanks.