在上周昆大神的图形学研究进展,害怕跟不上特地来复习一波opengl
1、显示框架:
/*************************************************************
*Project Name:
*Description:
*Author: lishichengyan
*Student ID:
*Date:
*Reference:http://www.lighthouse3d.com/tutorials/glut-tutorial/
**************************************************************/
#include<iostream>
using namespace std;
#include<gl/glut.h>
void renderScene(void){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(0.5f,0.0f,0.0f);
glVertex3f(0.0f,0.5f,0.0f);
glEnd();
glFlush();
}
/*改变窗口大小时防止图形变形*/
void reshape(int w,int h){
if(h==0)
h=1;
float ratio=1.0f*w/h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,w,h);
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0f,0.0f,5.0f,0.0f,0.0f,-1.0f,0.0f,1.0f,0.0f);
}
int main(int argc,char *argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_SINGLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);
glutCreateWindow("图形学的日常");
glutDisplayFunc(renderScene);
glutReshapeFunc(reshape);
//glutIdleFunc(idle);
glutMainLoop();
return 1;
}
2、GLUT_SINGLE和GLUT_DOUBLE
When using GL_SINGLE
, you can picture your code drawing directly to the display.
When using GL_DOUBLE
, you can picture having two buffers. One of them is always visible, the other one is not. You always render to the buffer that is not currently visible. When you're done rendering the frame, you swap the two buffers, making the one you just rendered visible. The one that was previously visible is now invisible, and you use it for rendering the next frame. So the role of the two buffers is reversed each frame.
(来自StackOverflow:点击打开链接)
3、glutMainLoop干了什么?
glutMainLoop() is a function that loops within itself, processing events and triggering your callback functions when necessary (glutDisplayFunc, etc). After a call to glutMainLoop(), you have no control over the execution of your program besides the callback functions you initialized before the call.
And another thing, the glutDisplayFunc() callback only gets triggered when your app's window receives a refresh event, unless you explicity post a redisplay. If you want your display function to be called repeatedly (i.e. for a game), you need to set your glutIdleFunc to post a redisplay.
(来自gamedev.net:点击打开链接)