Opengl es2.0 学习笔记(一)初始化
官网
http://khronos.org/bugzilla
一、vs使用opengles2.0
1. 下载库区分x86 x64
2. C++ ->常规->包含目录->include文件夹
3. 链接器->附加依赖项:libEGL.lib,libGLESv2.lib
4. 链接器->常规->附加库目录
5. opengl es没有windows实现,angular提供了windows版 windows平台调用direct3D 需要copy这两个库扔到debug下 d3dcompiler_43.dll d3dcompiler_46.dll
二、初始化所用到的API
//创建display返回display
eglGetDisplay(EGLNativeDisplayType display_id);
//初始化egl
eglInitialize
//选择config
eglChooseConfig
//根据conig获取format
eglGetConfigAttrib
//创建surface
eglCreateWindowSurface
//创建上下文
eglCreateContext
//关联起来
eglMakeCurrent
//查询宽度高度
eglQuerySurface
//使用rgba清空缓冲区的颜色
glClearColor(1,0,0,1);
//! 清空缓冲区
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
//! 视口,在Windows窗口指定的位置和大小上绘制OpenGL内容
glViewport(0,0,_width,_height);
//双缓冲,把缓冲区内容贴到屏幕上
eglSwapBuffers(_display,_surface);
//销毁display
eglTerminate
//销毁上下文
eglDestroyContext
//销毁surface
eglDestroySurface
三、撸代码
#include "CELLWinApp.hpp"
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(hInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(nCmdShow);
CELL::CELLWinApp app(hInstance);
app.main(800,600);
return 0;
}
#pragma once
#include <Windows.h>
#include <tchar.h>
#include <EGL/egl.h>
#include <gles2/gl2.h>
namespace CELL
{
class CELLWinApp
{
public:
//! 实例句柄
HINSTANCE _hInstance;
//! 窗口句柄
HWND _hWnd;
//! 窗口的高度
int _width;
//! 窗口的宽度
int _height;
/// for gles2.0
EGLConfig _config;
EGLSurface _surface;
EGLContext _context;
EGLDisplay _display;
public:
CELLWinApp(HINSTANCE hInstance)
:_hInstance(hInstance)
{
WNDCLASSEX winClass;
winClass.lpszClassName = _T("CELLWinApp");
winClass.cbSize = sizeof(winClass);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
winClass.lpfnWndProc = wndProc;
winClass.hInstance = hInstance;
winClass.hIcon = 0;
winClass.hIconSm = 0;
winClass.hCursor = LoadCursor(hInstance, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
RegisterClassEx(&winClass);
}
virtual ~CELLWinApp()
{
UnregisterClass(_T("CELLWinApp"),_hInstance);
}
/**
* 初始化 OpenGLES2.0
*/
bool initOpenGLES20()
{
const EGLint attribs[] =
{
//surrface 画在窗口上
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BLUE_SIZE, 8, //b 8位
EGL_GREEN_SIZE, 8, //g 8位
EGL_RED_SIZE, 8, //r 8位
EGL_DEPTH_SIZE,24,//深度24位
EGL_NONE//
};
EGLint format(0);
EGLint numConfigs(0);
EGLint major;
EGLint minor;
//! 1
_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
//! 2init
eglInitialize(_display, &major, &minor);
//! 3
eglChooseConfig(_display, attribs, &_config, 1, &numConfigs);
eglGetConfigAttrib(_display, _config, EGL_NATIVE_VISUAL_ID, &format);
//! 4
_surface = eglCreateWindowSurface(_display, _config, _hWnd, NULL);
//! 5
EGLint attr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };
_context = eglCreateContext(_display, _config, 0, attr);
//! 6
if (eglMakeCurrent(_display, _surface, _surface, _context) == EGL_FALSE)
{
return false;
}
eglQuerySurface(_display, _surface, EGL_WIDTH, &_width);
eglQuerySurface(_display, _surface, EGL_HEIGHT, &_height);
return true;
}
/**
* 销毁OpenGLES2.0
*/
void destroyOpenGLES20()
{
if (_display != EGL_NO_DISPLAY)
{
eglMakeCurrent(_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (_context != EGL_NO_CONTEXT)
{
eglDestroyContext(_display, _context);
}
if (_surface != EGL_NO_SURFACE)
{
eglDestroySurface(_display, _surface);
}
eglTerminate(_display);
}
_display = EGL_NO_DISPLAY;
_context = EGL_NO_CONTEXT;
_surface = EGL_NO_SURFACE;
}
protected:
static LRESULT CALLBACK wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
CELLWinApp* pThis = (CELLWinApp*)GetWindowLong(hWnd,GWL_USERDATA);
if (pThis)
{
return pThis->onEvent(hWnd,msg,wParam,lParam);
}
if (WM_CREATE == msg)
{
CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
SetWindowLong(hWnd,GWL_USERDATA,(DWORD_PTR)pCreate->lpCreateParams);
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
public:
/**
* 事件函数
*/
virtual LRESULT onEvent(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
case WM_DESTROY:
{
::PostQuitMessage(0);
}
break;
case WM_MOUSEMOVE:
break;
default:
return DefWindowProc( hWnd, msg, wParam, lParam );
}
return S_OK;
}
/**
* 绘制函数
*/
virtual void render()
{
glClearColor(1,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0,0,_width,_height);
}
/**
* 主函数
*/
int main(int width,int height)
{
//创建窗口句柄
_hWnd = CreateWindowEx( NULL,
_T("CELLWinApp"),
_T("CELLWinApp"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
width,
height,
NULL,
NULL,
_hInstance,
this
);
if (_hWnd == 0)
{
return -1;
}
//显示窗口
ShowWindow(_hWnd,SW_SHOW);
//初始化opengl es2.0
if (!initOpenGLES20())
{
return false;
}
MSG msg = {0};
while(msg.message != WM_QUIT)
{
if (msg.message == WM_DESTROY ||
msg.message == WM_CLOSE)
{
break;
}
/**
* 有消息,处理消息,无消息,则进行渲染绘制
*/
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
render();
eglSwapBuffers(_display,_surface);
}
}
/**
* 销毁OpenGLES20
*/
destroyOpenGLES20();
return 0;
}
};
}
四、创建错误
–error LNK2019: 无法解析的外部符号 _main,该符号在函数___tmainCRTStartup 中被引用_
1.进入project->setting->c/c++,在category中选择preprocessor,在processor definitions中删除_CONSOLE,添加_WINDOWS
2.进入project->setting->Link, 在Projectoptions(子系统)中将 /subsystem:console改为/subsystem:windows.
3.保存设置,Rebuild All.
–找不到 #include <EGL/egl.h> #include <gles2/gl2.h>文件
C+±>常规->包含目录->include文件夹存在问题,请检查
–无法启动
debug下,没扔这两个库
d3dcompiler_43.dll
d3dcompiler_46.dll
–找不到libEGL.lib
–找不到libGLESv2.lib
链接器->附加依赖项:libEGL.lib,libGLESv2.lib
链接器->常规->附加库目录 这两步存在问题
文章来源: yujiang.blog.csdn.net,作者:鱼酱2333,版权归原作者所有,如需转载,请联系作者。
原文链接:yujiang.blog.csdn.net/article/details/83868183
- 点赞
- 收藏
- 关注作者
评论(0)