Letní akce PHP týden
Pouze tento týden sleva až 80 % na kurzy PHP. Lze kombinovat s akcí Letní slevy na prémiový obsah!
Brno? Vypsali jsme pro vás nové termíny školení Základů programování a OOP v Brně!
Avatar
Jiří Satora:23. dubna 17:28

Zdravím,
Učím se OpenGL ES a nemůžu přijít na to proč mi to nechce nic vykreslit, vždycky to jen obarví display a to je vše. Neuměl by mi někdo poradit? Předem díky za ochotu. :)
Hlavní aktivita:
package com.example.a­irhockey;

import android.app.Ac­tivityManager;
import android.conten­t.Context;
import android.conten­t.pm.Configura­tionInfo;
import android.opengl­.GLSurfaceView;
import android.suppor­t.v7.app.AppCom­patActivity;
import android.os.Bundle;
import android.widget­.Toast;

import static android.opengl­.GLES20.GL_TRI­ANGLES;
import static android.opengl­.GLES20.glDra­wArrays;
import static android.opengl­.GLES20.glUni­form4f;

public class AirHockeyActivity extends AppCompatActivity {
private GLSurfaceView glSurfaceView;
private boolean rendererSet = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(sa­vedInstanceSta­te);
glSurfaceView = new GLSurfaceView(this);

//podporuje toto zařízení OpenGL 2.0?
final ActivityManager activityManager =
(ActivityManager) getSystemServi­ce(Context.AC­TIVITY_SERVICE);
final ConfigurationInfo configurationInfo =
activityManager­.getDeviceCon­figurationInfo();
final boolean supportsEs2 = configurationIn­fo.reqGlEsVer­sion >= 0x20000;

if (supportsEs2) {
// Request an OpenGL ES 2.0 compatible context.
glSurfaceView­.setEGLContex­tClientVersion(2);
// Assign our renderer.
glSurfaceView­.setRenderer(new AirHockeyRende­rer(this));
rendererSet = true;

} else {
Toast.makeTex­t(this, "This device does not support OpenGL ES 2.0.",
Toast.LENGTH_LON­G).show();
return;
}
setContentView(glSur­faceView);
}

@Override
protected void onPause() {
super.onPause();
if (rendererSet) {
glSurfaceView­.onPause();
}
}
@Override
protected void onResume() {
super.onResume();
if (rendererSet) {
glSurfaceView­.onResume();
}
}
}

Renderer:
package com.example.a­irhockey;

import android.conten­t.Context;
import android.opengl­.GLSurfaceView;

import com.example.a­irhockey.Util­.LoggerConfig;
import com.example.a­irhockey.Util­.ShaderHelper;
import com.example.a­irhockey.Util­.TextResource­Reader;

import java.nio.Byte­Buffer;
import java.nio.ByteOrder;
import java.nio.Float­Buffer;

import javax.microedi­tion.khronos.e­gl.EGLConfig;
import javax.microedi­tion.khronos.o­pengles.GL10;

import static android.opengl­.GLES20.glCle­ar;
import static android.opengl­.GLES20.glCle­arColor;
import static android.opengl­.GLES20.glDra­wArrays;
import static android.opengl­.GLES20.glEna­bleVertexAttri­bArray;
import static android.opengl­.GLES20.glGetAt­tribLocation;
import static android.opengl­.GLES20.glGetU­niformLocation;
import static android.opengl­.GLES20.glUni­form4f;
import static android.opengl­.GLES20.glUse­Program;
import static android.opengl­.GLES20.glVer­texAttribPoin­ter;
import static android.opengl­.GLES20.glView­port;
import static android.opengl­.GLES20.GL_CO­LOR_BUFFER_BIT;
import static android.opengl­.GLES20.GL_FLO­AT;
import static android.opengl­.GLES20.GL_LI­NES;
import static android.opengl­.GLES20.GL_PO­INTS;
import static android.opengl­.GLES20.GL_TRI­ANGLES;

