偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

如何使用OpenGL繪制更復雜的形狀

移動開發(fā) Android
在OpenGL ES視圖中定義所要繪制的圖形,是創(chuàng)建高質量圖形的第一步。使用OpenGL ES繪制圖形時,如果不了解怎樣基于OpenGL ES定義圖形對象,將會是一件棘手的事。本文將會為大家介紹以Android設備屏幕為基準的OpenGL ES坐標系統(tǒng),定義圖形的基本方法,圖形輪廓,以及定義三角形、方形。

原文鏈接:http://docs.eoeandroid.com/training/graphics/opengl/shapes.html

  在OpenGL ES視圖中定義所要繪制的圖形,是創(chuàng)建高質量圖形的第一步。使用OpenGL ES繪制圖形時,如果不了解怎樣基于OpenGL ES定義圖形對象,將會是一件棘手的事。 這節(jié)課將介紹以Android設備屏幕為基準的OpenGL ES坐標系統(tǒng),定義圖形的基本方法,圖形輪廓,以及定義三角形、方形。

定義三角形 Define a Triangle


  OpenGL ES允許可以在三維坐標上定義你要繪制的對象。所以,在繪制三角形前,你要定義好它的坐標。在OpenGL中,定義坐標最典型的方法,就是定義坐標定點的一組浮點型數(shù)據。為了提高效率,你可以把這些坐標值寫進一組ByteBuffer,它將會傳遞給OpenGL ES圖形管道進行處理。

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Triangle {

    private FloatBuffer vertexBuffer;

    // number of coordinates per vertex in this array
    static final int COORDS_PER_VERTEX = 3;
    static float triangleCoords[] = { // in counterclockwise order:
         0.0f,  0.622008459f, 0.0f,   // top
        -0.5f, -0.311004243f, 0.0f,   // bottom left
         0.5f, -0.311004243f, 0.0f    // bottom right
    };

    // Set color with red, green, blue and alpha (opacity) values
    float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };

    public Triangle() {
        // initialize vertex byte buffer for shape coordinates
        ByteBuffer bb = ByteBuffer.allocateDirect(
                // (number of coordinate values *  4 bytes per float)
                triangleCoords.length *  4);
        // use the device hardware's native byte order
        bb.order(ByteOrder.nativeOrder());

        // create a floating point buffer from the ByteBuffer
        vertexBuffer = bb.asFloatBuffer();
        // add the coordinates to the FloatBuffer
        vertexBuffer.put(triangleCoords);
        // set the buffer to read the first coordinate
        vertexBuffer.position(0);
    }
    }

 

  OpenGL ES定義了以下的默認坐標系統(tǒng):[0,0,0] (X,Y,Z)作為GLSurfaceView圖像的中點,[1,1,0]是圖像的右上角頂點,[-1,-1,0]是左下角頂點。如果需要該坐標系統(tǒng)的圖片,請移步OpenGL ES開發(fā)指南。 請注意,圖形的坐標是按逆時針方向定義的,繪制的順序是非常重要的,因為它定義圖形的正面以及反面,正面可以被直接繪制,而反面你可能選擇以OpenGL ES消除面方法使其不被繪制出來。想要獲取更多關于面與消除的信息,請查看OpenGL ES開發(fā)指南。

定義方形 Define a Square

  在OpenGL中,定義三角形是非常簡單的,但你是否想要來點高難度的?比如,方形?要定義方形,有很多種方法,其中典型的方法就是把兩個三角形畫在一起:

圖1.使用兩個三角形繪制方形

  同樣,你需要按照逆時針方向定義代表方形的兩個三角形的坐標頂點,并把值寫到ByteBuffer。為了避免每個三角形都定義坐標產生兩種坐標系統(tǒng),使用繪制列表告訴OpenGL ES圖像管道如何繪制這些頂點,下面是該種形狀繪制方法的代碼:

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Square {

    private FloatBuffer vertexBuffer;
    private ShortBuffer drawListBuffer;

    // number of coordinates per vertex in this array
    static final int COORDS_PER_VERTEX = 3;
    static float squareCoords[] = { -0.5f,  0.5f, 0.0f,   // top left
                                    -0.5f, -0.5f, 0.0f,   // bottom left
                                     0.5f, -0.5f, 0.0f,   // bottom right
                                     0.5f,  0.5f, 0.0f }; // top right

    private short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices

    public Square() {
        // initialize vertex byte buffer for shape coordinates
        ByteBuffer bb = ByteBuffer.allocateDirect(
        // (# of coordinate values *  4 bytes per float)
                squareCoords.length *  4);
        bb.order(ByteOrder.nativeOrder());
        vertexBuffer = bb.asFloatBuffer();
        vertexBuffer.put(squareCoords);
        vertexBuffer.position(0);

        // initialize byte buffer for the draw list
        ByteBuffer dlb = ByteBuffer.allocateDirect(
        // (# of coordinate values *  2 bytes per short)
                drawOrder.length *  2);
        dlb.order(ByteOrder.nativeOrder());
        drawListBuffer = dlb.asShortBuffer();
        drawListBuffer.put(drawOrder);
        drawListBuffer.position(0);
    }
    }

 

  這個例子給你展示如何使用OpenGL繪制更復雜的形狀。一般來說,都是使用好幾個三角形來繪制圖形對象。在下節(jié)課,你將學習如何把這些圖像畫在屏幕上。

責任編輯:佚名 來源: eoe
相關推薦

2013-04-26 10:26:08

2017-05-02 13:38:51

CSS繪制形狀

2011-10-10 13:21:12

架構

2014-04-29 14:27:59

OpenGL ES 2Android繪制紋理

2010-03-12 19:03:48

Python 拼寫檢查

2013-01-06 09:26:06

Wi-Fi網絡協(xié)議

2013-06-27 09:30:48

2022-04-26 13:54:31

隱私幣網絡犯罪分子財務領導者

2013-09-26 14:09:31

iOS開發(fā)OpenGL ES教程繪制矩形

2023-11-03 08:28:19

2022-06-06 10:44:10

C++語言鴻蒙

2019-01-23 07:41:27

私有云企業(yè)虛擬化

2020-10-26 10:51:09

人工智能AI語言

2015-10-13 09:24:24

Chrome開發(fā)者工具

2021-01-11 08:34:16

緩存穿透QPS

2012-04-20 13:56:16

2019-08-20 11:57:47

云計算開發(fā)安全

2023-04-07 14:04:52

增強分析人工智能

2018-12-17 10:30:34

Linux命令行boxes

2025-09-05 01:25:00

CSSHTML元素clip
點贊
收藏

51CTO技術棧公眾號