#import "ViewController.h" #import "sphere.h" #define BUFFER_OFFSET(i) ((char *)NULL + (i)) @interface ViewController () { //私有变量 GLuint _vertexArray; GLuint _positionBuffer; GLuint _normalBuffer; GLuint _textureBuffer; } @end @implementation ViewController @synthesize baseEffect; @synthesize earthTextureInfo; @synthesize moonTextureInfo; @synthesize modelviewMatrixStack; @synthesize earthRotationAngleDegrees; @synthesize moonRotationAngleDegrees; //设置一些常量 // Constants static const GLfloat SceneEarthAxialTiltDeg = 23.5f; static const GLfloat SceneDaysPerMoonOrbit = 28.0f; static const GLfloat SceneMoonRadiusFractionOfEarth = 0.25; static const GLfloat SceneMoonDistanceFromEarth = 1.5; // Setup a light to simulate the Sun - (void)configureLight { self.baseEffect.light0.enabled = GL_TRUE; self.baseEffect.light0.diffuseColor = GLKVector4Make( 1.0f, // Red 1.0f, // Green 1.0f, // Blue 1.0f);// Alpha self.baseEffect.light0.position = GLKVector4Make( 1.0f, 0.0f, 0.8f, 0.0f); self.baseEffect.light0.ambientColor = GLKVector4Make( 0.2f, // Red 0.2f, // Green 0.2f, // Blue 1.0f);// Alpha } - (void)viewDidLoad { [super viewDidLoad]; //1. 设置context EAGLContext *context = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES2]; [EAGLContext setCurrentContext:context]; //2.设置当前视图为GLView GLKView *glView = (GLKView *) self.view; NSAssert([glView isKindOfClass:[GLKView class]], @"View controller‘s view is not a GLKView"); //3.设置GLView的属性 glView.context = context; glView.drawableDepthFormat = GLKViewDrawableDepthFormat24; //4. 创建一个baseEffect self.baseEffect = [[GLKBaseEffect alloc] init]; //5. 自主设置场景 //创建一个stack,A newly created stack is initialized with the identity matrix. self.modelviewMatrixStack = GLKMatrixStackCreate(kCFAllocatorDefault); //设置light模拟太阳 [self configureLight]; //设置投影矩阵 self.baseEffect.transform.projectionMatrix = GLKMatrix4MakeOrtho( -1.0 * 768.0 / 1024.0, 1.0 * 768.0 / 1024.0, -1.0, 1.0, 1.0, 120.0); //设置模型视图矩阵 self.baseEffect.transform.modelviewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -5.0); //设置场景的背景颜色 glClearColor(0.0f, 0.1f, 0.1f, 1.0f); //设置用于绘制earth和moon的VAO glGenVertexArraysOES(1, &_vertexArray); glBindVertexArrayOES(_vertexArray); //绑定球体的顶点数据 glGenBuffers(1, &_positionBuffer); glBindBuffer(GL_ARRAY_BUFFER, _positionBuffer); glBufferData( GL_ARRAY_BUFFER, sizeof(sphereVerts), sphereVerts, GL_STATIC_DRAW); glEnableVertexAttribArray(GLKVertexAttribPosition); glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), BUFFER_OFFSET(0)); //绑定球体的法线数据 glGenBuffers(1, &_normalBuffer); glBindBuffer(GL_ARRAY_BUFFER, _normalBuffer); glBufferData( GL_ARRAY_BUFFER, sizeof(sphereNormals), sphereNormals, GL_STATIC_DRAW); glEnableVertexAttribArray(GLKVertexAttribNormal); glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), BUFFER_OFFSET(0)); //绑定球体的纹理数据 glGenBuffers(1, &_textureBuffer); glBindBuffer(GL_ARRAY_BUFFER, _textureBuffer); glBufferData( GL_ARRAY_BUFFER, sizeof(sphereTexCoords), sphereTexCoords, GL_STATIC_DRAW); glEnableVertexAttribArray(GLKVertexAttribTexCoord0); glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), BUFFER_OFFSET(0)); //Bind back to the default state glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArrayOES(0); // Setup Earth texture CGImageRef earthImageRef = [[UIImage imageNamed:@"Earth512x256.jpg"] CGImage]; earthTextureInfo = [GLKTextureLoader textureWithCGImage:earthImageRef options:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], GLKTextureLoaderOriginBottomLeft, nil] error:NULL]; // Setup Moon texture CGImageRef moonImageRef = [[UIImage imageNamed:@"Moon256x128.png"] CGImage]; moonTextureInfo = [GLKTextureLoader textureWithCGImage:moonImageRef options:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], GLKTextureLoaderOriginBottomLeft, nil] error:NULL]; // Initialize the matrix stack GLKMatrixStackLoadMatrix4( self.modelviewMatrixStack, self.baseEffect.transform.modelviewMatrix); // Initialize Moon position in orbit(轨道) self.moonRotationAngleDegrees = -20.0f; } //绘制模型一:地球 - (void)drawEarth { //设置纹理 self.baseEffect.texture2d0.name = earthTextureInfo.name; self.baseEffect.texture2d0.target = earthTextureInfo.target; //把基本的模型视图矩阵(最先设置的)入栈 GLKMatrixStackPush(self.modelviewMatrixStack); //对入栈矩阵进行操作 GLKMatrixStackRotate( // Rotate (tilt Earth‘s axis) self.modelviewMatrixStack, GLKMathDegreesToRadians(SceneEarthAxialTiltDeg), 1.0, 0.0, 0.0); GLKMatrixStackRotate( // Rotate about Earth‘s axis self.modelviewMatrixStack, GLKMathDegreesToRadians(earthRotationAngleDegrees), 0.0, 1.0, 0.0); //GLKMatrixStackGetMatrix4()函数从栈中获取矩阵 self.baseEffect.transform.modelviewMatrix = GLKMatrixStackGetMatrix4(self.modelviewMatrixStack); //开始绘制earth [self.baseEffect prepareToDraw]; //绘制earth glBindVertexArrayOES(_vertexArray); glDrawArrays(GL_TRIANGLES, 0, sphereNumVerts); //出栈 GLKMatrixStackPop(self.modelviewMatrixStack); self.baseEffect.transform.modelviewMatrix = GLKMatrixStackGetMatrix4(self.modelviewMatrixStack); } //绘制模型二:moon - (void)drawMoon { //设置月球纹理 self.baseEffect.texture2d0.name = moonTextureInfo.name; self.baseEffect.texture2d0.target = moonTextureInfo.target; GLKMatrixStackPush(self.modelviewMatrixStack); GLKMatrixStackRotate( // Rotate to position in orbit self.modelviewMatrixStack, GLKMathDegreesToRadians(moonRotationAngleDegrees), 0.0, 1.0, 0.0); GLKMatrixStackTranslate(// Translate to distance from Earth self.modelviewMatrixStack, 0.0, 0.0, SceneMoonDistanceFromEarth); GLKMatrixStackScale( // Scale to size of Moon self.modelviewMatrixStack, SceneMoonRadiusFractionOfEarth, SceneMoonRadiusFractionOfEarth, SceneMoonRadiusFractionOfEarth); GLKMatrixStackRotate( // Rotate Moon on its own axis self.modelviewMatrixStack, GLKMathDegreesToRadians(moonRotationAngleDegrees), 0.0, 1.0, 0.0); //设置当前模型的MV矩阵 self.baseEffect.transform.modelviewMatrix = GLKMatrixStackGetMatrix4(self.modelviewMatrixStack); //准备绘制Moon [self.baseEffect prepareToDraw]; //绘制Moon glBindVertexArrayOES(_vertexArray); glDrawArrays(GL_TRIANGLES, 0, sphereNumVerts); GLKMatrixStackPop(self.modelviewMatrixStack); self.baseEffect.transform.modelviewMatrix = GLKMatrixStackGetMatrix4(self.modelviewMatrixStack); } #pragma mask GLKView Delegate - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { // Update the angles every frame to animate // (one day every 60 display updates) //每帧调用一次代理方法 self.earthRotationAngleDegrees += 360.0f / 120.0f; self.moonRotationAngleDegrees += (360.0f / 120.0f) / SceneDaysPerMoonOrbit; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); [self drawEarth]; [self drawMoon]; } - (void)viewDidUnload { [super viewDidUnload]; // Make the view‘s context current GLKView *view = (GLKView *)self.view; //[AGLKContext setCurrentContext:view.context]; [EAGLContext setCurrentContext:view.context]; glDeleteBuffers(1, &_positionBuffer); glDeleteBuffers(1, &_normalBuffer); glDeleteBuffers(1, &_textureBuffer); // Stop using the context created in -viewDidLoad ((GLKView *)self.view).context = nil; [EAGLContext setCurrentContext:nil]; CFRelease(self.modelviewMatrixStack); self.modelviewMatrixStack = NULL; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)takeShouldUsePerspective:(UISwitch *)sender { GLfloat aspectRatio = (float)((GLKView *)self.view).drawableWidth / (float)((GLKView *)self.view).drawableHeight; if([sender isOn]) //UISwith 的属性 { self.baseEffect.transform.projectionMatrix = GLKMatrix4MakeFrustum( -1.0 * aspectRatio, 1.0 * aspectRatio, -1.0, 1.0, 1.0, 120.0); } else { self.baseEffect.transform.projectionMatrix = GLKMatrix4MakeOrtho( -1.0 * aspectRatio, 1.0 * aspectRatio, -1.0, 1.0, 1.0, 120.0); } }
原文:http://blog.csdn.net/xby1993/article/details/24002545