谁能给我几个openGL的入门级程序的源代码

谁能给我几个openGL的入门级程序的源代码,第1张

下面以画一条 Bezier 曲线为例,详细介绍VC++ 上 OpenGL编程的方法。文中给出了详细注释,以便给初学者明确的指引。一步一步地按所述去做,你将顺利地画出第一个 OpenGL 平台上的图形来。

一、产生程序框架 Test.dsw

New Project | MFC Application Wizard (EXE) | "Test" | OK

*注* : 加“”者指要手工敲入的字串

二、导入 Bezier 曲线类的文件

用下面方法产生 BezierCurve.h BezierCurve.cpp 两个文件:

WorkSpace | ClassView | Test Classes| <右击d出>New Class | Generic Class(不用MFC类) | "CBezierCurve" | OK

三、编辑好 Bezier 曲线类的定义与实现

写好下面两个文件:

BezierCurve.h BezierCurve.cpp

四、设置编译环境:

1. 在 BezierCurve.h 和 TestView.h 内各加上:

#include <GL/gl.h>

#include <GL/glu.h>

#include <GL/glaux.h>

2. 在集成环境中

Project | Settings | Link | Object/library module | "opengl32.lib glu32.lib glaux.lib" | OK

五、设置 OpenGL 工雹拍销作环境:(下面各个 *** 作,均针对 TestView.cpp )

1. 处理 PreCreateWindow(): 设置 OpenGL 绘图窗口的风格

cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CS_OWNDC

2. 处理 OnCreate():创建 OpenGL 的绘图设备。

OpenGL 绘图的机制是: 先用 OpenGL 的绘图上下文 Rendering Context (简称为 RC )把图画好,再把所绘结果通过 SwapBuffer() 函数传给 Window 的 绘图上下文 Device Context (简记为 DC).要注意的是,程序贺弊运行过程中,可以有多个 DC,但只能有一个 RC。因此当一个 DC 画完图后,要立即释放 RC,以便其它的 DC 也使用。在后面的代码中,将有详细注释。

int CTestView::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

if (CView::OnCreate(lpCreateStruct) == -1)

return -1

myInitOpenGL()

return 0

}

void CTestView::myInitOpenGL()

{

m_pDC = new CClientDC(this)//创建 DC

ASSERT(m_pDC != NULL)

if (!mySetupPixelFormat()) //设定绘图的位图格式,函数下面列出

return

m_hRC = wglCreateContext(m_pDC->m_hDC)//创建 RC

wglMakeCurrent(m_pDC->m_hDC, m_hRC)//RC 与当前 DC 相关联

} //CClient * m_pDCHGLRC m_hRC是 CTestView 的成员变量

BOOL CTestView::mySetupPixelFormat()

{//我们暂时不管格式的具体内容是什么,以后熟悉源游了再改变格式

static PIXELFORMATDESCRIPTOR pfd =

{

sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd

1, // version number

PFD_DRAW_TO_WINDOW | // support window

PFD_SUPPORT_OPENGL | // support OpenGL

PFD_DOUBLEBUFFER, // double buffered

PFD_TYPE_RGBA, // RGBA type

24, // 24-bit color depth

0, 0, 0, 0, 0, 0, // color bits ignored

0, // no alpha buffer

0, // shift bit ignored

0, // no accumulation buffer

0, 0, 0, 0, // accum bits ignored

32, // 32-bit z-buffer

0, // no stencil buffer

0, // no auxiliary buffer

PFD_MAIN_PLANE, // main layer

0, // reserved

0, 0, 0 // layer masks ignored

}

int pixelformat

if ( (pixelformat = ChoosePixelFormat(m_pDC->m_hDC, &pfd)) == 0 )

{

MessageBox("ChoosePixelFormat failed")

return FALSE

}

if (SetPixelFormat(m_pDC->m_hDC, pixelformat, &pfd) == FALSE)

{

MessageBox("SetPixelFormat failed")

return FALSE

}

return TRUE

}

3. 处理 OnDestroy()

void CTestView::OnDestroy()

{

wglMakeCurrent(m_pDC->m_hDC,NULL)//释放与m_hDC 对应的 RC

wglDeleteContext(m_hRC)//删除 RC

if (m_pDC)

delete m_pDC//删除当前 View 拥有的 DC

CView::OnDestroy()

}

4. 处理 OnEraseBkgnd()

BOOL CTestView::OnEraseBkgnd(CDC* pDC)

{

// TODO: Add your message handler code here and/or call default

// return CView::OnEraseBkgnd(pDC)

//把这句话注释掉,若不然,Window

//会用白色北景来刷新,导致画面闪烁

return TRUE//只要空返回即可。

}

5. 处理 OnDraw()

void CTestView::OnDraw(CDC* pDC)

{

wglMakeCurrent(m_pDC->m_hDC,m_hRC)//使 RC 与当前 DC 相关联

myDrawScene( )//具体的绘图函数,在 RC 中绘制

SwapBuffers(m_pDC->m_hDC)//把 RC 中所绘传到当前的 DC 上,从而

//在屏幕上显示

wglMakeCurrent(m_pDC->m_hDC,NULL)//释放 RC,以便其它 DC 进行绘图

}

void CTestView::myDrawScene( )

