动态添加顶点时,屏幕上不显示形状

时间:2015-10-14 04:31:51

标签: android

我的opengl渲染器类中存在严重问题 这是我在glsurfaceview上绘制形状的Renderer类。 我是OpenGl的新手。

package com.view9.stoddart.opengl;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.FloatMath;
import android.view.MotionEvent;

import com.view9.stoddart.model.OpenGlPointObject;
import com.view9.stoddart.model.OpenGlRoofObject;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

public class StoddartRenderer implements GLSurfaceView.Renderer {
    private Context mContext;
    private FloatBuffer mVertexBuffer = null;
    private ShortBuffer mTriangleBorderIndicesBuffer = null;
    private int mNumOfTriangleBorderIndices = 0;

    public float mAngleX = 0.0f;
    public float mAngleY = 0.0f;
    public float mAngleZ = 0.0f;
    private float mPreviousX;
    private float mPreviousY;
    private final float TOUCH_SCALE_FACTOR = 0.6f;
    int pointers = 0;
    float distance = 1;
    RenderListener renderListener;
    private OpenGlRoofObject roofObject;

    public void setRenderListener(RenderListener renderListener) {
        this.renderListener = renderListener;
    }


    public StoddartRenderer(Context context, OpenGlRoofObject roofObject) {
        mContext = context;
        this.roofObject = roofObject;
    }

    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();

        gl.glTranslatef(0.0f, 0.0f, -3.0f);
        gl.glRotatef(mAngleX, 1, 0, 0);
        gl.glRotatef(mAngleY, 0, 1, 0);
        gl.glRotatef(mAngleZ, 0, 0, 1);

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);

        // Set line color to black
        gl.glColor4f(0.0f, 0.0f, 0.0f, 1.0f);

        // Draw all lines
        gl.glDrawElements(GL10.GL_LINES, mNumOfTriangleBorderIndices,
                GL10.GL_UNSIGNED_SHORT, mTriangleBorderIndicesBuffer);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, javax.microedition.khronos.egl.EGLConfig config) {
        gl.glClearColor(1f, 1f, 1f, 1);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
        gl.glEnable(GL10.GL_DEPTH_TEST);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        // Get all the buffers ready
        setAllBuffers();
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        if (height == 0) height = 1;
        gl.glViewport(0, 0, width, height);
        float aspect = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);
//        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glFrustumf(-aspect, aspect, -1.0f, 1.0f, 1.0f, 0.0f);
    }

    private void setAllBuffers() {
        ArrayList<CoOrdinatesModel> cordinateList = new ArrayList<>();


        cordinateList.add(new CoOrdinatesModel(0.0f, 0.0f, 0.0f));
        for (int pointCount = 0; pointCount < roofObject.getPoinstList().size(); pointCount++) {
            OpenGlPointObject pointObject = roofObject.getPoinstList().get(pointCount);
            cordinateList.add(new CoOrdinatesModel(pointObject.getxAsis()/10000, pointObject.getyAxis()/10000, pointObject.getzAxis()/10000));
        }


        List<Float> vList = new ArrayList<>();
        for (int i = 0; i < cordinateList.size(); i++) {
            vList.add(cordinateList.get(i).getxAxis());
            vList.add(cordinateList.get(i).getyAxis());
            vList.add(cordinateList.get(i).getzAxis());
        }

        float[] vertexlist = new float[vList.size()];
        int count = 0;
        for (Float f : vList) {
            vertexlist[count++] = f;
        }

        ByteBuffer vbb = ByteBuffer.allocateDirect(vertexlist.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asFloatBuffer();
        mVertexBuffer.put(vertexlist);
        mVertexBuffer.position(0);

        // Set triangle border buffer with vertex indices
        short trigborderindexlist[] = {
                89, 78,
                79, 89,
                78, 81,
                90, 86,
                83, 82,
                88, 83,
                92, 79,
                87, 78,
                79, 85,
                91, 80,
                90, 84,
                89, 80,
                88, 84,
                83, 84,
                82, 87,
                80, 84,
                86, 88,
                87, 92,
                81, 85,
                91, 90,
                92, 91,
                98, 97,
                93, 98,
                94, 93,
                95, 94,
                96, 95,
                97, 96


        };
        mNumOfTriangleBorderIndices = trigborderindexlist.length;
        ByteBuffer tbibb = ByteBuffer.allocateDirect(trigborderindexlist.length * 2);
        tbibb.order(ByteOrder.nativeOrder());
        mTriangleBorderIndicesBuffer = tbibb.asShortBuffer();
        mTriangleBorderIndicesBuffer.put(trigborderindexlist);
        mTriangleBorderIndicesBuffer.position(0);
    }

    public boolean onTouchEvent(MotionEvent e) {
        float x = e.getX();
        float y = e.getY();
        switch (e.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float dx = x - mPreviousX;
                float dy = y - mPreviousY;
                mAngleY = (mAngleY + (int) (dx * TOUCH_SCALE_FACTOR) + 360) % 360;
                mAngleX = (mAngleX + (int) (dy * TOUCH_SCALE_FACTOR) + 360) % 360;
                break;

        }
        mPreviousX = x;
        mPreviousY = y;
        return true;
    }


    protected final float fingerDist(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return FloatMath.sqrt(x * x + y * y);
    }


}

它不起作用我有什么遗漏的东西吗?在我的xml api中,我得到了字符串格式的点,所以我需要将它解析为float pointObject 在float中携带三个数据。任何帮助将不胜感激。提前谢谢。请挽救我的生命我已经浪费了两天的时间来制作一些逻辑而不值得。

0 个答案:

没有答案