正确的顶点着色器code? OpenGL ES 2.0的 [英] Correct vertex shader code? OpenGL ES 2.0

查看:281
本文介绍了正确的顶点着色器code? OpenGL ES 2.0的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改 code补充,请参见下面

Edit Code added, please see below

编辑2 - 从截图与解释说明包括在底部装置

Edit 2 - Screenshots from device included at bottom along with explanation

修改3 - 新的code添加

Edit 3 - New code added

我有2个班,渲染和定制'四'类。

I have 2 classes, a rendered and a custom 'quad' class.

我的这些声明在一流水平在我的渲染器类:

I have these declared at class level in my renderer class:

final float[] mMVPMatrix = new float[16];
final float[] mProjMatrix = new float[16];
final float[] mVMatrix = new float[16];

在我onSurfaceChanged方法,我有:

And in my onSurfaceChanged method I have:

@覆盖
    公共无效onSurfaceChanged(GL10 GL,诠释的宽度,高度INT){

@Override public void onSurfaceChanged(GL10 gl, int width, int height) {

    GLES20.glViewport(0, 0, width, height);

    float ratio = (float) width / height;
    Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);

}

和....

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // TODO Auto-generated method stub

    myBitmap = BitmapFactory.decodeResource(curView.getResources(), R.drawable.box);

        //Create new Dot objects

        dot1 = new Quad();
        dot1.setTexture(curView, myBitmap);
        dot1.setSize(300,187);    //These numbers are the size but are redundant/not used at the moment.

        myBitmap.recycle();


           //Set colour to black
           GLES20.glClearColor(0, 0, 0, 1);

}

最后,来自这个类,onDrawFrame:

And finally from this class, onDrawFrame:

@Override
public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub

    //Paint the screen the colour defined in onSurfaceCreated
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

     // Set the camera position (View matrix) so looking from the front
   Matrix.setLookAtM(mVMatrix, 0, 0, 0, 3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    // Combine
   Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);

    dot1.rotateQuad(0,0,45, mMVPMatrix); //x,y,angle and matrix passed in

}

然后,在我四类:

Then, in my quad class:

本声明在类级别:

        private float[] mRotationMatrix = new float[16];        
    private final float[] mMVPMatrix = new float[16];
    private final float[] mProjMatrix = new float[16];
    private final float[] mVMatrix = new float[16];
    private int mMVPMatrixHandle;
    private int mPositionHandle;
    private int mRotationHandle;
//Create our vertex shader
    String strVShader =  
              "uniform mat4 uMVPMatrix;" +
              "uniform mat4 uRotate;" +
              "attribute vec4 a_position;\n"+
              "attribute vec2 a_texCoords;" +
              "varying vec2 v_texCoords;" +
              "void main()\n" +
              "{\n" +
         //     "gl_Position = a_position * uRotate;\n"+
         //          "gl_Position = uRotate * a_position;\n"+
               "gl_Position = a_position * uMVPMatrix;\n"+  
        //        "gl_Position = uMVPMatrix * a_position;\n"+  
                  "v_texCoords = a_texCoords;" +
              "}";

    //Fragment shader

    String strFShader =
        "precision mediump float;" +
        "varying vec2 v_texCoords;" +
        "uniform sampler2D u_baseMap;" +
        "void main()" +
        "{" +
        "gl_FragColor = texture2D(u_baseMap, v_texCoords);" +
        "}";

然后设置纹理方法(不认为这是有关这个问题,但!!)

