[jimmyzhouj 翻译] Nehe iOS OpenGL ES 2.0教程 --Lesson 02
http://jimmyzhouj.blog.51cto.com/2317513/883520
[jimmyzhouj 翻译] Nehe iOS OpenGL ES 2.0教程
使用OpenGL将几何图形(geometry)绘制到屏幕上需要完成几个步骤。首先我们需要告诉OpenGL绘制什么几何图形。通常是一系列的三角形,每个三角形用三个顶点来指定。OpenGL为每个顶点调用顶点着色器(vertex shader),它可以通过旋转和移动来执行几何变换,或者是简单的光照(lighting)。得到的平面被光栅化(rasterized),意思是OpenGL会计算出被各平面覆盖的所有像素点。对每一个被覆盖的像素点,OpenGL会调用片段着色器(fragment shader,如果我们使用多重抽样,每个像素会运行不止一次,所以总的来说这是片段而不是像素)。片段着色器的任务是根据颜色,光照来决定片段的颜色,或者是将图像映射到几何图形上。
几何图形可以被指定为几种几何图元:GL_POINT, GL_LINES或者GL_TRIANGLES,其中直线和三角形都有一些变体,可以绘制chain的或者strip的。所有这些几何图元都是由一组顶点组成的。一个顶点是三维空间的一个点,参照坐标系是x轴指向正右方,y轴指向正上方,z轴垂直于屏幕指向读者。
- //create a triangle
- std::vector<float> geometryData;
- //4 floats define one vertex (x, y, z and w), first one is lower left
- geometryData.push_back(-0.5); geometryData.push_back(-0.5); geometryData.push_back(0.0); geometryData.push_back(1.0);
- //we go counter clockwise, so lower right vertex next
- geometryData.push_back( 0.5); geometryData.push_back(-0.5); geometryData.push_back(0.0); geometryData.push_back(1.0);
- //top vertex is last
- geometryData.push_back( 0.0); geometryData.push_back( 0.5); geometryData.push_back(0.0); geometryData.push_back(1.0);
- //generate an ID for our geometry buffer in the video memory and make it the active one
- glGenBuffers(1, &m_geometryBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, m_geometryBuffer);
- //send the data to the video memory
- glBufferData(GL_ARRAY_BUFFER, geometryData.size() * sizeof(float), &geometryData[0], GL_STATIC_DRAW);
- //create a color buffer, to make our triangle look pretty
- std::vector<float> colorData;
- //3 floats define one color value (red, green and blue) with 0 no intensity and 1 full intensity
- //each color triplet is assigned to the vertex at the same position in the buffer, so first color -> first vertex
- //first vertex is red
- colorData.push_back(1.0); colorData.push_back(0.0); colorData.push_back(0.0);
- //lower right vertex is green
- colorData.push_back(0.0); colorData.push_back(1.0); colorData.push_back(0.0);
- //top vertex is blue
- colorData.push_back(0.0); colorData.push_back(0.0); colorData.push_back(1.0);
- //generate an ID for the color buffer in the video memory and make it the active one
- glGenBuffers(1, &m_colorBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
- //send the data to the video memory
- glBufferData(GL_ARRAY_BUFFER, colorData.size() * sizeof(float), &colorData[0], GL_STATIC_DRAW);
- //load our shader
- m_shader = new Shader("shader.vert", "shader.frag");
- if(!m_shader->compileAndLink())
- {
- NSLog(@"Encountered problems when loading shader, application will crash...");
- }
- //tell OpenGL to use this shader for all coming rendering
- glUseProgram(m_shader->getProgram());
- //get the attachment points for the attributes position and color
- m_positionLocation = glGetAttribLocation(m_shader->getProgram(), "position");
- m_colorLocation = glGetAttribLocation(m_shader->getProgram(), "color");
- //check that the locations are valid, negative value means invalid
- if(m_positionLocation < 0 || m_colorLocation < 0)
- {
- NSLog(@"Could not query attribute locations");
- }
- //enable these attributes
- glEnableVertexAttribArray(m_positionLocation);
- glEnableVertexAttribArray(m_colorLocation);
- //bind the geometry VBO
- glBindBuffer(GL_ARRAY_BUFFER, m_geometryBuffer);
- //point the position attribute to this buffer, being tuples of 4 floats for each vertex
- glVertexAttribPointer(m_positionLocation, 4, GL_FLOAT, GL_FALSE, 0, NULL);
- //bint the color VBO
- glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
- //this attribute is only 3 floats per vertex
- glVertexAttribPointer(m_colorLocation, 3, GL_FLOAT, GL_FALSE, 0, NULL);
- //initiate the drawing process, we want a triangle, start at index 0 and draw 3 vertices
- glDrawArrays(GL_TRIANGLES, 0, 3);
函数glDrawArrays需要三个参数:我们要画的图元(可以是GL_POINTS,GL_LINE_STRIP,GL_LINE_LOOP, GL_LINES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_TRIANGLES之一),离缓冲区首个元素的偏移量,和需要画多少个顶点。
OpenGL Shading Language
- //the incoming vertex' position
- attribute vec4 position;
- //and its color
- attribute vec3 color;
- //the varying statement tells the shader pipeline that this variable
- //has to be passed on to the next stage (so the fragment shader)
- varying lowp vec3 colorVarying;
- //the shader entry point is the main method
- void main()
- {
- colorVarying = color; //save the color for the fragment shader
- gl_Position = position; //copy the position
- }
- //incoming values from the vertex shader stage.
- //if the vertices of a primitive have different values, they are interpolated!
- varying lowp vec3 colorVarying;
- void main()
- {
- //create a vec4 from the vec3 by padding a 1.0 for alpha
- //and assign that color to be this fragment's color
- gl_FragColor = vec4(colorVarying, 1.0);
- }
[jimmyzhouj 翻译] Nehe iOS OpenGL ES 2.0教程 --Lesson 02相关推荐
- OpenGL ES 2 0 (iOS)[06 1]:基础纹理
前言:如果你没有 OpenGL ES 2 的基础知识,请先移步 <OpenGL ES 2.0 (iOS) 笔记大纲> 学习一下基础的知识. 目录 一.软件运行效果演示 (一).最终效果 ( ...
- OpenGL ES 2.0 for Android教程(一)
OpenGL ES 2 前言&第一章 文章传送门 OpenGL ES 2.0 for Android教程(二) OpenGL ES 2.0 for Android教程(三) OpenGL ES ...
- 【AR实验室】OpenGL ES绘制相机(OpenGL ES 1.0版本)
0x00 - 前言 之前做一些移动端的AR应用以及目前看到的一些AR应用,基本上都是这样一个套路:手机背景显示现实场景,然后在该背景上进行图形学绘制.至于图形学绘制时,相机外参的解算使用的是V-SLA ...
- OpenGL ES 2.0 for iPhone Tutorial
来源:http://www.raywenderlich.com/3664/opengl-es-2-0-for-iphone-tutorial If you're new here, you may w ...
- IOS – OpenGL ES 调节图像单色 GPUImageMonochromeFilter
目录 一.简介 二.效果演示 三.源码下载 四.猜你喜欢 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 基础 零基础 Ope ...
- IOS – OpenGL ES 调节图像色度 GPUImageHueFilter
目录 一.简介 二.效果演示 三.源码下载 二.猜你喜欢 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 基础 零基础 Ope ...
- IOS – OpenGL ES 调节图像色彩替换 GPUImageFalseColorFilter
目录 一.简介 二.效果演示 三.源码下载 四.猜你喜欢 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 基础 零基础 Ope ...
- IOS – OpenGL ES 调节图像阴影 GPUImageHighlightShadowFilter
目录 一.简介 二.效果演示 三.源码下载 四.猜你喜欢 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 基础 零基础 Ope ...
- IOS – OpenGL ES 调节图像灰色 GPUImageGrayscaleFilter
目录 一.简介 二.效果演示 三.源码下载 四.猜你喜欢 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 基础 零基础 Ope ...
最新文章
- [转]autoid文件上传
- 漫扯:从polling到Websocket(ZZ)
- jQuery使用ajax跨域请求获取数据
- 公安网络安全部门封杀的2000家淘宝钓鱼网站
- VS2017新建HTML项目,VS2017创建项目模板和项模板(方便实用)
- 九号机器人田奇峰_九号公司成功登陆科创板
- 银行家算法及其c++代码实现
- [Asp.net Mvc]通过UrlHelper扩展为js,css静态文件添加版本号
- DirectX修复工具
- 利用matlab求解二维水动力,[转载]MIKE系列软件之二维河口与海岸模拟软件MIKE 21...
- uniapp实现打印PDF文件
- 应试教育——人性的扼杀
- android 10.0 Camera2 去掉后置摄像头 仅支持前置摄像头功能
- postman——集合——执行集合——脚本的执行顺序——验证
- linux系统进去是guest用户t,ubuntu普通用户变为root用户后,只能guest身份进系统(linux的权限问题)...
- Metasploit Framework(3)Meterpreter
- 用pyth写一个代码输入数字到列表中,直到输入空值为止 ,依次将每个数字的最大数挑出来,生成一个新的列表...
- DolphinPHP 框架wangeditor编辑器图片路径改为绝对链接
- 计算机说话技巧,每天学点说话技巧,让你成为一个沟通达人
- Android 9.0 切换系统语言
热门文章
- 学习笔记:Ng的深度书籍学习记录
- 利用stress-ng压测来理解linux平均负载
- 想拥有属于自己的名片吗?用word其实就可以做!为自己做一个吧!
- 【收藏款】平面设计标准尺寸规范总结
- 成功的背后!(给所有IT人)(转载来自http://blog.csdn.net/ysuncn/archive/2007/10/07/1814127.aspx)
- Mendeley中正确设置GB/T 7714-2005中文参考文献格式
- 未来社区系统整体解决方案
- Project2007操作手册(原创)
- 冰点还原精灵 7.30 简体中文版 每次重启 您的电脑都是焕然一新
- 页式管理 多级页表