我有 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 中的值,但不知道为什么它没有被应用。