首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JOGL白色纹理?

JOGL白色纹理?
EN

Stack Overflow用户
提问于 2011-03-08 08:34:55
回答 2查看 4.6K关注 0票数 4

我正在尝试加载earth.png并将其放在一个三角形上。图像大小为256x256。我遵循了一个在线教程,玩了几个小时,但三角形仍然是白色的。有谁能给我指出正确的方向。

代码语言:javascript
复制
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

    }



}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-08 08:56:09

我注意到了几件事:

您需要在OpenGL中显式启用纹理,方法如下:

代码语言:javascript
复制
gl.glEnable(GL.GL_TEXTURE_2D);

您还需要为纹理指定坐标(通常表示为u,v坐标),这需要为每个3D点执行此操作:

代码语言:javascript
复制
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

票数 4
EN

Stack Overflow用户

发布于 2011-03-08 22:55:37

在glBegin/glEnd语句之间绑定纹理。在glBegin之前需要这样做。开始/结束对之间的纹理切换可能会被忽略。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5227023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档