fix: Simple5——1未完成

This commit is contained in:
xiaoyan 2022-09-23 18:00:53 +08:00
parent 738afcb259
commit 751a180677
9 changed files with 132 additions and 14 deletions

View File

@ -14,7 +14,7 @@
android:theme="@style/Theme.MyOpenGLLearn"
tools:targetApi="31">
<activity
android:name=".Sample3_1Activity"
android:name=".simple3.Sample3_1Activity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -11,17 +11,14 @@ import android.util.Log;
public class ShaderUtil
{
//加载制定shader的方法
public static int loadShader
(
public static int loadShader(
int shaderType, //shader的类型 GLES20.GL_VERTEX_SHADER(顶点) GLES20.GL_FRAGMENT_SHADER(片元)
String source //shader的脚本字符串
)
{
) {
//创建一个新shader
int shader = GLES20.glCreateShader(shaderType);
//若创建成功则加载shader
if (shader != 0)
{
if (shader != 0) {
//加载shader的源代码
GLES20.glShaderSource(shader, source);
//编译shader

View File

@ -1,4 +1,4 @@
package com.navinfo.myopengllearn;
package com.navinfo.myopengllearn.simple3;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

View File

@ -1,4 +1,4 @@
package com.navinfo.myopengllearn;
package com.navinfo.myopengllearn.simple3;
import android.app.Activity;
import android.content.pm.ActivityInfo;

View File

@ -1,4 +1,4 @@
package com.navinfo.myopengllearn;
package com.navinfo.myopengllearn.simple3;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@ -6,6 +6,8 @@ import java.nio.FloatBuffer;
import android.opengl.GLES20;
import android.opengl.Matrix;
import com.navinfo.myopengllearn.ShaderUtil;
//三角形
public class Triangle
{
@ -51,8 +53,7 @@ public class Triangle
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
float colors[]=new float[]
{
float colors[]=new float[] {
1,1,1,0,
0,0,1,0,
0,1,0,0,
@ -69,7 +70,7 @@ public class Triangle
public void initShader(MyTDView mv)
{
//加载顶点着色器的脚本内容
mVertexShader=ShaderUtil.loadFromAssetsFile("vertex.glsl", mv.getResources());
mVertexShader= ShaderUtil.loadFromAssetsFile("vertex.glsl", mv.getResources());
//加载片元着色器的脚本内容
mFragmentShader=ShaderUtil.loadFromAssetsFile("frag.glsl", mv.getResources());
//基于顶点着色器与片元着色器创建程序

View File

@ -0,0 +1,41 @@
package com.navinfo.myopengllearn.simple5;
import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class MySurfaceView extends GLSurfaceView {
private SceneRenderer sceneRenderer;
public MySurfaceView(Context context) {
super(context);
sceneRenderer = new SceneRenderer();
this.setRenderer(sceneRenderer);
this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}
private class SceneRenderer implements GLSurfaceView.Renderer {
@Override
public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
// 初始化六角形数据
GLES20.glEnable(GLES20.GL_DEPTH_TEST); // 打开深度检测
}
@Override
public void onSurfaceChanged(GL10 gl10, int i, int i1) {
}
@Override
public void onDrawFrame(GL10 gl10) {
}
}
}

View File

@ -0,0 +1,23 @@
package com.navinfo.myopengllearn.simple5;
import android.app.Activity;
import android.os.Bundle;
import androidx.annotation.Nullable;
public class Simple5_1Activity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
}

View File

@ -0,0 +1,56 @@
package com.navinfo.myopengllearn.simple5;
import android.view.SurfaceView;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;
/**
* 六角形图形
* */
public class SixPointStar {
public SixPointStar(SurfaceView surfaceView, float r, float R, float centX, float centY, float centZ) {
initVtexData(r, R, centX, centY, centZ);
initShader();
}
/**
* 初始化顶点数据
* */
private void initVtexData(float r, float R, float centX, float centY, float centZ) {
List<Double> pointList = new ArrayList<>();
float stepAngle = 60f;
// 中心点位置
pointList.add((double) centX);
pointList.add((double) centY);
pointList.add((double) centZ);
for (int i = 0; i < 6; i++) {
float currentAngle = stepAngle*i;
// 长边半径的点
pointList.add(R*Math.sin(Math.toRadians(currentAngle)));
pointList.add(R*Math.cos(Math.toRadians(currentAngle)));
pointList.add((double) centZ);
// 短边半径的点
pointList.add(r*Math.sin(Math.toRadians(currentAngle)));
pointList.add(r*Math.cos(Math.toRadians(currentAngle)));
pointList.add((double) centZ);
}
int pointSize = pointList.size()/3;
ByteBuffer vbb = ByteBuffer.allocateDirect(pointList.size()*4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer pointBuffer = vbb.asFloatBuffer();
float[] floatArray = new float[pointList.size()];
pointBuffer.put(pointList.toArray(floatArray));
}
private void initShader() {
}
}

View File

@ -4,6 +4,6 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Sample3_1Activity">
tools:context=".simple3.Sample3_1Activity">
</androidx.constraintlayout.widget.ConstraintLayout>