OpenGL es3.0 初始化及渲染
class FOpenglEs
{
public: /**
* 初始化 OpenGLES3.0
*/
bool initOpenGLES30(HWND hwnd)
{
EGLConfig config;
EGLint majorVersion;
EGLint minorVersion;
EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, , EGL_NONE }; if ( hwnd == NULL )
{
return GL_FALSE;
}
EGLNativeDisplayType displaytype = GetDC(hwnd); m_Display = eglGetDisplay( displaytype );
if ( m_Display == EGL_NO_DISPLAY )
{
return GL_FALSE;
} // Initialize EGL
if ( !eglInitialize ( m_Display, &majorVersion, &minorVersion ) )
{
return GL_FALSE;
} GLuint flags = ES_WINDOW_RGB;
{
EGLint numConfigs = ;
EGLint attribList[] =
{
EGL_RED_SIZE, ,
EGL_GREEN_SIZE, ,
EGL_BLUE_SIZE, ,
EGL_ALPHA_SIZE, ( flags & ES_WINDOW_ALPHA ) ? : EGL_DONT_CARE,
EGL_DEPTH_SIZE, ( flags & ES_WINDOW_DEPTH ) ? : EGL_DONT_CARE,
EGL_STENCIL_SIZE, ( flags & ES_WINDOW_STENCIL ) ? : EGL_DONT_CARE,
EGL_SAMPLE_BUFFERS, ( flags & ES_WINDOW_MULTISAMPLE ) ? : ,
// if EGL_KHR_create_context extension is supported, then we will use
// EGL_OPENGL_ES3_BIT_KHR instead of EGL_OPENGL_ES2_BIT in the attribute list
EGL_RENDERABLE_TYPE, GetContextRenderableType ( m_Display),
EGL_NONE
}; // Choose config
if ( !eglChooseConfig (m_Display, attribList, &config, , &numConfigs ) )
{
return GL_FALSE;
} if ( numConfigs < )
{
return GL_FALSE;
}
} // Create a surface
m_Surface = eglCreateWindowSurface (m_Display, config,
hwnd, NULL ); if ( m_Surface == EGL_NO_SURFACE )
{
return GL_FALSE;
} // Create a GL context
m_Context = eglCreateContext (m_Display, config,
EGL_NO_CONTEXT, contextAttribs ); if ( m_Context == EGL_NO_CONTEXT )
{
return GL_FALSE;
} // Make the context current
if ( !eglMakeCurrent (m_Display, m_Surface,
m_Surface, m_Context ) )
{
return GL_FALSE;
} return true; } EGLint GetContextRenderableType ( EGLDisplay eglDisplay )
{
#ifdef EGL_KHR_create_context
const char *extensions = eglQueryString ( eglDisplay, EGL_EXTENSIONS ); // check whether EGL_KHR_create_context is in the extension string
if ( extensions != NULL && strstr( extensions, "EGL_KHR_create_context" ) )
{
// extension is supported
return EGL_OPENGL_ES3_BIT_KHR;
}
#endif
// extension is not supported
return EGL_OPENGL_ES2_BIT;
}
/**
* 销毁OpenGLES3.0
*/
void destroyOpenGLES20()
{ } void SwapBuffers()
{
eglSwapBuffers(m_Display,m_Surface);
} void setViewPort( int x,int y,int width,int height )
{
glViewport(GLint(x),GLint(y),GLsizei(width),GLsizei(height));
} void clear(unsigned int mask)
{
//! GL_DEPTH_BUFFER_BIT
//! GL_COLOR_BUFFER_BIT
//! GL_COLOR_BUFFER_BIT
//! GL_STENCIL_BUFFER_BIT
//! GL_ACCUM_BUFFER_BIT;
//! GL_TRANSFORM_BIT
//! GL_ENABLE_BIT
//! GL_HINT_BIT
//! GL_EVAL_BIT
glClear(mask);
} void clearColor(float r,float g,float b,float a)
{
glClearColor(r,g,b,a);
} void CreateOglBuffer(OglBuffer* buf)
{
glGenBuffers(, &buf->m_VertexBufferId);
glBindBuffer(GL_ARRAY_BUFFER, buf->m_VertexBufferId); glBufferData(GL_ARRAY_BUFFER, sizeof(FVertex)* buf->m_vertexs.size(), &buf->m_vertexs[].m_Pos.x, GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, ); glGenBuffers(, &buf->m_IndexBufferId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buf->m_IndexBufferId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(FMeshTriangle) * buf->m_Indexs.size(), &buf->m_Indexs[].index0, GL_DYNAMIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,); glGenVertexArrays(,&buf->m_VaoBufferId);
glBindVertexArray(buf->m_VaoBufferId); glBindBuffer(GL_ARRAY_BUFFER,buf->m_VertexBufferId); glEnableVertexAttribArray();
glEnableVertexAttribArray();
glVertexAttribPointer(,,GL_FLOAT,(GLboolean)false,sizeof(FVertex),);
glVertexAttribPointer(,,GL_FLOAT,(GLboolean)false,sizeof(FVertex),(GLvoid*)(sizeof(float)*)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,buf->m_IndexBufferId); glBindVertexArray();
glBindBuffer(GL_ARRAY_BUFFER,);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,);
glDisableVertexAttribArray();
glDisableVertexAttribArray();
printf("vertex:%d bufId:%d \n",buf->m_vertexs.size(),buf->m_VertexBufferId);
printf("index:%d bufId:%d \n",buf->m_Indexs.size(),buf->m_IndexBufferId); } void bindVao(OglBuffer* buf)
{
glBindVertexArray(buf->m_VaoBufferId);
} void destroyVertexBuffer( OglBuffer* buf )
{ glDeleteVertexArrays(,&buf->m_VaoBufferId);
glDeleteBuffers(,&buf->m_IndexBufferId);
glDeleteBuffers(,&buf->m_VertexBufferId); } void drawBuffers(const OglBuffer* buf)
{
glBindVertexArray(buf->m_VaoBufferId);
glDrawElements(GL_TRIANGLES,buf->m_Indexs.size() *,GL_UNSIGNED_INT,);
} public:
/// Display handle
EGLNativeDisplayType eglNativeDisplay; /// Window handle
EGLNativeWindowType eglNativeWindow; EGLConfig m_Config;
EGLSurface m_Surface;
EGLContext m_Context;
EGLDisplay m_Display; /// Window width
GLint m_width; /// Window height
GLint m_height;
};
class FVertex
{
public:
FVertex()
{ }
vec3 m_Pos;
vec3 m_Normal;
vec2 m_Uv;
vec3 m_tangent;
vec3 m_bitangent; }; class OglBuffer
{
public:
OglBuffer()
{
m_VaoBufferId = ;
m_VertexBufferId =;
m_IndexBufferId =; } Fuint m_VaoBufferId;
Fuint m_VertexBufferId;
Fuint m_IndexBufferId; std::vector<FVertex> m_vertexs;
std::vector<FMeshTriangle> m_Indexs; };
用的时候直接这样就好了,比较方便
g_Opengles.setViewPort(,,,);
g_Opengles.clearColor(0.1,,0.1,);
g_Opengles.clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); m_Shader.useProgram(); m_Shader.setUniformMatrix4fv("_MVP",&m_ProjectMatrix.m00); g_Opengles.drawBuffers(&m_Buff); g_Opengles.SwapBuffers();
shader ,不能用varying,只能用in out这样
const char* vs =
{
"#version 300 es \n"
"layout(location = 0) in vec4 vPosition; \n"
"layout(location = 1) in vec3 vNormal; \n"
"uniform mat4 _MVP; \n"
"out vec3 oNormal; \n"
"void main() \n"
"{ \n"
" gl_Position = _MVP * vPosition; \n"
" oNormal = vNormal; \n"
"} \n"
};
const char* ps =
{
"#version 300 es \n"
"precision mediump float; \n"
"out vec4 fragColor; \n"
"in vec3 oNormal; \n"
"void main() \n"
"{ \n"
" \n"
" float dotN = dot(oNormal,vec3(0,1,0)); \n"
" float realColor = max(0.0,dotN); \n"
" fragColor = vec4 ( realColor, realColor, realColor, 1.0 ); \n"
"} \n"
};
OpenGL es3.0 初始化及渲染的更多相关文章
- OpenGL ES3.0
到今天,喜欢上了非常酷的图片处理和游戏,经了解,大部分都要使用opengl es,所以准备开始学习,然后深入学习cocos2d,GPUImage.平台为IOS OpenGL ES OpenGL ES ...
- [工作积累] OpenGL ES3.0: glInvalidateFramebuffer
https://www.khronos.org/opengles/sdk/docs/man3/html/glInvalidateFramebuffer.xhtml 这个在GLES2.0上只有Exten ...
- OpenGL ES3使用MSAA(多重采样抗锯齿)的方法
昨晚花费了我2个多小时的时间终于把OpenGL ES3.0中的MSAA给搞定了.在OpenGL ES2.0中,Khronos官方没有引入标准的MSAA全屏抗锯齿的方法,而Apple则采用了自己的GL_ ...
- Cocos2d-x中使用OpenGL ES2.0编写shader
这几天在看子龙山人的关于OpenGL的文章,先依葫芦画瓢,能看到些东西,才能慢慢深入了解,当入门文章不错,但是其中遇到的一些问题,折腾了一些时间,为了方便和我一样的小白们,在这篇文章中进行写补充. O ...
- iOS开发——图形编程OC篇&OpenGL ES2.0编程步骤
OpenGL ES2.0编程步骤 OpenGL ES (OpenGL for Embedded Systems) 是 OpenGL 三维图形 API 的子集,针对手机.PDA和游戏主机等嵌入式设备而设 ...
- OpenGL ES2.0入门详解
引自:http://blog.csdn.net/wangyuchun_799/article/details/7736928 1.决定你要支持的OpenGL ES的版本.目前,OpenGL ES包含 ...
- OpenGL 4.0的Tessellation Shader(细分曲面着色器)
细分曲面着色器(Tessellation Shader)处于顶点着色器阶段的下一个阶段,我们可以看以下链接的OpenGL渲染流水线的图:https://www.opengl.org/wiki/Rend ...
- OpenGL ES2.0编程三步曲 -转
原地址:http://blog.csdn.net/myarrow/article/details/7707943 1. 保存全局变量的数据结构 以下例子程序均基于Linux平台. typedef st ...
- OpenGL ES2.0 基本编程
1. EGL OpenGL ES命令须要一个rendering context和一个drawing surface. Rendering Context: 保存当前的OpenGL ES状态. Draw ...
随机推荐
- 写给自己看的Linux运维基础(二) - Apache/MySQL. 安全设置. 定时任务
本文使用环境为CentOS 6 Apache, PHP, MySQL等常用软件均可通过yum安装包获取 yum install httpd php mysql-server # mysql: 客户端; ...
- [Android] Android Sutdio on Surface Pro 3
Install Android Studio http://www.android-studio.org/index.php/download/androidstudio-download-baidu ...
- ubuntu安装redis
1.下载安装root@21ebdf03a086:/# apt-cache search redisroot@21ebdf03a086:/# apt-get install redis-server a ...
- JSP JSTL EL
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> Html代码 复制代 ...
- SQL order by的用法
首先,order by是用来写在where之后,给多个字段来排序的一个DQL查询语句. 其次,order by写法: 1. select 字段列表/* from 表名 where 条件 order ...
- 【Android Studio】Android Studio 安装及设置
版权所有, 禁止转载, 如有需要, 请站内联系. 本文地址: http://blog.csdn.net/caroline_wendy/article/details/20845807 时间: 2014 ...
- Atitit.dwr3 不能显示错误详细信息的解决方案,控件显示错误详细信息的解决方案 java .net php
Atitit.dwr3 不能显示错误详细信息的解决方案,控件显示错误详细信息的解决方案 java .net php 1. Keyword/subtitle 1 2. 使用dwr3的异常convert处 ...
- 详解Bootstrap面板组件
面板组件主要作用是用来处理一些其他组件无法完成的功能,在不同的版本中具有不同的源码: LESS:panels.less SASS:_panels.scss 基础面板非常简单,就是一个div容器中运用了 ...
- FindFriendsServer服务搭建
本文介绍如何搭建FindFriendsServer(https://github.com/hnrainll/FindFriendsServer)所需的环境. 环境需要: Windows+Apache+ ...
- iOS7隐藏状态栏 statusBar
转:http://blog.csdn.net/dqjyong/article/details/17896145 评:通过这点变化,可以看出苹果倾向于使用delegate取代全局变量. IOS7中,不仅 ...