本文讲述用OpenGL显示STL模型文件。
STL模型文件:Android OpenGL STL详解
Android OpenGL初级开发:Android OpenGL之使用GLSurfaceView创建一个3D旋转的图形
1.Model.java
public class Model {
//三角面个数
private int facetCount;
//顶点坐标数组
private float[] verts;
//每个顶点对应的法向量数组
private float[] vnorms;
//每个三角面的属性信息
private short[] remarks;
//顶点数组转换而来的Buffer
private FloatBuffer vertBuffer;
//每个顶点对应的法向量转换而来的Buffer
private FloatBuffer vnormBuffer;
//以下分别保存所有点在x,y,z方向上的最大值、最小值
float maxX;
float minX;
float maxY;
float minY;
float maxZ;
float minZ;
//返回模型的中心点
public Point getCentrePoint() {
float cx = (maxX - minX) / 2;
float cy = (maxY - minY) / 2;
float cz = (maxZ - minZ) / 2;
return new Point(cx, cy, cz);
}
//包裹模型的最大半径
public float getR() {
float dx = (maxX - minX);
float dy = (maxY - minY);
float dz = (maxZ - minZ);
float max = dx;
if (dy > max)
max = dy;
if (dz > max)
max = dz;
return max;
}
public int getFacetCount() {
return facetCount;
}
public void setFacetCount(int facetCount) {
this.facetCount = facetCount;
}
public float[] getVerts() {
return verts;
}
public void setVerts(float[] verts) {
this.verts = verts;
vertBuffer = Util.floatToBuffer(verts);
}
public FloatBuffer getVertBuffer() {
return vertBuffer;
}
public FloatBuffer getVnormBuffer() {
return vnormBuffer;
}
public float[] getVnorms() {
return vnorms;
}
public void setVnorms(float[] vnorms) {
this.vnorms = vnorms;
vnormBuffer = Util.floatToBuffer(vnorms);
}
public short[] getRemarks() {
return remarks;
}
public void setRemarks(short[] remarks) {
this.remarks = remarks;
}
}
2.Point.java
public class Point {
public float x;
public float y;
public float z;
public Point(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
}
3.STLReader.java
public class STLReader {
private StlLoadListener stlLoadListener;
public Model parserBinStlInSDCard(String path)
throws IOException {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
return parserBinStl(fis);
}
public Model parserBinStlInAssets(Context context, String fileName)
throws IOException {
InputStream is = context.getAssets().open(fileName);
return parserBinStl(is);
}
public static interface StlLoadListener {
void onstart();
void onLoading(int cur, int total);
void onFinished();
void onFailure(Exception e);
}
}
4.OpenGLESActivity.java
public class OpenGLESActivity extends Activity {
private GLSurfaceView glView;
private float rotateDegreen = 0;
private GLRenderer glRenderer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 如果本设备支持OpenGl ES 2.0
if (IsSupported()) {
glView = new GLSurfaceView(this);
glRenderer = new GLRenderer(this);
glView.setRenderer(glRenderer);
setContentView(glView);
}else{
Toast.makeText(OpenGLESActivity.this,"此设备不支持OpenGL ES 2.0!",Toast.LENGTH_LONG).show();
}
}
public void rotate(float degree) {
glRenderer.rotate(degree);
glView.invalidate();
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
rotate(rotateDegreen);
}
};
/**
* 当前设备是否支持OpenGL ES 2.0
* */
private boolean IsSupported() {
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
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")));
return supportsEs2;
}
/**
* onPause方法 glSurfaceView.onPause
* */
@Override
protected void onPause() {
super.onPause();
if (glView != null) {
glView.onPause();
}
}
/**
* onResume方法 glSurfaceView.onResume
* */
@Override
protected void onResume() {
super.onResume();
if (glView != null) {
glView.onResume();
//不断改变rotateDegreen值,实现旋转
new Thread() {
@Override
public void run() {
while (true) {
try {
sleep(100);
rotateDegreen += 5;
handler.sendEmptyMessage(0x001);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
}
}
}
5.结果
————————————————
版权声明:本文为CSDN博主「jianning-wu」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_37730482/article/details/80519779