【问题标题】:Problem drawing a sphere in OPENGL ES在 OPENGL ES 中绘制球体的问题
【发布时间】:2013-05-09 15:49:21
【问题描述】:

我一直在尝试使用 OpenGL ES 创建自己的球体,并遵循此处描述的数学http://www.math.montana.edu/frankw/ccp/multiworld/multipleIVP/spherical/learn.htm

但是,当我绘制球体(仅顶点)时,只绘制了一半的球体。您能否指出我在下面给出的代码中的确切问题是什么:

public class Sphere {

    static private FloatBuffer sphereVertex;
    static private FloatBuffer sphereNormal;
    static float sphere_parms[]=new float[3];

    double mRaduis;
    double mStep;
    float mVertices[];
    private static double DEG = Math.PI/180;
    int mPoints;

    /**
     * The value of step will define the size of each facet as well as the number of facets
     *  
     * @param radius
     * @param step
     */

    public Sphere( float radius, double step) {
        this.mRaduis = radius;
        this.mStep = step;
        sphereVertex = FloatBuffer.allocate(40000);
        mPoints = build();
        Log.d("ALIS CHECK!!!!!!", " COUNT:" + mPoints);
    }

    public void draw(GL10 gl) {
        gl.glFrontFace(GL10.GL_CW);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, sphereVertex);

        gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
        gl.glDrawArrays(GL10.GL_POINTS, 0, mPoints);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }

    private int build() {

        /**
         * x = p * sin(phi) * cos(theta)
         * y = p * sin(phi) * sin(theta)
         * z = p * cos(phi)
         */
        double dTheta = mStep * DEG;
        double dPhi = dTheta;
        int points = 0;

        for(double phi = -(Math.PI/2); phi <= Math.PI/2; phi+=dPhi) {
            //for each stage calculating the slices
            for(double theta = 0.0; theta <= (Math.PI * 2); theta+=dTheta) {
                sphereVertex.put((float) (mRaduis * Math.sin(phi) * Math.cos(theta)) );
                sphereVertex.put((float) (mRaduis * Math.sin(phi) * Math.sin(theta)) );
                sphereVertex.put((float) (mRaduis * Math.cos(phi)) );
                points++;

            }
        }
        sphereVertex.position(0);
        return points;
    }
}

这也是我的渲染器类:

/**
 * These values are used to rotate the image by a certain value
 */
private float xRot;
private float yRot;

public void setxRot(float xRot) {
    this.xRot += xRot;
}

public void setyRot(float yRot) {
    this.yRot += yRot;
}

public GLViewRenderer(Context ctx) {
    //initialize our 3D triangle here
    mSphere = new Sphere(1, 25);
    //Initializing the rate counter object
    //This will help us in calculating the frames per second
    fpsCalculator = new RateCounter(TAG);
    fpsCalculator.start();
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    //initialize all the things required for openGL configurations
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    

    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); 
}

public void onDrawFrame(GL10 gl) {
    if(fpsCalculator != null) 
        fpsCalculator.countOnce();

    //write the drawing code needed
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);   
    gl.glLoadIdentity();

    /**
     * Change this value in z if you want to see the image zoomed in
     */
    gl.glTranslatef(0.0f, 0.0f, -5.0f); 

    gl.glRotatef(xRot, 0.0f, 1.0f, 0.0f);
    gl.glRotatef(yRot, 1.0f, 0.0f, 0.0f);
    mSphere.draw(gl);
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
    if(height == 0) {                       
        height = 1;                         
    }

    gl.glViewport(0, 0, width, height); 
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();                

    //Calculate The Aspect Ratio Of The Window
    GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);

    gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select The Modelview Matrix
    gl.glLoadIdentity();                    //Reset The Modelview Matrix
}

/**
 * Used to stop the FPS counter
 */
public void pause() {
    if(fpsCalculator != null)
        fpsCalculator.stop();
}

}

非常感谢您对此的帮助。

P.S:我尝试将 CW 更改为 CCW 仍然没有运气 谢谢

【问题讨论】:

  • 你能贴一张你得到的球体的截图吗?我相信这个问题与剔除有关。
  • 对不起,我不能在这里发布图片(它不允许我发布)这里是 rapidshare 链接rapidshare.com/files/1813958102/question.png
  • 你的活动课Lok怎么样?

标签: android opengl-es


【解决方案1】:

改变这个

for(double phi = -(Math.PI/2); phi <= Math.PI/2; phi+=dPhi)

到这里

for(double phi = -(Math.PI); phi <= Math.PI; phi+=dPhi)

【讨论】:

    【解决方案2】:

    其实改变

    for(double phi = -(Math.PI/2); phi <= Math.PI/2; phi+=dPhi)
    

    for(double phi = -(Math.PI); phi <= 0; phi+=dPhi)
    

    就够了。使用从 -(Math.PI) 到 +(Math.PI) 的 phi,您可以进行 360 度旋转并对每个点计数两次。你也可以这样做:

    for(double phi = -(Math.PI); phi <= Math.PI; phi+=dPhi) {
         for(double theta = 0.0; theta <= (Math.PI); theta+=dTheta) {
         ... 
         }
    }
    

    避免计数两次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多