gpt4 book ai didi

iphone - OpenGL ES 光照问题

转载 作者:行者123 更新时间:2023-12-03 20:26:51 26 4
gpt4 key购买 nike

我是 OpenGL ES 新手,正在使用 Objective-C 进行 iPhone 开发。

我想要了解的是光是如何在我的视锥体中定位的。我就是这样做的:

我创建了一个正方形并将其作为我的平截头体中的“地板”。我将灯放在地板正上方的中间,并将光的方向指向正下方。现在,我预计地板上的光圈位于中间。但事实并非如此,它更靠后(更远)。怎么会?

near = 8.0
far = 28
light position = {0.0, 0.0, -10.0}
light direction = {0.0, -1.0, 0.0}

Floor coordinates = {-0.3153, -0.473, -20.0},
{-0.3153, -0.473, 0.0},
{0.3153, -0.473, -20.0},
{0.3153, -0.473, 0.0},

这不应该使光环位于地板中间而不是稍微向后吗?

这是我的重要代码:

设置 View :

 -(void)setupView:(TDRoomView*)view
{


const GLfloat zNear = 8, zFar = 28, fieldOfView = 5;

GLfloat size;
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
NSLog(@"tanf(DEGREES_TO_RADIANS(fieldOfView)/2.0); %f ", tanf(DEGREES_TO_RADIANS(fieldOfView)/2.0));

CGRect rect = view.bounds;
glFrustumf(-size, size, -size / (rect.size.width / rect.size.height), size /
(rect.size.width / rect.size.height), zNear, zFar);
glViewport(-size, size, rect.size.width, rect.size.height);
glMatrixMode(GL_MODELVIEW);
glShadeModel(GL_SMOOTH);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

static const Color3D light0Ambient[] = {{1.0, 1.0, 1.0, 1.0}};
glLightfv(GL_LIGHT0, GL_AMBIENT, (const GLfloat *)light0Ambient);

static const Color3D light0Diffuse[] = {{0.5, 0.5, 0.5, 1.0}};
glLightfv(GL_LIGHT0, GL_DIFFUSE, (const GLfloat *)light0Diffuse);

static const Color3D light0Specular[] = {{0.5, 0.5, 0.5, 1.0}};
glLightfv(GL_LIGHT0, GL_SPECULAR, (const GLfloat *)light0Specular);

static const Vertex3D light0Position[] = {{0.0, 0.473, -10.0}};
glLightfv(GL_LIGHT0, GL_POSITION, (const GLfloat *)light0Position);

static const Vertex3D lightVector= {0.0, -1.0, 0.0};

glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, (GLfloat *)&lightVector);

glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 85);

glLoadIdentity();
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);

}

和绘图:

     -(void)drawView:(TDRoomView*)view
{

static const Vertex3D vertices[]= {
{-0.3153, -0.473, -20.0},
{-0.3153, -0.473, 0.0},
{0.3153, -0.473, -20.0},
{0.3153, -0.473, 0.0},
{-0.1, -0.25, -3.0},
{0.0, 0.0, -10.0},
{0.1, -0.25, -3.0}
};

static const Color3D colors[] = {
{0.7, 0.7, 0.7, 1.0},
{0.7, 0.7, 0.7, 1.0},
{0.7, 0.7, 0.7, 1.0},
{0.7, 0.7, 0.7, 1.0},
{1.0, 0.0, 0.0, 1.0},
{0.0, 1.0, 0.0, 1.0},
{0.0, 0.0, 1.0, 1.0}
};


static const GLubyte roomFaces[] = {
0, 1, 2,
1, 2, 3};


static const Vector3D normals[] = {
{0.000000, 1.000000, 0.000000},
{0.000000, 1.000000, 0.000000},
{0.000000, 1.000000, 0.000000},
{0.000000, 1.000000, 0.000000},
{0.000000, 1.000000, 0.000000},
{0.000000, 1.000000, 0.000000},
{0.000000, 1.000000, 0.000000}
};

glLoadIdentity();

glClearColor(0.7, 0.7, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(4, GL_FLOAT, 0, colors);
glNormalPointer(GL_FLOAT, 0, normals);
glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_BYTE, roomFaces);

glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY); }

另外,我重新考虑更改

static const Vertex3D light0Position[] = {{0.0, 0.473, -10.0}};

static const Vertex3D light0Position[] = {{0.0, 0.473, -8.0}}; 

边框变暗,没有光照射到上面。为什么,我只是把灯移近了两步……?如果我像这样将其移远两步,也会发生同样的事情:

static const Vertex3D light0Position[] = {{0.0, 0.473, -12.0}};

非常感谢任何有助于“阐明”我的问题的帮助! (没办法……;))

提前致谢!

最佳答案

我建议你做一个更大的网格,有更多的顶点,最好是 n × n 的正方形。尽量不要有太长的细三角形。这将使您更容易看到光线似乎在哪里,因为光线是针对每个顶点而不是表面的每个像素计算的。

http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/

2. Poor Tessellation Hurts Lighting

OpenGL's lighting calculations are done per-vertex. This means that the shading calculations due to light sources interacting with the surface material of a 3D object are only calculated at the object's vertices. Typically, OpenGL just interpolates or smooth shades between vertex colors. OpenGL's per-vertex lighting works pretty well except when a lighting effect such as a specular highlight or a spotlight is lost or blurred because the effect is not sufficiently sampled by an object's vertices. Such under-sampling of lighting effects occurs when objects are coarsely modeled to use a minimal number of vertices.

:

Novice OpenGL programmers are often tempted to enable OpenGL's spotlight functionality and shine a spotlight on a wall modeled as a single huge polygon. Unfortunately, no sharp spotlight pattern will appear as the novice intended; you probably will not see any spotlight affect at all. The problem is that the spotlight's cutoff means that the extreme corners of the wall where the vertices are specified get no contribution from the spotlight and since those are the only vertices the wall has, there will be no spotlight pattern on the wall.

关于iphone - OpenGL ES 光照问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2127770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com