Android OpenGL ES 2 Problem.

  • 2 Antworten
  • Neuester Beitrag
Diskutiere Android OpenGL ES 2 Problem. im Android App Entwicklung im Bereich Betriebssysteme & Apps.
A

Andi0310

Neues Mitglied
Hallo zusammen.

Ich hoffe mir kann jemand helfen.

Ich programmiere auf meinem Tablett direkt über AIDE.
Jetzt habe ich mir die Kindle-Version von "OpenGL ES 2 for Android" von Kevin Brothaler zugelegt und habe das Beispiel 1 zu Air-Hockey nachvolzogen.
Ich habe alles mehrmals zugeschaut und weiss nicht woran es liegt.
Der Display verfärbt sich so wie es im Beispiel sein soll Rot, aber von dem Air-Hockeytable wie es im Buch abgebildet ist fehlt jede Spur.
Auch im LocCat, welches ich auch in AIDE ausgeben kann ist nichts zu finden.

Danke.
 
S

swordi

Lexikon
wie wie genau glaubst sollte dir jemand helfen können ?

du zeigst weder fehlermeldungen, noch source code.

ich denke mal niemand wird sagen können "der fehler liegt in zeile 201" - du musst schon weitere infos bereit stellen
 
A

Andi0310

Neues Mitglied
Da ich keine Fehlermeldungen habe zeige ich jetzt einfach den Code.

1. AirHockeyActivity.java
Code:
package com.mycompany.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.view.Menu.*;
import android.content.*;
import android.widget.*;
import java.util.zip.*;
import android.opengl.*;
import android.content.pm.*;
import android.util.Log;

public class AirHockeyActivity extends Activity
{
    /** Called when the activity is first created. */
	
	private GLSurfaceView glSurfaceView;
	private boolean rendererSet=false;
	
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
		glSurfaceView=new GLSurfaceView(this);
		
		final ActivityManager activityManager= 
							(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
		
		final ConfigurationInfo configurationInfo=
							activityManager.getDeviceConfigurationInfo();
							
		final boolean supportsEs2 = configurationInfo.reqGlEsVersion>=0x20000
								|| (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 &&
								(Build.FINGERPRINT.startsWith("generic") ||
								Build.FINGERPRINT.startsWith("unknown") || 
								Build.MODEL.contains("google_sdk") || 
								Build.MODEL.contains("Emulator") || 
								Build.MODEL.contains("Android SDK built for x86")));	
		if (supportsEs2) 
		{ 
			// Die OpenGL-Version festlegen.
			glSurfaceView.setEGLContextClientVersion(2);
				// Den Renderer festlegen.
			glSurfaceView.setRenderer(new AirHockeyRenderer(this)); 
			rendererSet = true;
		} 
		else {
			Toast.makeText(this, "This device does not support OpenGL ES 2.0.", Toast.LENGTH_LONG).show(); return; 
		} 
			
		Log.w("Airhocky","i say hello.");
		
        setContentView(glSurfaceView);
    }

	@Override
	protected void onPause()
	{
		// TODO: Implement this method
		super.onPause();
		if(rendererSet)
		{
			glSurfaceView.onPause();
		}
	}

	@Override
	protected void onResume()
	{
		// TODO: Implement this method
		super.onResume();
		if(rendererSet)
		{
			glSurfaceView.onResume();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// TODO: Implement this method
		getMenuInflater().inflate(R.layout.main,menu);
	
		return true;
	}
}
2.AirHockeyRenderer.java
Code:
package com.mycompany.myapp;

import com.mycompany.myapp.*;

import javax.microedition.khronos.egl.EGLConfig.*;
import javax.microedition.khronos.opengles.GL10.*;
import android.opengl.GLSurfaceView.Renderer;
import static android.opengl.GLES20.*;
import android.content.*;
import  android.util.Log;
import javax.microedition.khronos.opengles.*;
import javax.microedition.khronos.egl.*;
import java.nio.*;

public class AirHockeyRenderer implements Renderer
{
	// Alls erstes ein 2D Object.
	private static final int POSITION_COMPONENT_COUNT=2;
	private static final int BYTES_PER_FLOAT=4;
	private static final String U_COLOR="u_Color";
	private static final String A_POSITION="a_Position";
	private int uColorLocation;
	private int aPositionLocation;
	private int program;
	private final Context context;
	
