太阳系模型――OpenGL
太阳系模型――OpenGL
2010年06月16日
OpenGL红宝书中一个例子--太阳系模型
先粘上源码:
#include "stdafx.h"
#define GLUT_DISABLE_ATEXIT_HACK
#include
#include
#include
#include
static int year = 0, day = 0;
void init()
{
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glPushMatrix();
glutWireSphere(1.0,20.,16);
glTranslatef(2.0,0.0,0.0);
glRotatef((GLfloat)day,0.0,1.0,0.0);
glutWireSphere(0.2,10,8);
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0,(GLsizei)w/(GLsizei)h,1.0,20.0 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
}
void keyboard(unsigned char k, int x, int y)
{
switch(k){
case 'd':
day = (day+10)%360;
glutPostRedisplay();
break;
case 'D':
day = (day -10) % 360;
glutPostRedisplay();
break;
case 'y':
year = (year + 5) % 360;
glutPostRedisplay();
break;
case 'Y':
year = (year - 5) % 360;
glutPostRedisplay();
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);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
由于最开始用的是_tmain(),编译出现了从"_TCHAR *[]"转换为"char **"出错。这个是2005默认的"使用 Unicode 字符集",解决是
"配置属性"-->"字符集"-->"使用多字节字符集"。
如果是用 main(),无需改动,改为多字符集反而出错。
改正后没有错误。但是在运行时出现找不到MSVCR80D.dll提示。解决方案:
1. 在"属性->配置属性->清单工具->常规"下有一个"使用FAT32解决办法。(注意:一定要先配置这个选项,然后再编译工程,要不然还是不好用:)
2. 找到你的工程的文件夹,如(myproject),找到其下的myproject\myproject\Debug\ myproject.rec,把它删掉(删掉整个Debug目录也可以),重新编译,搞定!
http://www.cppblog.com/LiBerTine/archive/2009/09/2 1/73323.html
最后效果如附件。
glutKeyboardFunc(void (GLUTCALLBACK *func)(unsigned char key, int x, int y));
x,y显示鼠标位置 glutKeyboardFunc sets the keyboard callback for the current window. When a user types into the window, each key press generating an ASCII character will generate a keyboard callback。 http://www.opengl.org/resources/libraries/glut/spe c3/node49.html
猜你喜欢
转载自owcot54k.iteye.com/blog/1362700
今日推荐
周排行