前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OpenGL ES _ 入门练习_002

OpenGL ES _ 入门练习_002

作者头像
酷走天涯
发布2022-05-13 16:18:25
2520
发布2022-05-13 16:18:25
举报

[OpenGL ES _ 入门_01](http://www.jianshu.com/p/f66906b27819)

[OpenGL ES _ 入门_02](http://www.jianshu.com/p/dc49c946438e)

[OpenGL ES _ 入门_03](http://www.jianshu.com/p/00b5be729e4b)

[OpenGL ES _ 入门_04](http://www.jianshu.com/p/516b0b8be9d8)

[OpenGL ES _ 入门_05](http://www.jianshu.com/p/08c51c298d47)

[OpenGL ES _ 入门练习_01](http://www.jianshu.com/p/eebaf64e3e0a)

[OpenGL ES _ 入门练习_02](http://www.jianshu.com/p/6155d60dab20)

[OpenGL ES _ 入门练习_03](http://www.jianshu.com/p/36d9dac03345)

[OpenGL ES _ 入门练习_04](http://www.jianshu.com/p/1ca30e9387dd)

[OpenGL ES _ 入门练习_05](http://www.jianshu.com/p/ac9375962f34)

[OpenGL ES _ 入门练习_06](http://www.jianshu.com/p/c63dc219f7a0)

[OpenGL ES _ 着色器 _ 介绍](http://www.jianshu.com/p/309d489bc344)

[OpenGL ES _ 着色器 _ 程序](http://www.jianshu.com/p/ed0c617bcd67)

[OpenGL ES _ 着色器 _ 语法](http://www.jianshu.com/p/c5b89b294995)

[OpenGL ES_着色器_纹理图像](http://www.jianshu.com/p/8c0ad4e3e40f)

[OpenGL ES_着色器_预处理](http://www.jianshu.com/p/5e9837b0b219)

[OpenGL ES_着色器_顶点着色器详解](http://www.jianshu.com/p/9d7dca6b70c7)

[OpenGL ES_着色器_片断着色器详解](http://www.jianshu.com/p/55461927c419 )

[OpenGL ES_着色器_实战01](http://www.jianshu.com/p/18d6b37363c8)

[OpenGL ES_着色器_实战02](http://www.jianshu.com/p/45d959c8f1db)

[OpenGL ES_着色器_实战03](http://www.jianshu.com/p/0f740901da59)

演示效果:

学习目标:绘制一个旋转移动的立方体

技术: OpenGL ES 1

实现思路:

第一步: 创建GLKViewController 控制器(在里面实现方法)

第二步: 创建EAGContext 跟踪所有状态,命令和资源

第三步: 清除命令

第四步: 创建投影坐标系

第五步: 创建对象坐标

第六步: 导入顶点数据

第七步: 导入颜色数据

第八步: 绘制

代码部分:

/**
*  创建EAGContext 跟踪所有状态,命令和资源
*/
- (void)createEagContext{
self.eagContext = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES1];
[EAGLContext setCurrentContext:self.eagContext];
}
/**
*  配置view
*/
- (void)configure{
GLKView *view = (GLKView*)self.view;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
view.context = self.eagContext;
}
/**
*  清除
*/
-(void)clear{
glEnable(GL_DEPTH_TEST);
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
}
/**
*  创建投影坐标
*/
- (void)initProjectionMatrix{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
/**
*  创建物体坐标
*/
-(void)initModelViewMatrix{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
static GLfloat transY = 0.0;
static GLfloat z=-2.0;
//1
static GLfloat spinX=0;
static GLfloat spinY=0;
glTranslatef(0.0, (GLfloat)(sinf(transY)/2.0), z);
glRotatef(spinY, 0.0, 1.0, 0.0);
glRotatef(spinX, 1.0, 0.0, 0.0);
transY += 0.075f;
spinY+=.25;
spinX+=.25;
}
/**
*  导出顶点坐标
*  glVertexPointer 第一个参数:每个顶点数据的个数,第二个参数,顶点数据的数据类型,第三个偏移量,第四个顶点数组地址
*/
- (void)loadVertexData{
glVertexPointer(3, GL_FLOAT, 0, cubeVertices);
glEnableClientState(GL_VERTEX_ARRAY);
}
/**
*  导入颜色数据
*/
- (void)loadColorBuffer{
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeColors);
glEnableClientState(GL_COLOR_ARRAY);
}
/**
*  导入索引数据
*/
-(void)draw{
// 开启剔除面功能
glEnable(GL_CULL_FACE);                                                             //3
glCullFace(GL_BACK); // 剔除背面
glDrawElements( GL_TRIANGLE_FAN, 18, GL_UNSIGNED_BYTE, tfan1);
glDrawElements( GL_TRIANGLE_FAN, 18, GL_UNSIGNED_BYTE, tfan2);
}
/**
*  设置窗口及投影坐标的位置
*/
-(void)setClipping
{
float aspectRatio;
const float zNear = .1;
const float zFar = 1000;
const float fieldOfView = 60.0;
GLfloat    size;
CGRect frame = [[UIScreen mainScreen] bounds];
aspectRatio=(float)frame.size.width/(float)frame.size.height;
[self initProjectionMatrix];
size = zNear * tanf(GLKMathDegreesToRadians (fieldOfView) / 2.0);
// 设置视图窗口的大小 和 坐标系统
glFrustumf(-size, size, -size /aspectRatio, size /aspectRatio, zNear, zFar);
glViewport(0, 0, frame.size.width, frame.size.height);
}
GLKViewController 实现代码
- (void)viewDidLoad {
[super viewDidLoad];
[self createEagContext];
[self configure];
[self setClipping];
}
-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
[self clear];
[self initModelViewMatrix];
[self loadVertexData];
[self loadColorBuffer];
[self draw];
}

代码下载地址: https://github.com/XJALYN/OpenGLES_004

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档