index buffer object를 그리기 위해 glDrawElements를 찾다가 stackoverflow에 좋은 글이 있었다.
stackoverflow.com/a/2949191 (Creative Commons BY-SA, Jonathan Hartley)
glDrawElements를 사용해보기 위해 아래 코드로 ㄷ자 모양을 그려보았다.(빈 공간이 -z 방향을 향하도록)
#includeGLfloat vertex[] = { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0 }; GLfloat color[] = { 0.3f, 0.4f, 0.2f, 0.3f, 0.4f, 0.2f, 0.3f, 0.4f, 0.2f, 0.3f, 0.4f, 0.2f, 0.3f, 0.4f, 0.2f, 0.3f, 0.4f, 0.2f, 0.3f, 0.4f, 0.2f, 0.3f, 0.4f, 0.2f }; GLfloat normal[] = { -1, 0, 0, 1, 0, 0, 0, 0, -1 }; GLubyte index[] = { 0, 4, 7, 3, 1, 2, 6, 5, 4, 5, 6, 7 }; void displayAxis(float t) { glBegin(GL_LINE_LOOP); glColor3f(t, 0.0, 0.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(t, 0.0, 0.0); glEnd(); glBegin(GL_LINE_LOOP); glColor3f(0.0, t, 0.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, t, 0.0); glEnd(); glBegin(GL_LINE_LOOP); glColor3f(0.0, 0.0, t); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, t); glEnd(); } void displayFaces() { glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(3, GL_FLOAT, 0, vertex); glColorPointer(3, GL_FLOAT, 0, color); glNormalPointer(3, 0, normal); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, index); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, &index[4]); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, &index[8]); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, &index[12]); } void display(void) { glMatrixMode(GL_MODELVIEW); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLineWidth(2.0); glPushMatrix(); gluLookAt(5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); displayAxis(5.0); displayFaces(); glPopMatrix(); glFlush(); } void reshape(int x, int y) { glViewport(0, 0, x, y); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(100.0, (GLdouble)x / (GLdouble)y, 5.0, 30.0); } void idle(void) { display(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(800, 600); glutCreateWindow("opengl"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutIdleFunc(idle); glutMainLoop(); return 0; }
'Computer Graphics, Geometry - 수업' 카테고리의 다른 글
collision detection library (0) | 2019.08.18 |
---|---|
Intersection test (0) | 2018.07.16 |
BVH, Collision Detection(GJK+Minkowski sum) (1) | 2018.06.29 |
homogeneous coordinates (0) | 2018.06.21 |