我试图用calvin和hobbes的图像在jogl中构建一个立方体。我在网上看到的每一个消息来源都说有这条线
gl.glEnable(GL2.GL_TEXTURE_2D);
在那里。但是,当我添加它时,我得到一个黑屏。当我评论它时,我在屏幕上得到一个立方体。欢迎任何帮助,因为我已经搜索了这个问题,并尝试了无数的解决方案。我的源代码如下。
package project1;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GL3;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureIO;
/**
* Simple Implements GLEventListener so that I can modify
* what happens when application runs
*
* @author Alex Thoennes
*
*/
public class Simple extends GLCanvas implements GLEventListener
{
private static final long serialVersionUID = 1L;
private double eyeX = 5;
private double eyeY = 5;
private double eyeZ = 5;
private float o = 0.0001f;
// this is the title of the frame
static final String TITLE = "Simple";
// these are the dimensions of the frame
static final int CANVAS_WIDTH = 500;
static final int CANVAS_HEIGHT = 500;
int texture;
Texture t;
public static void main (String [] args)
{
// this class extends the GLCanvas class
// so that I have something to reference
// when I want to draw
GLCanvas canvas = new Simple();
canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
// create a new frame to hold the canvas
JFrame frame = new JFrame();
// add the canvas to the frame
frame.getContentPane().add(canvas);
// set the title of the frame
frame.setTitle(TITLE);
// this is not required but is convenient. When you
// click the 'x' in the top left of the screen, the
// application will close
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// pack the frame
frame.pack();
// then make it displayable to the user
frame.setVisible(true);
// Create an animator object and then add it to
// the canvas and start the animation
Animator animator = new Animator(canvas);
animator.start();
}
// constructor for the class
// you need to add a GLEventListener
// to it so that you can draw with the gl methods
public Simple ()
{
this.addGLEventListener(this);
}
@Override
public void init(GLAutoDrawable drawable)
{
final GL2 gl = drawable.getGL().getGL2();
gl.glEnable(GL2.GL_DEPTH_TEST);
//gl.glEnable(GL2.GL_TEXTURE_2D);
try
{
File im = new File("calvin-and-hobbes.png");
t = TextureIO.newTexture(im, true);
texture = t.getTextureObject(gl);
gl.glBindTexture(GL2.GL_TEXTURE_2D, texture);
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
}
catch(IOException e)
{
e.printStackTrace();
}
}
@Override
public void dispose(GLAutoDrawable drawable) {}
@Override
public void display(GLAutoDrawable drawable)
{
// create objects used for drawing
final GL2 gl = drawable.getGL().getGL2();
GLU glu = new GLU();
transformEye();
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(40.0, 1.0, 1.0, 10000.0);
gl.glMatrixMode(GL2.GL_TEXTURE);
gl.glLoadIdentity();
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
glu.gluLookAt(eyeX, eyeY, eyeZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
//gl.glEnable(GL2.GL_TEXTURE_2D);
//t.bind(gl);
//t.enable(gl);
gl.glBindTexture(GL2.GL_TEXTURE_2D, texture);
//gl.glEnable(GL2.GL_TEXTURE_2D);
drawTriangles(gl);
//gl.glFlush();
// increment the angle so that the viewer rotates
//o += 0.0001f;
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {}
/**
* This method rotates the viewer
*/
private void transformEye()
{
// use o for theta which is the amount of degrees
eyeX = (5 * Math.cos(o)) + (5 * Math.sin(o));
eyeZ = (-5 * Math.sin(o)) + (5 * Math.cos(o));
}
/**
* This method draws all the triangles as the viewer rotates
*
* @param gl
*/
private void drawTriangles(GL2 gl)
{
// gl.glColor3f(1.0f, 1.0f, 1.0f);
// gl.glColor3f(1.0f, 0.0f, 0.0f);
// Triangle 0
gl.glBegin(GL3.GL_TRIANGLES);
gl.glTexCoord2f(0f, 0f);
gl.glVertex3f(0.5f, 0.5f, 0.5f);
gl.glTexCoord2f(0f, 1f);
gl.glVertex3f(0.5f, -0.5f, 0.5f);
gl.glTexCoord2f(1f, 0f);
gl.glVertex3f(-0.5f, 0.5f, 0.5f);
gl.glEnd();
// Triangle 1
gl.glBegin(GL3.GL_TRIANGLES);
// gl.glTexCoord3f(-0.5f, -0.5f, 0.5f);
gl.glVertex3f(-0.5f, -0.5f, 0.5f);
// gl.glTexCoord3f(0.5f, -0.5f, 0.5f);
gl.glVertex3f(0.5f, -0.5f, 0.5f);
// gl.glTexCoord3f(-0.5f, 0.5f, 0.5f);
gl.glVertex3f(-0.5f, 0.5f, 0.5f);
gl.glEnd();
// gl.glColor3f(0.0f, 0.5f, 0.0f);
// Triangle 2
gl.glBegin(GL3.GL_TRIANGLES);
gl.glVertex3f(0.5f, 0.5f, 0.5f);
gl.glVertex3f(0.5f, 0.5f, -0.5f);
gl.glVertex3f(-0.5f, 0.5f, 0.5f);
gl.glEnd();
// Triangle 3
gl.glBegin(GL3.GL_TRIANGLES);
gl.glVertex3f(-0.5f, 0.5f, -0.5f);
gl.glVertex3f(0.5f, 0.5f, -0.5f);
gl.glVertex3f(-0.5f, 0.5f, 0.5f);
gl.glEnd();
// gl.glColor3f(0.0f, 0.0f, 0.5f);
// Triangle 4
gl.glBegin(GL3.GL_TRIANGLES);
gl.glVertex3f(0.5f, -0.5f, 0.5f);
gl.glVertex3f(0.5f, 0.5f, 0.5f);
gl.glVertex3f(0.5f, 0.5f, -0.5f);
gl.glEnd();
// Triangle 5
gl.glBegin(GL3.GL_TRIANGLES);
gl.glVertex3f(0.5f, -0.5f, 0.5f);
gl.glVertex3f(0.5f, -0.5f, -0.5f);
gl.glVertex3f(0.5f, 0.5f, -0.5f);
gl.glEnd();
// gl.glColor3f(0.5f, 0.0f, 0.5f);
// Triangle 6
gl.glBegin(GL3.GL_TRIANGLES);
gl.glVertex3f(-0.5f, -0.5f, 0.5f);
gl.glVertex3f(-0.5f, -0.5f, -0.5f);
gl.glVertex3f(0.5f, -0.5f, -0.5f);
gl.glEnd();
// Triangle 7
gl.glBegin(GL3.GL_TRIANGLES);
gl.glVertex3f(-0.5f, -0.5f, 0.5f);
gl.glVertex3f(0.5f, -0.5f, 0.5f);
gl.glVertex3f(0.5f, -0.5f, -0.5f);
gl.glEnd();
// gl.glColor3f(0.0f, 0.2f, 0.613f);
// Triangle 8
gl.glBegin(GL3.GL_TRIANGLES);
gl.glVertex3f(-0.5f, -0.5f, 0.5f);
gl.glVertex3f(-0.5f, -0.5f, -0.5f);
gl.glVertex3f(-0.5f, 0.5f, -0.5f);
gl.glEnd();
// Triangle 9
gl.glBegin(GL3.GL_TRIANGLES);
gl.glVertex3f(-0.5f, 0.5f, 0.5f);
gl.glVertex3f(-0.5f, -0.5f, 0.5f);
gl.glVertex3f(-0.5f, 0.5f, -0.5f);
gl.glEnd();
// gl.glColor3f(0.6134f, 0.4513f, 0.1423f);
// Triangle 10
gl.glBegin(GL3.GL_TRIANGLES);
gl.glVertex3f(-0.5f, -0.5f, -0.5f);
gl.glVertex3f(0.5f, -0.5f, -0.5f);
gl.glVertex3f(0.5f, 0.5f, -0.5f);
gl.glEnd();
// Triangle 11
gl.glBegin(GL3.GL_TRIANGLES);
gl.glVertex3f(-0.5f, -0.5f, -0.5f);
gl.glVertex3f(-0.5f, 0.5f, -0.5f);
gl.glVertex3f(0.5f, 0.5f, -0.5f);
gl.glEnd();
}
}