最近公司一直沒事情做,就開始逛論壇關注一些東西。無意中又看到了android遊戲引擎的資料,于是突發奇想的開始想學下openGL。個人感覺這個應該是學習遊戲引擎的一個基礎吧。剛好最近也在看《the c programming language》,是以打算從C語言入手學習opengl.
也許java做久了,特别的不喜歡微軟的開發界面,是以決定用自己比較喜歡的DEV-C++。
DEV-C++起源于貝爾實驗室,但是很久以前版本更新到4.9.9.2。便不更新了,後來有兩個衍生産品代替了。
DEV-C++ 自帶的有openGL的示例程式。檔案-》建立-》工程-》multMedia-》OpenGL,便可以建構一個OpenGL的demo,編譯運作之後是一個在不斷旋轉的三角形。
網上搜了一些openGl入門的資料,其中推薦使用GLUT工具包,據說能帶來很多的友善。
于是開始給DEC-C++安裝GLUT。
下載下傳GLUT的glut-3.7.6-bin,裡面有幾個檔案
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsISMzAzMxMjMzIDOxYDMzEDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
把glut.h 拷貝到DEV-C++的安裝目錄下面的 ..\include\GL 下面。
glut.def拷貝到 lib 檔案夾下面。
glut32.dll拷貝到 系統 C:\Windows\System32 下面。
然後建立一個項目
#define GLUT_DISABLE_ATEXIT_HACK
#include <windows.h>
#include <GL/glut.h>
const int A = 500; /* length of a side of the monitor window */
const float B = 500; /* length of a side of the clipping rectangle */
const float C = 200; /* length of a side of the square the program draws */
void myinit(void)
{
glClearColor(0.7, 0.7, 0.7, 0.0); /* gray background */
glMatrixMode(GL_PROJECTION); /* In World coordinates: */
glLoadIdentity(); /* position the "clipping rectangle" */
gluOrtho2D( -B/2, B/2, -B/2, B/2);/* at -B/2, its right edge at +B/2, its bottom */
glMatrixMode(GL_MODELVIEW); /* edge at -B/2 and its top edge at +B/2 */
}
void display( void )
{
glClear(GL_COLOR_BUFFER_BIT); /* clear the window */
glMatrixMode(GL_MODELVIEW); /* The following coordinates are expressed */
glLoadIdentity(); /* in terms of World coordinates */
glBegin(GL_POLYGON) ; /* draw a filled polygon */
glColor3f ( 1.0, 0.3, 0.2); /* draw in light red */
glVertex2f( -C/2, -C/2 ); /* (x,y) */
glVertex2f( C/2, -C/2 ); /* (x,y) */
glVertex2f( C/2, C/2 ); /* (x,y) */
glVertex2f( -C/2, C/2 ); /* (x,y) */
glEnd();
glFlush(); /* send all commands */
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitWindowSize( A, A ); /* A x A pixel screen window */
glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE);
glutCreateWindow("My Rectangle"); /* window title */
glutDisplayFunc(display); /* tell OpenGL main loop what */
myinit(); /* set attributes */
glutMainLoop(); /* pass control to the main loop */
return 0;
}
這裡要提醒一下:
#define GLUT_DISABLE_ATEXIT_HACK
#include <windows.h>
這兩行是必須帶有的,如果不加上,編譯的時候會包錯誤:50 E:\Program Files\DEV-CPP\include\GL\glut.h redeclaration of C++ built-in type `short'
加上之後仍舊不能編譯,需要做一下設定。
工程-》工程選項-》參數 添加三個檔案 libglu32.a 、 libglut32.a 、libopengl32.a
這三個檔案都在DEV-C++的安裝目錄中的lib檔案夾中。
然後編譯運作
結果如下:
這裡就結束了opengl學習的第一步了。
----------------------------------------------------------------------------------------分割線-------------------------------------------------------------------------
如何安裝GLUT,google老師給了很多可用的資料。但是每次編譯總是出現:50 E:\Program Files\DEV-CPP\include\GL\glut.h redeclaration of C++ built-in type `short' ,着實讓我糾結了很久。最後在這裡找打的答案點選打開連結。具體那兩行添加的代碼
#define GLUT_DISABLE_ATEXIT_HACK
#include <windows.h>
是什麼意思,沒有搜到具體的解釋,根據名稱推斷,應該是忽略一些錯誤資訊吧……