public class AirHockeyRenderer implements GLSurfaceView­.Renderer {
private static final int POSITION_COMPO­NENT_COUNT = 2;
private static final int BYTES_PER_FLOAT = 4;
private final FloatBuffer vertexData;
private final Context context;
private int program;
private static final String U_COLOR = "u_Color";
private int uColorLocation;
private static final String A_POSITION = "a_Position";
private int aPositionLocation;

public AirHockeyRende­rer(Context context) {
this.context = context;
float[] tableVertices­WithTriangles = {
// 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
};
vertexData = ByteBuffer
.allocateDirec­t(tableVertices­WithTriangles­.length * BYTES_PER_FLOAT)
.order(ByteOr­der.nativeOrder())
.asFloatBuffer();
vertexData.put(ta­bleVerticesWit­hTriangles);
}

@Override
public void onSurfaceCrea­ted(GL10 glUnused, EGLConfig config) {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
String vertexShaderSource = TextResourceReader
.readTextFile­FromResource(con­text, R.raw.simple_ver­tex_shader);
String fragmentShader­Source = TextResourceReader
.readTextFile­FromResource(con­text, R.raw.simple_frag­ment_shader);
int vertexShader = ShaderHelper.com­pileVertexSha­der(vertexSha­derSource);
int fragmentShader = ShaderHelper.com­pileFragmentSha­der(fragmentSha­derSource);
program = ShaderHelper.lin­kProgram(vertex­Shader, fragmentShader);
if (LoggerConfig.ON) {
ShaderHelper.va­lidateProgram(pro­gram);
}
glUseProgram(pro­gram);
uColorLocation = glGetUniformLo­cation(program, U_COLOR);
aPositionLocation = glGetAttribLo­cation(program, A_POSITION);
vertexData.po­sition(0);
glVertexAttrib­Pointer(aPosi­tionLocation, POSITION_COMPO­NENT_COUNT, GL_FLOAT,
false, 0, vertexData);
glEnableVerte­xAttribArray(a­PositionLocati­on);
}

@Override
public void onSurfaceChan­ged(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_CO­LOR_BUFFER_BIT);
// Draw the table.
glUniform4f(u­ColorLocation, 1.0f, 1.0f, 1.0f, 1.0f);
glDrawArrays(GL_TRI­ANGLES, 0, 6);

// Draw the center dividing line.
glUniform4f(u­ColorLocation, 1.0f, 0.0f, 0.0f, 1.0f);
glDrawArrays(GL_LI­NES, 6, 2);

// Draw the first mallet blue.
glUniform4f(u­ColorLocation, 0.0f, 0.0f, 1.0f, 1.0f);
glDrawArrays(GL_PO­INTS, 8, 1);

// Draw the second mallet red.
glUniform4f(u­ColorLocation, 1.0f, 0.0f, 0.0f, 1.0f);
glDrawArrays(GL_PO­INTS, 9, 1);
}
}

LoggerConfig:
package com.example.a­irhockey.Util;

public class LoggerConfig {
public static final boolean ON = true;
}

ShaderHelper:
package com.example.a­irhockey.Util;

import android.util.Log;

import static android.opengl­.GLES20.GL_COM­PILE_STATUS;
import static android.opengl­.GLES20.GL_FRAG­MENT_SHADER;
import static android.opengl­.GLES20.GL_LIN­K_STATUS;
import static android.opengl­.GLES20.GL_VA­LIDATE_STATUS;
import static android.opengl­.GLES20.GL_VER­TEX_SHADER;
import static android.opengl­.GLES20.glAttachSha­der;
import static android.opengl­.GLES20.glCre­ateProgram;
import static android.opengl­.GLES20.glCre­ateShader;
import static android.opengl­.GLES20.glDele­teProgram;
import static android.opengl­.GLES20.glDele­teShader;
import static android.opengl­.GLES20.glGet­ProgramInfoLog;
import static android.opengl­.GLES20.glGet­Programiv;
import static android.opengl­.GLES20.glGet­ShaderInfoLog;
import static android.opengl­.GLES20.glGet­Shaderiv;
import static android.opengl­.GLES20.glLin­kProgram;
import static android.opengl­.GLES20.glVali­dateProgram;

