[OpenGL] 3. Objects

scarleter99·2023년 3월 30일
0

OpenGL

목록 보기
3/10

📌 Objects

  • 그래픽세계에서 컴퓨터는 가상으로 만든 3D 모델을 사용해 객체를 화면에 출력한다. (Synthetic Objects)
  • 3D 모델은 정점(Vertices)과 면(Faces)로 이루어져 있다.

Rendering Way

Polygon Soup

  • 정점데이터 복제하는 방식으로 3D 모델 데이터를 저장하는 모델이다.
  • 각 정점들이 복제된 만큼 메모리 사용량이 증가하지만, 참조를 하지않아 빠르다.
  • 각 모델의 정점은 반시계 방향 순으로 저장한다.

Vertex List & Polygons

  • 정점 데이터의 정보를 List로 관리하는 방식으로 3D 모델 데이터를 저장하는 모델이다.
  • 각 정점들을 사용할 때 List를 참조해서 사용해 메모리 사용량이 감소하지만, 느리다.
  • 각 모델의 정점은 반시계 방향 순으로 저장한다.

Triangle Meshes

  • 모든 Polygon Primitive를 삼각형으로만 저장하는 기법이다.
  • 속도가 향상된다.

Rendering Architectures

Vertex Arrays

  • 기존 CPU에 연결된 메모리에 Vertex 데이터를 저장하는 기법이다.
  • 매 프레임마다 GPU로 Vertex 데이터를 전송하기 때문에 느리지만, 관리가 편하다.

Sample Code

// buffers in client space
GLfloat position[] = { 0,0,0, 1,0,0, 1,1,0, 1,0,0, 2,1,0, 1,1,0 };
GLfloat color[] = { 1,0,0, 0,1,0, 0,0,1, 0,1,0, 0,0,0, 0,0,1 };

// attribute IDs in server space
GLint loc_a_position = glGetAttribLocation(program, "a_position");
GLint loc_a_color = glGetAttribLocation(program, "a_color");

// 버텍스 쉐이더의 attribute 중 a_position 부분 활성화
glEnableVertexAttribArray (loc_a_position);
// 현재 배열 버퍼에 있는 데이터를 버텍스 쉐이더 a_position에 해당하는 attribute와 연결
glVertexAttribPointer(loc_a_position, 3, GL_FLOAT, GL_FALSE, 0, position);

// 버텍스 쉐이더의 attribute 중 a_color 부분 활성화
glEnableVertexAttribArray (loc_a_color);
// 현재 배열 버퍼에 있는 데이터를 버텍스 쉐이더 a_color에 해당하는 attribute와 연결
glVertexAttribPointer(loc_a_color, 3, GL_FLOAT, GL_FALSE, 0, color);

// triangle soup으로 물체 그리기
glDrawArrays(GL_TRIANGLES, 0, 6);

// 정점 attribute 배열 비활성화
glDiableVertexAttribArray(loc_a_position);
glDisableVertexAttribArray(loc_a_color);

Vertex Buffer Objects(VBOs)

  • 처음 랜더링 시 Vertex 데이터를 모두 GPU 메모리로 전송하는 기법이다.
  • Vertex 데이터를 GPU가 관리하기 때문에 빠르지만, 관리가 불편하다.

Sample Code - Initialization

// buffers in client space
GLfloat position[] = { 0,0,0, 1,0,0, 1,1,0, 1,0,0, 2,1,0, 1,1,0 };
GLfloat color[] = { 1,0,0, 0,1,0, 0,0,1, 0,1,0, 0,0,0, 0,0,1 };

// buffer IDs in server space
GLuint position_buffer;
GLuint color_buffer;

// create a vertex buffer & trasfer vertices data from client space to server space
glGenBuffers(1, &position_buffer);
glBindBuffer(GL_ARRAY_BUFFER, position_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(position), position, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

// create an index buffer & trasfer vertices data from client space to server space
glGenBuffers(1, &color_buffer);
glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(color), color, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

Sample Code - Rendering

// 버텍스 쉐이더의 attribute 중 a_position 부분 활성화
glEnableVertexAttribArray(GL_ARRAY_BUFFER, loc_a_position);
// 앞으로 언급하는 배열 버퍼(GL_ARRAY_BUFFER)는 position_buffer로 지정
glBindBuffer(GL_ARRAY_BUFFER, position_buffer);
// 현재 배열 버퍼에 있는 데이터를 버텍스 쉐이더 a_position에 해당하는 attribute와 연결
glVertexAttribPointer(loc_a_position, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0);

// 버텍스 쉐이더의 attribute 중 a_color 부분 활성화
glEnableVertexAttribArray(GL_ARRAY_BUFFER, loc_a_color);
// 앞으로 언급하는 배열 버퍼(GL_ARRAY_BUFFER)는 color_buffer로 지정
glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
// 현재 배열 버퍼에 있는 데이터를 버텍스 쉐이더 a_color에 해당하는 attribute와 연결
glVertexAttribPointer(loc_a_color, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0);

// triangle soup으로 물체 그리기
glDrawArrays(GL_TRIANGLES, 0, 6); // 렌더링

// 정점 attribute 배열 비활성화
glDisableVertexAttribArray(GL_ARRAY_BUFFER, loc_a_position);
glDisableVertexAttribArray(loc_a_color);
profile
매일 공부

0개의 댓글