Then method for setting texture (don't think this is relevant to this problem though!!)

    public void setTexture(GLSurfaceView view, Bitmap imgTexture){
        this.imgTexture=imgTexture;

        iProgId = Utils.LoadProgram(strVShader, strFShader);
        iBaseMap = GLES20.glGetUniformLocation(iProgId, "u_baseMap");
                iPosition = GLES20.glGetAttribLocation(iProgId, "a_position");
        iTexCoords = GLES20.glGetAttribLocation(iProgId, "a_texCoords");
        texID = Utils.LoadTexture(view, imgTexture);

    }

最后,我的'rotateQuad的方法(目前应该绘制和旋转四)。

And finally, my 'rotateQuad' method (which currently is supposed to draw and rotate the quad).

public void rotateQuad(float x, float y, int angle, float[] mvpMatrix){

Matrix.setRotateM(mRotationMatrix, 0, angle, 0, 0, 0.1f);

//  Matrix.translateM(mRotationMatrix, 0, 0, 0, 0);  //Removed temporarily

// Combine the rotation matrix with the projection and camera view
   Matrix.multiplyMM(mvpMatrix, 0, mRotationMatrix, 0, mvpMatrix, 0);

float[] vertices = {

        -.5f,.5f,0, 0,0,
        .5f,.5f,0, 1,0,
        -.5f,-.5f,0, 0,1,
        .5f,-.5f,0, 1,1

        };

 vertexBuf = ByteBuffer.allocateDirect(vertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
 vertexBuf.put(vertices).position(0);

//Bind the correct texture
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texID);      
//Use program
GLES20.glUseProgram(iProgId);
// get handle to shape's transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(iProgId, "uMVPMatrix");
   // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
    // get handle to shape's rotation matrix
mRotationHandle = GLES20.glGetUniformLocation(iProgId, "uRotate");
  // Apply the projection and view transformation
  GLES20.glUniformMatrix4fv(mRotationHandle, 1, false, mRotationMatrix, 0);

//Set starting position for vertices
vertexBuf.position(0);
//Specify attributes for vertex
GLES20.glVertexAttribPointer(iPosition, 3, GLES20.GL_FLOAT, false, 5 * 4, vertexBuf);
//Enable attribute for position
GLES20.glEnableVertexAttribArray(iPosition);
//Set starting position for texture
vertexBuf.position(3);
//Specify attributes for vertex
GLES20.glVertexAttribPointer(iTexCoords, 2, GLES20.GL_FLOAT, false, 5 * 4, vertexBuf);
//Enable attribute for texture
GLES20.glEnableVertexAttribArray(iTexCoords);
//Draw it
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
}

有关编辑2

这是因为在屏幕的中心画出我的四边形。不进行旋转。

This is my quad as drawn in the center of the screen. No rotation.

这是相同的四在+45度与code旋转GL_POSITION = a_position * uMVPMatrix;在我的顶点着色器+(它从一个不同的项目,现在是如此的着色器变量a_position而不是vPosition),它看起来是正确的!

This is the same quad rotated at +45 Degrees with the code "gl_Position = a_position * uMVPMatrix;" + in my vertex shader (it's from a different project now so the shader variable is a_position and not vPosition), it's looks correct!

不过,这是相同的四在+45度旋转的2着色器变量切换(所以他们读GL_POSITION = uMVPMatrix * a_position; - 你可以看到,它不是完全正确。

However, this is the same quad rotated at +45 Degrees with the 2 shader variables switched (so they read "gl_Position = uMVPMatrix * a_position;" - as you can see, it's not quite right.

也只需一个侧面说明,你不能在这里看到它作为quare是对称的,但每种方法也给其他旋转在相反的方向....

Also just a side note, you can't see it here as the quare is symetrical, but each method also rotates in the opposite direction to the other....

任何帮助AP preciated。

Any help appreciated.

推荐答案

这是真的不可能说,因为我们不知道要传递什么,这两个变量。

It's really impossible to tell because we don't know what you are passing to these two variables.

OpenGL是列为主的格式,所以如果 vPosition 其实是一个载体,而 uMVPMatrix 是一个矩阵,那么第一个选项是正确的,如果这是你的着色器。

OpenGL is column-major format, so if vPosition is in fact a vector, and uMVPMatrix is a matrix, then the first option is correct, if this is in your shader.

如果这不是你的着色器,但在你的程序code,那么就没有足够的信息。

If this is not in your shader but in your program code, then there is not enough information.

如果您使用的是第一个选项,但得到意想不到的效果,你可能不会计算你的矩阵正常与否传递正确的顶点。

If you are using the first option but getting unexpected results, you are likely not computing your matrix properly or not passing the correct vertices.

这篇关于正确的顶点着色器code? OpenGL ES 2.0的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