java - 在特定坐标处绘制立方体?

标签 java android opengl-es

我想在特定坐标处绘制一个立方体。我有所有 4 个角和中心的 x/y 值。我现在想在这些坐标处构建一个立方体。有谁知道任何教程或有关于我将如何完成上述任务的任何信息?

我得到了以下代码。我想将它的每个角映射到特定的 x/y 坐标

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();
 }

}

最佳答案

如果您只想将立方体转换为 x y 坐标,请使用

gl.glTranslatef(x, y, 0);

在绘制之前。

this是 android 中 opengl 转换的教程。

关于java - 在特定坐标处绘制立方体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3148708/

相关文章:

java - 我如何为 future 类(class)的类似潜在数据做好准备?

android - 无法在三星 S20、S20+ 和 S20 Ultra 上生成用于生物识别身份验证的 key 对

java - 测验应用程序与字符串不匹配

android - 如何手动创建 mipmap?

iphone - 锐化 UIImage

java - 将旧版 Spring MVC + JSP 应用程序与 Angular 应用程序一起运行是个好主意吗?

java - 为什么在Java中的文件输入输出代码中出现一些错误?

java - HttpsURLConnection 连接问题

ios - 当我通过 OpenGLES 自定义 imageView 时出了什么问题?

java - 在 Apache Poi 中调用 trackAllColumnsForAutoSIizing 的内存影响是什么