#include <OpenGL/glu.h>
static GLfloat spin = 0.0;
#define PI 3.1415926
GLint circle_points = 100;
static GLint vertices[] =
{
100, 200, 0,
100, 100, 0,
200, 100, 0,
200, 200, 0
};
static GLfloat colors[] =
{
1.0, 0.2, 0.2, 0.2, 0.2, 1.0, 0.8, 1.0, 0.2,
0.75, 0.75, 0.75,
0.35, 0.35, 0.35,
0.5, 0.5, 0.5
};
static GLubyte allIndices[] = { 0, 1, 2, 0, 2, 3};
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
// a. 启用或叫激活数组, 有8种同类要启用的数组GL_COLOR_ARRAY,GL_VERTEX_ARRAY
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
// b. 把数据放入到数组中
glColorPointer(3, GL_FLOAT, 0, colors);
glVertexPointer(3, GL_INT, 0, vertices);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
// c. 用这些数据绘制几何图形, 这句话的调用会导致客户端的数据发送到了服务端
//glDrawArrays (GL_TRIANGLE_FAN, 0, 6); // 是仅仅按照数组的顺序进行访问,索引的控制可以更灵活
glDrawElements(GL_TRIANGLE_FAN, 24, GL_UNSIGNED_BYTE, allIndices);
//glRotatef(spin, 0.0, 0.0, 1.0);
//glColor3f(1.0, 1.0, 0.0);
//glRectf(-25.0, -25.0, 25.0, 25.0);
//glutWireCube(20);
//glCirclefN();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w , (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (0, (GLdouble) w, 0.0, (GLdouble) h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
break;
case GLUT_MIDDLE_BUTTON:
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
}
OpenGL使用顶点数组进行图形的绘制
最新推荐文章于 2025-02-24 00:27:51 发布