首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 平面设计 > 图形图像 >

请问关于OpenGL聚光灯的有关问题

2013-06-25 
请教关于OpenGL聚光灯的问题代码如下:class CBox{private:intmWindowWidthintmWindowHeightfloatmAngle

请教关于OpenGL聚光灯的问题
代码如下:



class CBox
{
private:
intmWindowWidth;
intmWindowHeight;
floatmAngle;
floats;
floatc;
public:
CBox();
~CBox();

bool init();
bool ShutDown();

void SetProjection(int width, int height);
void Prepare(float fAngle);
void Render();
void Light();
};

float light[] = {1.0, 0.0, 0.0, 1.0};//灯光颜色
float position[] = {0.0, 1.0, 0.0, 1.0};//灯光位置,为点光源
float direction[] = {-1.0, -1.0, 0.0};//灯光方向

void CBox::Light()//设置灯光
{
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light);
glLightfv(GL_LIGHT0, GL_SPECULAR, light);
glLightfv(GL_LIGHT0, GL_POSITION, position);

glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 0.0);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 30.0);

glEnable(GL_LIGHT0);
}

float material1[] = {0.0, 0.0, 0.0, 1.0};//材质1不反射任何颜色
float material2[] = {1.0, 1.0, 1.0, 1.0};//材质2反射所以颜色

float x = 1.0;

void CBox::Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glLoadIdentity();
gluLookAt(2.0, -2.0, 0.0, -1.0, 1.0, 0.0, 0.0, 0.0, 1.0);

Light();

glMaterialfv(GL_FRONT,GL_AMBIENT, material1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, material1);
glMaterialfv(GL_FRONT, GL_SPECULAR, material2);//使平面只有反射光

glBegin(GL_QUADS);//在X轴-1出垂直X轴画一个平面
glNormal3f(1.0, 0.0, 0.0);
glVertex3f(-x, x, x);
glVertex3f(-x, x, -x);
glVertex3f(-x, -x, -x);
glVertex3f(-x, -x, x);
glEnd();
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
   LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEXwindowClass;
HWNDhwnd;
MSGmsg;
DWORDdwExStyle;
DWORDdwStyle;
RECTwindowRect;



windowRect.left= (long)0;
windowRect.right= (long)windowWidth;
windowRect.top= (long)0;
windowRect.bottom= (long)windowHeight;

memset(&windowClass, 0, sizeof(WNDCLASSEX));

windowClass.cbSize= sizeof(WNDCLASSEX);
windowClass.style= CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc= MainWindowProc;
windowClass.cbClsExtra= 0;
windowClass.cbWndExtra= 0;
windowClass.hInstance= hInstance;
windowClass.hIcon= LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor= LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground= NULL;
windowClass.lpszClassName= "OpenGL";
windowClass.hIconSm= LoadIcon(NULL, IDI_WINLOGO);

if( !RegisterClassEx(&windowClass))
return 0;

dwExStyle= WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle= WS_OVERLAPPEDWINDOW;

AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);

hwnd = CreateWindowEx(NULL, "OpenGL",
"myOpenGL", dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
0, 0,
windowRect.right - windowRect.left,
windowRect.bottom - windowRect.top,


NULL, NULL, hInstance, NULL);

hDC = GetDC(hwnd);
mBox = new CBox;

if( !hwnd )
return 0;

ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);

mBox->init();

while( exiting )
{
mBox->Prepare(0.0f);
mBox->Render();
SwapBuffers(hDC);

while( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) )
{
if( !GetMessage(&msg, NULL, 0, 0) )
{
//exiting = true;
break;
}

TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int)msg.wParam;
}



想实现的效果就像是一个手电筒照射到一个平面,出现一个圆形的光亮区,但是这个希望的效果一直没有出现。
1,当把聚光灯的角度调至小于30度时,突然变暗。
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 30.0); 
   这是什么原因?
2,怎么才能实现手电筒的效果?


以下图片是程序输出:
请问关于OpenGL聚光灯的有关问题 OpenGL 聚光灯
[解决办法]
mesh 分辨率不够,gl 只对每个顶点计算光照,其余部分用插值填充,把 mesh 细化就行了.

热点排行