求一个C语言Opengl代码

Python08

求一个C语言Opengl代码,第1张

// Bounce.c

// Demonstrates a simple animated rectangle program with GLUT

// OpenGL SuperBible, 2nd Edition

// Richard S. Wright Jr.

#include"stdafx.h"

#include <windows.h>

#include <gl/glut.h>

// Initial square position and size

GLfloat x1 = 100.0f

GLfloat y1 = 150.0f

GLsizei rsize = 50

// Step size in x and y directions

// (number of pixels to move each time)

GLfloat xstep = 1.0f

GLfloat ystep = 1.0f

// Keep track of windows changing width and height

GLfloat windowWidth

GLfloat windowHeight

// Called to draw scene

void RenderScene(void)

{

// Clear the window with current clearing color

glClear(GL_COLOR_BUFFER_BIT)

// Set current drawing color to red

// R GB

glColor3f(1.0f, 0.0f, 0.0f)

// Draw a filled rectangle with current color

glRectf(x1, y1, x1+rsize, y1+rsize)

// Flush drawing commands

glutSwapBuffers()

}

// Called by GLUT library when idle (window not being

// resized or moved)

void TimerFunction(int value)

{

// Reverse direction when you reach left or right edge

if(x1 >windowWidth-rsize || x1 <0)

xstep = -xstep

// Reverse direction when you reach top or bottom edge

if(y1 >windowHeight-rsize || y1 <0)

ystep = -ystep

// Check bounds. This is incase the window is made

// smaller and the rectangle is outside the new

// clipping volume

if(x1 >windowWidth-rsize)

x1 = windowWidth-rsize-1

if(y1 >windowHeight-rsize)

y1 = windowHeight-rsize-1

// Actually move the square

x1 += xstep

y1 += ystep

// Redraw the scene with new coordinates

glutPostRedisplay()

glutTimerFunc(33,TimerFunction, 1)

}

// Setup the rendering state

void SetupRC(void)

{

// Set clear color to blue

glClearColor(0.0f, 0.0f, 1.0f, 1.0f)

}

// Called by GLUT library when the window has chanaged size

void ChangeSize(GLsizei w, GLsizei h)

{

// Prevent a divide by zero

if(h == 0)

h = 1

// Set Viewport to window dimensions

glViewport(0, 0, w, h)

// Reset coordinate system

glMatrixMode(GL_PROJECTION)

glLoadIdentity()

// Keep the square square, this time, save calculated

// width and height for later use

if (w <= h)

{

windowHeight = 250.0f*h/w

windowWidth = 250.0f

}

else

{

windowWidth = 250.0f*w/h

windowHeight = 250.0f

}

// Set the clipping volume

glOrtho(0.0f, windowWidth, 0.0f, windowHeight, 1.0f, -1.0f)

glMatrixMode(GL_MODELVIEW)

glLoadIdentity()

}

// Main program entry point

void main(void)

{

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)

glutCreateWindow("Bounce")

glutDisplayFunc(RenderScene)

glutReshapeFunc(ChangeSize)

glutTimerFunc(33, TimerFunction, 1)

SetupRC()

glutMainLoop()

}

//是正方形的,你改一下吧~

程序还是自己试着编,这样才会进步,加油哈!

OpenGL就是基于C语言的,只需要下载OpenGL的SDK库安装即可,在编写源码时:

1、添加头文件glut.h。

注意glut.h文件中已经包含gl.h,glu.h在实际编译中可以只加入头文件glut.h,很多相关的例子都是这样的,但是在mingwstudio上编译发现,在glut.h前还是需要加入glu.h, gl.h.如:

#include <gl/gl.h>

#include <gl/glu.h>

#include <gl/glut.h>

2、在工程中添加OpenGL的库,有关命令行加入,glu32 opengl32 glut32库就可以编译了。

查找 MSDN 可以得知,MSG 里面的 pt 坐标是相对于窗口的左上角的;

2. 至于声音控制和播放,可以使用 Windows 自带的 MCI API,或者使用 DirectSound 来播放,我推荐你使用 un4seen 的 BASS,简单实用强大,一两个函数就可以播放音效了;

3. 你要使用 alpha blend 与桌面进行镂空运算,就必须首先获得桌面的窗体句柄,OpenGL 的 alppha 运算我不是很懂,不过 Direct3D 的话就简单多了;

4. 屏幕常亮,其实就是阻止系统进入休眠状态,每当系统要进入休眠状态之前,都会向系统的所有窗口发送一条消息,你拦截这条消息,进行特别的处理就可以防止系统进入休眠了,至于是什么消息,请查看 MSDN,我也好久没用过这条消息了;

5.bmp 文件可以保存 alpha 通道,使用 32bit 色深的 bmp 文件就可以了,RGB 分别 8bit,alpha 通道 8bit,不过说到 alpha 通道,tga 或者 png 图片更加合适,因为他们可以进行无损压缩;

6.用 GetPocAddress 导出函数,只能用类型强制转换,这个是 windows 的原则,我们只能去迎合它了 ...

7. 执行 NULL 指针的话,不同的系统会有不同的反应,XP 是直接程序崩溃,Vista 或者以上的系统,就会提示无响应

8. 如果你建立的工程是 Win32 窗口程序,那么就不会有 DOS 窗口,如果你建立的是 Win32 控制台程序,那么就会有 DOS 窗口;如果你使用 OpenGL 实用库来创建 OpenGL 程序,那个 DOS 窗口是无法消除的,它可以帮助你进行错误排查

9. 不要用 Dev C++ 了,用 VS2010 吧,这是行业规范

最后,祝楼主学习愉快