import android.app.Activity;
import android.graphics.Color;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.os.Bundle;
import android.view.ViewGroup;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
/**
* Created by Administrator on 2016/8/31 0031.
*/
public class Test2 extends Activity {
GLSurfaceView glSurfaceView;
//定點(diǎn)著色器
public static String VL = "attribute vec4 vPosition;\n" +
"uniform mat4 u_Matrix;"
+ "void main() {\n"
+ " gl_Position = u_Matrix*vPosition;\n"
+ "}";
//片段著色器
public static String FL = "precision mediump float;\n" +
"uniform vec4 u_Color;"
+ "void main() {\n"
+ " gl_FragColor = u_Color;\n"
+ "}";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glSurfaceView = new GLSurfaceView(this);
glSurfaceView.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
setContentView(glSurfaceView);
//設(shè)置opengl es版本
glSurfaceView.setEGLContextClientVersion(2);
glSurfaceView.setRenderer(new RenderListener());
glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}
class RenderListener implements GLSurfaceView.Renderer {
FloatBuffer verticalsBuffer;
//多邊形定點(diǎn)
/* float[] verticals = new float[]{
-1, -1, 0.0f, // top
-1f, 1, 0.0f, // bottom left
1f, 1, 0.0f, // bottom right
// 1,-0.5f,0.0f,
};*/
float[] verticals = new float[20 * 40 * 6 * 3];
float[] projectMatrix = new float[16];
ShortBuffer verticalsIndexBuffer;
//每個(gè)三角形的定點(diǎn)編號(hào)馏臭,多邊形有兩個(gè)三角形組成
short[] verticalsIndex = new short[]{
0, 1, 2,
0, 2, 3
};
private int mProgram;
private int mPositionHandle;
private int mColorHandle;
private int mMatricHandle;
public RenderListener() {
float x = 0;
float y = 0;
float z = -1;
float r = 1;
int index=0;
double d = 9 * Math.PI / 180;
for (int i = 0; i < 180; i += 9) {
double d1 = i * Math.PI / 180;
for (int j = 0; j < 360; j += 9) {
double d2 = j * Math.PI / 180;
verticals[index++] = (float) (x+r*Math.sin(d1+d)*Math.cos(d2+d));
verticals[index++] = (float) (y+r*Math.cos(d1+d));
verticals[index++] = (float) (z+r*Math.sin(d1+d)*Math.sin(d2+d));
verticals[index++] = (float) (x+r*Math.sin(d1)*Math.cos(d2));
verticals[index++] = (float) (y+r*Math.cos(d1));
verticals[index++] = (float) (z+r*Math.sin(d1)*Math.sin(d2));
verticals[index++] = (float) (x+r*Math.sin(d1)*Math.cos(d2+d));
verticals[index++] = (float) (y+r*Math.cos(d1));
verticals[index++] = (float) (z+r*Math.sin(d1)*Math.sin(d2+d));
verticals[index++] = (float) (x+r*Math.sin(d1+d)*Math.cos(d2+d));
verticals[index++] = (float) (y+r*Math.cos(d1+d));
verticals[index++] = (float) (z+r*Math.sin(d1+d)*Math.sin(d2+d));
verticals[index++] = (float) (x+r*Math.sin(d1+d)*Math.cos(d2));
verticals[index++] = (float) (y+r*Math.cos(d1+d));
verticals[index++] = (float) (z+r*Math.sin(d1+d)*Math.sin(d2));
verticals[index++] = (float) (x+r*Math.sin(d1)*Math.cos(d2));
verticals[index++] = (float) (y+r*Math.cos(d1));
verticals[index++] = (float) (z+r*Math.sin(d1)*Math.sin(d2));
}
}
verticalsBuffer = ByteBuffer.allocateDirect(verticals.length * 4)
.order(ByteOrder.nativeOrder())
.asFloatBuffer()
.put(verticals);
verticalsBuffer.position(0);
ByteBuffer dlb = ByteBuffer.allocateDirect(
// (對(duì)應(yīng)順序的坐標(biāo)數(shù) * 2)short是2字節(jié)
verticalsIndex.length * 2);
dlb.order(ByteOrder.nativeOrder());
verticalsIndexBuffer = dlb.asShortBuffer();
verticalsIndexBuffer.put(verticalsIndex);
verticalsIndexBuffer.position(0);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
mProgram = GLES20.glCreateProgram();
int vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
GLES20.glShaderSource(vertexShader, VL);
GLES20.glCompileShader(vertexShader);
int fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
GLES20.glShaderSource(fragmentShader, FL);
GLES20.glCompileShader(fragmentShader);
GLES20.glAttachShader(mProgram, vertexShader);
GLES20.glAttachShader(mProgram, fragmentShader);
GLES20.glLinkProgram(mProgram);
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
mColorHandle = GLES20.glGetUniformLocation(mProgram, "u_Color");
mMatricHandle = GLES20.glGetUniformLocation(mProgram, "u_Matrix");
final float aspectRatio = width > height ? width * 1f / height : height * 1f / width;
if (width > height) {
Matrix.orthoM(projectMatrix, 0, -aspectRatio, aspectRatio, -1f, 1f, -1f, 1f);
} else {
Matrix.orthoM(projectMatrix, 0, -1f, 1f, -aspectRatio, aspectRatio, -1f, 1f);
}
}
@Override
public void onDrawFrame(GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
GLES20.glUseProgram(mProgram);
GLES20.glEnableVertexAttribArray(mPositionHandle);
GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false,
12, verticalsBuffer);
GLES20.glUniform4fv(mColorHandle, 1, new float[]{0, 1, 1, 5}, 0);
GLES20.glUniformMatrix4fv(mMatricHandle, 1, false, projectMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0,20 * 40 * 6 );
//GLES20.glDrawElements(GLES20.GL_TRIANGLES, verticalsIndex.length, GLES20.GL_UNSIGNED_SHORT, verticalsIndexBuffer);
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
}
}
opengl es2.0繪制球體
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)兵多,“玉大人尖啡,你說(shuō)我怎么就攤上這事橄仆。” “怎么了衅斩?”我有些...
- 文/不壞的土叔 我叫張陵盆顾,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我畏梆,道長(zhǎng)椎扬,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任具温,我火速辦了婚禮蚕涤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘铣猩。我一直安慰自己揖铜,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布达皿。 她就那樣靜靜地躺著天吓,像睡著了一般。 火紅的嫁衣襯著肌膚如雪峦椰。 梳的紋絲不亂的頭發(fā)上龄寞,一...
- 那天,我揣著相機(jī)與錄音汤功,去河邊找鬼物邑。 笑死,一個(gè)胖子當(dāng)著我的面吹牛滔金,可吹牛的內(nèi)容都是我干的色解。 我是一名探鬼主播,決...
- 文/蒼蘭香墨 我猛地睜開眼餐茵,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼科阎!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起忿族,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤锣笨,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后道批,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體错英,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年屹徘,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了走趋。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站豆励,受9級(jí)特大地震影響夺荒,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜良蒸,卻給世界環(huán)境...
- 文/蒙蒙 一技扼、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧嫩痰,春花似錦剿吻、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至纺棺,卻和暖如春榄笙,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背祷蝌。 一陣腳步聲響...
- 正文 我出身青樓乡翅,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親罪郊。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蠕蚜,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 先上效果圖 我是android opengl es的初學(xué)者,有很多東西還不懂悔橄,仍在學(xué)習(xí)靶累;這里實(shí)現(xiàn)全景圖瀏覽的一個(gè)思...
- Android使用OpenGL ES2.0繪制3D圖像或者加載3D模型時(shí),為了達(dá)到立體效果往往需要設(shè)置視見轉(zhuǎn)換矩陣...
- 實(shí)現(xiàn)效果 步驟 實(shí)現(xiàn)球體需要三步驟第一 設(shè)置視口和視圖矩陣 //1// 設(shè)置視口和投影矩陣void ChangeS...
- 本文主要記錄使用所學(xué)OpenGL相關(guān)知識(shí)繪制OpenGL中的一個(gè)經(jīng)典案例--球體世界癣疟,最終實(shí)現(xiàn)效果如下: 準(zhǔn)備工作...