LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Schnitter on April 25, 2008, 15:18:22

Title: Problem with AWTGLCanvas and Texture Loading(DevIL)
Post by: Schnitter on April 25, 2008, 15:18:22
Hi :)

I try to work with the AWTGLCanvas.
In an other thread, I've seen this solution(for the AWTGLCanvas):

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.AWTGLCanvas;
import org.lwjgl.opengl.PixelFormat;
import java.awt.*;
import javax.swing.*;
import org.lwjgl.opengl.glu.GLU;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.Sys;

public class GLPanel extends JPanel {

    private int width;
    private int height;
    private int FRAMERATE = 1;
    private long tm;
    private MyCanvas canvas;

    public GLPanel(int width, int height) {
        this.width = width;
        this.height = height;
        setSize(width, height);
        setLayout(new BorderLayout());

        Sys.initialize();
        tm = Sys.getTime();

        try {
            canvas = new MyCanvas();
            add(canvas, BorderLayout.CENTER);
        } catch (LWJGLException e) {
            e.printStackTrace();
        }
    }

    /**
     * The canvas that will show the GL scene
     */
    private class MyCanvas extends AWTGLCanvas {

        /**
         * Creates a new Canvas
         * @throws  LWJGLException If we can't make a view
         */
        public MyCanvas() throws LWJGLException {
            super(new PixelFormat());
        }

        public void initGL() {
            glEnable(GL_TEXTURE_2D);
            glShadeModel(GL_SMOOTH);
            glClearColor(0, 0, 0, 1.0f);
            glClearDepth(1.0f);
            glEnable(GL_DEPTH_TEST);
            glDepthFunc(GL_LEQUAL);
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();

            GLU.gluPerspective(45.0f, ((float) width) / (float) height, 0.1f, 150.0f);
            glMatrixMode(GL_MODELVIEW);

            glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
        }

        private long sync(int fps, long timeThen) {
            long gapTo = Sys.getTimerResolution() / fps + timeThen;
            long timeNow = Sys.getTime();

            while (gapTo > timeNow) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                }
                timeNow = Sys.getTime();
            }
            return timeThen = timeNow;
        }

        public void paintGL() {
            while (true) {
                render();

                if (glGetError() != GL_NO_ERROR) {
                    System.out.println("error occured");
                }
                try {
                    swapBuffers();
                } catch (LWJGLException e) {
                    e.printStackTrace();
                }
                tm = sync(FRAMERATE, tm);
            }
        }
       
        private void render() {
            glClearColor((float) Math.random(), (float) Math.random(), (float) Math.random(), 1.0f);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glLoadIdentity();
            glTranslatef(0.0f, 0.0f, -6.0f);
            glColor3f(1.0f, 0.0f, 0.0f);
            glBegin(GL_QUADS);
            glVertex3f(-1.0f, 1.0f, 0.0f);
            glVertex3f(1.0f, 1.0f, 0.0f);
            glVertex3f(1.0f, -1.0f, 0.0f);
            glVertex3f(-1.0f, -1.0f, 0.0f);
            glEnd();
        }
    }
}


My TextureLoader-Class looks like this:

package org.graphics;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.HashMap;

import org.lwjgl.BufferUtils;
import org.lwjgl.devil.IL;
import static org.lwjgl.opengl.GL11.*;


public class TextureLoader {
private static HashMap<String, Texture> textures = new HashMap<String, Texture>();


public static Texture loadTexture(String ref, int filter){
return loadTexture(ref, filter, filter);
}

public static Texture loadTexture(String ref){
return loadTexture(ref, GL_LINEAR, GL_LINEAR);
}


public static Texture loadTexture(String ref, int minfilter, int magfilter){
//if texture already exists, return the existing one
if(textures.containsKey(ref.toLowerCase()))
return textures.get(ref.toLowerCase());


Texture tex;
ByteBuffer imageData;
int oglImageHandle;//ID für OGL
IntBuffer scratch = BufferUtils.createIntBuffer(1);

//Create IL-Image and bind
IL.ilGenImages(scratch);
IL.ilBindImage(scratch.get(0));
//load the image

try{
if(!IL.ilLoadFromURL(IL.class.getClassLoader().getResource(ref)))
return null;
}catch(IOException e){
e.printStackTrace();
}

IL.ilConvertImage(IL.IL_RGBA, IL.IL_BYTE);

//set image-Attributes
int width  = IL.ilGetInteger(IL.IL_IMAGE_WIDTH);
int height = IL.ilGetInteger(IL.IL_IMAGE_HEIGHT);

imageData = IL.ilGetData();
glGenTextures(scratch);
glBindTexture(GL_TEXTURE_2D, scratch.get(0));
oglImageHandle = scratch.get(0);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, imageData);

tex = new Texture(oglImageHandle, width, height);

//delete image data
scratch.put(0, oglImageHandle);
IL.ilDeleteImages(scratch);

//unbind the texture
glBindTexture(GL_TEXTURE_2D,0);

textures.put(ref.toLowerCase(), tex);
return tex;
}
}



I'm calling IL.create() in the Constructor of the GLPanel. Now Trying to load an Texture, I get an Exception:
Quote
Exception in thread "RenderThread" java.lang.NullPointerException
   at org.lwjgl.opengl.GL11.glGenTextures(GL11.java:1348)
   at org.graphics.TextureLoader.loadTexture(TextureLoader.java:55)
   at org.graphics.TextureLoader.loadTexture(TextureLoader.java:21)
   at org.graphics.Sprite.<init>(Sprite.java:28)
   at org.graphics.Sprite.<init>(Sprite.java:36)
   at org.util.mapEditor.EditorPanel.run(EditorPanel.java:121)
   at java.lang.Thread.run(Unknown Source)

(TextureLoader.java:55) is glGenTextures(scratch). But scratch is not null.

Does anyone know how to fix it?
Sorry, but I'm new to the canvas :/

Title: Re: Problem with AWTGLCanvas and Texture Loading(DevIL)
Post by: Schnitter on April 26, 2008, 13:25:39
No idea?
Title: Re: Problem with AWTGLCanvas and Texture Loading(DevIL)
Post by: Fool Running on April 29, 2008, 19:27:37
The crash you have is created by trying to use OpenGL methods in a different thread (outside of the paintGL() method).