cocos-2dx OPENGL渲染(1)

cocos-2dx OPENGL渲染(1),第1张

概述cocos-2dx v3.5 ,windows的opengl初始化: 0. main.cpp中会直接调用Application::getInstance()->run(), int Application::run(){ PVRFrameEnableControlWindow(false); // Main message loop: LARGE_INTEGER nLa

cocos-2dx v3.5,windows的opengl初始化:

0. main.cpp中会直接调用Application::getInstance()->run(),

int Application::run(){    PVRFrameEnableControlWindow(false);    // Main message loop:    LARGE_INTEGER nLast;    LARGE_INTEGER nNow;    queryPerformanceCounter(&nLast); <span >   //1-opengl像素格式等参数初始化</span>    <strong>initGLContextAttrs();</strong>  <span > //2-cocos初始化</span>    if (!applicationDIDFinishLaunching())    {        return 0;    }    auto director = Director::getInstance();    auto glvIEw = director->getopenGLVIEw();    // Retain glvIEw to avoID glvIEw being released in the while loop    glvIEw->retain();    while(!glvIEw->windowshouldClose())    {        queryPerformanceCounter(&nNow);        if (nNow.QuadPart - nLast.QuadPart > _animationInterval.QuadPart)        {            nLast.QuadPart = nNow.QuadPart - (nNow.QuadPart % _animationInterval.QuadPart);                        director->mainLoop();            glvIEw->pollEvents();        }        else        {            Sleep(1);        }    }    // Director should still do a cleanup if the window was closed manually.    if (glvIEw->isOpenglready())    {        director->end();        director->mainLoop();        director = nullptr;    }    glvIEw->release();    return true;}


1.下面初始化了opgl所使用的rgba所要使用的位数以及depthBit,stencilBits

voID AppDelegate::initGLContextAttrs(){    GLContextAttrs glContextAttrs = {8,8,24,8};    GLVIEw::setGLContextAttrs(glContextAttrs);}

2.cocos程序初始化入口:
bool AppDelegate::applicationDIDFinishLaunching()

 auto director = Director::getInstance();    auto glvIEw = director->getopenGLVIEw();    if(!glvIEw) {       <strong> glvIEw = GLVIEwImpl::create("Cpp Empty Test");</strong>        director->setopenGLVIEw(glvIEw);    }


3.create opgl窗口

GLVIEwImpl* GLVIEwImpl::create(const std::string& vIEwname){  auto ret = new (std::nothrow) GLVIEwImpl;  if(ret &&<strong> ret->initWithRect(vIEwname,Rect(0,960,640),1)</strong>) {    ret->autorelease();    return ret;  }  return nullptr;}
4.默认使用640,960的矩形创建窗口,下面会对窗口初始化参数值,像素格式是OpenGL窗口的重要属性,它包括是否使用双缓冲,颜色位数和类型以及深度位数等。
bool GLVIEwImpl::initWithRect(const std::string& vIEwname,Rect rect,float frameZoomFactor){    setVIEwname(vIEwname);    _frameZoomFactor = frameZoomFactor;
    <span > //windows像素格式</span>
 <strong>  lfwWindowHint(GLFW_RESIZABLE,GL_FALSE); //不可改变窗口大小    glfwWindowHint(GLFW_RED_BITS,_glContextAttrs.redBits); //1中初始化的参数传入    glfwWindowHint(GLFW_GREEN_BITS,_glContextAttrs.greenBits);    glfwWindowHint(GLFW_BLUE_BITS,_glContextAttrs.blueBits);    glfwWindowHint(GLFW_Alpha_BITS,_glContextAttrs.AlphaBits);    glfwWindowHint(GLFW_DEPTH_BITS,_glContextAttrs.depthBits);    glfwWindowHint(GLFW_STENCIL_BITS,_glContextAttrs.stencilBits);</strong>
    <span >//windows窗口创建</span>
    _mainWindow = <strong>glfwCreateWindow</strong>(rect.size.wIDth * _frameZoomFactor,rect.size.height * _frameZoomFactor,_vIEwname.c_str(),_monitor,nullptr);    glfwMakeContextCurrent(_mainWindow);<span >//make当前windows线程的上下文context</span>    glfwSetMousebuttonCallback(_mainWindow,GLFWEventHandler::onGLFWMouseCallBack);    glfwSetCursorPosCallback(_mainWindow,GLFWEventHandler::onGLFWMouseMoveCallBack);    glfwSetScrollCallback(_mainWindow,GLFWEventHandler::onGLFWMouseScrollCallback);    glfwSetCharCallback(_mainWindow,GLFWEventHandler::onGLFWCharCallback);    glfwSetKeyCallback(_mainWindow,GLFWEventHandler::onGLFWKeyCallback);    glfwSetwindowPosCallback(_mainWindow,GLFWEventHandler::onGLFWWindowPosCallback);    glfwSetFramebufferSizeCallback(_mainWindow,GLFWEventHandler::onGLFWframebuffersize);    glfwSetwindowsizeCallback(_mainWindow,GLFWEventHandler::onGLFWwindowsizefunCallback);    glfwSetwindowIconifyCallback(_mainWindow,GLFWEventHandler::onGLFWWindowIconifyCallback);    setFrameSize(rect.size.wIDth,rect.size.height);   <span >// OPENGL版本检查</span>    const glubyte* glVersion = glGetString(GL_VERSION);    if ( utils::atof((const char*)glVersion) < 1.5 )    {        char strComplain[256] = {0};        sprintf(strComplain,"OpenGL 1.5 or higher is required (your version is %s). Please upgrade the driver of your vIDeo card.",glVersion);        MessageBox(strComplain,"OpenGL version too old");        return false;    }   
    <span >//初始化GLEW,GLEW(OpenGL Extension Wrangler library)是一个跨平台的C/C++库,用来查询和加载OpenGL扩展</span>    initGlew();    // Enable point size by default.    glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);    return true;}
5.判断GLEW是否初始化成功
bool GLVIEwImpl::initGlew(){#if (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)    GLenum GlewInitResult = glewInit();<span >//初始化结果</span>    if (GLEW_OK != GlewInitResult)    {        MessageBox((char *)glewGetErrorString(GlewInitResult),"OpenGL error");        return false;    }    if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)    {        log("Ready for GLSL");    }    else    {        log("Not totally ready :(");    }    if (glewIsSupported("GL_VERSION_2_0"))    {        log("Ready for OpenGL 2.0");    }    else    {        log("OpenGL 2.0 not supported");    }#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
    if(glew_dynamic_binding() == false)<span >//启用帧缓冲对象</span>    {        MessageBox("No OpenGL framebuffer support. Please upgrade the driver of your vIDeo card.","OpenGL error");        return false;    }#endif#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)    return true;}


 通过上面的过程,cocos-2dx的opengl初始化过程就搞定了。通过initGLContextAttrs 设置opengl像素格式, 

在applicationDIDFinishLaunching中create opengl窗口,创建过程中,将先前设定的像素格式传入,然后根据参数创建默认大小为(960,640)的窗口。


不同平台上面,OpenGL的初始化流程不完全一样。详细的区别可以查看平台相关的CCGLVIEwImpl-平台(如CCGLVIEwImpl-androID)类,

cocos2d-x-3.5\cocos\platform\下

接下来就是游戏过程了(见下一章) 正在学习cocos-2dx 关于opengl渲染相关的东西,这里就抛砖引玉了。 总结

以上是内存溢出为你收集整理的cocos-2dx OPENGL渲染(1)全部内容,希望文章能够帮你解决cocos-2dx OPENGL渲染(1)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1068536.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存