導航:首頁 > 源碼編譯 > 樂其防紅源碼

樂其防紅源碼

發布時間:2023-03-31 19:02:28

Ⅰ 如何運行opengl紅寶書中的源碼,傻瓜版

如何運行opengl紅寶書中的源碼
一、安裝GLUT工具包
1下載OpenGL需要的庫文件 ,一般可以選擇下載glut庫(內含所有必須文件)

2解壓後將得到的glut.lib和glut32.lib這兩個靜態函數庫復制到文件目錄的lib文件夾
X:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib

3將glut.dll,glut32.dll這兩個動態庫文件放到操作系統目錄下面的C:\Windows\system32文件夾內(32位系統)或‪C:\Windows\SysWOW64(64位系統)。
為了兼容性考慮,最好在這兩個目錄下都復制相應的文件。

4將解壓得到的頭文件glut.h復制到目錄如下目錄下:
X:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\GL
提示:如果在incluce目錄下沒有GL文件夾,則需要手動創建

二、VS2013中的配置
創建一個空白的Win32控制台應用程序
在代碼最前面添加包含目錄
#include <GL/glut.h>
然後就可以編輯自己的OpenGL程序了
例如:復制如下代碼到剛配置好的VS中
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
static int year = 0,spin=0, day = 0;
static GLint fogMode;
const int n = 100;
const GLfloat R = 1.0f;
const GLfloat Pi = 3.1415926536f;
void DrawCircle()
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
for (i = 0; i < n; ++i)
{
glColor3f(1.0, 0.0, 0.0);
glVertex2f(R*cos(2 * Pi / n*i), R*sin(2 * Pi / n*i));
}
glEnd();
glFlush();
}
void init(void)
{
GLfloat position[] = { 0.5, 0.5, 3.0, 0.0 };
glEnable(GL_DEPTH_TEST); //防止遮擋
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
{
GLfloat mat[3] = { 0.1745, 0.01175, 0.01175 };
glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
mat[0] = 0.61424; mat[1] = 0.04136; mat[2] = 0.04136;
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959;
glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
glMaterialf(GL_FRONT, GL_SHININESS, 0.6*128.0);
}
glEnable(GL_FOG);
{
GLfloat fogColor[4] = { 0.5, 0.5, 0.5, 1.0 };
fogMode = GL_EXP;
glFogi(GL_FOG_MODE, fogMode);
glFogfv(GL_FOG_COLOR, fogColor);
glFogf(GL_FOG_DENSITY, 0.35);
glHint(GL_FOG_HINT, GL_DONT_CARE);
glFogf(GL_FOG_START, 1.0);
glFogf(GL_FOG_END, 5.0);
}
glClearColor(0.5, 0.9, 0.9, 1.0); /* fog color */
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 1.0, 1.0);
glPushMatrix(); //記住自己的位置
glutSolidSphere(1.0, 20, 16); /* 畫太陽半徑、 20經度、16緯度*/
glRotatef(spin, 0.0, 1.0, 0.0); //自轉,繞著一個向量以給定角度旋轉(正的為逆時針)
glTranslatef(2.0, 1.0, 0.0);
glRotatef(spin, 1.0, 0.0, 0.0); //公轉
glRectf(0.1,0.1,0.5,0.5);
glColor3f(0.0, 0.0, 1.0);
glutWireSphere(0.2, 8, 8); /* 畫第一顆小行星 */
glColor3f(1.0, 0.0, 0.0);
glTranslatef(2.0, 1.0, 0.0);
glRotatef(2 * spin, 0.0, 1.0, 0.0);
glutSolidSphere(0.5, 16, 8);
glPopMatrix();//回到原來的位置
glutSwapBuffers();
}
void spinDisplay(void)
{
spin = spin + 2;
if (spin > 360)
spin = spin - 360;
glutPostRedisplay();
}
void mouse(int button,int state,int x,int y )
{
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
case GLUT_MIDDLE_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 0.5, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
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;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpengGL 程序設計--楊超");
init();
//glutDisplayFunc(DrawCircle);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
//glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}

5編譯後能正確運行說明配置成功!

閱讀全文

與樂其防紅源碼相關的資料

熱點內容
如來佛祖命令雷神去下界 瀏覽:854
新電腦管家下載好怎麼解壓 瀏覽:528
php獲取介面數據 瀏覽:763
最後的命令 瀏覽:921
如何添加手機app桌面快捷圖標 瀏覽:427
ui設計師與程序員 瀏覽:417
壽司pdf 瀏覽:828
pythonbg是什麼 瀏覽:248
c數值演算法程序大全 瀏覽:785
android整點報時 瀏覽:221
稀土pdf 瀏覽:536
單片機電子鎖 瀏覽:596
通達信機智資金流指標公式源碼 瀏覽:216
php安裝xsl擴展 瀏覽:842
python如何使用help 瀏覽:367
上汽榮威app在哪裡查詢 瀏覽:903
冰櫃壓縮機溫度108 瀏覽:720
阿里雲郵smtp伺服器地址 瀏覽:252
解壓館認知理解 瀏覽:239
為什麼使用非官方伺服器會封號 瀏覽:9