【问题标题】:Jogl - Picture won't load on texture mapping procedure? (JAVA)Jogl - 图片不会在纹理映射过程中加载? (JAVA)
【发布时间】:2012-03-16 19:41:36
【问题描述】:

任何人都可以看到有什么问题吗? 我正在尝试在 QUAD 形状上加载图像并将其显示在框架上。当我运行它时,没有任何东西可以显示,无论是四边形还是图像。

程序代码如下:

package firstAttempt;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;
import java.io.File;
import java.io.IOException;
import java.nio.IntBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;

/**
 * For now we will focus only two of the GLEventListeners init() and display().
 */
public class TextureGLEventListener implements GLEventListener {

    /**
     * Interface to the GLU library.
     */
    private GLU glu;
    Texture tex;

    /**
     * Take care of initialization here.
     */
    public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        glu = new GLU(); 

        //activate texture mapping for 2D
        gl.glEnable(GL.GL_TEXTURE_2D);
        try{
            //load texture
            tex = TextureIO.newTexture(new File("C://Users/encore/Desktop/DSC05255final.jpg"), true);
            tex.bind();
        }
        catch(IOException ex){
            System.err.println(ex);
        }


        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        gl.glViewport(0, 0, 900, 550);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluOrtho2D(0.0, 900.0, 0.0, 550.0);
    }

    /**
     * Take care of drawing here.
     */
    public void display(GLAutoDrawable drawable) {

        GL gl = drawable.getGL();

        gl.glBegin (GL.GL_QUADS);
        gl.glTexCoord2d(0, 0);
        gl.glVertex2d (-0.5, -0.5);
        gl.glTexCoord2d(1, 0);
        gl.glVertex2d (0.5, -0.5);
        gl.glTexCoord2d(1, 1);
        gl.glVertex2d (0.5, 0.5);
        gl.glTexCoord2d(0, 1);
        gl.glVertex2d (-0.5, 0.5);
        gl.glEnd ();



    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width,
            int height) {
    }

    public void displayChanged(GLAutoDrawable drawable,
            boolean modeChanged, boolean deviceChanged) {
    }


}

【问题讨论】:

    标签: java opengl textures jogl


    【解决方案1】:

    你不写

    gl.glFlush();
    

    四边形创建后(glEnd())。

    【讨论】:

      【解决方案2】:

      这准确地解释了你做错了什么http://www.opengl.org/wiki/Common_Mistakes#Creating_a_Texture

      您忽略了重要的 opengl 调用。因此纹理已加载但未“拉伸”。

      【讨论】:

        【解决方案3】:

        你永远不会进入纹理模式。

        我已使用下面的代码进行纹理处理,效果很好

                gl.glMatrixMode(GL.GL_TEXTURE);
                gl.glLoadIdentity();
        
                if(texture == null) {
        
                    texture = TextureIO.newTexture(img, true);
                    texture.enable();
                    texture.bind();
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-22
          • 1970-01-01
          • 1970-01-01
          • 2015-12-28
          • 1970-01-01
          • 2014-12-30
          相关资源
          最近更新 更多