标签:cocos2d-x opengl 绘制纹理 opengl es
在之前成功绘制变色的几何图形之后,今天利用Openg ES的可编程管线绘制出第一张纹理。学校时候不知道OpenGL的重要性,怕晦涩的语法,没有跟老师学习OpenGL的环境配置,如今只能利用cocos2dx 2.2.3 配置好的环境学习OpenGL ES。源代码来自《cocos2d-x高级开发教程》,注释是本人的。
void HelloWorld::draw() { //opengl世界坐标轴的读取和绘制默认是逆时针顺序 static GLfloat vertext[] = { 0.0f,0.0f,0.0f, 480.0f,0.0f,0.0f, 0.0f,320.0f,0.0f, 480.0f,320.0f,0.0f }; //但是纹理的读取则是顺时针,因为EG是用三角形 static GLfloat coord[] = { 0.0f,1.0f, 1.0f,1.0f, 0.0f,0.0f, 1.0f,0.0f }; static GLfloat colors[] = { 1.0f,1.0f,1.0f,1.0f, 1.0f,1.0f,1.0f,1.0f, 1.0f,1.0f,1.0f,1.0f, 1.0f,1.0f,1.0f,1.0f, }; //初始化纹理,即将纹理映射至openGL ES的世界坐标系中 static CCTexture2D * texture2d = CCTextureCache::sharedTextureCache()->addImage("HelloWorld.png"); coord[2] = coord[6] = texture2d->getMaxS(); coord[1] = coord[3] = texture2d->getMaxT(); //设置着色器 //开启着色器,坐标、颜色、纹理 ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex); //目前没看懂具体执行了的use()操作,启用可编程管线?? texture2d->getShaderProgram()->use(); //在2.2.3中setUniformsForBuiltins 替换掉 setUniformForModelViewPorjectionMatrix(), //设置统一属性,即M模型、V视图、P投影,完成从Opengl的世界坐标到屏幕坐标的映射 texture2d->getShaderProgram()->setUniformsForBuiltins(); //绑定纹理,texture2d->getName() 纹理在内存或显存中存储的下标(应该是下标,我没看纹理加载,还不了解GPU和CPU的通信) glBindTexture(GL_TEXTURE_2D,texture2d->getName()); //设置顶点,每个顶点向量维数为3,数据类型为GL_FLOAT,不归一化(真实物理坐标不能归一化),两个个顶点间的偏移量为0, glVertexAttribPointer(kCCVertexAttrib_Position,3,GL_FLOAT,GL_FALSE,0,vertext); glVertexAttribPointer(kCCVertexAttrib_TexCoords,2,GL_FLOAT,GL_FALSE,0,coord); glVertexAttribPointer(kCCVertexAttrib_Color,4,GL_FLOAT,GL_FALSE,0,colors); //绘图,从下标0开始,有4个顶点 glDrawArrays(GL_TRIANGLE_STRIP,0,4); }
标签:cocos2d-x opengl 绘制纹理 opengl es
原文地址:http://blog.csdn.net/jingzhewangzi/article/details/39649283