- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
在Opengl环境下绘球形.doc
此程序在OpenGL环境下绘制球体以及填充的矩形。并对球体进行光照效果处理;对矩形进行运动化处理,当矩形与窗口的四边相撞时,程序中的TimerFunction(int value)函数可以控制其跳离窗口边缘,改变运动方向。
该程序的源代码:
#include stdafx.h
#include windows.h
#include gl/glut.h
// Initial square position and size
GLfloat x1 = 100.0f;
GLfloat y1 = 100.0f;
GLsizei rsize = 50;
// Step size in x and y directions
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)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glutSolidSphere(50.0f, 200, 200);
// Set current drawing color to red
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 -windowWidth)
xstep = -xstep;
// Reverse direction when you reach top or bottom edge
if(y1 windowHeight-rsize || y1 -windowHeight)
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)
{
static float red_light[4] = { 1.0, 0.0, 0.0, 1.0 }; //定义红色光源
static float red_pos[4] = { 1.0, 1.0, 1.0, 0.0 };
static float blue_light[4] = { 0.0, 0.0, 1.0, 1.0 }; //定义绿色光源
static float blue_pos[4] = { -1.0, -1.0, -1.0, 0.0 };
glLightfv(GL_LIGHT0, GL_DIFFUSE, red_light);
glLightfv(GL_LIGHT0, GL_POSITION, red_p
文档评论(0)