使用Java或C#在Y轴上打开GL翻译

时间:2016-03-05 06:35:03

标签: java c# opengl opengl-es opengl-es-2.0

我是OpenGL的新手,我有一个Triangle可以创建到我的surfaceView,然后在我的屏幕上显示。我已经能够添加旋转以使我的三角旋转,但这不是我需要的。我一直在寻找OpenGL,并阅读转换,然后进入翻译,我认为这对于向我的Triangle添加垂直移动是正确的,但是根据我目前的理解,我无法实现这一点。

示例:

三角形在屏幕顶部创建,沿Y轴向下移动到屏幕底部。

这个翻译是在一个角度,但它显示了我的意思,我只想在y轴移动。

enter image description here

我有一个Render,如下所示:

public class MyGLRenderer implements GLSurfaceView.Renderer {
    //Project Matrix
    private float mMatrix[] = new float[16];
    private Triangle mTriangle;

    private final float[] mMVPMatrix = new float[16];
    private final float[] mModelMatrix = new float[16];
    private float[] mTempMatrix = new float[16];
    private final float[] mProjectionMatrix = new float[16];
    private final float[] mViewMatrix = new float[16];
    private final float[] mRotationMatrix = new float[16];

    // Called once to set up the view's opengl es environment
    public void onSurfaceCreated(GL10 unused, EGLConfig config){

    //Set the background frame color
    GLES30.glClearColor(208.0f,208.0f,208.0f,1.0f);

    mTriangle= new Triangle();
}

// Called for each redraw of the view
public void onDrawFrame(GL10 gl){
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    //Redraw background color
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);

    // Apply transformation, start with translation
    Matrix.setIdentityM(mModelMatrix, 0); // initialize to identity matrix
    Matrix.translateM(mModelMatrix, 0, -0.5f, 0, 0); // translation to the left
    // Create a rotation transformation for the triangle
    long time = SystemClock.uptimeMillis() % 4000L;
    float mAngle = 0.090f * ((int) time);
    Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);

    Matrix.orthoM(mMatrix, 0, -1, 1, -1, 1, -1, 1);

    // Combine Rotation and Translation matrices
    mTempMatrix = mModelMatrix.clone();
    Matrix.multiplyMM(mModelMatrix, 0, mTempMatrix, 0, mRotationMatrix, 0);

    // Combine the model matrix with the projection and camera view
    mTempMatrix = mMVPMatrix.clone();
    Matrix.multiplyMM(mMVPMatrix, 0, mTempMatrix, 0, mModelMatrix, 0);

    mTriangle.draw(mMatrix);

}

// Called if the geometry of the view changes (example is when the screen orientation changes from landscape to portrait
public void onSurfaceChanged(GL10 unused, int width, int height){
    // Called if the geometry of the viewport changes
    GLES30.glViewport(0, 0, width, height);
}

public static int loadShader(int type, String shaderCode){

    // create a vertex shader type (GLES30.GL_VERTEX_SHADER)
    // or a fragment shader type (GLES30.GL_FRAGMENT_SHADER)
    int shader = GLES30.glCreateShader(type);

    // add the source code to the shader and compile it
    GLES30.glShaderSource(shader, shaderCode);
    GLES30.glCompileShader(shader);

    return shader;
}
}

我有一个三角形如下:

public class Triangle {

    private final String vertexShaderCode =
    // This matrix member variable provides a hook to manipulate
    // the coordinates of the objects that use this vertex shader
    "uniform mat4 uMVPMatrix;" +
    "attribute vec4 vPosition;" +
     "void main() {" +
     // the matrix must be included as a modifier of gl_Position
     // Note that the uMVPMatrix factor *must be first* in order
     // for the matrix multiplication product to be correct.
     "  gl_Position = uMVPMatrix * vPosition;" +
     "}";

    private final String fragmentShaderCode =
      "precision mediump float;" +
      "uniform vec4 vColor;" +
      "void main() {" +
      "  gl_FragColor = vColor;" +
      "}";

    private final FloatBuffer vertexBuffer;
    private final int mProgram;
    private int mPositionHandle;
    private int mColorHandle;
    private int mMVPMatrixHandle;

