У меня проблема с созданием анимации вращающихся кубиков в JAVA в OpenGL. Куб виден, окрашен и вращается, но мне также нужно с каждой стороны разную текстуру. На данный момент я не вижу текстуры там, но когда я использую println, кажется, что все текстуры были инициированы и буферизованы.JAVA OpenGL 3D Cube несколько текстур
public class Renderer implements GLEventListener, MouseListener,
MouseMotionListener, KeyListener {
private int rotationAngle;
private boolean isClicked = false;
private static int[] textures = new int[6];
private String textureList[] = { "Dice1.jpg","Dice2.jpg","Dice3.jpg","Dice4.jpg","Dice5.jpg","Dice6.jpg" };
private Texture texture;
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
glu.gluLookAt(
0f, 0f, 3f,
0f, 0f, 0f,
0f, 1f, 0f
);
gl.glRotatef(rotationAngle, 1f, 0f, 1f);
draw(gl);
if(!isClicked){
rotationAngle += 3f;
} else {
rotationAngle += 0f;
}
if (rotationAngle >= 360f)
rotationAngle %= 360f;
}
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glGenTextures(6, textures, 0);
for (int i=0;i<6;i++)
{
//...and bind it to our array
gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[i]);
gl.glEnable(GL2.GL_TEXTURE_2D);
File file = new File(textureList[i]);
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
texture = TextureIO.newTexture(file, false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(image!=null && texture != null){
ByteBuffer buf = TextureLoader.convertImageData(image);
gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, 3, 100, 100, 0, GL2.GL_BGR, GL2.GL_UNSIGNED_BYTE, buf);
//Create Nearest Filtered Texture
gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NEAREST);
//Different possible texture parameters, e.g. GL2.GL_CLAMP_TO_EDGE
gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);
gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);
file = null;
}
}
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glDepthFunc(GL2.GL_LEQUAL);
gl.glClearDepthf(1.0f);
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
GL2 gl = drawable.getGL().getGL2();
gl.glViewport(x, y, width, height);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(60.0f, (float) width/height, 1f, 10f);
gl.glMatrixMode(GL2.GL_MODELVIEW);
}
private GLUgl2 glu = new GLUgl2();
public static void draw(GL2 gl){
//Point to our buffers
gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[0]); // Texture 0 = 0.bmp
gl.glBegin(GL2.GL_QUADS);
// Front Face Of Cube
gl.glColor3f(0.0f, 0.0f, 1.0f);
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glEnd();
gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[1]); // Texture 1 = 1.bmp
gl.glBegin(GL2.GL_QUADS);
// Back Face Of Cube
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(1.0f, -1.0f, -1.0f);
gl.glEnd();
gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[2]); // Texture 2 = 2.bmp
gl.glBegin(GL2.GL_QUADS);
// Top Face Of Cube
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(1.0f, 1.0f, -1.0f);
gl.glEnd();
gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[3]); // Texture 3 = 3.bmp
gl.glBegin(GL2.GL_QUADS);
// Bottom Face Of Cube
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glEnd();
gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[4]); // Texture 4 = = 4.bmp
gl.glBegin(GL2.GL_QUADS);
// Right face Of Cube
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(1.0f, -1.0f, 1.0f);
gl.glEnd();
gl.glBindTexture(GL2.GL_TEXTURE_2D, textures[5]); // Texture 5 = 5.bmp
gl.glBegin(GL2.GL_QUADS);
// Left Face Of Cube
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glEnd();
}
@Override public void dispose(GLAutoDrawable drawable) {}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
public void mouseClicked(MouseEvent e) { isClicked = true; }
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
@Override public void keyPressed(KeyEvent arg0) {}
@Override public void mouseDragged(MouseEvent arg0) {}
@Override public void mousePressed(MouseEvent arg0) {}
}
Было бы лучше опубликовать sscce: http://sscce.org Скорее позвоните GLU.createGLU (GL), чтобы правый подкласс GLU использовался, даже если вы используете другой профиль с другой реализацией GL, это более надежное будущее , – gouessej