1. 程式人生 > >圖解VC++ opengl環境配置和幾個入門例子

圖解VC++ opengl環境配置和幾個入門例子

VC6下載

http://blog.csdn.net/bcbobo21cn/article/details/44200205

demo工程和glut庫下載

http://pan.baidu.com/s/1pLSpKiZ

一 環境配置

參閱

http://blog.itpub.net/17267437/viewspace-545635/

解壓glut庫

拷貝glut.h到如下目錄;


拷貝圖1的兩個lib到如下目錄;


拷貝圖1的兩個dll到如下目錄(偶是64位系統);


工程設定新增四個lib


二例子1

#include <GL/glut.h>
#include <math.h>

const int n = 200;
const GLfloat R = 0.5f;
const GLfloat Pi = 3.1415926536f;

void myDisplay(void)
{
int i;
// 畫一個綠色正方形
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0f,1.0f,0.0f);
    glRectf(-0.5f, 0.4f, 0.0f, 0.9f); //畫一個綠色的正方形
    //glFlush();

//glClearColor(0.0,0.0,0.0,0.0);
     //glClear(GL_COLOR_BUFFER_BIT);
     glColor4f(1.0,0.0,0.0,1.0);     // set the quad color
     glBegin(GL_QUADS);
         glVertex3f(-0.2,-0.2,0.0);
         glVertex3f(0.2,-0.2,0.0);
         glVertex3f(0.2,0.2,0.0);
         glVertex3f(-0.2,0.2,0.0);
    glEnd();

glColor4f(1.0,0.0,0.0,1.0);   // set the point color
      glPointSize(10);
      glBegin(GL_POINTS);
          glVertex3f(-0.5,-0.5,0.0);
          glVertex3f(0.5,-0.5,0.0);
          glVertex3f(0.5,0.5,0.0);
          glVertex3f(-0.5,0.5,0.0);
     glEnd();

glColor4f(1.0,0.0,0.0,1.0); // set the line color
      glLineWidth(5);
      glBegin(GL_LINES);
            glVertex3f(-0.5,-0.5,0.0);
            glVertex3f(0.5,-0.5,0.0);
            glVertex3f(0.5,0.5,0.0);
            glVertex3f(-0.5,0.5,0.0);
      glEnd();

 /*glBegin(GL_POLYGON);
for(i=0; i<n; ++i)
glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));
glEnd();*/

    glFlush();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 0);
    glutInitWindowSize(1024, 768);
    glutCreateWindow("OpenGL基本繪製");
    glutDisplayFunc(&myDisplay);
    glutMainLoop();
    return 0;
}

網上說要改下圖的_CONSOLE,我沒改也能在開發環境下執行;

三 例子2

#include <GL/glut.h>

void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
glFlush();
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("第一個OpenGL程式");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}


四 例子3

#include <GL/glut.h>

//Called to draw scene
void RenderSence(void)
{
//Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
//Flush drawing commands
glFlush();
}
//Set up the rendering state
void SetupRC(void)
{
glClearColor(0.0f,0.0f,1.0f,1.0f);      //此時背景色為藍色
}

int main(int argc, char *argv[])
{

//glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_SINGLE|GLUT_RGB);
glutCreateWindow("Simple");        //視窗名為“Simple”
glutDisplayFunc(RenderSence);
SetupRC();
glutMainLoop();

return 0;
}


編譯,出現一堆下述錯誤;

demo4.cpp
y:\dddd10\vc6opengldemo\demo4\demo4.cpp(5) : error C2018: unknown character '0xa1'
y:\dddd10\vc6opengldemo\demo4\demo4.cpp(5) : error C2018: unknown character '0xa1'
y:\dddd10\vc6opengldemo\demo4\demo4.cpp(10) : error C2018: unknown character '0xa1'

......

是因為程式碼中包含全形字元;尋找發現該全形字元是空格;用替換功能,把全形空格全部替換為半形空格再編譯即可;


五 例子4

#include <GL/glut.h> 

void background(void)
{
glClearColor(0.0,0.0,0.0,0.0);//設定背景顏色為黑色
}

void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);//buffer設定為顏色可寫

 glBegin(GL_TRIANGLES);//開始畫三角形
 glShadeModel(GL_SMOOTH);//設定為光滑明暗模式

 glColor3f(1.0,0.0,0.0);//設定第一個頂點為紅色
 glVertex2f(-1.0,-1.0);//設定第一個頂點的座標為(-1.0,-1.0)

 glColor3f(0.0,1.0,0.0);//設定第二個頂點為綠色
 glVertex2f(0.0,-1.0);//設定第二個頂點的座標為(0.0,-1.0)

 glColor3f(0.0,0.0,1.0);//設定第三個頂點為藍色
 glVertex2f(-0.5,1.0);//設定第三個頂點的座標為(-0.5,1.0)
 glEnd();//三角形結束

 glFlush();//強制OpenGL函式在有限時間內執行
}

void myReshape(GLsizei w,GLsizei h)
{
 glViewport(0,0,w,h);//設定視口

 glMatrixMode(GL_PROJECTION);//指明當前矩陣為GL_PROJECTION
 glLoadIdentity();//將當前矩陣置換為單位陣

 if(w <= h)
  gluOrtho2D(-1.0,1.5,-1.5,1.5*(GLfloat)h/(GLfloat)w);//定義二維正視投影矩陣
 else
  gluOrtho2D(-1.0,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5);
  glMatrixMode(GL_MODELVIEW);//指明當前矩陣為GL_MODELVIEW
}

int main(int argc,char ** argv)
{
 /*初始化*/
 glutInit(&argc,argv);
 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
 glutInitWindowSize(400,400);
 glutInitWindowPosition(200,200);

 /*建立視窗*/
 glutCreateWindow("Triangle");

 /*繪製與顯示*/
 background();
 glutReshapeFunc(myReshape);
 glutDisplayFunc(myDisplay);

 glutMainLoop();
 return(0);
}


六 opengl資源連結

http://www.cnblogs.com/phinecos/category/90224.html

基於MFC的opengl程式設計