{

glClearColor(0.0f,0.0f,0.0f,1.0f)//设置背景颜色为黑色

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

glPushMatrix()

glTranslated(0.0f,0.0f,-3.0f)//把物体沿(0,0,-1)方向平移

//以便投影时可见。因为缺省的视点在(0,0,0),只有移开

//物体才能可见。

//本例是为了演示平面 Bezier 曲线的,只要作一个旋转

//变换,可更清楚的看到其 3D 效果。

//下面画一条 Bezier 曲线

bezier_curve.myPolygon()//画Bezier曲线的控制多边形

bezier_curve.myDraw()//CBezierCurve bezier_curve

//是 CTestView 的成员变量

//具体的函数见附录

glPopMatrix()

glFlush()//结束 RC 绘图

return

}

6. 处理 OnSize()

void CTestView::OnSize(UINT nType, int cx, int cy)

{

CView::OnSize(nType, cx, cy)

VERIFY(wglMakeCurrent(m_pDC->m_hDC,m_hRC))//确认RC与当前DC关联

w=cx

h=cy

VERIFY(wglMakeCurrent(NULL,NULL))//确认DC释放RC

}

7 处理 OnLButtonDown()

void CTestView::OnLButtonDown(UINT nFlags, CPoint point)

{

CView::OnLButtonDown(nFlags, point)

if(bezier_curve.m_N>MAX-1)

{

MessageBox("顶点个数超过了最大数MAX=50")

return

}

//以下为坐标变换作准备

GetClientRect(&m_ClientRect)//获取视口区域大小

w=m_ClientRect.right-m_ClientRect.left//视口宽度 w

h=m_ClientRect.bottom-m_ClientRect.top//视口高度 h

//w,h 是CTestView的成员变量

centerx=(m_ClientRect.left+m_ClientRect.right)/2//中心位置,

centery=(m_ClientRect.top+m_ClientRect.bottom)/2//取之作原点

//centerx,centery 是 CTestView 的成员变量

GLdouble tmpx,tmpy

tmpx=scrx2glx(point.x)//屏幕上点坐标转化为OpenGL画图的规范坐标

tmpy=scry2gly(point.y)

bezier_curve.m_Vertex[bezier_curve.m_N].x=tmpx//加一个顶点

bezier_curve.m_Vertex[bezier_curve.m_N].y=tmpy

bezier_curve.m_N++//顶点数加一

InvalidateRect(NULL,TRUE)//发送刷新重绘消息

}

double CTestView::scrx2glx(int scrx)

{

return (double)(scrx-centerx)/double(h)

}

double CTestView::scry2gly(int scry)

{

}

附录:

1.CBezierCurve 的声明: (BezierCurve.h)

class CBezierCurve

{

public:

myPOINT2D m_Vertex[MAX]//控制顶点,以数组存储

//myPOINT2D 是一个存二维点的结构

//成员为Gldouble x,y

int m_N//控制顶点的个数

public:

CBezierCurve()

virtual ~CBezierCurve()

void bezier_generation(myPOINT2D P[MAX],int level)

//算法的具体实现

void myDraw()//画曲线函数

void myPolygon()//画控制多边形

}

2. CBezierCurve 的实现: (BezierCurve.cpp)

CBezierCurve::CBezierCurve()

{

m_N=4

m_Vertex[0].x=-0.5f

m_Vertex[0].y=-0.5f

m_Vertex[1].x=-0.5f

m_Vertex[1].y=0.5f

m_Vertex[2].x=0.5f

m_Vertex[2].y=0.5f

m_Vertex[3].x=0.5f

m_Vertex[3].y=-0.5f

}

CBezierCurve::~CBezierCurve()

{

}

void CBezierCurve::myDraw()

{

bezier_generation(m_Vertex,LEVEL)

}

void CBezierCurve::bezier_generation(myPOINT2D P[MAX], int level)

{ //算法的具体描述,请参考相关书本

int i,j

level--

if(level<0)return

if(level==0)

{

glColor3f(1.0f,1.0f,1.0f)

glBegin(GL_LINES)//画出线段

glVertex2d(P[0].x,P[0].y)

glVertex2d(P[m_N-1].x,P[m_N-1].y)

glEnd()//结束画线段

return//递归到了最底层,跳出递归

}

myPOINT2D Q[MAX],R[MAX]

for(i=0i {

Q.x=P.x

Q.y=P.y

}

for(i=1i<m_Ni++)

{

R[m_N-i].x=Q[m_N-1].x

R[m_N-i].y=Q[m_N-1].y

for(j=m_N-1j>=ij--)

{

Q[j].x=(Q[j-1].x+Q[j].x)/double(2)

Q[j].y=(Q[j-1].y+Q[j].y)/double(2)

}

}

R[0].x=Q[m_N-1].x

R[0].y=Q[m_N-1].y

bezier_generation(Q,level)

bezier_generation(R,level)

}

void CBezierCurve::myPolygon()

{

glBegin(GL_LINE_STRIP)//画出连线段

glColor3f(0.2f,0.4f,0.4f)

for(int i=0i<m_Ni++)

{

glVertex2d(m_Vertex.x,m_Vertex.y)

}

glEnd()//结束画连线段

}

如何运行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 = 0i <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.61424mat[1] = 0.04136mat[2] = 0.04136

glMaterialfv(GL_FRONT, GL_DIFFUSE, mat)

mat[0] = 0.727811mat[1] = 0.626959mat[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编译后能正确运行说明配置成功!

要获燃迅得GLUT源代码,请访问下面的网页

http://www.opengl.org/developers/documentation/glut/index.html

也可搜顷以用ftp

ftp://ftp.sgi.com/opengl/opengl114.tar.z

ftp://ftp.sgi.com/世段陆opengl/opengl114.taz

ftp://ftp.sgi.com/opengl/opengl114.zip


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12229671.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-22
下一篇2023-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存