	private FloatBuffer vertexData;
	
	public AirHockeyRenderer()
	{
		context=null;
	}
	public AirHockeyRenderer(Context context)
	{
		this.context=context;
		
		float[] tableVertices2Triangles=
							  {
								  // Triangle 1
								  0f,0f,
								  9f,14f,
								  0f,14,
								  // Triangle 2
								  0f,0f,
								  9f,0f,
								  9f,14f,
								  // Line
								  0f,7f,
								  9f,7f,
								  // Mallets
								  4.5f,2f,
								  4.5f,12f
							  };
		
vertexData=ByteBuffer.allocateDirect(tableVertices2Triangles.length*BYTES_PER_FLOAT)
		.order(ByteOrder.nativeOrder()).asFloatBuffer();
		vertexData.put(tableVertices2Triangles);
	}

	@Override
	public void onSurfaceCreated(GL10 unused, EGLConfig eglConf)
	{
		// TODO: Implement this method
		glClearColor(1.0f,0.0f,0.0f,0.0f);
		
		String vertexShaderSource = TextResourceReader
			.readTextFileFromResource(context,R.raw.simple_vertex_shader);
		
		String fragmentShaderSource = TextResourceReader
			.readTextFileFromResource(context,R.raw.simple_fragment_shader);
		// und hier werden sie compiliert.
		int vertexShader = ShaderHelper.compileVertexShader(vertexShaderSource);
		int fragmentShader = ShaderHelper.compileFragmentShader(fragmentShaderSource);
		
		program=ShaderHelper.linkProgramm(vertexShader,fragmentShader);
		
		if(LoggerConfig.ON)
		{
			ShaderHelper.validateProgram(program);
		}
		glUseProgram(program);
		
		uColorLocation=glGetUniformLocation(program,U_COLOR);
		
		// aPositon arbeitet mit den vertexDaten zusammen.
		aPositionLocation=glGetAttribLocation(program,A_POSITION);
		
		vertexData.position(0);
		glVertexAttribPointer(aPositionLocation,POSITION_COMPONENT_COUNT,GL_FLOAT,false,0,vertexData);
		glEnableVertexAttribArray(aPositionLocation);
		
	}

	@Override
	public void onSurfaceChanged(GL10 unused, int width, int height)
	{
		// TODO: Implement this method
		glViewport(0,0,width,height);
	}

	@Override
	public void onDrawFrame(GL10 unused)
	{
		// TODO: Implement this method
		glClear(GL_COLOR_BUFFER_BIT);
		glUniform4f(uColorLocation,1.0f,1.0f,1.0f,1.0f);
		glDrawArrays(GL_TRIANGLES,0,6);
		
		glUniform4f(uColorLocation,1.0f,0.0f,0.0f,1.0f);
		glDrawArrays(GL_LINES,6,2);
		
		glUniform4f(uColorLocation,0.0f,0.0f,1.0f,1.0f);
		glDrawArrays(GL_POINTS,8,1);
		
		glUniform4f(uColorLocation,1.0f,0.0f,0.0f,1.0f);
		glDrawArrays(GL_POINTS,9,1);
	}
}
3.ShaderHelper.java
Code:
package com.mycompany.myapp;

import static android.opengl.GLES20.glCreateShader;
import static android.opengl.GLES20.GL_VERTEX_SHADER;
import static android.opengl.GLES20.GL_FRAGMENT_SHADER;
import static android.opengl.GLES20.GL_COMPILE_STATUS;
import static android.opengl.GLES20.GL_VALIDATE_STATUS;
import static android.opengl.GLES20.GL_LINK_STATUS;
import static android.opengl.GLES20.glShaderSource;
import static android.opengl.GLES20.glCompileShader;
import static android.opengl.GLES20.glGetShaderiv;
import static android.opengl.GLES20.glGetShaderInfoLog;
import static android.opengl.GLES20.glDeleteShader;
import static android.opengl.GLES20.glCreateProgram;
import static android.opengl.GLES20.glAttachShader;
import static android.opengl.GLES20.glLinkProgram;
import static android.opengl.GLES20.glGetProgramiv;
import static android.opengl.GLES20.glGetProgramInfoLog;
import static android.opengl.GLES20.glDeleteProgram;
import static android.opengl.GLES20.glValidateProgram;
import android.util.Log;

public class ShaderHelper
{
	private static final String TAG="ShaderHelper";
	
