OpenGL入門学習(一)

1474 ワード

初日にOpenGLを勉強して、
以下のプログラムは、点、直線、円などを描くために使用されます.
#include <GL/glut.h>
#include <math.h>

const int n = 20;
const GLfloat R = 0.5f;
const GLfloat Pi = 3.1415926536f;

void myDisplay(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
	glFlush();
}

// The part is used to draw point.
void myDisplay1(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glPointSize(5.0f);
	glBegin(GL_POINTS);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(0.5f, 0.0f);
	glEnd();
	glFlush();
}

//The part is used to draw lines
void myDisplay2(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	// The function is used to set the size of line.
	glLineWidth(2.0f);
	// Start the mode od stipple.
	glEnable(GL_LINE_STIPPLE);
	// The function is used to set the pattern of line.
	glLineStipple(2,0x0F0F);
	glBegin(GL_LINES);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(0.5f, 0.0f);
	glEnd();
	glFlush();
}

//This part is used to draw circle.
void myDisplay3(void)
{
	int i;
	glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_POLYGON);
	for(i = 0; i < n; i++)
	{
		glVertex2f(R*cos(2*Pi/n*i),R*sin(2*Pi/n*i));

	}
	glEnd();
	glFlush();
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(400,400);
	glutCreateWindow("The first OpenGL program");
	glutDisplayFunc(&myDisplay2);
	glutMainLoop();

	return 0;
}

勉強する本は『OpenGL入門学習』です.コードもここから