2D texture not visible

Started by mark, March 01, 2006, 18:44:25

Previous topic - Next topic

mark

I'm using SWT & I'm trying to render a texture onto a square. I've followed numerous examples but can't get the texture to appear. I know the image acquisition is working because I am able to display it with GL.glDrawPixels(). The important section is as follows (textureBuffer is an int[] array, loadBitmap is my own function, createIntBuffer creates the buffer using allocateDirect):

  /*prepare texture*/
  File bitmap=new File("texture.bmp");
  textureBuffer=loadBitmap(bitmap);
  IntBuffer textureID = createIntBuffer(1);
  GL11.glGenTextures(textureID);
  scratch = ByteBuffer.allocateDirect(lightningBuffer.length);
  GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID.get(0));
  // produce a texture from the byte buffer
  GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, 64, 64,0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, scratch );

I then have a render function as follows:

GL11.glBegin(GL11.GL_QUADS);
      {
            GL11.glTexCoord2f(0, 0);
            GL11.glVertex2f(0, 0);
            GL11.glTexCoord2f(0, -0.5f);
            GL11.glVertex2f(0, -0.5f);
            GL11.glTexCoord2f(-0.5f,-0.5f);
            GL11.glVertex2f(-0.5f,-0.5f);
            GL11.glTexCoord2f(-0.5f, 0);
            GL11.glVertex2f(-0.5f,0);
      }
 GL11.glEnd();


Any feedback on what I'm doing wrong would be appreciated. How easy is it to then texture map a sphere? The whole code looks like this:

public class Schumann_LWJGL
{

static int[] textureBuffer;
static ByteBuffer scratch;
public static void main(String [] args) throws IOException
{
  final Display display = new Display();
  Shell shell = new Shell(display);
  shell.setText("OpenGL in SWT (JLWGL bindings)");
  shell.setSize(1100, 700);
  shell.setLayout(new FillLayout());
  Composite comp = new Composite(shell, SWT.NONE);
  comp.setLayout(new FillLayout());
  GLData data = new GLData ();
  data.doubleBuffer = true;
  final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data);
   
  canvas.addControlListener(new ControlAdapter() {
      public void controlResized(ControlEvent e) {
          resize(canvas);
      }
  });
  init(canvas);
 
  /*prepare texture*/
  File bitmap=new File("texture.bmp");
  textureBuffer=loadBitmap(bitmap);
  IntBuffer textureID = createIntBuffer(1);
  GL11.glGenTextures(textureID);
  scratch = ByteBuffer.allocateDirect(lightningBuffer.length);
  GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID.get(0));
  // produce a texture from the byte buffer
  GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, 64, 64, 0, GL11.GL_RGB,
          GL11.GL_UNSIGNED_BYTE,
            scratch );

  new Runnable()
  {
    public void run()
      {
       if (canvas.isDisposed()) return;
        render();
       canvas.swapBuffers();
       canvas.getDisplay().timerExec(30,this);
       }
   }.run();
      
   shell.open();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch())
            display.sleep();
      }
      display.dispose();
   }

static void init(GLCanvas canvas) {
   
  canvas.setCurrent();
            try {
               GLContext.useContext(canvas);
              } catch(LWJGLException e) { e.printStackTrace(); }
   
   GL11.glShadeModel(GL11.GL_SMOOTH); // Enables Smooth Shading
   GL11.glClearColor(0.0f,0.0f,0.0f,0.0f);    // Black Background
   GL11.glClearDepth(1.0f); // Depth Buffer Setup
   GL11.glDisable(GL11.GL_DEPTH_TEST);   // Disables Depth Testing
   GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT,GL11.GL_NICEST);
   GL11.glHint(GL11.GL_POINT_SMOOTH_HINT,GL11.GL_NICEST);   
   GL11.glEnable(GL11.GL_TEXTURE_2D);                  // Enable Texture Mapping
}
   
}
static void render()
{            
 
 GL11.glBegin(GL11.GL_QUADS);
      {
            GL11.glTexCoord2f(0, 0);
            GL11.glVertex2f(0, 0);
            GL11.glTexCoord2f(0, -0.5f);
            GL11.glVertex2f(0, -0.5f);
            GL11.glTexCoord2f(-0.5f,-0.5f);
            GL11.glVertex2f(-0.5f,-0.5f);
            GL11.glTexCoord2f(-0.5f, 0);
            GL11.glVertex2f(-0.5f,0);
      }
 GL11.glEnd();

}
 
static void resize(GLCanvas canvas) {
     
       canvas.setCurrent();
       Rectangle bounds = canvas.getClientArea();
       GL11.glViewport(0, 0, bounds.width, bounds.height);
       GL11.glMatrixMode(GL11.GL_PROJECTION);
       GL11.glLoadIdentity();
       float fAspect = (float) bounds.width / (float) bounds.height;
      GLU.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
       GL11.glMatrixMode(GL11.GL_MODELVIEW);
       GL11.glLoadIdentity();
     
}
static IntBuffer createIntBuffer(int size) {
     ByteBuffer temp = ByteBuffer.allocateDirect(4 * size);
     temp.order(ByteOrder.nativeOrder());

     return temp.asIntBuffer();
   }

}

Evil-Devil

I imagine that glTranslatef(x,y,z) need to be called for those SWT widgets too. Else your texture would be rendered before the drawing area of the widget and won't be visible to you.