OpenGL绘制函数!
| 绘制方式 | 说明 |
|---|
| GL_POINTS点) | 绘制点 |
| GL_LINES(线段) | 连个点一组进行点的绘制,如果只有一个点就会舍弃这个点 |
| GL_LINES_STRIP(条带线) | 按照顶点顺序连接顶点 |
| GL_LINES_LOOP(循环线) | 按照顶点顺序连接顶点,最后一个点连接第一点 |
| GL_TRIANGLES(三角形) | 三个点一组,如果不够三个点就会舍弃 多余的点 |
| GL_TRIANGLE_STRIP(三角形带) | 顶点按照顺序依次 组成三角形绘制,最后实际形成的是一个三角型带 |
| GL_TRIANGLE_FAN(三角形扇面) | 将第一个点作为中心点,其他点作为边缘点,绘制一系列的组成扇形的三角形 |
/**
* 绘制点
* @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;
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
};
verIntBuffer = getIntBuffer(ver);
int one = 65536;
int color[] = new int[]{
one, 0, 0, 0,
one, 0, 0, 0,
one, 0, 0, 0,
one, 0, 0, 0
};
colorIntBuffer = getIntBuffer(color);
byte indices[] = new byte[]{
0,3,2
};
indbuffer = getByteBuffer(indices);
}
public void drawSelf(GL10 gl){
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glVertexPointer(3,
GL10.GL_FIXED,
0,
verIntBuffer);
gl.glColorPointer(4,
GL10.GL_FIXED,
0,
colorIntBuffer);
gl.glPointSize(10);
gl.glDrawElements(GL10.GL_POINTS,
3,
GL10.GL_UNSIGNED_BYTE,
indbuffer);
}
}

/**
*
* @author qiaosen
* @date 2018/3/15
*/
public class Lines extends OpenGLUtils {
private IntBuffer verIntBuffer, colorIntBuffer;
private ByteBuffer indbuffer;
public Lines() {
init();
}
private void init() {
int UNIT_SIZE = 10000;
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
};
verIntBuffer = getIntBuffer(ver);
int one = 65536;
int color[] = new int[]{
one, 0, 0, 0, one, 0, 0, 0, one, 0, 0, 0, one, 0, 0, 0
};
colorIntBuffer = getIntBuffer(color);
byte indices[] = new byte[]{
0, 3, 2, 1
};
indbuffer = getByteBuffer(indices);
}
public void drawSelf(GL10 gl) {
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glVertexPointer(3
, GL10.GL_FIXED
, 0
, verIntBuffer);
gl.glColorPointer(4
, GL10.GL_FIXED
, 0
, colorIntBuffer);
gl.glLineWidth(10);
gl.glDrawElements(GL10.GL_LINES
, 4
, GL10.GL_UNSIGNED_BYTE
, indbuffer);
}
}

/**
*
* @author qiaosen
* @date 2018/3/15
*/
public class Triangle extends OpenGLUtils {
private IntBuffer verIntBuffer, colorIntBuffer;
private ByteBuffer indbuffer;
public Triangle() {
init();
}
private void init() {
int UNIT_SIZE = 10000;
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,
};
verIntBuffer = getIntBuffer(ver);
int one = 65536;
int color[] = new int[]{
one, 0, 0, 0, one, 0, 1, 0, one, 0, 1, 0
};
colorIntBuffer = getIntBuffer(color);
}
public void drawSelf(GL10 gl) {
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glVertexPointer(3
, GL10.GL_FIXED
, 0
, verIntBuffer);
gl.glColorPointer(4
, GL10.GL_FIXED
, 0
, colorIntBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
}
}

/**
*
* @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;
verNum = (360 / length + 2);
float ver[] = new float[verNum * 3];
float color[] = new float[verNum * 4];
int count = 0;
int count2 = 0;
int one = 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) {
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glVertexPointer(3
, GL10.GL_FLOAT
, 0
, verBuffer);
gl.glColorPointer(4
, GL10.GL_FLOAT
, 0
, colorBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, verNum);
}
}

/**
* 继承GLSurfaceView 初始化GLSurfaceView
* @author qiaosen
* @date 2018/3/16
*/
public class DGLView extends GLSurfaceView {
/**画点**/
Points mPoints;
/**画线**/
Lines mLines;
/**画三角形**/
Triangle mTriangle;
/**画多变形**/
Polygon mPolygon;
public DGLView(Context context) {
super(context);
init();
}
private void init() {
setRenderer(new DGLRender());
setRenderMode(RENDERMODE_CONTINUOUSLY);
}
private class DGLRender implements Renderer {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glClearColor(0,0,0,0);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glEnable(GL10.GL_DEPTH_TEST);
mPolygon = new Polygon();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0,0,width,height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
float r = (float)width/height;
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);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0,0,-3.0f);
/**画点**/
/**画线**/
/**画三角形**/
/**画多边形**/
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();
}
}
demo下载地址