图解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編程
總結
以上是生活随笔為你收集整理的图解VC++ opengl环境配置和几个入门例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++反汇编代码分析
- 下一篇: PostgreSQL源码分析