- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
美好的一天!
我正在使用适用于 Android 的 OpenGL ES 2.0 进行一些简单测试。我正在使用在模拟器中运行良好的模型加载器。然而,当我尝试在 ASUS ZenFone 2E (Android 5.0.1)(预付费手机)上使用它时,它只显示清晰的背景颜色,没有旋转模型。我希望在 OpenGL ES 2.0 和 Android 方面都有丰富经验的人能帮助我。抱歉冗长,我真的不知道为什么它不能在电话上工作。这是来源(我是一个极端的新手):
游戏 View .java:
package wise.child.dials;
import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.os.SystemClock;
import java.io.IOException;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import model.Model;
import render.Program;
import render.Shader;
import util.OBJLoader;
public class GameView extends GLSurfaceView implements GLSurfaceView.Renderer {
// App Context
private Context mContext;
// Handles
private int mPositionHandle;
private int mColorHandle;
private int mMVPMatrixHandle;
// Program & Shaders
private Program testProgram;
private Shader testVertexShader;
private Shader testFragmentShader;
// Model
private Model model;
private FloatBuffer vertexFloatBuffer;
private int vertexCount;
// Matrices
private final float[] mMVPMatrix = new float[16];
private final float[] mProjectionMatrix = new float[16];
private final float[] mViewMatrix = new float[16];
private float[] mRotationMatrix = new float[16];
// Constructor
public GameView(Context context) {
super(context);
// App Context
mContext = context;
// OpenGL ES 2.0 Context
setEGLContextClientVersion(2);
// Renderer
setRenderer(this);
}
/*-------------------*/
/* Rendering Methods */
/*-------------------*/
// One Time Initialization
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLES20.glClearColor(0.95f, 0.95f, 0.95f, 1f);
// Initialize Shaders
testVertexShader = new Shader(GLES20.GL_VERTEX_SHADER, mContext, R.raw.test_vertex_shader);
testFragmentShader = new Shader(GLES20.GL_FRAGMENT_SHADER, mContext, R.raw.test_fragment_shader);
// Create Program
testProgram = new Program(testVertexShader, testFragmentShader);
testProgram.use();
// Get Handles - Uniforms & Attributes
mPositionHandle = testProgram.getAttribute("vPosition");
mColorHandle = testProgram.getUniform("vColor");
mMVPMatrixHandle = testProgram.getUniform("uMVPMatrix");
// Model
try {
model = OBJLoader.loadOBJ(mContext, R.raw.spider);
vertexFloatBuffer = model.getVerticesFromIndices();
vertexCount = model.getVertexCount();
} catch (IOException e) {
e.printStackTrace();
}
}
// Drawing Call
@Override
public void onDrawFrame(GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// Time and Rotation Animation
float[] scratch = new float[16];
long time = SystemClock.uptimeMillis() % 4000L;
float angle = 0.090f * ((int) time);
Matrix.setRotateM(mRotationMatrix, 0, angle, angle, angle, angle);
// Set and Bind Data
GLES20.glEnableVertexAttribArray(mPositionHandle);
GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, 12, vertexFloatBuffer);
// Set Color
float[] color = {.75f, 0f, 0f, 1f};
GLES20.glUniform4fv(mColorHandle, 1, color, 0);
// Camera Position - View Matrix
Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -15, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
// Projection x View
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
// Rotation x MVP
Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0);
// Final Matrix
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, scratch, 0);
// Draw
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
// Disable
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
// GLSurface Changed
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// GL Viewport & Aspect Ratio
GLES20.glViewport(0, 0, width, height);
float aspectRatio = (float) width / height;
// Calculate Projection
Matrix.frustumM(mProjectionMatrix, 0, -aspectRatio, aspectRatio, -1, 1, 3, 50);
}
}
Shader.java
package render;
import android.content.Context;
import android.opengl.GLES20;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Shader {
// Shader Source Code
public String shaderSource;
// Shader Handle
public int shaderHandle;
public int programHandle;
public Shader(int type, Context context, int resID) {
try {
shaderSource = loadShader(context, resID);
Log.d("Shader Load", "Success!");
System.out.println(shaderSource);
} catch (IOException e) {
Log.d("Shader Load", "Failed.");
e.printStackTrace();
}
shaderHandle = GLES20.glCreateShader(type);
GLES20.glShaderSource(shaderHandle, shaderSource);
GLES20.glCompileShader(shaderHandle);
}
// Get From Raw Folder
private String loadShader(Context context, int resID) throws IOException {
BufferedReader reader = new BufferedReader(
new InputStreamReader(context.getResources().openRawResource(resID))
);
String line, shader;
StringBuilder builder = new StringBuilder();
while ((line = reader.readLine()) != null) {
builder.append(line).append('\n');
}
reader.close();
shader = builder.toString();
return shader;
}
// Associated Program
public void setProgram(int handle) {
programHandle = handle;
}
}
程序.java
package render;
import android.opengl.GLES20;
import java.util.ArrayList;
import java.util.List;
public class Program {
public int handle;
private Shader vertexShader;
private Shader fragmentShader;
private List<String> attributes = new ArrayList<String>();
private List<String> uniforms = new ArrayList<String>();
public Program(Shader vertexShader, Shader fragmentShader) {
this.vertexShader = vertexShader;
this.fragmentShader = fragmentShader;
this.vertexShader.setProgram(handle);
this.fragmentShader.setProgram(handle);
handle = GLES20.glCreateProgram();
GLES20.glAttachShader(handle, vertexShader.shaderHandle);
GLES20.glAttachShader(handle, fragmentShader.shaderHandle);
GLES20.glLinkProgram(handle);
}
public void use() {
GLES20.glUseProgram(handle);
}
public int getAttribute(String name) {
return GLES20.glGetAttribLocation(handle, name);
}
public void setAttribute(String name) {
}
public int getUniform(String name) {
return GLES20.glGetUniformLocation(handle, name);
}
}
模型.java
package model;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;
public class Model {
private static final int NUM_OF_COORDS = 3;
public List<Vertex> vertices = new ArrayList<Vertex>();
public List<Vertex> normals = new ArrayList<Vertex>();
public List<Face> faces = new ArrayList<Face>();
public Model() {}
public int getVertexCount() {
return faces.size() * NUM_OF_COORDS;
}
public FloatBuffer getVerticesFromIndices() {
int numOfVertices = 3;
int bytesPerFloat = 4;
ByteBuffer bb = ByteBuffer.allocateDirect(faces.size() * numOfVertices * NUM_OF_COORDS * bytesPerFloat);
bb.order(ByteOrder.nativeOrder());
FloatBuffer vertexFloatBuffer = bb.asFloatBuffer();
// Use indices to find proper vertex
for (Face face : faces) {
// VERTEX 1
vertexFloatBuffer.put(vertices.get((int) (face.vertex.x - 1)).x);
vertexFloatBuffer.put(vertices.get((int) (face.vertex.x - 1)).y);
vertexFloatBuffer.put(vertices.get((int)(face.vertex.x - 1)).z);
// VERTEX 2
vertexFloatBuffer.put(vertices.get((int)(face.vertex.y - 1)).x);
vertexFloatBuffer.put(vertices.get((int)(face.vertex.y - 1)).y);
vertexFloatBuffer.put(vertices.get((int)(face.vertex.y - 1)).z);
// VERTEX 3
vertexFloatBuffer.put(vertices.get((int)(face.vertex.z - 1)).x);
vertexFloatBuffer.put(vertices.get((int)(face.vertex.z - 1)).y);
vertexFloatBuffer.put(vertices.get((int)(face.vertex.z - 1)).z);
}
vertexFloatBuffer.position(0);
return vertexFloatBuffer;
}
public FloatBuffer getNormalsFromIndices() {
int numOfVertices = 3;
int bytesPerFloat = 4;
ByteBuffer bb = ByteBuffer.allocateDirect(faces.size() * numOfVertices * NUM_OF_COORDS * bytesPerFloat);
bb.order(ByteOrder.nativeOrder());
FloatBuffer normalFloatBuffer = bb.asFloatBuffer();
// Use indices to find proper normal
for (Face face : faces) {
// VERTEX 1
normalFloatBuffer.put(normals.get((int) (face.normal.x - 1)).x);
normalFloatBuffer.put(normals.get((int) (face.normal.x - 1)).y);
normalFloatBuffer.put(normals.get((int)(face.normal.x - 1)).z);
// VERTEX 2
normalFloatBuffer.put(normals.get((int)(face.normal.y - 1)).x);
normalFloatBuffer.put(normals.get((int)(face.normal.y - 1)).y);
normalFloatBuffer.put(normals.get((int)(face.normal.y - 1)).z);
// VERTEX 3
normalFloatBuffer.put(normals.get((int)(face.normal.z - 1)).x);
normalFloatBuffer.put(normals.get((int)(face.normal.z - 1)).y);
normalFloatBuffer.put(normals.get((int)(face.normal.z - 1)).z);
}
normalFloatBuffer.position(0);
return normalFloatBuffer;
}
}
OBJLoader.java
package util;
import android.content.Context;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import model.Face;
import model.Model;
import model.Vertex;
public class OBJLoader {
/* loads .obj data from file in res/raw folder */
public static Model loadOBJ(Context context, int resID) throws IOException {
Model model = new Model();
BufferedReader reader = new BufferedReader(
new InputStreamReader(context.getResources().openRawResource(resID))
);
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("v ")) {
// Vertex
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
float z = Float.valueOf(line.split(" ")[3]);
model.vertices.add(new Vertex(x, y, z));
} else if (line.startsWith("vn ")) {
// Normal
float x = Float.valueOf(line.split(" ")[1]);
float y = Float.valueOf(line.split(" ")[2]);
float z = Float.valueOf(line.split(" ")[3]);
model.normals.add(new Vertex(x, y, z));
} else if (line.startsWith("f ")) {
// Face
Vertex vertexIndices = new Vertex(
Float.valueOf(line.split(" ")[1].split("/")[0]),
Float.valueOf(line.split(" ")[2].split("/")[0]),
Float.valueOf(line.split(" ")[3].split("/")[0])
);
Vertex normalIndices = new Vertex(
Float.valueOf(line.split(" ")[1].split("/")[2]),
Float.valueOf(line.split(" ")[2].split("/")[2]),
Float.valueOf(line.split(" ")[3].split("/")[2])
);
model.faces.add(new Face(vertexIndices, normalIndices));
}
}
reader.close();
return model;
}
}
MainActivity.java
package wise.child.dials;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Fullscreen & No Title Bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Set OpenGL ES Drawing Surface (Game View)
setContentView(new GameView(this));
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="wise.child.dials">
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture" />
<supports-gl-texture android:name="GL_OES_compressed_paletted_texture" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MenuActivity"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen">
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SplashScreen" />
</application>
</manifest>
顶点着色器
attribute vec4 vPosition;
uniform mat4 uMVPMatrix;
void main() {
gl_Position = uMVPMatrix * vPosition;
}
fragment 着色器
uniform vec4 vColor;
void main() {
gl_FragColor = vColor;
}
最佳答案
这个特定问题的解决方案:
因此,我将这一行添加到我的 fragment 着色器中:
精度中等 float ;
给我们:
fragment 着色器
precision mediump float;
uniform vec4 vColor;
void main() {
gl_FragColor = vColor;
}
为什么这行得通,我不好意思说我不知道。如果有人愿意进一步详细说明,我很好奇。我仍在学习 OpenGL ES 2.0。在阅读 OpenGL ES 2.0 编程指南 (Good Book) 时,我遇到了那行。他们指出,“在 OpenGL ES 2.0 中,除非加载了有效的顶点和 fragment 着色器,否则无法绘制任何内容。” Android 教程也包括这一行,所以我知道它很重要。
关于java - Android - OpenGL ES 2.0 : Emulator (Works) - Device (Doesn't),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34284811/
我使用线程将数据发送到我的 USB 设备(不是数据存储)并从设备获取返回数据。 它在前 5 分钟内运行良好。数据输入,数据输出。 (我几乎在第六分钟开始唱歌。) 然后突然失去了联系。 有logcat消
这个问题在这里已经有了答案: 9年前关闭。 Possible Duplicate: adb cannot find my device for Android debugging. Why? 我是安卓
我有一台安装了win10的电脑, 我已在外部硬盘上安装了 ubuntu 16.04, 现在,当我启动计算机并且外部硬盘已连接到电脑时 grub 菜单打开,我可以在 win10 或 ubuntu 之间进
假设我想使用 Microsoft.Azure.Devices 包/SDK 加载设备信息,这与我所说的有什么区别: RegistryManager regManager = RegistryManage
我在 Windows PC 上使用 VS-2017。我可以使用模拟器测试和运行 iOS 应用程序。 现在我开始为 AppStore 构建应用程序。 构建成功。 现在我在项目解决方案菜单中选择“部署..
任何帮助,将不胜感激。我一般对tensorflow和编程都是陌生的。我正在按照github(https://github.com/experiencor/keras-yolo3)中的说明学习YOLO-
我用 adb 将 4 台设备连接到我的笔记本电脑。 最近我开始看到我的一个 android 设备“离线”,而其他设备运行文件。 我的问题是,离线到底是什么意思,是什么原因造成的? 最佳答案 每次adb
我想在我的物理设备上开始用espresso编写的仪器测试。 想法如下:我的公司有一个应用程序的白标解决方案,因此我们有很多应用程序可以放入 Play 商店。为了支持我们的支持团队,我编写了一个测试,为
当出现这种情况时,我的应用程序当前正在接收与之前的应用程序用户帐户关联的推送通知: 玩家安装应用程序并注册为“bob” bob 的设备 token 在网络服务中注册 玩家重新安装了应用程序并注册为“p
我正在使用命令行编译和安装 Android 应用程序,当我尝试将其安装到设备上时,会发生以下情况: $> adb devices List of devices attached 8c092420
我使用标准 CUDA malloc 在 CUDA 中分配了一个内存数组,并将其传递给函数,如下所示: void MyClass::run(uchar4 * input_data) 我还有一个类成员,它
我的真实设备是 Sony Xperia c6502 Android 版本 4.3我确定我已将它连接到我的计算机并打开了开发人员选项/USB 调试 在 SDK 管理器中,已经安装了 Google USB
某些设备的设备密度返回 0。以下代码用于计算设备密度: WindowManager wm = (WindowManager) context .getSystemService(Con
我正在将一个小文件 (8.5 Mb) 上传到 flask 测试服务器。 当文件上传完成后,服务器报告: File "/home/ubuntu/.virtualenvs/eco_app/lib/
我正在编写一个小应用程序,我需要将一个变量传递给一个函数。问题是该变量是一个绑定(bind),但该函数必须接受一个常规变量。 代码: ForEach($deviceArrays.devices, id
我正在编写一个小应用程序,我需要将一个变量传递给一个函数。问题是该变量是一个绑定(bind),但该函数必须接受一个常规变量。 代码: ForEach($deviceArrays.devices, id
我创建了一个新的 quasar 项目,添加了 cordova 并进行了一些尝试。 一切正常,我可以在手机上部署该应用程序并在模拟器中运行它。 现在,我需要获取一些设备信息,我想为此使用 cordova
根据 W3C The ‘device-width’ media feature describes the width of the rendering surface of the output d
我的数据迭代器当前在 CPU 上运行,因为 device=0 参数已弃用。但我需要它与模型的其余部分一起在 GPU 上运行。 这是我的代码: pad_idx = TGT.vocab.stoi[""]
Cordova 版本:3.5.0 我正在尝试使用 pushPlugin 实现推送通知.但是当我在android平台上运行这个应用程序时(我没有尝试过其他平台),有一个错误“device is not
我是一名优秀的程序员,十分优秀!