请教关于OpenGL聚光灯的问题

zhouping200511638 2013-01-05 11:46:56
代码如下:


class CBox
{
private:
int mWindowWidth;
int mWindowHeight;
float mAngle;
float s;
float c;
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)
{
WNDCLASSEX windowClass;
HWND hwnd;
MSG msg;
DWORD dwExStyle;
DWORD dwStyle;
RECT windowRect;



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,怎么才能实现手电筒的效果?


以下图片是程序输出:
...全文
445 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljforever 2013-12-23
  • 打赏
  • 举报
回复
你 diffuse,ambient 和 specular 值全都一样的?这3个值的含义你搞清楚了? 还有 你没有添加 shiness 这个值,默认是1 就是面积最大的全反射,你要调成 128以内的值,应该就有手电的效果了
ljforever 2013-12-23
  • 打赏
  • 举报
回复
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};//灯光方向 灯光位置 在y轴 1点位置,有问题 灯光方向 ,从 y轴,x轴 负方向,有问题,建议用默认 建议仔细看下gl书再弄,貌似很多基础概念你没有弄明白,所有的系数的设置都是关联的,和你那个glulookat,建议自己写个glulookat,搞清楚里面的含义
jizhen521 2013-07-05
  • 打赏
  • 举报
回复
First of all, does rightspotlightpos have 4 floats? For a spotlight, you need to have the 4th float be 1.0. If it is 0.0 a directional light is created. Next, you may have to try subdividing your polys. Lighting in OpenGL is calculated per-vertex, so if you have a really big triangle, and the spotlight shines right in the center of that triangle without hitting any of the vertices, you are not going to see any effect. Finally, lights are affected by the current matrix, so the order that you set light position/direction and matrix transformations does make a difference.
zhouping200511638 2013-05-14
  • 打赏
  • 举报
回复
谢谢,发了老半天没人!
ri_aje 2013-02-20
  • 打赏
  • 举报
回复
mesh 分辨率不够,gl 只对每个顶点计算光照,其余部分用插值填充,把 mesh 细化就行了.

4,448

社区成员

发帖
与我相关
我的任务
社区描述
图形图像/机器视觉
社区管理员
  • 机器视觉
  • 迪菲赫尔曼
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