我想在特定的坐标上绘制一个立方体。我有四个角和中心x / y值。我现在想在这些坐标上构建一个立方体。有没有人知道任何教程或有关于我将如何进行所述任务的任何信息?
我有以下代码。我想将每个角落映射到特定的x / y co-ord
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
class GLCube {
private final IntBuffer mVertexBuffer;
private final IntBuffer mTextureBuffer;
public GLCube() {
int one = 65536;
int half = one / 2;
int vertices[] = {
// FRONT
-half, -half, half, half, -half, half,
-half, half, half, half, half, half,
// BACK
-half, -half, -half, -half, half, -half,
half, -half, -half, half, half, -half,
// LEFT
-half, -half, half, -half, half, half,
-half, -half, -half, -half, half, -half,
// RIGHT
half, -half, -half, half, half, -half,
half, -half, half, half, half, half,
// TOP
-half, half, half, half, half, half,
-half, half, -half, half, half, -half,
// BOTTOM
-half, -half, half, -half, -half, -half,
half, -half, half, half, -half, -half, };
int texCoords[] = {
// FRONT
0, one, one, one, 0, 0, one, 0,
// BACK
one, one, one, 0, 0, one, 0, 0,
// LEFT
one, one, one, 0, 0, one, 0, 0,
// RIGHT
one, one, one, 0, 0, one, 0, 0,
// TOP
one, 0, 0, 0, one, one, 0, one,
// BOTTOM
0, 0, 0, one, one, 0, one, one, };
// Buffers to be passed to gl*Pointer() functions must be
// direct, i.e., they must be placed on the native heap
// where the garbage collector cannot move them.
//
// Buffers with multi-byte data types (e.g., short, int,
// float) must have their byte order set to native order
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asIntBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
// ...
ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
tbb.order(ByteOrder.nativeOrder());
mTextureBuffer = tbb.asIntBuffer();
mTextureBuffer.put(texCoords);
mTextureBuffer.position(0);
}
public void draw(GL10 gl) {
gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
gl.glEnable(GL10.GL_TEXTURE_2D); // workaround bug 3623
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, mTextureBuffer);
gl.glColor4f(1, 1, 1, 1);
gl.glNormal3f(0, 0, 1);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glNormal3f(0, 0, -1);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
gl.glColor4f(1, 1, 1, 1);
gl.glNormal3f(-1, 0, 0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
gl.glNormal3f(1, 0, 0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);
gl.glColor4f(1, 1, 1, 1);
gl.glNormal3f(0, 1, 0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
gl.glNormal3f(0, -1, 0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);
}
static void loadTexture(GL10 gl, Context context, int resource) {
Bitmap bmp = BitmapFactory.decodeResource(
context.getResources(), resource);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
gl.glTexParameterx(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterx(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
bmp.recycle();
}
}
答案 0 :(得分:1)
答案 1 :(得分:0)
package projectLab;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLJPanel;
import javax.swing.JFrame;
public class Cube extends GLJPanel implements GLEventListener, KeyListener {
public static void main(String[] args) {
JFrame window = new JFrame("Cube");
GLCapabilities caps = new GLCapabilities(null);
Cube panel = new Cube(caps);
window.setContentPane(panel);
window.pack();
window.setLocation(50, 50);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
panel.requestFocusInWindow();
}
private float rotateX, rotateY, rotateZ;
public Cube(GLCapabilities capabilities) {
super(capabilities);
setPreferredSize(new Dimension(500, 500));
addGLEventListener(this);
addKeyListener(this);
rotateX = 15;
rotateY = 15;
rotateZ = 0;
}
private void cube(GL2 gl) {
gl.glBegin(gl.GL_POLYGON);
gl.glColor3d(1.0, 1.0, 0.0);
gl.glVertex3d(0.5, -0.5, -0.5);
gl.glVertex3d(0.5, 0.5, -0.5);
gl.glVertex3d(-0.5, 0.5, -0.5);
gl.glVertex3d(-0.5, -0.5, -0.5);
gl.glEnd();
gl.glBegin(gl.GL_POLYGON);
gl.glColor3d(1.0, 1.0, 1.0);
gl.glVertex3d(0.5, -0.5, 0.5);
gl.glVertex3d(0.5, 0.5, 0.5);
gl.glVertex3d(-0.5, 0.5, 0.5);
gl.glVertex3d(-0.5, -0.5, 0.5);
gl.glEnd();
gl.glBegin(gl.GL_POLYGON);
gl.glColor3d(1.0, 0.0, 1.0);
gl.glVertex3d(0.5, -0.5, -0.5);
gl.glVertex3d(0.5, 0.5, -0.5);
gl.glVertex3d(0.5, 0.5, 0.5);
gl.glVertex3d(0.5, -0.5, 0.5);
gl.glEnd();
gl.glBegin(gl.GL_POLYGON);
gl.glColor3d(0.0, 1.0, 0.0);
gl.glVertex3d(-0.5, -0.5, 0.5);
gl.glVertex3d(-0.5, 0.5, 0.5);
gl.glVertex3d(-0.5, 0.5, -0.5);
gl.glVertex3d(-0.5, -0.5, -0.5);
gl.glEnd();
gl.glBegin(gl.GL_POLYGON);
gl.glColor3d(0.0, 0.0, 1.0);
gl.glVertex3d(0.5, 0.5, 0.5);
gl.glVertex3d(0.5, 0.5, -0.5);
gl.glVertex3d(-0.5, 0.5, -0.5);
gl.glVertex3d(-0.5, 0.5, 0.5);
gl.glEnd();
gl.glBegin(gl.GL_POLYGON);
gl.glColor3d(1.0, 0.0, 0.0);
gl.glVertex3d(0.5, -0.5, -0.5);
gl.glVertex3d(0.5, -0.5, 0.5);
gl.glVertex3d(-0.5, -0.5, 0.5);
gl.glVertex3d(-0.5, -0.5, -0.5);
gl.glEnd();
gl.glFlush();
}
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClearColor(0, 0, 0, 0);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glRotatef(rotateZ, 0, 0, 1);
gl.glRotatef(rotateY, 0, 1, 0);
gl.glRotatef(rotateX, 1, 0, 0);
cube(gl);
}
public void init(GLAutoDrawable drawable) {
// called when the panel is created
GL2 gl = drawable.getGL().getGL2();
gl.glEnable(GL.GL_DEPTH_TEST);
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
}
@Override
public void keyPressed(java.awt.event.KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
rotateY -= 15;
else if (key == KeyEvent.VK_RIGHT)
rotateY += 15;
else if (key == KeyEvent.VK_DOWN)
rotateX += 15;
else if (key == KeyEvent.VK_UP)
rotateX -= 15;
else if (key == KeyEvent.VK_PAGE_UP)
rotateZ += 15;
else if (key == KeyEvent.VK_PAGE_DOWN)
rotateZ -= 15;
else if (key == KeyEvent.VK_HOME)
rotateX = rotateY = rotateZ = 0;
repaint();
}
@Override
public void keyReleased(java.awt.event.KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(java.awt.event.KeyEvent e) {
// TODO Auto-generated method stub
}
}
我像这样画画。