2016-09-16 2 views
0

複数のキューブをテクスチャ処理できるプログラムを作成しようとしています。私は着色に成功していますが、主に自分のAndroidのアクティビティに関するいくつかの難しさに直面しています。Android Contextを使用したOpenGLテクスチャリング

2つのコンテンツを設定する方法(またはそれが可能な場合)、またはレンダラーをオリジナルのsetContentView(R.layout.activity_my_glsurface_view)に設定する方法がわかりません。

GLRenderermCube.loadTexture(this.context, R.drawable.app_icon_your_company)を呼び出すと、自分のテクスチャを自分のキューブに適用する必要があります。 Androidでこれを行う別の方法がある場合は私に教えてください。しかし私が出てきたチュートリアルはすべてこのようにして、私は同じ問題に直面しています。以下は

は私の主な活動です:

public class MainActivity extends Activity implements OnClickListener{ 

int TriData = (Triangle.triangleCoords.length)/Triangle.COORDS_PER_VERTEX; 
int SquData = (Square.squareCoords.length)/Square.COORDS_PER_VERTEX; 
int CubeData = Cube.COORDS_PER_VERTEX * 4; 
int amount = CubeData; 

private GLSurfaceView mGLView; 
private TextView mText; 
private Button mBTN; 




     @Override 
     public void onCreate (Bundle savedInstanceState){ 
      super.onCreate(savedInstanceState); 

      // Create a GLSurfaceView instance and set it-altered glsurfaceview to contain GL elements 
      // as the ContentView for this Activity. 
      // mGLView = new MyGLSurfaceView(this); 
      setContentView(R.layout.activity_my_glsurface_view); 

      FrameLayout surface = (FrameLayout) findViewById(R.id.middleSurface); 
      surface.addView(new MyGLSurfaceView(this)); 

      mGLView = new GLSurfaceView(this); //this is where my error occurs 
      mGLView.setRenderer(new MyGLRenderer(this)); 
      setContentView(mGLView); 

      mBTN = (Button) findViewById(R.id.MyButton); 
      mBTN.setOnClickListener(MainActivity.this); 



      mText = (TextView) findViewById(R.id.MyGl); 
      mText.setText("Vertices drawn: " + amount + "\r\n" + "FPS: " + MyGLRenderer.FPS + "\r\n" + "Frametime: " + MyGLRenderer.FRAME_TIME_MILLIS + "\r\n" + "Cube Total: "); 


     } 

@Override 
public void onClick(View v) { 
    Intent push = new Intent(MainActivity.this, Test.class); 
    //where the 2nd in this case is the class you want to run. 

    startActivity(push); 

    } 
} 

私も私のCube.javaクラスから私のloadTextureが含まれます:私は間違ってloadTextureを呼んでいたとonSurfaceCreated以内に次のようになっているはずです

public int loadTexture(final Context context, final int resourceId) { 
    //Get the texture from the Android resource directory 
    final int[] textureHandle = new int[1]; 
    InputStream is = context.getResources().openRawResource(+ R.drawable.app_icon_your_company); 
    Bitmap bitmap = null; 
    try { 
     //BitmapFactory is an Android graphics utility for images 
     bitmap = BitmapFactory.decodeStream(is); 

    } finally { 
     //Always clear and close 
     try { 
      is.close(); 
      is = null; 
     } catch (IOException e) { 
     } 
    } 

    //Generate one texture pointer... 
    GLES20.glGenTextures(1, textureHandle, 0); 
    //...and bind it to our array 
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]); 

    //Create Nearest Filtered Texture 
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); 
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); 

    //Accounting for different texture parameters 
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT); 
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT); 

    //Use the Android GLUtils to specify a two-dimensional texture image from our bitmap 
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0); 

    //Clean up 
    bitmap.recycle(); 


    if (textureHandle[0] == 0) 

    { 
     throw new RuntimeException("Error loading texture"); 
    } 

    return textureHandle[0]; 
} 

答えて

0

onSurfaceChangedではなく

TexturedCube.mTextureDataHandle = TexturedCube.loadTexture(context, R.drawable.app_icon_your_company); 

さらに、自分のコンテンツビューをmGLViewに設定するのではなく、提供することを怠ったMyGLSurfaceViewFunction経由でコンテキストに正しくアクセスすることができました。これにより、きちんとテクスチャをつけられました

関連する問題