新建一个 xcode 项目,在 ViewController.h 文件导入 <GLKit/GLKit.h> 并继承 GLKViewController,Main.storyboard 中把 View 的父类改为 GLKView,然后在 ViewController.m 实现代码。
根据下图绘制金字塔并实现光照
ViewController.m 代码如下
#import "ViewController.h"
//顶点数据结构
typedef struct {
GLKVector3 position; //顶点向量
GLKVector3 normal; //法线向量
}SceneVertex;
@interface ViewController ()
@property(nonatomic,strong)EAGLContext * myContext;
@property(nonatomic,strong)GLKBaseEffect * baseEffect;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//新建OpenGL ES 上下文
EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
//设置GLKView
GLKView * glkView = (GLKView *)self.view;
glkView.context = context;
glkView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
glkView.drawableDepthFormat = GLKViewDrawableDepthFormat24;
[EAGLContext setCurrentContext:context];
//1.金字塔Effect
self.baseEffect = [[GLKBaseEffect alloc] init];
self.baseEffect.constantColor = GLKVector4Make(0.1f, 0.2f, 0.3f, 1.0f);
self.baseEffect.light0.enabled = GL_TRUE;
//光的漫射部分 GLKVector4Make(R,G,B,A)
self.baseEffect.light0.diffuseColor = GLKVector4Make(0.7f, 0.7f, 0.7f, 1.0f);
//世界坐标中的光的位置
self.baseEffect.light0.position = GLKVector4Make(1.0f, 1.0f, 0.5f, 0.0f);
//3.调整模型矩阵,更好的观察
//可以尝试不执行这段代码,改为false
if (true)
{
//围绕x轴旋转-60度
//返回一个4x4矩阵进行绕任意矢量旋转
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeRotation(GLKMathDegreesToRadians(-60.0f), 1.0f, 0.0f, 0.0f);
//围绕z轴,旋转-30度
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(-30.0f), 0.0f, 0.0f, 1.0f);
//围绕Z方向,移动0.25f
modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, 0.0f, 0.0f, 0.25f);
//设置baseEffect模型矩阵
self.baseEffect.transform.modelviewMatrix = modelViewMatrix;
}
//设置顶点
GLKVector3 vertexA = {-0.5, 0.5, -0.5};
GLKVector3 vertexB = {-0.5, 0.0, -0.5};
GLKVector3 vertexC = {-0.5, -0.5, -0.5};
GLKVector3 vertexD = { 0.0, 0.5, -0.5};
GLKVector3 vertexE = { 0.0, 0.0, 0.0};
GLKVector3 vertexF = { 0.0, -0.5, -0.5};
GLKVector3 vertexG = { 0.5, 0.5, -0.5};
GLKVector3 vertexH = { 0.5, 0.0, -0.5};
GLKVector3 vertexI = { 0.5, -0.5, -0.5};
SceneVertex vertices[24];
//求出平面法向量
vertices[0] = (SceneVertex){vertexA,SceneTriangleFaceNormal(vertexA, vertexB,vertexD)};
vertices[1] = (SceneVertex){vertexB,SceneTriangleFaceNormal(vertexA, vertexB,vertexD)};
vertices[2] = (SceneVertex){vertexD,SceneTriangleFaceNormal(vertexA, vertexB,vertexD)};
vertices[3] = (SceneVertex){vertexB,SceneTriangleFaceNormal(vertexB, vertexC,vertexF)};
vertices[4] = (SceneVertex){vertexC,SceneTriangleFaceNormal(vertexB, vertexC,vertexF)};
vertices[5] = (SceneVertex){vertexF,SceneTriangleFaceNormal(vertexB, vertexC,vertexF)};
vertices[6] = (SceneVertex){vertexD,SceneTriangleFaceNormal(vertexD, vertexB,vertexE)};
vertices[7] = (SceneVertex){vertexB,SceneTriangleFaceNormal(vertexD, vertexB,vertexE)};
vertices[8] = (SceneVertex){vertexE,SceneTriangleFaceNormal(vertexD, vertexB,vertexE)};
vertices[9] = (SceneVertex){vertexE,SceneTriangleFaceNormal(vertexE, vertexB,vertexF)};
vertices[10] = (SceneVertex){vertexB,SceneTriangleFaceNormal(vertexE, vertexB,vertexF)};
vertices[11] = (SceneVertex){vertexF,SceneTriangleFaceNormal(vertexE, vertexB,vertexF)};
vertices[12] = (SceneVertex){vertexD,SceneTriangleFaceNormal(vertexD, vertexE,vertexH)};
vertices[13] = (SceneVertex){vertexE,SceneTriangleFaceNormal(vertexD, vertexE,vertexH)};
vertices[14] = (SceneVertex){vertexH,SceneTriangleFaceNormal(vertexD, vertexE,vertexH)};
vertices[15] = (SceneVertex){vertexE,SceneTriangleFaceNormal(vertexE, vertexF,vertexH)};
vertices[16] = (SceneVertex){vertexF,SceneTriangleFaceNormal(vertexE, vertexF,vertexH)};
vertices[17] = (SceneVertex){vertexH,SceneTriangleFaceNormal(vertexE, vertexF,vertexH)};
vertices[18] = (SceneVertex){vertexG,SceneTriangleFaceNormal(vertexG, vertexD,vertexH)};
vertices[19] = (SceneVertex){vertexD,SceneTriangleFaceNormal(vertexG, vertexD,vertexH)};
vertices[20] = (SceneVertex){vertexH,SceneTriangleFaceNormal(vertexG, vertexD,vertexH)};
vertices[21] = (SceneVertex){vertexH,SceneTriangleFaceNormal(vertexH, vertexF,vertexI)};
vertices[22] = (SceneVertex){vertexF,SceneTriangleFaceNormal(vertexH, vertexF,vertexI)};
vertices[23] = (SceneVertex){vertexI,SceneTriangleFaceNormal(vertexH, vertexF,vertexI)};
//开辟缓存区
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(SceneVertex) * 24, vertices, 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));
}
#pragma mark - 根据顶点求出法向量
GLKVector3 SceneTriangleFaceNormal(GLKVector3 a,GLKVector3 b,GLKVector3 c)
{
//vectorA = b - a
GLKVector3 vectorA = GLKVector3Subtract(b,a);
//vectorB = c - a
GLKVector3 vectorB = GLKVector3Subtract(c,a);
//通过 向量A和向量B的叉积求出平面法向量,单元化后返回
return GLKVector3Normalize(GLKVector3CrossProduct(vectorA, vectorB));
}
#pragma mark - GLKView DrawRect
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
[self.baseEffect prepareToDraw];
glDrawArrays(GL_TRIANGLES, 0, 24);
}
@end
运行效果如下