GL_QUAD_STRIP 上的 OpenGL 光照和着色
OpenGL新手,尝试实现带有阴影和照明的球体。我知道有一个我可以调用的球体函数,但尝试创建我自己的球体函数。但是,虽然我可以在场景中的其他对象(例如 glcubes 等)上获得照明和阴影,但我无法在球体上获得任何阴影或照明。任何建议将不胜感激!
波纹管是我绘制球体的函数:
void cg_SolidSphere( GLint slices, GLint stacks )
{
float x, y, z ;
for( float lat = 0 ; lat < PI ; lat = lat + PI/stacks )
{
glBegin( GL_QUAD_STRIP ) ;
for( float lon = 0 ; lon < 2*PI ; lon = lon + 2*PI/slices )
{
x = cosf( lat ) * sinf( lon ) ;
y = sinf( lat ) * sinf( lon ) ;
z = cosf( lon ) ;
glVertex3f( x, y, z ) ;
x = cosf( lat + PI/stacks ) * sinf( lon ) ;
y = sinf( lat + PI/stacks ) * sinf( lon ) ;
z = cosf( lon ) ;
glVertex3f( x, y, z ) ;
}
glEnd() ;
}
}
New to OpenGL, trying to implement a sphere with shading and lighting. I am aware that there is a sphere function I could call on, but trying to create my own. However, while I can get lighting and shading on other objects in the scene (such as glcubes, etc.), I am unable to get any shading or lighting on the sphere. Any suggestions would be greatly appreciated!
Bellow is my function to draw the sphere:
void cg_SolidSphere( GLint slices, GLint stacks )
{
float x, y, z ;
for( float lat = 0 ; lat < PI ; lat = lat + PI/stacks )
{
glBegin( GL_QUAD_STRIP ) ;
for( float lon = 0 ; lon < 2*PI ; lon = lon + 2*PI/slices )
{
x = cosf( lat ) * sinf( lon ) ;
y = sinf( lat ) * sinf( lon ) ;
z = cosf( lon ) ;
glVertex3f( x, y, z ) ;
x = cosf( lat + PI/stacks ) * sinf( lon ) ;
y = sinf( lat + PI/stacks ) * sinf( lon ) ;
z = cosf( lon ) ;
glVertex3f( x, y, z ) ;
}
glEnd() ;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还需要调用
glNormal3f
。在这种情况下,法线与顶点相同,因此我们可以使用相同的点:You need
glNormal3f
calls, too. In this case, the normal is the same as the vertex, so we can use the same points: