I'm trying to view some 3d geometry in a custom ipad application I'm writing. Problem I'm running into is that as I zoom, pan and rotate my model, the edges get clipped, and I'm not sure why. I don't think it's because 开发者_JS百科of the near/far clipping planes because I change those and nothing happens, it still clips. Also, it does it in only one direction. The other direction renders just fine. Any ideas? The transformation matrix is created using an arcball manipulation class I created, but I only included a hardcoded view matrix for obvious reasons. The hardcoded matrix shows the problem just fine. The scene just has a bunch of squares being rendered that get clipped on two of the corners.
To duplicate my problem, create a new opengl es project for the iPad, then paste the following code at the end of the ES1Renderer.m init method before "return self;":
glMatrixMode(GL_PROJECTION);
CGRect frame = [[UIScreen mainScreen] bounds];
glViewport(0, 0, frame.size.width, frame.size.height);
glFrustumf(-1000, 1000, -1000, 1000, 10000, 10000);
now replace the entire render method with the following:
static const GLfloat squareVertices[] = {
-0.5f, -0.5f,
0.5f, -0.5f,
-0.5f, 0.5f,
0.5f, 0.5f,
};
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
glClearColor(0.5f, 0.5f, 0.5f, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
CGRect frame = [[UIScreen mainScreen] bounds];
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, frame.size.width, frame.size.height);
glFrustumf(0, backingWidth, 0, backingHeight, -.1, -1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float mat2[16];
mat2[0] = .268496;
mat2[1] = .082372;
mat2[2] = .127658;
mat2[3] = 0;
mat2[4] = .051842;
mat2[5] = .193987;
mat2[6] = -.234207;
mat2[7] = 0;
mat2[8] = -.142808;
mat2[9] = .225290;
mat2[10] = .154991;
mat2[11] = 0;
mat2[12] = .034580;
mat2[13] = -.001087;
mat2[14] = -.013952;
mat2[15] = 1;
glMultMatrixf(mat2);
glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
for (float i = 0; i <= 6; i += 2) {
for (float j = 0; j <= 6; j+= 2) {
glPushMatrix();
glColor4f(i/6, j/6, 0, 1);
glTranslatef(i-3, j-3, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glPopMatrix();
}
}
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
finally, in the EAGLView.m file, replace the following line in the initWithCoder method
renderer = [[ES2Renderer alloc] init];
with:
renderer = [[ES1Renderer alloc] init];
and run. You will see the grid of squares rotated at an angle and the upper left and lower right edges are clipped. This is what I can't figure out how to get rid of. Any help is appreciated. Thanks.
This is strange:
glFrustumf(0, backingWidth, 0, backingHeight, -.1, -1000);
The last two values are the values for the near and far planes. These are both negative values. From what I can see, you are working only at z=0 which lies outside this range. It's weird that you're seeing anything.
What if you change this to:
glFrustumf(0, backingWidth, 0, backingHeight, 1000, -1000);
? Also the first four values correspond to left, right, top and bottom. These are all positive values while your squares could potentially have negative values. What if you make them more balanced as in
glFrustumf(-10, 10, -10, 10, 1000, -1000);
??
I figured out a way to fix it that works for me, but I'd still like to know what I'm doing wrong with my glFrustrumf call. If I use glOrthof, it works just fine and fixes the clipping plane issue I was having. Anyone know what I was doing wrong with glFrustrumf?
精彩评论