    // number of coordinates per vertex in this array
    static final int COORDS_PER_VERTEX = 3;
    static float triangleCoords[] = {
    // in counterclockwise order:
    0.0f,  0.622008459f, 0.0f,   // top
    -0.5f, -0.311004243f, 0.0f,   // bottom left
    0.5f, -0.311004243f, 0.0f    // bottom right
};

private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 0.0f };

/**
 * Sets up the drawing object data for use in an OpenGL ES context.
 */
public Triangle() {
    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(
    // (number of coordinate values * 4 bytes per float)
    triangleCoords.length * 4);
    // use the device hardware's native byte order
    bb.order(ByteOrder.nativeOrder());

    // create a floating point buffer from the ByteBuffer
    vertexBuffer = bb.asFloatBuffer();
    // add the coordinates to the FloatBuffer
    vertexBuffer.put(triangleCoords);
    // set the buffer to read the first coordinate
    vertexBuffer.position(0);

    // prepare shaders and OpenGL program
    int vertexShader = MyGLRenderer.loadShader(
      GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = MyGLRenderer.loadShader(
      GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

    mProgram = GLES20.glCreateProgram();             // create empty OpenGL Program
    GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
    GLES20.glLinkProgram(mProgram);                  // create OpenGL program executables

}

/**
 * Encapsulates the OpenGL ES instructions for drawing this shape.
 *
 * @param mvpMatrix - The Model View Project matrix in which to draw
 * this shape.
 */
public void draw(float[] mvpMatrix) {
    // Add program to OpenGL environment
    GLES20.glUseProgram(mProgram);

    // get handle to vertex shader's vPosition member
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

    // Enable a handle to the triangle vertices
    GLES20.glEnableVertexAttribArray(mPositionHandle);

    // Prepare the triangle coordinate data
    GLES20.glVertexAttribPointer(
      mPositionHandle, COORDS_PER_VERTEX,
      GLES20.GL_FLOAT, false,
      vertexStride, vertexBuffer);

    // get handle to fragment shader's vColor member
    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

    // Set color for drawing the triangle
    GLES20.glUniform4fv(mColorHandle, 1, color, 0);

    // get handle to shape's transformation matrix
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

    // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);

    // Draw the triangle
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);
}

}

我有一个Surface View,如下所示:

public class MyGLSurfaceView extends GLSurfaceView {

    private final MyGLRenderer mRenderer;

    public MyGLSurfaceView(Context context, AttributeSet attrs){
    super(context, attrs);

    //Create an OpenGl 3.0 context
    setEGLContextClientVersion(3);

    mRenderer = new MyGLRenderer();

    //Set the Renderer for drawing on the GLSurfaceView
    setRenderer(mRenderer);
}

}

我正在寻找一个可以完成在y轴上移动绘制三角形的裸骨示例/答案/ URL(我假设使用翻译)。

我已阅读并尝试实施以下链接(某些方式高于我目前的理解)。

  1. http://www.swiftless.com/tutorials/opengl/rotation.html
  2. http://www.songho.ca/opengl/gl_transform.html
  3. https://open.gl/transformations
  4. http://www.informit.com/articles/article.aspx?p=2111395&seqNum=3
  5. http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/
  6. 和其他一些人一样,这些似乎是我找到的最好的资源。

1 个答案:

答案 0 :(得分:1)

是的,这将是一个翻译。这很简单,您需要做的就是在mvpMatrix的{​​{1}}方法中修改draw()

然而,重要的是要跟踪三角形的位置以使其移动。您可以将私有成员添加到类似于此的类:

Triangle

请记住,代码中的private Point mPosition; 是一个简单的点/向量类,我假设你已经知道这是在Android上吗?查看this class

然后,您需要增加每帧三角形的y位置。您可以在Point中执行此操作,但我会添加另一种移动方法。

draw()

注意:我看到你有一个MVP矩阵作为mPosition.y += 0.01; 的参数。现在,由于这是2D和正交的,它并不重要,但考虑将您的旋转从draw函数移动到onDrawFrame(),因为M代表模型,通常是每个模型/三角形。通常,您只能将VP(视图和投影)传递给Triangle.draw()方法。检查this

最后,您可以将Triangle.draw()方法中的mvp矩阵转换为三角形的位置。因此,在将矩阵传递到着色器(Triangle.draw()之前)之前,请翻译它:

GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);