YeJin's Footsteps

OpenGL 기초 (실습) 본문

Computer Science & Engineering/OpenGL

OpenGL 기초 (실습)

YeJinii 2021. 4. 5. 00:18

1. First Example

2. Teapot Example

3. Teapot Example 2 (My favorite color)

#include <glut.h>

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);//화면을 지우고
    glColor3f(0.0, 0.9, 0.9);//민트색
    glutSolidTeapot(0.5);//솔리드 티팟
    glColor3f(1, 1, 1);//하얀색
    glutWireTeapot(0.5);//와이어 티팟

    glFlush();
}
void main(int argc, char**argv)
{
   
    glutCreateWindow("Teapot");
    glClearColor(0.0, 0.0, 0.0, 0.0);//검은 배경
    glutDisplayFunc(display);
    glutMainLoop();
}

 

4. Point Example

#include <glut.h>
#include <math.h>
#define Pi 3.141592

void display(void) {
    GLfloat size[2];
    GLfloat angle;
    glClear(GL_COLOR_BUFFER_BIT);
    /**                제일 중요한 부분                   **/
    glColor3f(1.0, 1.0, 1.0);
    glGetFloatv(GL_POINT_SIZE_RANGE, size);
    glPointSize(size[0]);//하드웨어가 서포트 할수있는 가장 작은 점의 크기(size[1]은 가장 큰 점)
    //size[0]+((size[1]-size[0])/2.0) -> 그 중간 값. 원이 이쁘게 나옴
    glBegin(GL_POINTS);//점들을 계속 지정
    for (angle = 0.0; angle <= 2.0 * Pi; angle += Pi / 30.0) //0부터 360도까지 6도 간격으로 
        glVertex3f(0.5 * cos(angle), 0.5 * sin(angle), 0.0);
    glEnd();
    /*-----------------------------------------------------*/
    glFlush();
}
void main(int argc, char**argv)
{
   
    glutCreateWindow("Point Example");
    glutDisplayFunc(display);
    glutMainLoop();
}

5. Line Example

#include <glut.h>

void myDisplay() {
	glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_LINES);
		glLineWidth(5.0);
		glColor3f(0.0, 0.9, 0.9);//민트색에서
		glVertex3f(1.0, 0.0, 0.0);
		glColor3f(1.0, 1.0, 1.0);//하얀색으로 그라데이션
		glVertex3f(-1.0, 0.0, 0.0);
	glEnd();
	glFlush();
}

void main(int argc, char** argv) {
	glutCreateWindow("Line Example");
	glutDisplayFunc(myDisplay);
	glutMainLoop();
}

6. Triangle Example

#include <glut.h>

void myDisplay() {
	glClear(GL_COLOR_BUFFER_BIT);
	glShadeModel(GL_SMOOTH);//그라데이션으로 다각형을 채움
	glBegin(GL_TRIANGLES);
		glColor3f(0.0, 0.9, 0.9);
		glVertex3f(0.0, 0.5, 0.0);
		glColor3f(0.9, 0.9, 0.0);
		glVertex3f(-0.5, -0.5, 0.0);
		glColor3f(0.9, 0.0, 0.9);
		glVertex3f(0.5, -0.5, 0.0);
	glEnd();
	glFlush();
}

void main(int argc, char** argv) {
	glutCreateWindow("Triangle Example");
	glutDisplayFunc(myDisplay);
	glutMainLoop();
}

FLAT으로 하면 맨 마지막 색상으로 단일 색상 처리 됨

 

'Computer Science & Engineering > OpenGL' 카테고리의 다른 글

OpenGL 응용1  (0) 2021.04.10
Comments