Compare commits
2 Commits
3740d114b8
...
751a180677
| Author | SHA1 | Date | |
|---|---|---|---|
| 751a180677 | |||
| 738afcb259 |
1
MyOpenGLLearn/.gitignore
vendored
1
MyOpenGLLearn/.gitignore
vendored
@@ -14,4 +14,3 @@
|
||||
.externalNativeBuild
|
||||
.cxx
|
||||
local.properties
|
||||
/app/
|
||||
|
||||
@@ -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" />
|
||||
|
||||
6
MyOpenGLLearn/app/src/main/assets/frag.glsl
Normal file
6
MyOpenGLLearn/app/src/main/assets/frag.glsl
Normal file
@@ -0,0 +1,6 @@
|
||||
precision mediump float;
|
||||
varying vec4 vColor; //接收从顶点着色器过来的参数
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = vColor;//给此片元颜色值
|
||||
}
|
||||
10
MyOpenGLLearn/app/src/main/assets/vertex.glsl
Normal file
10
MyOpenGLLearn/app/src/main/assets/vertex.glsl
Normal file
@@ -0,0 +1,10 @@
|
||||
uniform mat4 uMVPMatrix; //总变换矩阵
|
||||
attribute vec3 aPosition; //顶点位置
|
||||
attribute vec4 aColor; //顶点颜色
|
||||
varying vec4 vColor; //用于传递给片元着色器的变量
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uMVPMatrix * vec4(aPosition,1); //根据总变换矩阵计算此次绘制此顶点位置
|
||||
vColor = aColor;//将接收的颜色传递给片元着色器
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.navinfo.myopengllearn;
|
||||
package com.navinfo.myopengllearn.simple3;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.pm.ActivityInfo;
|
||||
@@ -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
|
||||
{
|
||||
@@ -40,9 +42,9 @@ public class Triangle
|
||||
final float UNIT_SIZE=0.2f;
|
||||
float vertices[]=new float[]
|
||||
{
|
||||
-4*UNIT_SIZE,0,
|
||||
0,0,-4*UNIT_SIZE,
|
||||
0,4*UNIT_SIZE,0,0
|
||||
-4*UNIT_SIZE,0, 0,
|
||||
0,-4*UNIT_SIZE, 0,
|
||||
4*UNIT_SIZE,0,0
|
||||
};
|
||||
|
||||
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
|
||||
@@ -51,11 +53,10 @@ 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
|
||||
0,1,0,0,
|
||||
};
|
||||
|
||||
ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
|
||||
@@ -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());
|
||||
//基于顶点着色器与片元着色器创建程序
|
||||
@@ -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) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user