Java 从GLSurfaceview传递上下文

Java 从GLSurfaceview传递上下文,java,android,opengl-es,Java,Android,Opengl Es,我正在尝试运行obj加载程序。 我正在使用Java、OpenGL ES和Android studio。 我正在尝试使用上下文调用一个方法,结果出现错误。 错误是: Model () in Model cannot be applied to (int, android.content.context) 我试图传递的上下文来自GLsurfaceview。即: glSurfaceView.setRenderer(新的ObjRenderer(this)) 如何修复此错误? 我的代码如下: packa

我正在尝试运行obj加载程序。 我正在使用Java、OpenGL ES和Android studio。 我正在尝试使用上下文调用一个方法,结果出现错误。 错误是:

Model () in Model cannot be applied to (int, android.content.context)
我试图传递的上下文来自
GLsurfaceview
。即:
glSurfaceView.setRenderer(新的ObjRenderer(this))

如何修复此错误? 我的代码如下:

package com.android.objectloader.objectloader;

import android.content.Context;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;

class GroupObject  extends Model {
    String objectName;

    public GroupObject(){

    }



    public void setObjectName(String string) {
        this.objectName = string;
    }

    public String getObjectName() {
        return objectName;
    }



// Android Stuff!
public Context context;  // changed to public
private int modelID;


    public int Model (int modelID, Context activity) {       // return type is int
        this.vertices = new ArrayList<Model.Vector3D>();
        this.vertexTexture = new ArrayList<Model.Vector3D>();
        this.vertexNormal = new ArrayList<Model.Vector3D>();
        this.faces = new ArrayList<Face>();

        this.groupObjects = new ArrayList<GroupObject>();

        //this.modelID = modelID;
        this.modelID = modelID;
        this.context = activity;

        loadFile();
    }

