OpenGL繪制函數(shù)!
繪制方式 | 說明 |
---|---|
GL_POINTS點(diǎn)) | 繪制點(diǎn) |
GL_LINES(線段) | 連個(gè)點(diǎn)一組進(jìn)行點(diǎn)的繪制,如果只有一個(gè)點(diǎn)就會(huì)舍棄這個(gè)點(diǎn) |
GL_LINES_STRIP(條帶線) | 按照頂點(diǎn)順序連接頂點(diǎn) |
GL_LINES_LOOP(循環(huán)線) | 按照頂點(diǎn)順序連接頂點(diǎn)澜掩,最后一個(gè)點(diǎn)連接第一點(diǎn) |
GL_TRIANGLES(三角形) | 三個(gè)點(diǎn)一組跃脊,如果不夠三個(gè)點(diǎn)就會(huì)舍棄 多余的點(diǎn) |
GL_TRIANGLE_STRIP(三角形帶) | 頂點(diǎn)按照順序依次 組成三角形繪制,最后實(shí)際形成的是一個(gè)三角型帶 |
GL_TRIANGLE_FAN(三角形扇面) | 將第一個(gè)點(diǎn)作為中心點(diǎn)荧止,其他點(diǎn)作為邊緣點(diǎn),繪制一系列的組成扇形的三角形 |
/**
* 繪制點(diǎn)
* @author qiaosen
* @date 2018/3/16
*/
public class Points extends OpenGLUtils {
private IntBuffer verIntBuffer, colorIntBuffer;
private ByteBuffer indbuffer;
public Points() {
init();
}
// 初始化
private void init() {
//比例
int UNIT_SIZE = 10000;
//頂點(diǎn)數(shù)據(jù)(xyz)
int ver[] = new int[]{
-2 * UNIT_SIZE, 3 * UNIT_SIZE, 0,
1 * UNIT_SIZE, 1 * UNIT_SIZE, 0,
-1 * UNIT_SIZE, -2 * UNIT_SIZE, 0
};
//創(chuàng)建頂點(diǎn)緩沖
verIntBuffer = getIntBuffer(ver);
//支持65536色彩通道
int one = 65536;
//頂點(diǎn)個(gè)數(shù)=顏色個(gè)數(shù)
//顏色數(shù)據(jù)(RGB A)
int color[] = new int[]{
one, 0, 0, 0,
one, 0, 0, 0,
one, 0, 0, 0,
one, 0, 0, 0
};
//創(chuàng)建頂點(diǎn)顏色緩沖
colorIntBuffer = getIntBuffer(color);
// 索引
byte indices[] = new byte[]{
0,3,2
};
// 創(chuàng)建頂點(diǎn)索引緩沖
indbuffer = getByteBuffer(indices);
}
public void drawSelf(GL10 gl){
// 啟用頂點(diǎn)數(shù)組坐標(biāo)
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// 啟用頂點(diǎn)顏色數(shù)組坐標(biāo)
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
// 設(shè)置 畫筆
//給畫筆指定頂點(diǎn)數(shù)據(jù)
gl.glVertexPointer(3,//坐標(biāo)個(gè)數(shù)xyz
GL10.GL_FIXED,//頂點(diǎn)的數(shù)據(jù)類型
0,//間隔 默認(rèn)0
verIntBuffer);///頂點(diǎn)數(shù)據(jù)
//給畫筆指定頂點(diǎn)顏色數(shù)據(jù)
gl.glColorPointer(4,//顏色組成個(gè)數(shù)
GL10.GL_FIXED,//顏色的數(shù)據(jù)類型
0,//間隔 默認(rèn)0
colorIntBuffer);///頂點(diǎn)顏色數(shù)據(jù)
//設(shè)置頂點(diǎn)大小
gl.glPointSize(10);
//繪制(索引法)
gl.glDrawElements(GL10.GL_POINTS,//繪制模型(點(diǎn)1 線段3 三角形3)
3,//索引個(gè)數(shù)
GL10.GL_UNSIGNED_BYTE,//數(shù)據(jù)類型
indbuffer);//索引數(shù)據(jù)
}
}
8D7EEE84-237C-46B6-B610-CAD9D3ECF4CE.png
/**
*
* @author qiaosen
* @date 2018/3/15
*/
// 繪制線
public class Lines extends OpenGLUtils {
private IntBuffer verIntBuffer, colorIntBuffer;
private ByteBuffer indbuffer;
public Lines() {
//初始化View
init();
}
private void init() {
//比例
int UNIT_SIZE = 10000;
//頂點(diǎn)數(shù)據(jù)(xyz)
int ver[] = new int[]{
-2 * UNIT_SIZE,
3 * UNIT_SIZE,
0,
1 * UNIT_SIZE,
1 * UNIT_SIZE,
0,
-1 * UNIT_SIZE,
-2 * UNIT_SIZE,
0,
2 * UNIT_SIZE,
-3 * UNIT_SIZE,
0
};
//創(chuàng)建頂點(diǎn)緩沖
verIntBuffer = getIntBuffer(ver);
//支持65536色彩通道
int one = 65536;
//頂點(diǎn)個(gè)數(shù)=顏色個(gè)數(shù)
//顏色數(shù)據(jù)(RGB A)
int color[] = new int[]{
one, 0, 0, 0, one, 0, 0, 0, one, 0, 0, 0, one, 0, 0, 0
};
//創(chuàng)建頂點(diǎn)緩沖
colorIntBuffer = getIntBuffer(color);
//索引
byte indices[] = new byte[]{
0, 3, 2, 1
};
//創(chuàng)建頂點(diǎn)索引緩沖
indbuffer = getByteBuffer(indices);
}
public void drawSelf(GL10 gl) {
//啟用頂點(diǎn)數(shù)組坐標(biāo)
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
//啟用頂點(diǎn)顏色數(shù)組坐標(biāo)
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
//設(shè)置“畫筆”
//給畫筆指定頂點(diǎn)數(shù)據(jù)
gl.glVertexPointer(3//坐標(biāo)個(gè)數(shù)xyz
, GL10.GL_FIXED//頂點(diǎn)的數(shù)據(jù)類型
, 0//間隔 默認(rèn)0
, verIntBuffer);///頂點(diǎn)數(shù)據(jù)
//給畫筆指定頂點(diǎn)顏色數(shù)據(jù)
gl.glColorPointer(4//顏色組成個(gè)數(shù)
, GL10.GL_FIXED//顏色的數(shù)據(jù)類型
, 0//間隔 默認(rèn)0
, colorIntBuffer);///頂點(diǎn)顏色數(shù)據(jù)
gl.glLineWidth(10);
// //繪制(索引法)
gl.glDrawElements(GL10.GL_LINES //繪制模型(點(diǎn)1 線段3 三角形3)
, 4//索引個(gè)數(shù)
, GL10.GL_UNSIGNED_BYTE//數(shù)據(jù)類型
, indbuffer);//索引數(shù)據(jù)
}
}
23419D9C-4246-4C60-945B-3DFADE14441D.png
/**
*
* @author qiaosen
* @date 2018/3/15
*/
// 三角形
public class Triangle extends OpenGLUtils {
private IntBuffer verIntBuffer, colorIntBuffer;
private ByteBuffer indbuffer;
public Triangle() {
init();
}
//初始化view
private void init() {
int UNIT_SIZE = 10000;//比例
//頂點(diǎn)數(shù)據(jù)(xyz)
int ver[] = new int[]{
-2 * UNIT_SIZE,
5 * UNIT_SIZE,
0,
1 * UNIT_SIZE,
6 * UNIT_SIZE,
0,
-8 * UNIT_SIZE,
-8 * UNIT_SIZE,
0,
};
//創(chuàng)建頂點(diǎn)緩沖
verIntBuffer = getIntBuffer(ver);
int one = 65536;//支持65536色彩通道
//頂點(diǎn)個(gè)數(shù)=顏色個(gè)數(shù)
//顏色數(shù)據(jù)(RGB A)
int color[] = new int[]{
one, 0, 0, 0, one, 0, 1, 0, one, 0, 1, 0
};
//創(chuàng)建頂點(diǎn)緩沖
colorIntBuffer = getIntBuffer(color);
}
public void drawSelf(GL10 gl) {
//啟用頂點(diǎn)數(shù)組坐標(biāo)
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
//啟用頂點(diǎn)顏色數(shù)組坐標(biāo)
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
//設(shè)置“畫筆”
//給畫筆指定頂點(diǎn)數(shù)據(jù)
gl.glVertexPointer(3//坐標(biāo)個(gè)數(shù)xyz
, GL10.GL_FIXED//頂點(diǎn)的數(shù)據(jù)類型
, 0//間隔 默認(rèn)0
, verIntBuffer);///頂點(diǎn)數(shù)據(jù)
//給畫筆指定頂點(diǎn)顏色數(shù)據(jù)
gl.glColorPointer(4//顏色組成個(gè)數(shù)
, GL10.GL_FIXED//顏色的數(shù)據(jù)類型
, 0//間隔 默認(rèn)0
, colorIntBuffer);///頂點(diǎn)顏色數(shù)據(jù)
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
}
}
06E27014-2E58-4818-9EB4-9796A58A6B7E.png
/**
*
* @author qiaosen
* @date 2018/3/15
*/
//多邊形
public class Polygon extends OpenGLUtils {
private FloatBuffer verBuffer;
private FloatBuffer colorBuffer;
private int verNum;
public Polygon() {
init();
}
//初始化
private void init() {
int length = 45;//邊長
//頂點(diǎn)個(gè)數(shù)
verNum = (360 / length + 2);
float ver[] = new float[verNum * 3];
float color[] = new float[verNum * 4];
//計(jì)數(shù)器
int count = 0;
int count2 = 0;
int one = 65536;//支持65536色彩通道
for (int i = 0; i < 360 + length; i += length) {
double di = Math.toRadians(i);
ver[count++] = (float) (Math.cos(di) - Math.sin(di));
ver[count++] = (float) (Math.cos(di) + Math.sin(di));
ver[count++] = 0;
color[count2++]= 0;
color[count2++]= one;
color[count2++]= one;
color[count2++]= one;
}
verBuffer = getFloatbuffer(ver);
colorBuffer = getFloatbuffer(color);
}
public void drawSelf(GL10 gl) {
//啟用頂點(diǎn)數(shù)組坐標(biāo)
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
//啟用頂點(diǎn)顏色數(shù)組坐標(biāo)
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
//設(shè)置“畫筆”
//給畫筆指定頂點(diǎn)數(shù)據(jù)
gl.glVertexPointer(3//坐標(biāo)個(gè)數(shù)xyz
, GL10.GL_FLOAT//頂點(diǎn)的數(shù)據(jù)類型
, 0//間隔 默認(rèn)0
, verBuffer);///頂點(diǎn)數(shù)據(jù)
//給畫筆指定頂點(diǎn)顏色數(shù)據(jù)
gl.glColorPointer(4//顏色組成個(gè)數(shù)
, GL10.GL_FLOAT//顏色的數(shù)據(jù)類型
, 0//間隔 默認(rèn)0
, colorBuffer);///頂點(diǎn)顏色數(shù)據(jù)
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, verNum);
}
}
B554564D-119D-44A9-B674-6BDE11E200DE.png
/**
* 繼承GLSurfaceView 初始化GLSurfaceView
* @author qiaosen
* @date 2018/3/16
*/
public class DGLView extends GLSurfaceView {
/**畫點(diǎn)**/
Points mPoints;
/**畫線**/
Lines mLines;
/**畫三角形**/
Triangle mTriangle;
/**畫多變形**/
Polygon mPolygon;
public DGLView(Context context) {
super(context);
init();
}
//初始化
private void init() {
// 渲染器
setRenderer(new DGLRender());
setRenderMode(RENDERMODE_CONTINUOUSLY);
// 渲染模式 2中
// RENDERMODE_CONTINUOUSLY 主動(dòng)渲染 一直刷新
// RENDERMODE_WHEN_DIRTY 被動(dòng)渲染 需要requestRender();刷新
}
private class DGLRender implements Renderer {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//關(guān)閉抗抖動(dòng) :(對(duì)于可用顏色較少的系統(tǒng) ,可以犧牲分辨率跃巡,通過顏色抖動(dòng)來增加顏色數(shù)量)
gl.glDisable(GL10.GL_DITHER);
//設(shè)置背景為黑色 RGBA
gl.glClearColor(0,0,0,0);
//設(shè)置hint模式 (快速模式)
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
//開啟深度測(cè)試(3D里面)
gl.glEnable(GL10.GL_DEPTH_TEST);
// mPoints = new Points();
// mLines = new Lines();
// mTriangle = new Triangle();
mPolygon = new Polygon();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
//設(shè)置視口
//x y 手機(jī)屏幕原點(diǎn)坐標(biāo)
//width height 視口大小
gl.glViewport(0,0,width,height);
//設(shè)置矩陣(投影矩陣)
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
//得到寬高比
float r = (float)width/height;
//設(shè)置視角
gl.glFrustumf(-r,r,-1,1,1,20);
}
@Override
public void onDrawFrame(GL10 gl) {
//清除顏色和深度緩存
gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
//設(shè)置矩陣((模型矩陣)
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0,0,-3.0f);
//繪制
/**畫點(diǎn)**/
// mPoints.drawSelf(gl);
/**畫線**/
// mLines.drawSelf(gl);
/**畫三角形**/
// mTriangle.drawSelf(gl);
/**畫多邊形**/
mPolygon.drawSelf(gl);
}
}
}
public class MainActivity extends AppCompatActivity {
DGLView mDGLView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDGLView = new DGLView(this);
setContentView(mDGLView);
}
@Override
protected void onResume() {
super.onResume();
mDGLView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mDGLView.onPause();
}
}