//使用上面的头文件是不行的,需要GLUT的窗口框架,win7 64只能用GL/glut.h
#include <GL/freeglut.h>
#include <stdio.h>
//设置背景颜色和坐标系统
void init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 400.0, 0.0, 550.0);
//gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
typedef struct S2DFPoint//用矩阵比用结构体或类的方式易于初始化
{
GLfloat p[2];
}TwoDFPoint;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
int point1[] = { 10, 10 };
int point2[] = { 350, 530 };
int point3[] = { 380, 40 };
int point4[] = { 160, 500 };
int point5[] = { 20, 350 };
::glBegin(GL_LINES);
::glBegin(GL_LINE_STRIP);
glBegin(GL_LINE_LOOP);
glVertex2iv(point1);
glVertex2iv(point2);
glVertex2iv(point3);
glVertex2iv(point4);
glVertex2iv(point5);
glEnd();
glBegin(GL_TRIANGLES);
glutSolidCube(1.0f);
glEnd();
glFlush();
}
void reshapeWnd(int w, int h)
{
printf("reshapeWnd, w:%d, h:%d",w, h);
}
void keyEvent(unsigned char key, int x, int y)
{
printf("keyEvent, key:%c, x:%d, y:%d", key, x, y);
}
void mouseEvent(int button, int state, int x, int y)
{
printf("mouseEvent, button:%d, state:%d, x:%d, y:%d", button, state, x, y);
}
void dragEvent(int x, int y)
{
printf("dragEvent, x:%d, y:%d", x, y);
}
void loopIdle()
{
printf("loopIdle");
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
//初始化显卡模式
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(900, 550);
//指定使用的Opengl版本号
glutInitContextVersion(3, 1);
glutInitContextFlags(GLUT_CORE_PROFILE); //GLUT_COMPATIBILITY_PROFILE
glutCreateWindow("Hello OpenGL program I am Jeese!");
init();
/* glutMainLoop中每帧重绘的回调函数注册,如果注册了新的函数,那么需要用glutPostRedisplay()来通知glutMainLoop
使用新的回调函数。*/
glutDisplayFunc(display);
glutReshapeFunc(reshapeWnd);
glutKeyboardFunc(keyEvent);
glutMouseFunc(mouseEvent);
glutMotionFunc(dragEvent);
glutIdleFunc(NULL/*loopIdle*/);
glutMainLoop();
}
//#include <GL/glut.h>
////本来OpenGL程序一般还要包含<GL/gl.h>和<GL/glu.h>,但GLUT的头文件中已经自动将这两个文件包含了,不必再次包含。
//#pragma comment(lib,"opengl32.lib")
//#pragma comment(lib, "glu32.lib")
//#pragma comment(lib, "glut32.lib")
//
//void myDisplay(void)
//{
// glClear(GL_COLOR_BUFFER_BIT);
// glColor3f(1, 0, 0);
// glRectf(-0.3f, -0.3f, 0.3f, 0.3f);
// glColor3f(0, 1, 0);
// glRectf(-0.3f, -0.5f, 0.3f, -0.3f);
// glFlush();
//}
//int main(int argc, char *argv[])
//{
// glutInit(&argc, argv);
// // GLUT_RGB表示使用RGB颜色,GLUT_INDEX(表示使用索引颜色)
// // GLUT_SINGLE表示使用单缓冲,与之对应的还有GLUT_DOUBLE(使用双缓冲)
// glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
// glutInitWindowPosition(100, 100);
// glutInitWindowSize(960, 640);
// glutCreateWindow("第一个OpenGL程序");
// // 设置绘制回调函数
// glutDisplayFunc(&myDisplay);
// // 启动整个Opengl状态机,进入绘制
// glutMainLoop();
// return 0;
//}