    private int loadFile() {

       InputStream inputStream = context.getResources().openRawResource(
                       modelID);


        BufferedReader in = new BufferedReader(new InputStreamReader(
                inputStream));

        try {
            loadOBJ(in);
            Log.d("LOADING FILE", "FILE LOADED SUCESSFULLY====================");
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return 1;
    }

    private void loadOBJ(BufferedReader in) throws IOException {
        Log.d("LOADING FILE", "STARTING!====================");
        GroupObject defaultObject = new GroupObject();
        GroupObject currentObject = defaultObject;

        this.groupObjects.add(defaultObject);

        String Line; // Stores ever line we read!
        String[] Blocks; // Stores string fragments after the split!!
        String CommandBlock; // Stores Command Blocks such as: v, vt, vn, g,
        // etc...

        while ((Line = in.readLine()) != null) {
            Blocks = Line.split(" ");
            CommandBlock = Blocks[0];

            // Log.d("COMMAND BLOCK" , "---------- " + CommandBlock +
            // " ----------");

            if (CommandBlock.equals("g")) {
                if (Blocks[1] == "default")
                    currentObject = defaultObject;
                else {
                    GroupObject groupObject = new GroupObject();
                    groupObject.setObjectName(Blocks[1]);
                    currentObject = groupObject;
                    groupObjects.add(groupObject);
                }
            }

            if (CommandBlock.equals("v")) {
                Model.Vector3D vertex = new Model.Vector3D(Float.parseFloat(Blocks[1]),
                        Float.parseFloat(Blocks[2]),
                        Float.parseFloat(Blocks[3]));
                this.vertices.add(vertex);
                // Log.d("VERTEX DATA", " " + vertex.getX() + ", " +
                // vertex.getY() + ", " + vertex.getZ());
            }

            if (CommandBlock.equals("vt")) {
                Model.Vector3D vertexTex = new Model.Vector3D(Float.parseFloat(Blocks[1]),
                        Float.parseFloat(Blocks[2]), 0.0f);
                this.vertexTexture.add(vertexTex);
                // Log.d("TEXTURE DATA", " " + vertexTex.getX() + ", " +
                // vertexTex.getY() + ", " + vertexTex.getZ());
            }

            if (CommandBlock.equals("vn")) {
                Model.Vector3D vertexNorm = new Model.Vector3D(Float.parseFloat(Blocks[1]),
                        Float.parseFloat(Blocks[2]),
                        Float.parseFloat(Blocks[3]));
                this.vertexNormal.add(vertexNorm);
                // Log.d("NORMAL DATA", " " + vertexNorm.getX() + ", " +
                // vertexNorm.getY() + ", " + vertexNorm.getZ());
            }

            if (CommandBlock.equals("f")) {
                Face face = new Face();
                faces.add(face);

                String[] faceParams;

                for (int i = 1; i < Blocks.length; i++) {
                    faceParams = Blocks[i].split("/");

                    face.getVertices()
                            .add(this.vertices.get(Integer
                                    .parseInt(faceParams[0]) - 1));

                    if (faceParams[1] == "") {
                    } else {
                        face.getUvws().add(
                                this.vertexTexture.get(Integer
                                        .parseInt(faceParams[1]) - 1));
                        face.getNormals().add(
                                this.vertexNormal.get(Integer
                                        .parseInt(faceParams[2]) - 1));
                    }
                }
            }
        }

        // fillInBuffers();
        fillInBuffersWithNormals();

        Log.d("OBJ OBJECT DATA", "V = " + vertices.size() + " VN = "
                + vertexTexture.size() + " VT = " + vertexNormal.size());

    }

    private void fillInBuffers() {

        int facesSize = faces.size();

        vertexCount = facesSize * 3;

        tempV = new float[facesSize * 3 * 3];
        tempVt = new float[facesSize * 2 * 3];
        indices = new short[facesSize * 3];

        for (int i = 0; i < facesSize; i++) {
            Face face = faces.get(i);
            tempV[i * 9] = face.getVertices().get(0).getX();
            tempV[i * 9 + 1] = face.getVertices().get(0).getY();
            tempV[i * 9 + 2] = face.getVertices().get(0).getZ();
            tempV[i * 9 + 3] = face.getVertices().get(1).getX();
            tempV[i * 9 + 4] = face.getVertices().get(1).getY();
            tempV[i * 9 + 5] = face.getVertices().get(1).getZ();
            tempV[i * 9 + 6] = face.getVertices().get(2).getX();
            tempV[i * 9 + 7] = face.getVertices().get(2).getY();
            tempV[i * 9 + 8] = face.getVertices().get(2).getZ();
            tempVt[i * 6] = face.getUvws().get(0).getX();
            tempVt[i * 6 + 1] = face.getUvws().get(0).getY();
            tempVt[i * 6 + 2] = face.getUvws().get(1).getX();
            tempVt[i * 6 + 3] = face.getUvws().get(1).getY();
            tempVt[i * 6 + 4] = face.getUvws().get(2).getX();
            tempVt[i * 6 + 5] = face.getUvws().get(2).getY();
            indices[i * 3] = (short) (i * 3);
            indices[i * 3 + 1] = (short) (i * 3 + 1);
            indices[i * 3 + 2] = (short) (i * 3 + 2);
        }

        _vb = ByteBuffer.allocateDirect(tempV.length * Model.FLOAT_SIZE_BYTES)
                .order(ByteOrder.nativeOrder()).asFloatBuffer();
        _vb.put(tempV);
        _vb.position(0);

        _tcb = ByteBuffer.allocateDirect(tempVt.length * Model.FLOAT_SIZE_BYTES)
                .order(ByteOrder.nativeOrder()).asFloatBuffer();
        _tcb.put(tempVt);
        _tcb.position(0);

        _ib = ByteBuffer.allocateDirect(indices.length * Model.SHORT_SIZE_BYTES)
                .order(ByteOrder.nativeOrder()).asShortBuffer();
        _ib.put(indices);
        _ib.position(0);
    }

    private void fillInBuffersWithNormals() {

        int facesSize = faces.size();

        vertexCount = facesSize * 3;

        tempV = new float[facesSize * 3 * 3];
        tempVt = new float[facesSize * 2 * 3];
        tempVn = new float[facesSize * 3 * 3];
        indices = new short[facesSize * 3];

        for (int i = 0; i < facesSize; i++) {
            Face face = faces.get(i);
            tempV[i * 9] = face.getVertices().get(0).getX();
            tempV[i * 9 + 1] = face.getVertices().get(0).getY();
            tempV[i * 9 + 2] = face.getVertices().get(0).getZ();
            tempV[i * 9 + 3] = face.getVertices().get(1).getX();
            tempV[i * 9 + 4] = face.getVertices().get(1).getY();
            tempV[i * 9 + 5] = face.getVertices().get(1).getZ();
            tempV[i * 9 + 6] = face.getVertices().get(2).getX();
            tempV[i * 9 + 7] = face.getVertices().get(2).getY();
            tempV[i * 9 + 8] = face.getVertices().get(2).getZ();

            tempVn[i * 9] = face.getNormals().get(0).getX();
            tempVn[i * 9 + 1] = face.getNormals().get(0).getY();
            tempVn[i * 9 + 2] = face.getNormals().get(0).getZ();
            tempVn[i * 9 + 3] = face.getNormals().get(1).getX();
            tempVn[i * 9 + 4] = face.getNormals().get(1).getY();
            tempVn[i * 9 + 5] = face.getNormals().get(1).getZ();
            tempVn[i * 9 + 6] = face.getNormals().get(2).getX();
            tempVn[i * 9 + 7] = face.getNormals().get(2).getY();
            tempVn[i * 9 + 8] = face.getNormals().get(2).getZ();

            tempVt[i * 6] = face.getUvws().get(0).getX();
            tempVt[i * 6 + 1] = face.getUvws().get(0).getY();
            tempVt[i * 6 + 2] = face.getUvws().get(1).getX();
            tempVt[i * 6 + 3] = face.getUvws().get(1).getY();
            tempVt[i * 6 + 4] = face.getUvws().get(2).getX();
            tempVt[i * 6 + 5] = face.getUvws().get(2).getY();

            indices[i * 3] = (short) (i * 3);
            indices[i * 3 + 1] = (short) (i * 3 + 1);
            indices[i * 3 + 2] = (short) (i * 3 + 2);
        }

        _vb = ByteBuffer.allocateDirect(tempV.length * Model.FLOAT_SIZE_BYTES)
                .order(ByteOrder.nativeOrder()).asFloatBuffer();
        _vb.put(tempV);
        _vb.position(0);

        _tcb = ByteBuffer.allocateDirect(tempVt.length * Model.FLOAT_SIZE_BYTES)
                .order(ByteOrder.nativeOrder()).asFloatBuffer();
        _tcb.put(tempVt);
        _tcb.position(0);

        _nb = ByteBuffer.allocateDirect(tempVn.length * Model.FLOAT_SIZE_BYTES)
                .order(ByteOrder.nativeOrder()).asFloatBuffer();
        _nb.put(tempVn);
        _nb.position(0);

        _ib = ByteBuffer.allocateDirect(indices.length * Model.SHORT_SIZE_BYTES)
                .order(ByteOrder.nativeOrder()).asShortBuffer();
        _ib.put(indices);
        _ib.position(0);
    }

    public  FloatBuffer getVertices() {
        return _vb;
    }

    public FloatBuffer getTexCoords() {
        return _tcb;
    }

    public ShortBuffer getIndices() {
        return _ib;
    }

    public FloatBuffer getNormals() {
        return _nb;
    }





}`package com.android.objectloader.objectloader;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView.Renderer;

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES20.GL_FLOAT;
import static android.opengl.GLES20.GL_TRIANGLES;
import static android.opengl.GLES20.GL_UNSIGNED_INT;
import static android.opengl.GLES20.glClear;
import static android.opengl.GLES20.glClearColor;
import static android.opengl.GLES20.glDrawArrays;
import static android.opengl.GLES20.glEnableVertexAttribArray;
import static android.opengl.GLES20.glGetAttribLocation;
import static android.opengl.GLES20.glGetUniformLocation;
import static android.opengl.GLES20.glUseProgram;
import static android.opengl.GLES20.glVertexAttribPointer;
import static android.opengl.GLES20.glViewport;


public class ObjRenderer extends GroupObject  implements Renderer {

        private static final String A_POSITION = "a_Position";
        private int aPositionLocation;
        private static final String U_COLOR = "u_Color";
        private int uColorLocation;
        private int program;
        private static final int POSITION_COMPONENT_COUNT = 2;
        private static final int BYTES_PER_FLOAT = 4;
        private static final int BYTES_PER_INT = 4;
        public FloatBuffer vertexData =  null;
        private ShortBuffer indexData = null;
        public Context context = null; // changed to public
        private int indicesSize;
        private int [ ] indicesRef;

//        public GroupObject GroupObjectReturn; //
        public ObjRenderer(Context context) {
            // this.context = context;

            new Model(R.raw.cube_obj, this.context);





//            float[] tableVerticesWithTriangles = {
//                    // Triangle 1
//                    -0.5f, -0.5f,
//                    0.5f,  0.5f,
//                    -0.5f,  0.5f,
//
//                    // Triangle 2
//                    -0.5f, -0.5f,
//                    0.5f, -0.5f,
//                    0.5f,  0.5f,
//
//                    // Line 1
//                    -0.5f, 0f,
//                    0.5f, 0f,
//
//                    // Mallets
//                    0f, -0.25f,
//                    0f,  0.25f
//            };


        }





        @Override
        public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
            // Set the background clear color to red. The first component is
            // red, the second is green, the third is blue, and the last
            // component is alpha, which we don't use in this lesson.
            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

            String vertexShaderSource = com.android.objectloader.objectloader.TextResourceReader
                    .readTextFileFromResource(context, R.raw.simple_vertex_shader);
            String fragmentShaderSource = com.android.objectloader.objectloader.TextResourceReader
                    .readTextFileFromResource(context, R.raw.simple_fragment_shader);

            int vertexShader = com.android.objectloader.objectloader.ShaderHelper.compileVertexShader(vertexShaderSource);
            int fragmentShader = com.android.objectloader.objectloader.ShaderHelper.compileFragmentShader(fragmentShaderSource);

            program = com.android.objectloader.objectloader.ShaderHelper.linkProgram(vertexShader, fragmentShader);

            if (LoggerConfig.ON){
                com.android.objectloader.objectloader.ShaderHelper.validateProgram(program);
            }
            glUseProgram(program);

            uColorLocation = glGetUniformLocation(program, U_COLOR);
            aPositionLocation = glGetAttribLocation(program, A_POSITION);
            // Bind our data, specified by the variable vertexData, to the vertex
            // attribute at location A_POSITION_LOCATION.

           // glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT, GL_FLOAT,
           //       false, 0, vertexData);
            getVertices().position(0);
            GLES20.glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT, GL_FLOAT,
                    false, 0, getVertices());

            glEnableVertexAttribArray(aPositionLocation);

        }

        @Override
        public void onSurfaceChanged(GL10 glUnused, int width, int height) {
            // Set the OpenGL viewport to fill the entire surface.
            glViewport(0, 0, width, height);
        }
        @Override
        public void onDrawFrame(GL10 glUnused) {
            // Clear the rendering surface.
            glClear(GL_COLOR_BUFFER_BIT);

            glEnableVertexAttribArray(aPositionLocation);
            glUnused.glDrawElements(GL_TRIANGLES, indicesSize, GL_UNSIGNED_INT, );

            //         glUniform4f(uColorLocation, 1.0f, 1.0f, 1.0f, 1.0f);
            glDrawArrays(GL_TRIANGLES, 0, 6);



        }















    }

}
package com.android.objectloader.objectloader;
导入android.content.Context;
导入android.util.Log;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.nio.ByteBuffer;
导入java.nio.ByteOrder;
导入java.nio.FloatBuffer;
导入java.nio.ShortBuffer;
导入java.util.ArrayList;
类GroupObject扩展模型{
字符串objectName;
公共组对象(){
}
public void setObjectName(字符串){
this.objectName=string;
}
公共字符串getObjectName(){
返回objectName;
}
//安卓的东西!
公共上下文;//已更改为公共
私有int modelID;
公共int模型(int modelID,Context活动){//返回类型为int
this.vertices=新的ArrayList();
this.vertexture=新的ArrayList();
this.vertexNormal=新的ArrayList();
this.faces=new ArrayList();
this.groupObjects=new ArrayList();
//this.modelID=modelID;
this.modelID=modelID;
这个上下文=活动;
loadFile();
}
私有int加载文件(){
InputStream InputStream=context.getResources().openRawResource(
modelID);
BufferedReader in=新的BufferedReader(新的InputStreamReader(
输入流);
试一试{
loadOBJ(in);
Log.d(“加载文件”,“文件加载成功=======================”;
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
in.close();
}捕获(IOE异常){
e、 printStackTrace();
}
返回1;
}
私有void loadOBJ(BufferedReader in)引发IOException{
Log.d(“正在加载文件”、“正在启动!=========================”;
GroupObject defaultObject=新的GroupObject();
GroupObject currentObject=defaultObject;
this.groupObjects.add(defaultObject);
String Line;//存储我们读过的任何一行!
String[]Blocks;//在拆分后存储字符串片段!!
String CommandBlock;//存储命令块,例如:v、vt、vn、g、,
//等等。。。
而((Line=in.readLine())!=null){
块=行。拆分(“”);
CommandBlock=块[0];
//Log.d(“命令块”、“------------”+CommandBlock+
// " ----------");
if(CommandBlock.equals(“g”)){
如果(块[1]=“默认”)
currentObject=defaultObject;
否则{
GroupObject GroupObject=新的GroupObject();
setObjectName(块[1]);
currentObject=groupObject;
groupObjects.add(groupObject);
}
}
if(CommandBlock.equals(“v”)){
Model.Vector3D顶点=新的Model.Vector3D(Float.parseFloat(块[1]),
Float.parseFloat(块[2]),
Float.parseFloat(块[3]);
this.vertexs.add(顶点);
//Log.d(“顶点数据”,“顶点+VERTEX.getX()+”,“+
//vertex.getY()+”,“+vertex.getZ());
}
if(CommandBlock.equals(“vt”)){
Model.Vector3D vertexTex=新的Model.Vector3D(Float.parseFloat(Blocks[1]),
Float.parseFloat(块[2]),0.0f;
this.vertexture.add(vertextextex);
//Log.d(“纹理数据”,“vertexTex.getX()+”,“+
//getY()+,“+vertexTex.getZ());
}
if(CommandBlock.equals(“vn”)){
Model.Vector3D vertexNorm=新Model.Vector3D(Float.parseFloat(Blocks[1]),
Float.parseFloat(块[2]),
Float.parseFloat(块[3]);
this.vertexNormal.add(vertexNorm);
//Log.d(“正常数据”,“vertexNorm.getX()+”,“+
//getY()+,“+vertexNorm.getZ());
}
if(CommandBlock.equals(“f”)){
面=新面();
面。添加(面);
字符串[]面参数;
对于(int i=1;i