//
// ViewController.m
// G2D
//
// Created by on 2017/10/13.
// Copyright © 2017年 GongYan. All rights reserved.
//
#import "ViewController.h"
typedef struct {
GLKVector3 positionCoords;
}SceneVertex;
static const SceneVertex vertices[] ={
{{-0.5f, -0.5f, 0.0} },
{{0.5f, -0.5f, 0.0}},
{{-0.5f, 0.5f, 0.0}}
};
@interface ViewController ()
{
}
@property (strong, nonatomic) EAGLContext *context;
@property (strong, nonatomic) GLKBaseEffect *effect;
@end
@implementation ViewController
@synthesize effect;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if(!self.context)
{
NSLog(@"Failed to create ES context");
}
[EAGLContext setCurrentContext:self.context];
GLKView *view = (GLKView*)self.view;
NSAssert([view isKindOfClass:[GLKView class]], @"View controller 's view is not a glkview");
view.context = self.context;
// drawableColorFormat
// 你的OpenGL上下文有一个缓冲区,它用以存储将在屏幕中显示的颜色。你可以使用其属性来设置缓冲区中每个像素的颜色格式。
// 缺省值是GLKViewDrawableColorFormatRGBA8888,即缓冲区的每个像素的最小组成部分(-个像素有四个元素组成 RGBA)使用8个bit(如R使用8个bit)(所以每个像素4个字节 既 4*8 个bit)。这非常好,因为它给了你提供了最广泛的颜色范围,让你的app看起来更好。
// 但是如果你的app允许更小范围的颜色,你可以设置为GLKViewDrawableColorFormatRGB565,从而使你的app消耗更少的资源(内存和处理时间)。
view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
// drawableDepthFormat
// 你的OpenGL上下文还可以(可选地)有另一个缓冲区,称为深度缓冲区。这帮助我们确保更接近观察者的对象显示在远一些的对象的前面(意思就是离观察者近一些的对象会挡住在它后面的对象)。
// 其缺省的工作方式是:OpenGL把接近观察者的对象的所有像素存储到深度缓冲区,当开始绘制一个像素时,它(OpenGL)首先检查深度缓冲区,看是否已经绘制了更接近观察者的什么东西,如果是则忽略它(要绘制的像素,就是说,在绘制一个像素之前,看看前面有没有挡着它的东西,如果有那就不用绘制了)。否则,把它增加到深度缓冲区和颜色缓冲区。
// 你可以设置这个属性,以选择深度缓冲区的格式。缺省值是GLKViewDrawableDepthFormatNone,意味着完全没有深度缓冲区。
// 但是如果你要使用这个属性(一般用于3D游戏),你应该选择GLKViewDrawableDepthFormat16或GLKViewDrawableDepthFormat24。这里的差别是使用GLKViewDrawableDepthFormat16将消耗更少的资源,但是当对象非常接近彼此时,你可能存在渲染问题()。
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
self.effect = [[GLKBaseEffect alloc] init];
self.effect.useConstantColor = GL_TRUE;
self.effect.constantColor = GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glGenBuffers(1, &vertexBufferId);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferId);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
if([self isViewLoaded] && ([[self view] window] == nil)){
self.view = nil;
if([EAGLContext currentContext] == self.context){
[EAGLContext setCurrentContext:nil];
}
self.context = nil;
}
}
//update glkView 这两个方法每帧都执行一次(循环执行),一般执行频率与屏幕刷新率相同(但也可以更改)。
//第一次循环时,先调用“glkView”再调用“update”。
//一般,将场景数据变化放在“update”中,而渲染代码则放在“glkView”中。
- (void)update
{
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
[self.effect prepareToDraw];
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition,
3,
GL_FLOAT,
GL_FALSE,
sizeof(SceneVertex),
NULL);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
-(const char*) GetFileResurse:(const char*) fileName :(const char*) fileType
{
NSString *NfileName = [[NSString alloc] initWithCString:(const char*)fileName encoding:NSUTF8StringEncoding];
NSString *NfileType = [[NSString alloc] initWithCString:(const char*)fileType encoding:NSUTF8StringEncoding];
NSString *filePath = [[NSBundle mainBundle] pathForResource:NfileName ofType:NfileType];
NSError *error;
NSString *fileData = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
return [fileData UTF8String];
}
@end
1、画一个三角形
最新推荐文章于 2022-04-12 01:35:10 发布