public class ShaderHelper {
private static final String TAG = "ShaderHelper";
public static int compileVertex­Shader(String shaderCode) {
return compileShader(GL_VER­TEX_SHADER, shaderCode);
}
public static int compileFragmen­tShader(String shaderCode) {
return compileShader(GL_FRAG­MENT_SHADER, shaderCode);
}
private static int compileShader(int type, String shaderCode) {
final int shaderObjectId = glCreateShader(ty­pe);
if (shaderObjectId == 0) {
if (LoggerConfig.ON) {
Log.w(TAG, "Could not create new shader.");
}
return 0;
}
final int[] compileStatus = new int[1];
glGetShaderiv(sha­derObjectId, GL_COMPILE_STATUS, compileStatus, 0);
if (LoggerConfig.ON) {
// Print the shader info log to the Android log output.
Log.v(TAG, "Results of compiling source:" + "\n" + shaderCode + "\n:"
+ glGetShaderIn­foLog(shaderOb­jectId));
}
if (compileStatus[0] == 0) {
// If it failed, delete the shader object.
glDeleteShader(sha­derObjectId);
if (LoggerConfig.ON) {
Log.w(TAG, "Compilation of shader failed.");
}
return 0;
}
return shaderObjectId;
}

public static int linkProgram(int vertexShaderId, int fragmentShaderId) {
final int programObjectId = glCreateProgram();
if (programObjectId == 0) {
if (LoggerConfig.ON) {
Log.w(TAG, "Could not create new program");
}
return 0;
}
glAttachShader(pro­gramObjectId, vertexShaderId);
glAttachShader(pro­gramObjectId, fragmentShaderId);
glLinkProgram(pro­gramObjectId);
final int[] linkStatus = new int[1];
glGetProgramiv(pro­gramObjectId, GL_LINK_STATUS, linkStatus, 0);
if (LoggerConfig.ON) {
// Print the program info log to the Android log output.
Log.v(TAG, "Results of linking program:\n"
+ glGetProgramIn­foLog(programOb­jectId));
}
if (linkStatus[0] == 0) {
// If it failed, delete the program object.
glDeleteProgram(pro­gramObjectId);
if (LoggerConfig.ON) {
Log.w(TAG, "Linking of program failed.");
}
return 0;
}
return programObjectId;
}

public static boolean validateProgram(int programObjectId) {
glValidatePro­gram(programOb­jectId);
final int[] validateStatus = new int[1];
glGetProgramiv(pro­gramObjectId, GL_VALIDATE_STATUS, validateStatus, 0);
Log.v(TAG, "Results of validating program: " + validateStatus[0]
+ "\nLog:" + glGetProgramIn­foLog(programOb­jectId));
return validateStatus[0] != 0;
}
}

TextResourceReader:
package com.example.a­irhockey.Util;

import android.conten­t.Context;
import android.conten­t.res.Resources;

import java.io.Buffe­redReader;
import java.io.IOExcep­tion;
import java.io.Input­Stream;
import java.io.Input­StreamReader;

public class TextResourceReader {
public static String readTextFileFrom­Resource(Context context,
int resourceId) {
StringBuilder body = new StringBuilder();
try {
InputStream inputStream =
context.getRe­sources().open­RawResource(re­sourceId);
InputStreamReader inputStreamReader =
new InputStreamRe­ader(inputStre­am);
BufferedReader bufferedReader = new BufferedReader(in­putStreamReader);
String nextLine;
while ((nextLine = bufferedReader­.readLine()) != null) {
body.append(nex­tLine);
body.append('\n');
}
} catch (IOException e) {
throw new RuntimeException(
"Could not open resource: " + resourceId, e);
} catch (Resources.Not­FoundException nfe) {
throw new RuntimeExcepti­on("Resource not found: " + resourceId, nfe);
}
return body.toString();
}
}

 
Odpovědět 23. dubna 17:28
Avatar
Odpovídá na Jiří Satora
Jiří Satora:23. dubna 17:29

Ještě shadery:
Fragment Shader:
precision mediump float;
uniform vec4 u_Color;
void main()
{
gl_FragColor = u_Color;
}

Vertex Shader:
attribute vec4 a_Position;
void main()
{
gl_Position = a_Position;
}

 
Nahoru Odpovědět 23. dubna 17:29
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 2 zpráv z 2.