Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Eriq Muhammad Adams J | eriq.adams@ub.ac.id 03 |Primitives Eriq Muhammad Adams J | eriq.adams@ub.ac.id.

Presentasi serupa


Presentasi berjudul: "Eriq Muhammad Adams J | eriq.adams@ub.ac.id 03 |Primitives Eriq Muhammad Adams J | eriq.adams@ub.ac.id."— Transcript presentasi:

1 Eriq Muhammad Adams J | eriq.adams@ub.ac.id
03 |Primitives Eriq Muhammad Adams J |

2 Primitives Primitives merupakan geometri sederhana (basic) : titik (points), garis (lines), segiempat (quads), dan segitiga (triangles).

3 Handling Primitives Ada beberapa cara untuk me-render primitives :
Immediate mode (OpenGL 1.0) Vertex arrays (OpenGL 1.1) VBO (Vertex Buffer Objects) (OpenGL 1.5) : Recommended

4 Immediate Mode Menggunakan glBegin()/glEnd() : glBegin(Glenum mode)
// Valid glBegin()/glEnd() functions glEnd()

5 Immediate Mode (cont.) Parameter glBegin() menunjukkan tipe primitive.

6 Immediate Mode (cont.) Fungsi valid glBegin()/glEnd() :
glVertex*() Sets vertex coordinates glColor*() Sets the current color glSecondaryColor() Sets the secondary color glIndex*() Sets the current color index glNormal*() Sets the normal vector coordinates glTexCoord*() Sets the texture coordinates glMultiTexCoord*() Sets texture coordinates for multitexturing glFogCoord*() Sets the fog coordinate glArrayElement() Specifies attributes for a single vertex based on elements in a vertex array glEvalCoord*() Generates coordinates when rendering Bezier curves and surfaces

7 glVertex*() → glVertex{234}{dfis}{v}()
Immediate Mode (cont.) glEvalPoint*() Generates points when rendering Bezier curves and surfaces glMaterial*() Sets material properties (affect shading when OpenGL lighting is used) glEdgeFlag*() Controls the drawing of edges glCallList*() Executes a display list glCallLists*() Executes display lists glVertex*() → glVertex{234}{dfis}{v}()

8 Immediate Mode (cont.) Contoh : glBegin(GL_TRIANGLES);
glVertex3f(-1.0f, -0.5f, 0.0f); glVertex3f(1.0f, -0.5f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f); glEnd();

9 Vertex Arrays Immediate mode tidak efektif untuk model geometri yang kompleks (yang terdiri dari jutaan vertex). Vertex arrays lebih baik dari immediate mode karena : Mendefinisikan data geometri sekali saja. Menyederhanakan kode.

10 Vertex Arrays (cont.) Tahapan menggunakan vertex arrays :
Meng-enable vertex arrays Mendefinisikan posisi dan format data yang disimpan dalam array. Me-render menggunakan vertex array.

11 Vertex Arrays (cont.) Meng-enable dan men-disable vertex arrays menggunakan : glEnableClientState(GLenum cap); glDisableClientState(GLenum cap); Parameter glEnableClientState() dan glDisableClientState() diisi dengan array type flags

12 Vertex Arrays (cont.) Daftar array type flags :
GL_COLOR_ARRAY Enables an array containing primary color information for each vertex GL_EDGE_FLAG_ARRAY Enables an array containing edge flags for each vertex GL_INDEX_ARRAY Enables an array containing indices to a color palette for each vertex GL_NORMAL_ARRAY Enables an array containing the vertex normal for each vertex GL_TEXTURE_COORD_ARRAY Enables an array containing the texture coordinate for each vertex GL_VERTEX_ARRAY Enables an array containing the position of each vertex GL_SECONDARY_COLOR_ARRAY Enables an array containing secondary colors

13 Vertex Arrays (cont.) Mendefinisikan posisi dan format data yang disimpan dalam array menggunakan : gl*Pointer(). Menyimpan koordinat vertex : void glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *array); size : 2 (x,y), 3 (x,y,z), 4(x,y,z,w) type : GL_SHORT, GL_INT, GL_FLOAT, GL_DOUBLE array : array yang digunakan stride : jumlah padding (dalam bytes) Contoh : glVertexPointer(3, GL_FLOAT, 0, &myArray[0]);

14 Vertex Arrays (cont.) Menyimpan warna primer vertex :
void glColorPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *array); size : 3 (r,g,b), 4(r,g,b,a) type : GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT, GL_FLOAT, GL_DOUBLE. array : array yang digunakan stride : jumlah padding (dalam bytes) Contoh : glColorPointer(3, GL_FLOAT, 0, &myArray[0]);

15 Vertex Arrays (cont.) Menyimpan koordinat normal (vektor arah tegak lurus dengan permukaan geometri, digunakan dalam kalkulasi pencahayaan): void glNormalPointer(GLenum type, GLsizei stride, const GLvoid *array); type : GL_BYTE,GL_SHORT,GL_INT,GL_FLOAT, GL_DOUBLE. array : array yang digunakan stride : jumlah padding (dalam bytes) Contoh : glNormalPointer(GL_FLOAT, 0, &myArray[0]);

16 Vertex Arrays (cont.) Menyimpan indeks warna palette mode display :
void glIndexPointer(Glenum type, GLsizei stride, const GLvoid *array); type : GL_SHORT,GL_INT,GL_FLOAT, GL_DOUBLE. array : array yang digunakan stride : jumlah padding (dalam bytes) Contoh : glIndexPointer(GL_FLOAT, 0, &myArray[0]);

17 Vertex Arrays (cont.) Menyimpan edge flags (dapat digunakan untuk menyembunyikan edge dari polygon ketika rendering dalam wireframe mode) : void glEdgeFlagPointer(GLsizei stride, const GLboolean *array); array : array bernilai boolean yang digunakan stride : jumlah padding (dalam bytes) Contoh : glEdgeFlagPointer(0, &myArray[0]);

18 Vertex Arrays (cont.) Menyimpan koordinat tekstur :
void glTexCoordPointer(GLint size, GLenum type, GLsizei stride, const Glvoid *array); size : jumlah koordinat per vertex (1,2,3,4) type : GL_SHORT,GL_INT, GL_FLOAT, GL_DOUBLE. array : array bernilai boolean yang digunakan stride : jumlah padding (dalam bytes) Contoh : glTexCoordPointer(2, GL_FLOAT, 0,&myArray[0]);

19 Vertex Arrays (cont.) Menyimpan warna sekunder :
void glSecondaryColorPointer(GLint size, GLenum type, GLsizei stride, const GLvoid * array); size : 3 (r,g,b) type : GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT, GL_FLOAT, GL_DOUBLE. array : array yang digunakan stride : jumlah padding (dalam bytes) Contoh : glSecondaryColorPointer(3, GL_FLOAT, 0, &myArray[0]);

20 Vertex Arrays (cont.) Ilustrasi penyimpanan menggunakan vertex arrays :

21 Vertex Arrays (cont.) Me-render menggunakan vertex array dapat menggunakan : glDrawArrays(). glDrawElements().

22 Vertex Arrays (cont.) glDrawArrays() : digunakan untuk me-render beberapa primitives sekaligus. void glDrawArrays(GLenum mode, GLint first, GLsizei count); : mode : tipe primitive first : index awal array yang akan di-render count : jumlah elemen yang akan di-render Contoh (menggambar triangles dengan 3 vertex) : -2.0f 0.0f 2.0f //Enable the vertex array glEnableClientState(GL_VERTEX_ARRAY); //Tell OpenGL where the vertices are glVertexPointer(3, GL_FLOAT, 0, &m_vertices[0]); //Draw the triangle, starting from vertex index zero glDrawArrays(GL_TRIANGLES, 0, 3); //Finally disable the vertex array glDisableClientState(GL_VERTEX_ARRAY);

23 Vertex Arrays (cont.) glDrawElements : digunakan untuk me-render (dan dapat berbagi vertices) beberapa primitives sekaligus. void glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices); : mode : tipe primitive type : tipe data array count : jumlah indeks elemen yang akan di-render indices : array untuk menyimpan indeks elemen

24 Vertex Arrays (cont.) Contoh menggambar persegi dengan 2 triangles :
m_vertices Vertex-0 Vertex-1 Vertex-2 Vertex-3 -2.0f 0.0f 2.0f m_Indices Triangle-0 Triangle-1 Vertex-0 Vertex-1 Vertex-3 Vertex-2

25 Vertex Arrays (cont.) //Enable the vertex array
glEnableClientState(GL_VERTEX_ARRAY); //Tell OpenGL where the vertices are glVertexPointer(3, GL_FLOAT, 0, &m_vertices[0]); //Draw the triangles, we pass in the number of indices, the data type of //the index array (GL_UNSIGNED_INT) and then the //pointer to the start of the array glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, &m_indices[0]); //Finally disable the vertex array glDisableClientState(GL_VERTEX_ARRAY);

26 Vertex Arrays (cont.) Ilustrasi : 2 3 Triangle-1 Triangle-0 1

27 Vertex Buffer Objects Vertex array efektif daripada immediate mode tetapi data disimpan dalam RAM (memori sistem) dan harus dikirimkan secara kontinyu ke GPU, dengan VBO (Vertex Buffer Objects) data dapat disimpan dalam VRAM (memori kartu grafis).

28 Vertex Buffer Objects (cont.)
Langkah-langkah menggunakan VBO : Meng-generate nama buffer. Meng-aktivasi (bind) buffer. Menyimpan data dalam buffer. Menggunakan buffer untuk me-render data. Menghapus (destroy) buffer.

29 Vertex Buffer Objects (cont.)
Meng-generate nama buffer dan menghapus buffer menggunakan glGenBuffers() dan glDeleteBuffers(). void glGenBuffers(GLsizei n, GLuint *buffers); void glDeleteBuffers(GLsizei n, const GLuint *buffers); buffers : pointer ke sebuah variabel atau array yang menyimpan sejumlah n nama buffer. n : jumlah nama buffer GLuint bufferID; //Generate the name and store it in bufferID glGenBuffers(1, &bufferID); // Do some initialization and rendering with the buffer // Release the name glDeleteBuffers(1, &bufferID);

30 Vertex Buffer Objects (cont.)
Meng-aktivasi (bind) buffer menggunakan glBindBuffer(). void glBindBuffer(GLenum target, GLuint buffer); target : GL_ARRAY_BUFFER : untuk per-vertex data (positions, colors, normals, etc.), GL_ELEMENT_ARRAY_BUFFER : untuk menyimpan indeks vertex, GL_PIXEL_PACK_BUFFER dan GL_PIXEL_UNPACK_BUFFER : untuk menyimpan pixel data. buffer : nama buffer yang di-generate sebelumnya, jika 0 akan unbind terhadap semua buffer. glBindBuffer(GL_ARRAY_BUFFER, bufferID);

31 Vertex Buffer Objects (cont.)
Menyimpan data dalam buffer menggunakan glBufferData(). void glBufferData(GLenum target, GLsizeiptr size, const GLvoid *data, Glenum usage); target : GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER, GL_PIXEL_PACK_BUFFER, GL_PIXEL_UNPACK_BUFFER size : ukuran dari vertex array dalam bytes data : pointer ke data yang akan disalin usage : penggunaan dari buffer, GL_{Buffer Frequency Values}_{Buffer Access Values} : GL_STREAM_DRAW, GL_STREAM_READ, GL_STREAM_COPY, GL_STATIC_DRAW, GL_STATIC_READ, GL_STATIC_COPY, GL_DYNAMIC_DRAW, GL_DYNAMIC_READ,or GL_DYNAMIC_COPY

32 Vertex Buffer Objects (cont.)
Buffer frequency values STREAM The data will be modified only once, and accessed only a few times. STATIC The data will be altered once and accessed multiple times (this hint is good for static geometry). DYNAMIC The buffer will be modified a lot and accessed many times (this is suitable for animated models).

33 Vertex Buffer Objects (cont.)
Buffer access values DRAW The contents of the buffer will be altered by the application and will be used for rendering using OpenGL. READ The contents will be filled by OpenGL and then subsequently read by the application. COPY The contents will be modified by OpenGL and then later used by OpenGL as the source for rendering. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3, &vertex[0], GL_STATIC_DRAW);

34 Vertex Buffer Objects (cont.)
Menggunakan buffer dan vertex array untuk me-render data. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,m_indexBuffer); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)); glDrawElements( GL_TRIANGLES, // mode m_indices_copy.size(), // count GL_UNSIGNED_INT, // type BUFFER_OFFSET(0) // element array buffer offset ); glDisableClientState(GL_VERTEX_ARRAY);

35 Vertex Buffer Objects (cont.)
//macro BUFFER_OFFSET mencegah compiler warning ketika memasukkan nilai integer sebagai parameter array. #define BUFFER_OFFSET(i) ((char *)NULL + (i)) i= offset dalam bytes

36 Hello Primitive !


Download ppt "Eriq Muhammad Adams J | eriq.adams@ub.ac.id 03 |Primitives Eriq Muhammad Adams J | eriq.adams@ub.ac.id."

Presentasi serupa


Iklan oleh Google