今天比照着教材画了一个金字塔,这个案例主要涉及到了图形变换矩阵的使用。但是其中遇到了一些问题不甚明了,在我自己的代码中,我设置了view.drawableDepth=GLKViewDrawabledepth16,结果绘制出来的图案给人的感觉是从三角形底部看去的,把设置绘制深度的代码注释掉,则我的代码的表现与教材上面一致,下面贴下工程的全部代码,如果有人知道为什么,恳请赐教。
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)GLKBaseEffect *baseEffect;
@end
typedef struct{
GLKVector3 position;
GLKVector3 normal;
}SceneVertex;
typedef struct {
SceneVertex vertices[3];
}SceneTriangle;
static const SceneVertex vertexA =
{{-0.5, 0.5, -0.5}, {0.0, 0.0, 1.0}};
static const SceneVertex vertexB =
{{-0.5, 0.0, -0.5}, {0.0, 0.0, 1.0}};
static const SceneVertex vertexC =
{{-0.5, -0.5, -0.5}, {0.0, 0.0, 1.0}};
static const SceneVertex vertexD =
{{ 0.0, 0.5, -0.5}, {0.0, 0.0, 1.0}};
static const SceneVertex vertexE =
{{ 0.0, 0.0, -0.5}, {0.0, 0.0, 1.0}};
static const SceneVertex vertexF =
{{ 0.0, -0.5, -0.5}, {0.0, 0.0, 1.0}};
static const SceneVertex vertexG =
{{ 0.5, 0.5, -0.5}, {0.0, 0.0, 1.0}};
static const SceneVertex vertexH =
{{ 0.5, 0.0, -0.5}, {0.0, 0.0, 1.0}};
static const SceneVertex vertexI =
{{ 0.5, -0.5, -0.5}, {0.0, 0.0, 1.0}};
static SceneTriangle SceneTriangleMake(const SceneVertex vertexA,const SceneVertex vertexB,const SceneVertex vertexC);
static void CaculateNormalForTriangles(SceneTriangle *triangle);
@implementation ViewController{
SceneTriangle triangles[8];
}
@synthesize baseEffect;
- (void)viewDidLoad {
[super viewDidLoad];
GLKView *view=(GLKView *)self.view;
view.context=[[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES2];
[EAGLContext setCurrentContext:view.context];
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
self.baseEffect = [[GLKBaseEffect alloc] init];
self.baseEffect.light0.enabled = GL_TRUE;
self.baseEffect.light0.diffuseColor = GLKVector4Make(
0.7f, // Red
0.7f, // Green
0.7f, // Blue
1.0f);// Alpha
self.baseEffect.light0.position = GLKVector4Make(
1.0f,
1.0f,
0.5f,
0.0f);
view.drawableDepthFormat=GLKViewDrawableDepthFormat16;
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeRotation(
GLKMathDegreesToRadians(-60.0f), 1.0f, 0.0f, 0.0f);
modelViewMatrix = GLKMatrix4Rotate(
modelViewMatrix,
GLKMathDegreesToRadians(-30.0f), 0.0f, 0.0f, 1.0f);
self.baseEffect.transform.modelviewMatrix = modelViewMatrix;
triangles[0] = SceneTriangleMake(vertexA, vertexB, vertexD);
triangles[1] = SceneTriangleMake(vertexB, vertexC, vertexF);
triangles[2] = SceneTriangleMake(vertexD, vertexB, vertexE);
triangles[3] = SceneTriangleMake(vertexE, vertexB, vertexF);
triangles[4] = SceneTriangleMake(vertexD, vertexE, vertexH);
triangles[5] = SceneTriangleMake(vertexE, vertexF, vertexH);
triangles[6] = SceneTriangleMake(vertexG, vertexD, vertexH);
triangles[7] = SceneTriangleMake(vertexH, vertexF, vertexI);
SceneVertex newVertexE = vertexE;
newVertexE.position.z = 0.0f;
triangles[2] = SceneTriangleMake(vertexD, vertexB, newVertexE);
triangles[3] = SceneTriangleMake(newVertexE, vertexB, vertexF);
triangles[4] = SceneTriangleMake(vertexD, newVertexE, vertexH);
triangles[5] = SceneTriangleMake(newVertexE, vertexF, vertexH);
for(int i=0;i<8;i++){
CaculateNormalForTriangles(&triangles[i]);
}
glEnable(GL_DEPTH_TEST);
GLuint position;
glGenBuffers(1, &position);
glBindBuffer(GL_ARRAY_BUFFER, position);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangles), triangles, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(SceneVertex), NULL+offsetof(SceneVertex, position));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT,GL_FALSE, sizeof(SceneVertex), NULL+offsetof(SceneVertex, normal));
}
- (IBAction)rotate:(id)sender {
static int i=0;
baseEffect.transform.modelviewMatrix=GLKMatrix4Rotate(baseEffect.transform.modelviewMatrix, GLKMathDegreesToRadians(-2), 1, 0, 0);
NSLog(@"%i",++i);
}
-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
[baseEffect prepareToDraw];
glDrawArrays(GL_TRIANGLES, 0, sizeof(triangles)/sizeof(SceneVertex));
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)low:(id)sender {
static CGFloat delt=0.1;
SceneVertex newVertexE = vertexE;
NSLog(@"%f",vertexE.position.z);
newVertexE.position.z = vertexE.position.z-delt;
delt+=0.1;
triangles[2] = SceneTriangleMake(vertexD, vertexB, newVertexE);
triangles[3] = SceneTriangleMake(newVertexE, vertexB, vertexF);
triangles[4] = SceneTriangleMake(vertexD, newVertexE, vertexH);
triangles[5] = SceneTriangleMake(newVertexE, vertexF, vertexH);
for(int i=0;i<8;i++){
CaculateNormalForTriangles(&triangles[i]);
}
glBufferData(GL_ARRAY_BUFFER, sizeof(triangles), triangles, GL_STATIC_DRAW);
}
- (IBAction)frustum:(id)sender {
}
- (IBAction)far:(id)sender {
// baseEffect.transform.modelviewMatrix=GLKMatrix4Translate(baseEffect.transform.modelviewMatrix, 0, 0, -0.1);
}
- (IBAction)high:(id)sender {
static CGFloat delt=0.1;
SceneVertex newVertexE = vertexE;
NSLog(@"%f",vertexE.position.z);
newVertexE.position.z = vertexE.position.z+delt;
delt+=0.1;
triangles[2] = SceneTriangleMake(vertexD, vertexB, newVertexE);
triangles[3] = SceneTriangleMake(newVertexE, vertexB, vertexF);
triangles[4] = SceneTriangleMake(vertexD, newVertexE, vertexH);
triangles[5] = SceneTriangleMake(newVertexE, vertexF, vertexH);
for(int i=0;i<8;i++){
CaculateNormalForTriangles(&triangles[i]);
}
glBufferData(GL_ARRAY_BUFFER, sizeof(triangles), triangles, GL_STATIC_DRAW);
}
- (IBAction)near:(id)sender {
// baseEffect.transform.modelviewMatrix=GLKMatrix4Translate(baseEffect.transform.modelviewMatrix, 0, 0, 0.1);
}
@end
static SceneTriangle SceneTriangleMake(const SceneVertex vertexA,const SceneVertex vertexB,const SceneVertex vertexC){
SceneTriangle triangle;
triangle.vertices[0].position=vertexA.position;
triangle.vertices[0].normal=vertexA.normal;
triangle.vertices[1].position=vertexB.position;
triangle.vertices[1].normal=vertexB.normal;
triangle.vertices[2].position=vertexC.position;
triangle.vertices[2].normal=vertexC.normal;
return triangle;
}
static void CaculateNormalForTriangles(SceneTriangle *triangle){
GLKVector3 vecA=GLKVector3Subtract(triangle->vertices[0].position, triangle->vertices[1].position);
GLKVector3 vecB=GLKVector3Subtract(triangle->vertices[0].position, triangle->vertices[2].position);
GLKVector3 normal=GLKVector3Normalize(GLKVector3CrossProduct(vecA, vecB));
triangle->vertices[0].normal=normal;
triangle->vertices[1].normal=normal;
triangle->vertices[2].normal=normal;
}