	public static int compileVertexShader(String shaderCode)
	{
		return compileShader(GL_VERTEX_SHADER,shaderCode);
	}
	
	public static int compileFragmentShader(String shaderCode)
	{
		return compileShader(GL_FRAGMENT_SHADER,shaderCode);
	}
	
	private static int compileShader(int type,String shaderCode)
	{
		final int shaderObjectId=glCreateShader(type);
		if(shaderObjectId==0)
		{
			if(LoggerConfig.ON)
			{
				Log.w(TAG,"Coult not create new shader.");
			}
			return 0;
		}
		
		glShaderSource(shaderObjectId,shaderCode);
		
		glCompileShader(shaderObjectId);
		
		final int[] compileStatus=new int[1];
		glGetShaderiv(shaderObjectId,GL_COMPILE_STATUS,compileStatus,0);
		if(LoggerConfig.ON)
		{
			Log.v(TAG,"Results of compiling source "+"\n"+shaderCode+"\n"+
					glGetShaderInfoLog(shaderObjectId));
		}
		if(compileStatus[0]==0)
		{
			glDeleteShader(shaderObjectId);
			if(LoggerConfig.ON)
			{
				Log.w(TAG,"Compilation of shader failed.");
			}
			return 0;
		}
		return shaderObjectId;
	}
	
	public static int linkProgramm(int vertexShaderId,int fragmentShaderId)
	{
		final int programObjectId=glCreateProgram();

		if(programObjectId==0)
		{
			if(LoggerConfig.ON)
			{
				Log.w(TAG,"Could not create a new Program");
			}
			return 0;
		}
		
		glAttachShader(programObjectId,vertexShaderId);
		glAttachShader(programObjectId,fragmentShaderId);
		
		glLinkProgram(programObjectId);
		
		final int[] linkStatus =new int[1];
		glGetProgramiv(programObjectId,GL_LINK_STATUS,linkStatus,0);
		if(LoggerConfig.ON)
		{
			Log.v(TAG,"Results of linking program:\n"+glGetProgramInfoLog(programObjectId));
		}
		if(linkStatus[0]==0)
		{
			glDeleteProgram(programObjectId);
			if(LoggerConfig.ON)
			{
				Log.w(TAG,"Linking of program failed.");
			}
			return 0;
		}
		return programObjectId;
	}
	
	public static boolean validateProgram(int programObjectId)
	{
		glValidateProgram(programObjectId);
		final int[] validateStatus =new int[1];
		glGetProgramiv(programObjectId,GL_VALIDATE_STATUS,validateStatus,0);
		Log.v(TAG,"Results of validating program:"+validateStatus+"\nLog"+
			glGetProgramInfoLog(programObjectId));
		return validateStatus[0]!=0;
	}
}
4.TextResourceReader.java
Code:
package com.mycompany.myapp;

import android.content.*;
import java.io.*;
import android.content.res.*;

public class TextResourceReader
{
	public static String readTextFileFromResource(Context context,int resourceId)
	{
		StringBuilder body = new StringBuilder();
		
		try
		{
			InputStream inputStream=context.getResources().openRawResource(resourceId);
			InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
			
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			
			String nextLine;
			
			while((nextLine=bufferedReader.readLine())!=null)
			{
				body.append(nextLine);
				body.append("\n");
			}
		}
		catch(IOException e)
		{
			throw new RuntimeException("could not open resource:"+resourceId,e);
		}
		catch(Resources.NotFoundException nfe)
		{
			throw new RuntimeException("Resource not found:"+ resourceId, nfe);
		}
		return body.toString();
	}
}
5.AndroidManifest.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

	<uses-fearure android:glEsVersion="0x00020000"></uses-fearure>
    <uses-sdk 
        android:minSdkVersion="8" 
        android:targetSdkVersion="8" />

	<uses-permission
		android:name="android.permission.WRITE_EXTERNAL_STORAGE">
	</uses-permission>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
 
Ähnliche Themen - Android OpenGL ES 2 Problem. Antworten Datum
8
2
6