openGL入门小程序
Posted 小菜鸟yjm
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了openGL入门小程序相关的知识,希望对你有一定的参考价值。
#include <stdlib.h> #include <stdio.h> #include <GL/glut.h> GLdouble angle = 0.0; GLdouble translation = 0.0; GLdouble scale = 1.0; void init(void) glClearColor(1.0,1.0,1.0,1.0);//设置清屏颜色,1,1,1,白色 //glClear(GL_COLOR_BUFFER_BIT);清屏 //指定着色模型 glShadeModel(GL_FLAT);//单调着色 glColor3f(0.0,0.0,0.0);//黑色 void keyboard(unsigned char key, int x, int y) if(key==27) exit(0); void display(void) glClear(GL_COLOR_BUFFER_BIT); //清除颜色缓存 glColor3f(0.0,0.0,0.0); //指定颜色 //模型变换 //指定模型 //glTranslatef(0.01f,0.0f,0.0f);//设定坐标原点 glRotatef(45,0.0f,0.0f,1.0f); glBegin(GL_TRIANGLES); glVertex3f( 0.0f, 1.0f, 0.0f);// 上顶点 glVertex3f(-1.0f,-1.0f, 0.0f);// 左下 glVertex3f( 1.0f,-1.0f, 0.0f);// 右下 glEnd();// 三角形绘制结束 glFlush();//马上执行 printf("display invoke!\\n"); //glTranslatef(0.3f, 0.0f, 0.0f); void mouseFunc(int button, int state, int x, int y) if(button == GLUT_LEFT_BUTTON && state==GLUT_DOWN)//点击左键,并处于按下状态 /* glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(x,0,0,1); glRotatef(y,1,0,0); */ //glTranslatef(300.0f,0.0f,0.0f);// 右移3单位 //glLoadIdentity();//复位键,使坐标回到初始状态 //glTranslatef(0.3f, 0.0f, 0.0f);//为什么在这个里面就不能执行? glutPostRedisplay();//申请重绘当前窗口 glutDisplayFunc(display); glRotatef(45,0.0f,0.0f,1.0f); printf("left click!\\n"); void reshape (int w, int h) glViewport(0,0,(GLsizei)w,(GLsizei)h); int main(int argc, char** argv) glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow("OpenGL中的建模与变换"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMouseFunc(mouseFunc); glutMainLoop(); return 0;
开始的话gult库中的各种函数可能对于初学者不太熟悉,请百度自行解决。这里必须调用glutPostRedisplay();申请重绘当前窗口,才能再次使回调函数触发,然后通过glRotatef(45,0.0f,0.0f,1.0f);实现旋转。
以上是关于openGL入门小程序的主要内容,如果未能解决你的问题,请参考以下文章