开发者

Texturing a 3D sphere(Can not find a post that helps me)

开发者 https://www.devze.com 2023-04-11 04:51 出处:网络
I am trying to create a 3D sphere and I currently have a sphere made out of points though I need to apply a texture to this and that is causing me problems.

I am trying to create a 3D sphere and I currently have a sphere made out of points though I need to apply a texture to this and that is causing me problems.

The code for generating the sphere looks like this:

    public Sphere(float radius, double step) {


    this.mRaduis = radius;
    this.mStep = step;
    sphereVertex = ByteBuffer.allocateDirect(400000)
            .order(ByteOrder.nativeOrder()).asFloatBuffer();
    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.glEnable(GL10.GL_TEXTURE_2D); // workaround bug 3623
    gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, sphereVertex);

    gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    gl.glDrawArrays(GL10.开发者_如何学PythonGL_POINTS, 0, mPoints);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}


private int build() {


    double dTheta = mStep * DEG;
    double dPhi = dTheta;
    int points = 0;

    for (double phi = -(Math.PI); phi <= Math.PI; 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))); //y-coord
            sphereVertex.put((float) (mRaduis * Math.sin(phi) * Math
                    .sin(theta))); //z
            sphereVertex.put((float) (mRaduis * Math.cos(phi))); //x
            points++;

        }
    }

and in my rendering class it looks like this:

 public SphereRenderer(Context context) {

    mSphere = new Sphere(1, 25);

    this.context = context;

    vibrator = SphereActivity.getVibrator();
    sphereActivity = SphereActivity.getInstance();



}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {

    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glEnable(GL10.GL_TEXTURE_2D);
    Sphere.loadTexture(gl, context, R.drawable.android);

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

}

public void onDrawFrame(GL10 gl) {

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    gl.glLoadIdentity();


    gl.glTranslatef(0.0f, 0.0f, -4.0f);

    rotateSphere(gl, startTime);


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

    numFrames++;
    long fpsElapsed = System.currentTimeMillis() - fpsStartTime;
    if (fpsElapsed > 5 * 1000) { // every 5 seconds
        float fps = (numFrames * 1000.0F) / fpsElapsed;
        Log.d("FPS", "Frames per second: " + fps + " (" + numFrames
                + " frames in " + fpsElapsed + " ms)");
        fpsStartTime = System.currentTimeMillis();
        numFrames = 0;
    }
}

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

    startTime = System.currentTimeMillis();
    fpsStartTime = startTime;
    numFrames = 0;
    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
}

I would really appreciate all the help I can get!


Although you can theoretically texture points, one pixel of texture means you would need a lot of points to do create a recognisable map. From what I can tell you need to draw your sphere as a set of triangles instead, which you can then apply textures to. Effectively this means just combining the points you have to create faces. Rather than GL_POINTS you probably want to look at using GL_TRIANGLES - see the OpenGL programming guide on points lines and polygons for a proper explanation.

0

精彩评论

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

关注公众号