1

我有 acube,并且要在所有侧面放置纹理。我在一个netbeans项目中成功地做到了这一点,但最后我知道它是在eclipse中完成的,起初听起来没什么大不了的。但问题是相同的代码在 Eclipse 中不起作用。显示立方体,但未应用纹理。继承人的代码。

        public void init(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2(); // Get the GL pipeline object this 
        // GLAutoDrawable uses

        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set clear color to black
        gl.glClearDepth(1.0f); // Set the depth value to 1.0 (the maximum value),
        // when the depth buffer is cleared

        gl.glEnable(GL_DEPTH_TEST); //Enable depth test

        gl.glEnable(GL_LIGHT0);     
        gl.glEnable(GL_LIGHTING);           


        try {
            texImg = readImage("PWelshDragon.jpg");
        } catch (IOException ex) {
         //   Logger.getLogger(CG0T.class.getName()).log(Level.SEVERE, null, ex);
        }

             gl.glDisable(GL.GL_TEXTURE_2D);
             gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, texWidth ,texHeight, 0, GL.GL_RGB, GL.  GL_UNSIGNED_BYTE,  texImg);
        gl.glEnable(GL.GL_TEXTURE_2D);

    }

在显示代码中。

public void display(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();

        gl.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        gl.glMatrixMode(GL_MODELVIEW);
        gl.glLoadIdentity();

        gl.glRotated(30, 1, 1, 1);

        gl.glBegin(GL_QUADS);
            gl.glNormal3f(1.0f, 0.0f, 0.0f);
            gl.glVertex3f(1.0f, 1.0f, 1.0f);gl.glTexCoord2d(0.0f, 0.0f);
            gl.glVertex3f(1.0f, -1.0f, 1.0f);gl.glTexCoord2d(1.0f, 0.0f);
            gl.glVertex3f(1.0f, -1.0f, -1.0f);gl.glTexCoord2d(1.0f, 1.0f);
            gl.glVertex3f(1.0f, 1.0f, -1.0f);gl.glTexCoord2d(0.0f, 1.0f);

            gl.glNormal3f(-1.0f, 0.0f, 0.0f);
            gl.glVertex3f(-1.0f, 1.0f, 1.0f);gl.glTexCoord2d(0.0f, 0.0f);
            gl.glVertex3f(-1.0f, 1.0f, -1.0f);gl.glTexCoord2d(1.0f, 0.0f);
            gl.glVertex3f(-1.0f, -1.0f, -1.0f);gl.glTexCoord2d(1.0f, 1.0f);
            gl.glVertex3f(-1.0f, -1.0f, 1.0f);gl.glTexCoord2d(0.0f, 1.0f);


            gl.glNormal3f(0.0f, 1.0f, 0.0f);
            gl.glVertex3f(1.0f, 1.0f, 1.0f);gl.glTexCoord2d(0.0f, 0.0f);
            gl.glVertex3f(1.0f, 1.0f, -1.0f);gl.glTexCoord2d(1.0f, 0.0f);
            gl.glVertex3f(-1.0f, 1.0f, -1.0f);gl.glTexCoord2d(1.0f, 1.0f);
            gl.glVertex3f(-1.0f, 1.0f, 1.0f);gl.glTexCoord2d(0.0f, 1.0f);

            gl.glNormal3f(0.0f, -1.0f, 0.0f);
            gl.glVertex3f(1.0f, -1.0f, 1.0f);gl.glTexCoord2d(0.0f, 0.0f);
            gl.glVertex3f(-1.0f, -1.0f, 1.0f);gl.glTexCoord2d(1.0f, 0.0f);
            gl.glVertex3f(-1.0f, -1.0f, -1.0f);gl.glTexCoord2d(1.0f, 1.0f);
            gl.glVertex3f(1.0f, -1.0f, -1.0f);gl.glTexCoord2d(0.0f, 1.0f);

            gl.glNormal3f(0.0f, 0.0f, 1.0f);
            gl.glVertex3f(1.0f, 1.0f, 1.0f);gl.glTexCoord2d(0.0f, 0.0f);
            gl.glVertex3f(-1.0f, 1.0f, 1.0f);gl.glTexCoord2d(1.0f, 0.0f);
            gl.glVertex3f(-1.0f, -1.0f, 1.0f);gl.glTexCoord2d(1.0f, 1.0f);
            gl.glVertex3f(1.0f, -1.0f, 1.0f);gl.glTexCoord2d(0.0f, 1.0f );


            gl.glNormal3f(0.0f, 0.0f, -1.0f);
            gl.glVertex3f(1.0f, 1.0f, -1.0f);gl.glTexCoord2d(0.0f, 0.0f);
            gl.glVertex3f(1.0f, -1.0f, -1.0f);gl.glTexCoord2d(1.0f, 0.0f);
            gl.glVertex3f(-1.0f, -1.0f, -1.0f);gl.glTexCoord2d(1.0f, 1.0f);
            gl.glVertex3f(-1.0f, 1.0f, -1.0f);gl.glTexCoord2d(0.0f, 1.0f);
        gl.glEnd();
    }

我得到了 texImg 中的值,但不知道为什么它没有被应用。

4

2 回答 2

1

我通过在显示功能的图像绘制代码之前添加以下几行来解决了这个问题。(之前gl.glBegin(GL_QUADS);

gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_S,GL2.GL_CLAMP);
gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_T,GL2.GL_CLAMP);
gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_NEAREST);
gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_NEAREST);
gl.glTexEnvi(GL2.GL_TEXTURE_ENV,GL2.GL_TEXTURE_ENV_MODE,GL2.GL_MODULATE);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT,GL2.GL_NICEST);

我真的不知道,这些线是做什么的。我在这里看到了

于 2012-11-25T14:12:15.060 回答
-1

我对Java中的opengl一无所知,但它不能仅仅是因为找不到或其他原因而没有加载您的图像文件“PWelshDragon.jpg”吗?

您是否尝试过放回注释行

  Logger.getLogger(CG0T.class.getName()).log(Level.SEVERE, null, ex);

在抓到这里看看有没有问题?

于 2012-11-24T15:19:48.267 回答