I can't find a way to use correctly JOGL with Textures in my 2D game, here's my code:

Init method

public void init(GLAutoDrawable drawable) {
    [...]
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    gl.glShadeModel(GL.GL_SMOOTH); 
    gl.glEnable(GL.GL_TEXTURE_2D);  
    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
    gl.glEnable(GL.GL_BLEND); 

    box.initTexture(gl, "image/tex1.png");
}

Display method

public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glLoadIdentity();
        box.render(gl); 
        gl.glFlush();   
}

Myclass.init

public boolean initTexture(GL gl,String textureName){
    boolean ris=true;
    try 
    {
        Texture = TextureIO.newTexture(new File(textureName),false);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
    } 
    catch (IOException ex) 
    {
        System.err.println(ex);
        ris=false;
    }
    return ris;
}

Myclass.render

public void render(GL gl){
    gl.glTranslatef(X,Y,0.0f);
    gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f);
        Texture.enable();
        Texture.bind();
        gl.glBegin(GL.GL_QUADS);

            gl.glColor3i(1,1,1);  
            gl.glTexCoord2f(0, 0);
            gl.glVertex2f(-Width/2,-Height/2);  
            gl.glTexCoord2i(0, 1);
            gl.glVertex2f(-Width/2,Height/2);   
            gl.glTexCoord2i(1, 1);
            gl.glVertex2f(Width/2,Height/2); 
            gl.glTexCoord2i(1, 0);
            gl.glVertex2f(Width/2,-Height/2); 

    gl.glEnd();
    gl.glLoadIdentity();
}

This code shows an empty screen and it doesn't give any sort of errors. I've tested this code without texture and it works just fine.

PS: the texture i'm using is in myprojectfolder\image\ and it is a 128 x 128 pixel png image

有帮助吗?

解决方案

gl.glColor3i(1,1,1); 
           ^^^^^^^^ wat

glColor():

... Signed integer color components, when specified, are linearly mapped to floating-point values such that the most positive representable value maps to 1.0, and the most negative representable value maps to -1.0. (Note that this mapping does not convert 0 precisely to 0.0.) ...

So, you're setting the current color to something almost black (RGB(0,0,0)).

Combined with the default texenv of GL_MODULATE all your incoming texels will be multiplied by near-zero, giving near-zero.

Either switch to glColor3f(1,1,1) or GL_DECAL.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top