簡體   English   中英

JOGL白色質感?

[英]JOGL white texture?

我正在嘗試加載earth.png並將其放在一個三角形上。 圖像為256x256。 我已經按照在線教程進行了幾個小時的討論,但三角形仍然是白色的。 任何人都可以指出我正確的方向。

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;

import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;

import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureData;
import com.jogamp.opengl.util.texture.TextureIO;

public class test implements GLEventListener {
    private Texture earthTexture;

    public static void main(String[] args) {
        GLProfile glp = GLProfile.getDefault();
        GLCapabilities caps = new GLCapabilities(glp);
        GLCanvas canvas = new GLCanvas(caps);

        final Frame frame = new Frame("AWT Window Test111");
        frame.setSize(700, 700);
        frame.add(canvas);
        frame.setVisible(true);

        // by default, an AWT Frame doesn't do anything when you click
        // the close button; this bit of code will terminate the program when
        // the window is asked to close
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                frame.dispose();
                System.exit(0);
            }
        });
    canvas.addGLEventListener(new test());

    }


    @Override
    public void display(GLAutoDrawable arg0) {


        update();
        render(arg0);


    }

    private void update() {
        // TODO Auto-generated method stub

    }

    private void render(GLAutoDrawable drawable) {

        GL2 gl = drawable.getGL().getGL2();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glEnable(GL.GL_TEXTURE_2D);

        gl.glBegin(GL2.GL_TRIANGLES);                           // Begin drawing triangle sides

        earthTexture.enable();
        earthTexture.bind();

        // gl.glColor3f( 1.0f, 0.0f, 0.0f);                     // Set colour to red
    gl.glTexCoord2f(0.0f, 0.0f);
    gl.glVertex3f( 0.0f, 1.0f, 1.0f);                       // Top vertex
    gl.glTexCoord2f(-1.0f, -2.0f);
    gl.glVertex3f(-1.0f,-1.0f, 0.0f);                       // Bottom left vertex
    gl.glTexCoord2f(1.0f, -2.0f);
    gl.glVertex3f( 1.0f,-1.0f, 0.0f);                       // Bottom right vertex

        gl.glEnd();

    }

    @Override
    public void dispose(GLAutoDrawable arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void init(GLAutoDrawable arg0) {
        GL2 gl = arg0.getGL().getGL2();

        // Load texture.
       try {
            InputStream stream = getClass().getResourceAsStream("earth.png");
           TextureData data = TextureIO.newTextureData(gl.getGLProfile(), stream, 100, 200, false, "png");
           earthTexture = TextureIO.newTexture(data);
       }
       catch (IOException exc) {
           exc.printStackTrace();
           System.exit(1);
       }


    }

    @Override
    public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
            int arg4) {
        // TODO Auto-generated method stub

    }



}

您在glBegin / glEnd語句之間綁定紋理。 它需要在glBegin 之前這樣做。 開始/結束對之間的紋理切換可能會被忽略。

我注意到的一些事情:

您需要使用以下內容在OpenGL中顯式啟用紋理:

gl.glEnable(GL.GL_TEXTURE_2D);

您還需要指定紋理的坐標(通常表示為u,v坐標),這需要針對每個3D點進行:

gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f( 0.0f, 1.0f, 1.0f); 
...

如今優秀的NeHe教程也有JOGL示例代碼,值得深入研究:

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=07

本文還有一些關於理解紋理坐標的好信息:

http://www.opengl.org/resources/code/samples/sig99/advanced99/notes/node52.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM