OpenGL

OpenGL: Index of Vertex Array

FunnyPR 2014. 5. 4. 17:25

 OpenGL: Index of Vertex Array


요번에는 꼭지점배열과 인덱스의 관계를 알아 보려 한다. 


꼭지점 인덱스 배열사용 이유

 - 메모리 절약과 불필요한 변환을 줄이는 효과


꼭지점과 법선벡터를 배열 선언하여 사용할 경우 

- 다시 계산하지 않고 재사용하게 되기 때문에 메모리 양을 줄일 수 있고, 버텍스에 대한 변환도 줄일 수 있다.(시간 절약).

- 다음의 사각형을 Vertex와 Index 배열을 사용해서 그려보장




http://www.anandtech.com/show/391/6



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
GLfloat RectVertex[]={
    -20.0f,  20.0f,  20.0f,//0
     20.0f,  20.0f,  20.0f,//1
     20.0f, -20.0f,  20.0f,//2
    -20.0f, -20.0f,  20.0f,//3
    -20.0f,  20.0f, -20.0f,//4
     20.0f,  20.0f, -20.0f,//5
     20.0f, -20.0f, -20.0f,//6
    -20.0f, -20.0f, -20.0f //7
};
 
GLubyte RectIndexes[]={
    0,1,2,3,//Front
    4,5,1,0,//Top
    3,2,6,7,//Bottom
    5,4,7,6,//Back
    1,5,6,2,//Right
    4,0,3,7 //Left
};


우선 사각형의 vertex 배열을 만들고 나서 각 인덱스 배열을 선언

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
void C3DScatterPlot::RenderScene(void)
{
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
    glMatrixMode(GL_MODELVIEW);
 
    glPushMatrix();
            glTranslatef(0.0f, 0.0f, -350.0f);
            glRotatef(xRot, 1.0f, 0.0f, 0.0f); //x 
            glRotatef(yRot, 0.0f, 1.0f, 0.0f); //y
 
    //배열의 사용
    glEnableClientState(GL_VERTEX_ARRAY);
 
    glColor3f(0.0f, 1.0f, 0.0f); //색상
 
    //데이터 위치 
    glVertexPointer(3, GL_FLOAT, 0, RectVertex);
 
    //드로잉
    glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, RectIndexes);
 
    glPopMatrix();
}


glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);// 와이어 프레임으로 드로잉

인덱스 값은 바이트 포멧을 가진다. 

glVertexPointer(3, GL_FLOAT, 0, RectVertex); //버텍스 배열에서 3개씩 묵음으로 위치를 가져온다. 

glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, RectIndexes);//

void glDrawElements(

GLenum   mode, //Specifies what kind of primitives to render

GLsizei   count, //Specifies the number of elements to be rendered.

GLenum   type,   //Specifies the type of the values in indices.

const GLvoid *   indices // Specifies a pointer to the location where the indices are stored.

);

여기서 모드는 다음과 같은 심볼을 써야 함

GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, 

GL_TRIANGLES, GL_QUAD_STRIP, GL_QUADS, and GL_POLYGON

type값은 다음과 같은 심볼을 써야 함

GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT.

http://www.opengl.org/sdk/docs/man2/xhtml/glDrawElements.xml