您的位置:首页 > 运维架构

OpenGL es3.0 初始化及渲染

2016-12-06 15:01 501 查看
class FOpenglEs
{
public:

/**
*   初始化 OpenGLES3.0
*/
bool    initOpenGLES30(HWND hwnd)
{
EGLConfig config;
EGLint majorVersion;
EGLint minorVersion;
EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 3, 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 = 0;
EGLint attribList[] =
{
EGL_RED_SIZE,       5,
EGL_GREEN_SIZE,     6,
EGL_BLUE_SIZE,      5,
EGL_ALPHA_SIZE,     ( flags & ES_WINDOW_ALPHA ) ? 8 : EGL_DONT_CARE,
EGL_DEPTH_SIZE,     ( flags & ES_WINDOW_DEPTH ) ? 8 : EGL_DONT_CARE,
EGL_STENCIL_SIZE,   ( flags & ES_WINDOW_STENCIL ) ? 8 : EGL_DONT_CARE,
EGL_SAMPLE_BUFFERS, ( flags & ES_WINDOW_MULTISAMPLE ) ? 1 : 0,
// 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, 1, &numConfigs ) )
{
return GL_FALSE;
}

if ( numConfigs < 1 )
{
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(1, &buf->m_VertexBufferId);
glBindBuffer(GL_ARRAY_BUFFER, buf->m_VertexBufferId);

glBufferData(GL_ARRAY_BUFFER, sizeof(FVertex)* buf->m_vertexs.size(), &buf->m_vertexs[0].m_Pos.x, GL_DYNAMIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, 0);

glGenBuffers(1, &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[0].index0, GL_DYNAMIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);

glGenVertexArrays(1,&buf->m_VaoBufferId);
glBindVertexArray(buf->m_VaoBufferId);

glBindBuffer(GL_ARRAY_BUFFER,buf->m_VertexBufferId);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0,3,GL_FLOAT,(GLboolean)false,sizeof(FVertex),0);
glVertexAttribPointer(1,3,GL_FLOAT,(GLboolean)false,sizeof(FVertex),(GLvoid*)(sizeof(float)*3));

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,buf->m_IndexBufferId);

glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
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(1,&buf->m_VaoBufferId);
glDeleteBuffers(1,&buf->m_IndexBufferId);
glDeleteBuffers(1,&buf->m_VertexBufferId);

}

void drawBuffers(const OglBuffer* buf)
{
glBindVertexArray(buf->m_VaoBufferId);
glDrawElements(GL_TRIANGLES,buf->m_Indexs.size() *3,GL_UNSIGNED_INT,0);
}

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 =0 ;
m_VertexBufferId =0;
m_IndexBufferId =0;

}

Fuint m_VaoBufferId;
Fuint m_VertexBufferId;
Fuint m_IndexBufferId;

std::vector<FVertex>       m_vertexs;
std::vector<FMeshTriangle> m_Indexs;

};


用的时候直接这样就好了,比较方便

g_Opengles.setViewPort(0,0,800,600);
g_Opengles.clearColor(0.1,1,0.1,0);
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"
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: