关于glClipPlane()截切平面
按照OpenGL官方资源的说法,在任何一个语言的OpenGL实现里,截切平面的数量都不应该少于6个. 但是在我的程序里多于六个的切平面却没有效果了。后来看到gl.h里面对GL_CLIP_PLANE是这样定义的:
#define GL_CLIP_PLANE0 0x3000
#define GL_CLIP_PLANE1 0x3001
#define GL_CLIP_PLANE2 0x3002
#define GL_CLIP_PLANE3 0x3003
#define GL_CLIP_PLANE4 0x3004
#define GL_CLIP_PLANE5 0x3005
也就是说地址为从0x3000到0x3005
用glGetIntegerv(GL_MAX_CLIP_PLANES, &x) (x是GLint类型)明明看到数量很多很多,但是为什么从0x3005之后的地址都没效果了呢?
以下是我的代码,截切平面只对灰色的球起作用

#include <stdio.h> #include <GL/glut.h> //Just like setup() in Processing. But it doesn't support drawing inside. //If want to draw a static picture, void init(int w, int h) { float ratio = 1.0 * w / h; glClearColor(1.0, 1.0, 1.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0, 0, w, h); gluPerspective(45,ratio,1,1000); } float angle=0.0; GLdouble plane0Eq[4] = {0.2, 0.3, 0.4, 0}; void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glRotatef(angle,angle,angle,0.0); glClipPlane(0x3006, plane0Eq); //把这儿改成0x3000到0x3005,就能看到灰球被切了一半。下面的也得改 glEnable(0x3006); glColor4f(0.2, 0.2, 0.2, 0.4); glutSolidSphere(1.4, 40, 40); glDisable(0x3006); glColor3f(0.3, 0.3, 0.3); glutWireSphere(1.4, 40, 40); glDisable(GL_BLEND); glPopMatrix(); glutSwapBuffers(); angle+=0.1; } void changeSize(int w, int h) { if(h == 0) h = 1; float ratio = 1.0* w / h; glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0, 0, w, h); gluPerspective(45,ratio,1,1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt( 0.0, 0.0, 5.0, 0.0, 0.0, -1.0, 0.0f, 1.0f, 0.0f); } int main(int argc, char** argv) { int width = 300; int height = 300; glutInit(&argc,argv); // Passes from ARGC & ARGV glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(width, height); glutInitWindowPosition(100,100); glutCreateWindow("Clipping"); glutDisplayFunc(display); glutIdleFunc(display); glutReshapeFunc(changeSize); init(width, height); glutMainLoop(); }然后是Linux的shell脚本
#!/bin/sh gcc -o glexec gltest.c -lX11 -lGL -lGLU -lglut ./glexec
[ 本帖最后由 MarvinTau 于 2010-5-21 17:38 编辑 ]