我有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中获取值,但不知道为什么它没有被应用。
答案 0 :(得分: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);
我真的不知道,这些线是做什么的。我看到它here
答案 1 :(得分:-1)
我对Java中的opengl一无所知但是它不能仅仅是你的图像文件“PWelshDragon.jpg”没有被加载因为找不到或者其他什么?
您是否尝试过放回注释行
Logger.getLogger(CG0T.class.getName()).log(Level.SEVERE, null, ex);
在捕获中看看这里是否有问题?