1,下载最新的glut动态库,1998年发布,并且不再更新: https://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip
2,编译openglDemo源码:参见命令行:cl.exe openTran.cpp /EHsc /I"./" glut32.lib /link /LIBPATH:"./GL"
3,程序效果:
4,源码示例:
#include <math.h> #include "GL/glut.h" #pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" ) void init(void){ glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_SMOOTH); } void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); //glTranslatef(0.8, 0.0, 0.0); glBegin(GL_TRIANGLES); glColor3f(1.0, 0.0, 0.0); glVertex2f(-0.5, -0.5); glColor3f(0.0, 1.0, 0.0); glVertex2f(0.5, -0.5); glColor3f(0.0, 0.0, 1.0); glVertex2f(0.5, 0.5); glEnd(); float PI=3.1415926f; glBegin(GL_LINE_STRIP); for(float x=-5*PI;x<5*PI;x+=0.01f) { //glVertex2f(x/(5*PI),sin(x)); glColor3f(0.0, 1.0, 0.0); glVertex2f(x/(5*PI),sin(x)); glColor3f(0.0, 1.0, 0.0); glVertex2f(x/(5*PI), 0); } glEnd(); glFlush(); } void myReshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) gluOrtho2D(-1.0, 1.5, -1.5, 1.5*(GLfloat)h / (GLfloat)w); else gluOrtho2D(-1.0, 1.5*(GLfloat)w / (GLfloat)h, -1.5, 1.5); glMatrixMode(GL_MODELVIEW); } int main(int argc, char ** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(200, 200); glutInitWindowSize(800, 800); glutCreateWindow("triangle"); init(); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutMainLoop(); return 0; }
//#include <GL/glut.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <OpenGL/gl.h> #include <OpenGL/glu.h> #include <GLUT/glut.h> // gcc -framework OpenGL -framework GLUT -framework Foundation -o opengl opengl.c #define __LOG__ #ifdef __LOG__ #define log(format,...) printf("LOG:[%s][%03d][%s][%s][%s]"format"\n", __FILE__, __LINE__, __DATE__, __TIME__, __FUNCTION__, ##__VA_ARGS__) #else #define log(format,...) #endif void init(void) { glClearColor(0.0, 0.0, 0.0, 0.0);//设置背景颜色为黑色 glShadeModel(GL_SMOOTH);//设置为光滑明暗模式 } void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT);// 将缓存清除为预先的设置值,即黑色 //glTranslatef(0.8, 0.0, 0.0);//平移函数,暂时可以不用 glBegin(GL_TRIANGLES);//开始画三角形 glColor3f(1.0, 0.0, 0.0);//设置第一个顶点为红色 glVertex2f(-1.0, -1.0);//设置第一个顶点的坐标 glColor3f(0.0, 1.0, 0.0);//设置第二个顶点为绿色 glVertex2f(0.0, -1.0);//设置第二个顶点的坐标 glColor3f(0.0, 0.0, 1.0);//设置第三个顶点为蓝色 glVertex2f(-0.5, 1.0);//设置第三个顶点的坐标 glEnd();//三角形结束 glFlush();//强制OpenGL函数在有限时间内运行 } void myReshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h);//设置视口 glMatrixMode(GL_PROJECTION);//指明当前矩阵为GL_PROJECTION glLoadIdentity();//将当前矩阵置换为单位阵 if (w <= h) gluOrtho2D(-1.0, 1.5, -1.5, 1.5*(GLfloat)h / (GLfloat)w);//定义二维正视投影矩阵 else gluOrtho2D(-1.0, 1.5*(GLfloat)w / (GLfloat)h, -1.5, 1.5); glMatrixMode(GL_MODELVIEW);//指明当前矩阵为GL_MODELVIEW } void myKeyFunc (unsigned char key, int x, int y) { printf("myKeyFunc key = char[%c] [0x%x]\n", key, key); //fflush(stdout); switch (key) { case 27: exit(0); break; default: break; } } int main(int argc, char ** argv) { /*初始化*/ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//单缓冲、RGB模式 glutInitWindowSize(960, 540); glutInitWindowPosition(200, 200); glutCreateWindow("GL_TRIANGLES");//窗口标题 init(); printf("GL_VERSION %s\n", (char *)glGetString(GL_VERSION)); printf("GL_VENDOR %s\n", (char *)glGetString(GL_VENDOR)); printf("GL_RENDERER %s\n", (char *)glGetString(GL_RENDERER)); printf("GL_EXTENSIONS %.64s\n", (char *)glGetString(GL_EXTENSIONS)); /*绘制与显示*/ glutReshapeFunc(myReshape);//窗口大小发生改变时采取的行为 glutDisplayFunc(myDisplay);//显示绘制图形 glutKeyboardFunc(myKeyFunc); glutMainLoop();//循环 return(0); }
#include <stdio.h> #include <stdlib.h> // #include <GL/glut.h> #include <OpenGL/gl.h> #include <OpenGL/glu.h> #include <GLUT/glut.h> #define Width 64 #define Height 64 GLubyte image[Height][Width][3];//rgb GLint height; void image_create()//在内存中创建一幅棋盘图像的矩阵 { int c; for(int i=0;i<Height;i++) { for(int j=0;j<Width;j++) { c=(((i&0x8)==0)^((j&0x8)==0))*255; image[i][j][0]=(GLubyte)c; image[i][j][1]=(GLubyte)c; image[i][j][2]=(GLubyte)c; } } } void setup_image() { glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glShadeModel(GL_FLAT); image_create(); glPixelStorei(GL_UNPACK_ALIGNMENT,1);//边界对齐 } void image_motion(int x,int y) { glRasterPos2i(x,y); glPixelZoom(2.0,2.0); glCopyPixels(0,0,Width,Height,GL_COLOR); glFlush(); } void image_display() { glClear(GL_COLOR_BUFFER_BIT); glRasterPos2i(0,0);//和位图一样先设好光栅位置 glDrawPixels(Width,Height,GL_RGB,GL_UNSIGNED_BYTE,image);//绘制 glFlush(); image_motion(2*Width,2*Height); } void reshape_image(int w, int h) { if (h == 0) h = 1; height=h; glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main_image() { glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowSize(100, 100); glutCreateWindow("text"); setup_image(); glutDisplayFunc(image_display); glutReshapeFunc(reshape_image); glutMainLoop(); return 0; } int main(int argc, char ** argv) { /*初始化*/ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//单缓冲、RGB模式 glutInitWindowSize(960, 540); glutInitWindowPosition(200, 200); glutCreateWindow("三角形");//窗口标题 //init(); /*绘制与显示*/ glutReshapeFunc(reshape_image);//窗口大小发生改变时采取的行为 glutDisplayFunc(image_display);//显示绘制图形 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glutMainLoop();//循环 return(0); }