开发者

How to display sky sphere?

I need to position sky sphere in 3D space right where camera is.

I set up camera as follows:

public void onSurfaceChanged(GL10 glUnused, int width, int height) {
    // Ignore the passed-in GL10 interface, and use the GLES20
    // class's static methods instead.
    GLES20.glViewport(0, 0, width, height);
    float ratio = (float) width / height;
    Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 2, 1000);
}

...
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
...

Sphere has a diameter of 100 units and it's center is in (0;0;0).

The method to draw a sphere:

private void drawSphere() {
    //Matrix.setLookAtM(mVMatrix, 0, 0, 0, 0, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    mTriangleVerticesSphere.position(TRI开发者_如何学JAVAANGLE_VERTICES_DATA_POS_OFFSET);
    GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVerticesSphere);
    mTriangleVerticesSphere.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
    GLES20.glEnableVertexAttribArray(maPositionHandle);
    GLES20.glVertexAttribPointer(maTextureHandle, 2, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVerticesSphere);
    GLES20.glEnableVertexAttribArray(maTextureHandle);

    Matrix.setRotateM(mMMatrix, 0, jitterX, 1.0f, 0, 0);
    Matrix.rotateM(mMMatrix, 0, angleYaw + jitterY, 0, 1.0f, 0);
    Matrix.scaleM(mMMatrix, 0, 0.01f, 0.01f, 0.01f);
    Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0);
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);

    GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, numPolysSphere);
}

This code displays a sphere. Backface culling is disabled, and I can see both sides of sphere (texture has transparent parts). In order to display sphere right in the camera position I try to move camera. But if I uncomment Matrix.setLookAtM(mVMatrix, 0, 0, 0, 0, 0f, 0f, 0f, 0f, 1.0f, 0.0f) in the beginning of drawSphere() it doesn't display anything. May be it has something to do with clipping planes? Please give an example how to position the sphere correctly.


I think your more looking to do something like

Matrix.setLookAtM(mvMatrix, 0, 0, 0, 10, 0, 0, 0, 0, 1, 0);

if I'm not wrong opengl doesn't know which way its looking at

your asking too look for (0,0,0) towards (0,0,0) while oriented towards (0,1.0,0)


OK, so I figured out how to do it.
1. Look at correct angle.
3. Draw sky sphere.
2. Clear Z-buffer.
3. Reposition camera.
4. Draw the rest.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