OpenGL es3.0 初始化及渲染

时间:2022-12-15 02:31:13
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();

OpenGL es3.0  初始化及渲染

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"
};