Рабочий первый рабочий коммит
This commit is contained in:
@@ -5,6 +5,9 @@ import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.opengl.GLES20;
|
||||
import android.opengl.GLUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
@@ -14,7 +17,6 @@ import javax.microedition.khronos.opengles.GL10;
|
||||
/* loaded from: classes.dex */
|
||||
public class SplashScreen {
|
||||
private static final String TAG = "SplashScreen";
|
||||
private static final int floatSize = 4;
|
||||
private Activity _activity;
|
||||
private int _attPosition;
|
||||
private int _attSampler;
|
||||
@@ -23,149 +25,231 @@ public class SplashScreen {
|
||||
private int _program;
|
||||
private int _vertexShader;
|
||||
private FloatBuffer vBuffer;
|
||||
private final String vShaderStr = "attribute vec4 a_position; \nattribute vec4 a_texCoord; \nvarying vec2 v_texCoord; \nvoid main() \n{ \n gl_Position = a_position; \n v_texCoord = a_texCoord.xy; \n} \n";
|
||||
private final String fShaderStr = "precision highp float; \nvarying vec2 v_texCoord; \nuniform sampler2D s_texture; \nvoid main() \n{ \n gl_FragColor = texture2D( s_texture, v_texCoord );\n} \n";
|
||||
|
||||
// Вершинный шейдер
|
||||
private final String vShaderStr =
|
||||
"attribute vec4 a_position; \n" +
|
||||
"attribute vec2 a_texCoord; \n" +
|
||||
"varying vec2 v_texCoord; \n" +
|
||||
"void main() { \n" +
|
||||
" gl_Position = a_position; \n" +
|
||||
" v_texCoord = a_texCoord; \n" +
|
||||
"} \n";
|
||||
|
||||
// Фрагментный шейдер
|
||||
private final String fShaderStr =
|
||||
"precision mediump float; \n" +
|
||||
"varying vec2 v_texCoord; \n" +
|
||||
"uniform sampler2D s_texture; \n" +
|
||||
"void main() { \n" +
|
||||
" gl_FragColor = texture2D(s_texture, v_texCoord); \n" +
|
||||
"} \n";
|
||||
|
||||
private int[] _textureId = new int[1];
|
||||
|
||||
public SplashScreen(Activity activity) {
|
||||
this._activity = activity;
|
||||
}
|
||||
|
||||
public void init(GL10 gl10, int i, int i2) {
|
||||
Bitmap bitmap;
|
||||
public void init(GL10 gl10, int width, int height) {
|
||||
if (!initRenderer()) {
|
||||
destroy(gl10);
|
||||
return;
|
||||
}
|
||||
GLES20.glGetError();
|
||||
GLES20.glGenTextures(1, this._textureId, 0);
|
||||
GLES20.glBindTexture(3553, this._textureId[0]);
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inPreferredConfig = Bitmap.Config.ARGB_4444;
|
||||
|
||||
// Загрузка текстуры
|
||||
GLES20.glGenTextures(1, _textureId, 0);
|
||||
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, _textureId[0]);
|
||||
|
||||
// Установка параметров текстуры
|
||||
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
|
||||
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
|
||||
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
|
||||
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
|
||||
|
||||
// Загрузка изображения
|
||||
Bitmap bitmap = null;
|
||||
try {
|
||||
bitmap = BitmapFactory.decodeStream(this._activity.getAssets().open("splash.png"), null, options);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "loadBitmap ", e);
|
||||
bitmap = null;
|
||||
InputStream is = _activity.getAssets().open("splash.png");
|
||||
bitmap = BitmapFactory.decodeStream(is);
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Could not load bitmap", e);
|
||||
}
|
||||
GLUtils.texImage2D(3553, 0, bitmap, 0);
|
||||
GLES20.glTexParameterf(3553, 10241, 9729.0f);
|
||||
GLES20.glTexParameterf(3553, 10240, 9729.0f);
|
||||
GLES20.glTexParameterf(3553, 10242, 33071.0f);
|
||||
GLES20.glTexParameterf(3553, 10243, 33071.0f);
|
||||
bitmap.recycle();
|
||||
int glGetError = GLES20.glGetError();
|
||||
if (glGetError != 0) {
|
||||
Log.e(TAG, "Texture Load GLError: " + glGetError);
|
||||
|
||||
if (bitmap != null) {
|
||||
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
|
||||
bitmap.recycle();
|
||||
} else {
|
||||
Log.e(TAG, "Failed to load bitmap");
|
||||
destroy(gl10);
|
||||
return;
|
||||
}
|
||||
|
||||
// Проверка ошибок
|
||||
int error = GLES20.glGetError();
|
||||
if (error != GLES20.GL_NO_ERROR) {
|
||||
Log.e(TAG, "Texture Load GLError: " + error);
|
||||
destroy(gl10);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean initRenderer() {
|
||||
this._vertexShader = LoadShader(35633, "attribute vec4 a_position; \nattribute vec4 a_texCoord; \nvarying vec2 v_texCoord; \nvoid main() \n{ \n gl_Position = a_position; \n v_texCoord = a_texCoord.xy; \n} \n");
|
||||
this._fragmentShader = LoadShader(35632, "precision highp float; \nvarying vec2 v_texCoord; \nuniform sampler2D s_texture; \nvoid main() \n{ \n gl_FragColor = texture2D( s_texture, v_texCoord );\n} \n");
|
||||
this._program = GLES20.glCreateProgram();
|
||||
if (this._program == 0 || this._vertexShader == 0 || this._fragmentShader == 0) {
|
||||
Log.e(TAG, "InitRender() - fail\n");
|
||||
// Загрузка шейдеров
|
||||
_vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vShaderStr);
|
||||
_fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fShaderStr);
|
||||
|
||||
// Создание программы
|
||||
_program = GLES20.glCreateProgram();
|
||||
if (_program == 0) {
|
||||
Log.e(TAG, "Failed to create program");
|
||||
return false;
|
||||
}
|
||||
GLES20.glAttachShader(this._program, this._vertexShader);
|
||||
GLES20.glAttachShader(this._program, this._fragmentShader);
|
||||
GLES20.glLinkProgram(this._program);
|
||||
int[] iArr = new int[1];
|
||||
GLES20.glGetProgramiv(this._program, 35714, iArr, 0);
|
||||
if (iArr[0] == 0) {
|
||||
Log.e(TAG, "InitRender() - fail link program");
|
||||
Log.e(TAG, GLES20.glGetProgramInfoLog(this._program));
|
||||
GLES20.glDeleteProgram(this._program);
|
||||
this._program = 0;
|
||||
|
||||
// Прикрепление шейдеров
|
||||
GLES20.glAttachShader(_program, _vertexShader);
|
||||
GLES20.glAttachShader(_program, _fragmentShader);
|
||||
|
||||
// Линковка программы
|
||||
GLES20.glLinkProgram(_program);
|
||||
|
||||
// Проверка статуса линковки
|
||||
int[] linkStatus = new int[1];
|
||||
GLES20.glGetProgramiv(_program, GLES20.GL_LINK_STATUS, linkStatus, 0);
|
||||
if (linkStatus[0] != GLES20.GL_TRUE) {
|
||||
Log.e(TAG, "Could not link program: " + GLES20.glGetProgramInfoLog(_program));
|
||||
GLES20.glDeleteProgram(_program);
|
||||
_program = 0;
|
||||
return false;
|
||||
}
|
||||
this._attPosition = GLES20.glGetAttribLocation(this._program, "a_position");
|
||||
this._attTexCoord = GLES20.glGetAttribLocation(this._program, "a_texCoord");
|
||||
this._attSampler = GLES20.glGetAttribLocation(this._program, "s_texture");
|
||||
|
||||
// Получение атрибутов
|
||||
_attPosition = GLES20.glGetAttribLocation(_program, "a_position");
|
||||
_attTexCoord = GLES20.glGetAttribLocation(_program, "a_texCoord");
|
||||
_attSampler = GLES20.glGetUniformLocation(_program, "s_texture");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private int LoadShader(int i, String str) {
|
||||
int glCreateShader = GLES20.glCreateShader(i);
|
||||
if (glCreateShader == 0) {
|
||||
Log.e(TAG, "LoadShader(" + i + ", " + str + " - create shader fail\n");
|
||||
private int loadShader(int type, String shaderCode) {
|
||||
int shader = GLES20.glCreateShader(type);
|
||||
if (shader == 0) {
|
||||
Log.e(TAG, "Failed to create shader");
|
||||
return 0;
|
||||
}
|
||||
GLES20.glShaderSource(glCreateShader, str);
|
||||
GLES20.glCompileShader(glCreateShader);
|
||||
int[] iArr = new int[1];
|
||||
GLES20.glGetShaderiv(glCreateShader, 35713, iArr, 0);
|
||||
if (iArr[0] != 0) {
|
||||
return glCreateShader;
|
||||
|
||||
GLES20.glShaderSource(shader, shaderCode);
|
||||
GLES20.glCompileShader(shader);
|
||||
|
||||
// Проверка статуса компиляции
|
||||
int[] compiled = new int[1];
|
||||
GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
|
||||
if (compiled[0] == 0) {
|
||||
Log.e(TAG, "Could not compile shader: " + GLES20.glGetShaderInfoLog(shader));
|
||||
GLES20.glDeleteShader(shader);
|
||||
return 0;
|
||||
}
|
||||
Log.e(TAG, "LoadShader(" + i + ", " + str + ") - compile shader fail\n");
|
||||
GLES20.glDeleteShader(glCreateShader);
|
||||
Log.e(TAG, GLES20.glGetShaderInfoLog(glCreateShader));
|
||||
return 0;
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
public boolean draw(GL10 gl10, int i, int i2) {
|
||||
float f;
|
||||
if (this._textureId[0] == 0) {
|
||||
public boolean draw(GL10 gl10, int width, int height) {
|
||||
if (_textureId[0] == 0 || _program == 0) {
|
||||
return false;
|
||||
}
|
||||
float f2 = 0.8f;
|
||||
if (i > i2) {
|
||||
f2 = (i2 / i) * 0.8f;
|
||||
f = 0.8f;
|
||||
|
||||
// Расчет координат с уменьшением на 20%
|
||||
float ratio = (float) width / height;
|
||||
float imageRatio = 1.0f; // Предполагаем квадратное изображение
|
||||
|
||||
float scaleX, scaleY;
|
||||
if (ratio > imageRatio) {
|
||||
// Шире, чем изображение
|
||||
scaleY = 0.8f; // Уменьшаем на 20%
|
||||
scaleX = imageRatio / ratio * 0.8f;
|
||||
} else {
|
||||
f = (i / i2) * 0.8f;
|
||||
// Уже, чем изображение
|
||||
scaleX = 0.8f; // Уменьшаем на 20%
|
||||
scaleY = ratio / imageRatio * 0.8f;
|
||||
}
|
||||
float f3 = -f2;
|
||||
float f4 = -f;
|
||||
float[][] fArr = {new float[]{f3, f4, 0.0f, 1.0f, f2, f4, 1.0f, 1.0f, f3, f, 0.0f, 0.0f, f2, f, 1.0f, 0.0f}, new float[]{f3, f4, 1.0f, 1.0f, f2, f4, 1.0f, 0.0f, f3, f, 0.0f, 1.0f, f2, f, 0.0f, 0.0f}};
|
||||
this.vBuffer = ByteBuffer.allocateDirect(fArr[0].length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
||||
if (i < i2) {
|
||||
this.vBuffer.put(fArr[1]);
|
||||
} else {
|
||||
this.vBuffer.put(fArr[0]);
|
||||
}
|
||||
GLES20.glViewport(0, 0, i, i2);
|
||||
GLES20.glUseProgram(this._program);
|
||||
GLES20.glEnable(3553);
|
||||
GLES20.glActiveTexture(33984);
|
||||
GLES20.glBindTexture(3553, this._textureId[0]);
|
||||
GLES20.glUniform1i(this._attSampler, 0);
|
||||
this.vBuffer.position(0);
|
||||
GLES20.glVertexAttribPointer(this._attPosition, 2, 5126, false, 16, (Buffer) this.vBuffer);
|
||||
GLES20.glEnableVertexAttribArray(this._attPosition);
|
||||
this.vBuffer.position(2);
|
||||
GLES20.glVertexAttribPointer(this._attTexCoord, 2, 5126, false, 16, (Buffer) this.vBuffer);
|
||||
GLES20.glEnableVertexAttribArray(this._attTexCoord);
|
||||
GLES20.glDrawArrays(5, 0, 4);
|
||||
GLES20.glDisableVertexAttribArray(this._attPosition);
|
||||
GLES20.glDisableVertexAttribArray(this._attTexCoord);
|
||||
GLES20.glUseProgram(0);
|
||||
|
||||
// Координаты вершин и текстур
|
||||
float[] vertices = {
|
||||
-scaleX, -scaleY, 0.0f, // нижний левый
|
||||
scaleX, -scaleY, 0.0f, // нижний правый
|
||||
-scaleX, scaleY, 0.0f, // верхний левый
|
||||
scaleX, scaleY, 0.0f // верхний правый
|
||||
};
|
||||
|
||||
float[] texCoords = {
|
||||
0.0f, 1.0f, // нижний левый
|
||||
1.0f, 1.0f, // нижний правый
|
||||
0.0f, 0.0f, // верхний левый
|
||||
1.0f, 0.0f // верхний правый
|
||||
};
|
||||
|
||||
// Создание буферов
|
||||
ByteBuffer bb = ByteBuffer.allocateDirect(vertices.length * 4);
|
||||
bb.order(ByteOrder.nativeOrder());
|
||||
FloatBuffer vertexBuffer = bb.asFloatBuffer();
|
||||
vertexBuffer.put(vertices);
|
||||
vertexBuffer.position(0);
|
||||
|
||||
bb = ByteBuffer.allocateDirect(texCoords.length * 4);
|
||||
bb.order(ByteOrder.nativeOrder());
|
||||
FloatBuffer texBuffer = bb.asFloatBuffer();
|
||||
texBuffer.put(texCoords);
|
||||
texBuffer.position(0);
|
||||
|
||||
// Отрисовка с белым фоном
|
||||
GLES20.glViewport(0, 0, width, height);
|
||||
GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Белый цвет
|
||||
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
|
||||
|
||||
GLES20.glUseProgram(_program);
|
||||
|
||||
// Передача вершин
|
||||
GLES20.glVertexAttribPointer(_attPosition, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer);
|
||||
GLES20.glEnableVertexAttribArray(_attPosition);
|
||||
|
||||
// Передача текстурных координат
|
||||
GLES20.glVertexAttribPointer(_attTexCoord, 2, GLES20.GL_FLOAT, false, 0, texBuffer);
|
||||
GLES20.glEnableVertexAttribArray(_attTexCoord);
|
||||
|
||||
// Активация текстуры
|
||||
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
|
||||
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, _textureId[0]);
|
||||
GLES20.glUniform1i(_attSampler, 0);
|
||||
|
||||
// Отрисовка
|
||||
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
|
||||
|
||||
// Отключение атрибутов
|
||||
GLES20.glDisableVertexAttribArray(_attPosition);
|
||||
GLES20.glDisableVertexAttribArray(_attTexCoord);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void destroy(GL10 gl10) {
|
||||
if (this._textureId[0] != 0) {
|
||||
GLES20.glDeleteTextures(1, this._textureId, 0);
|
||||
this._textureId[0] = 0;
|
||||
if (_textureId[0] != 0) {
|
||||
GLES20.glDeleteTextures(1, _textureId, 0);
|
||||
_textureId[0] = 0;
|
||||
}
|
||||
if (this._program != 0) {
|
||||
GLES20.glDeleteProgram(this._program);
|
||||
this._program = 0;
|
||||
if (_program != 0) {
|
||||
GLES20.glDeleteProgram(_program);
|
||||
_program = 0;
|
||||
}
|
||||
if (this._vertexShader != 0) {
|
||||
GLES20.glDeleteShader(this._vertexShader);
|
||||
this._vertexShader = 0;
|
||||
if (_vertexShader != 0) {
|
||||
GLES20.glDeleteShader(_vertexShader);
|
||||
_vertexShader = 0;
|
||||
}
|
||||
if (this._fragmentShader != 0) {
|
||||
GLES20.glDeleteShader(this._fragmentShader);
|
||||
this._fragmentShader = 0;
|
||||
if (_fragmentShader != 0) {
|
||||
GLES20.glDeleteShader(_fragmentShader);
|
||||
_fragmentShader = 0;
|
||||
}
|
||||
if (this.vBuffer != null) {
|
||||
this.vBuffer.clear();
|
||||
this.vBuffer = null;
|
||||
if (vBuffer != null) {
|
||||
vBuffer.clear();
|
||||
vBuffer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user