简体   繁体   English

OpenGL ES 2.0:填充球体而不是线框

[英]OpenGL ES 2.0: Filling in a Sphere instead of it being wireframe

So finally by following some other posts etc I have created a sphere in OpenGL 2.0 (Android) 所以最后通过关注其他一些帖子等,我在OpenGL 2.0(Android)中创建了一个球体

However it is currently rendering as a wireframe instead of being filled in. 但是,当前正在渲染为线框而不是被填充。

Draw code 抽奖码

public void draw()
    {
        // Set our per-vertex lighting program.
        GLES20.glUseProgram(mProgramHandle);

        // Set program handles for drawing.
        mRenderer.mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix");
        mRenderer.mMVMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVMatrix");
        mNormalHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Normal");
        mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position");
        mColorHandle = GLES20.glGetUniformLocation(mProgramHandle, "v_Color");

        // Translate the cube into the screen.
        Matrix.setIdentityM(mRenderer.mModelMatrix, 0); 
        Matrix.translateM(mRenderer.mModelMatrix, 0, position.x, position.y, position.z);
        Matrix.scaleM(mRenderer.mModelMatrix, 0, scale.x, scale.y, scale.z);

        Matrix.rotateM(mRenderer.mModelMatrix, 0, rotation.x, 1, 0,0);
        Matrix.rotateM(mRenderer.mModelMatrix, 0, rotation.y, 0, 1,0);
        Matrix.rotateM(mRenderer.mModelMatrix, 0, rotation.z, 0, 0,1);

        float color[] = { 0.0f, 0.0f, 1.0f, 1.0f };

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

        GLES20.glEnableVertexAttribArray(mPositionHandle);
        GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, BYTES_PER_VERTEX, vertexBuffer); 

        GLES20.glEnableVertexAttribArray(mNormalHandle);                

        // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
        // (which currently contains model * view).
        Matrix.multiplyMM(mRenderer.mMVPMatrix, 0, mRenderer.mViewMatrix, 0, mRenderer.mModelMatrix, 0);   

        // Pass in the modelview matrix.
        GLES20.glUniformMatrix4fv(mRenderer.mMVMatrixHandle, 1, false, mRenderer.mMVPMatrix, 0);                

        // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
        // (which now contains model * view * projection).        
        Matrix.multiplyMM(mTemporaryMatrix, 0, mRenderer.mProjectionMatrix, 0, mRenderer.mMVPMatrix, 0);
        System.arraycopy(mTemporaryMatrix, 0, mRenderer.mMVPMatrix, 0, 16);

        // Pass in the combined matrix.
        GLES20.glUniformMatrix4fv(mRenderer.mMVPMatrixHandle, 1, false, mRenderer.mMVPMatrix, 0);

        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, vertexCount);    
        //GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

    }

Sphere creation code 球体创建代码

private void generateSphereCoords(float radius, int stacks, int slices)
    {
        for (int stackNumber = 0; stackNumber <= stacks; ++stackNumber)
        {
            for (int sliceNumber = 0; sliceNumber < slices; ++sliceNumber)
            {
                float theta = (float) (stackNumber * Math.PI / stacks);
                float phi = (float) (sliceNumber * 2 * Math.PI / slices);
                float sinTheta = FloatMath.sin(theta);
                float sinPhi = FloatMath.sin(phi);
                float cosTheta = FloatMath.cos(theta);
                float cosPhi = FloatMath.cos(phi);
                vertexBuffer.put(new float[]{radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta});
            }
        }

        for (int stackNumber = 0; stackNumber < stacks; ++stackNumber)
        {
            for (int sliceNumber = 0; sliceNumber <= slices; ++sliceNumber)
            {
                indexBuffer.put((short) ((stackNumber * slices) + (sliceNumber % slices)));
                indexBuffer.put((short) (((stackNumber + 1) * slices) + (sliceNumber % slices)));
            }
        }
    }

Any ideas how I can make this 1 full colour instead of a wireframe? 有什么想法可以使这1种全彩色代替线框吗?

In the fragment shader I have 在片段着色器中

precision mediump float;        // Set the default precision to medium. We don't need as high of a 
                                // precision in the fragment shader.
uniform vec3 u_LightPos;        // The position of the light in eye space.
uniform sampler2D u_Texture;    // The input texture.

uniform vec4 v_Color;
varying vec3 v_Position;        // Interpolated position for this fragment.
varying vec3 v_Normal;          // Interpolated normal for this fragment.

// The entry point for our fragment shader.
void main()                         
{                              
    // Will be used for attenuation.
    float distance = length(u_LightPos - v_Position);                  

    // Get a lighting direction vector from the light to the vertex.
    vec3 lightVector = normalize(u_LightPos - v_Position);                  

    // Calculate the dot product of the light vector and vertex normal. If the normal and light vector are
    // pointing in the same direction then it will get max illumination.
    float diffuse = max(dot(v_Normal, lightVector), 0.0);                                                                                 

    // Add attenuation. 
    diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance)));

    // Add ambient lighting
    diffuse = diffuse + 0.7;  

    gl_FragColor = v_Color;                                     
  }  

Vertex 顶点

uniform mat4 u_MVPMatrix;       // A constant representing the combined model/view/projection matrix.                  
uniform mat4 u_MVMatrix;        // A constant representing the combined model/view matrix.              

attribute vec4 a_Position;      // Per-vertex position information we will pass in.                             
attribute vec3 a_Normal;        // Per-vertex normal information we will pass in.       

varying vec3 v_Position;        // This will be passed into the fragment shader.                            
varying vec3 v_Normal;          // This will be passed into the fragment shader.  

// The entry point for our vertex shader.  
void main()                                                     
{         

    // Transform the vertex into eye space.     
    v_Position = vec3(u_MVMatrix * a_Position);                                                

    // Transform the normal's orientation into eye space.
    v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));

    // gl_Position is a special variable used to store the final position.
    // Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
    gl_Position = u_MVPMatrix * a_Position;                               
}                                                          

It looks like you are calculating vertex indices in your setup code, but not using them. 似乎您正在设置代码中计算顶点索引,但没有使用它们。 Consider using glDrawElements instead of glDrawArrays. 考虑使用glDrawElements代替glDrawArrays。

It also looks like you are half way between using GL_TRIANGLE_STRIP (one new index per triangle after the first triangle) and GL_TRIANGLES (three indices per triangle). 看起来您还处于使用GL_TRIANGLE_STRIP(第一个三角形之后的每个三角形一个新的索引)和GL_TRIANGLES(每个三角形三个索引)之间的一半。 You will probably find it easier to use GL_TRIANGLES. 您可能会发现使用GL_TRIANGLES更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM