cocos2d-3D模型 抗锯齿方法

来源:互联网 发布:龙芯3b1500 php mysql 编辑:程序博客网 时间:2024/04/30 00:45

随着cocos2d-x对3D特性的支持,很多开发者开始尝试在自己的游戏中载入3D模型,不过最近有些开发者表示不能接受3D模型显示时边缘的锯齿现象,特别是在windows上看着会更明显,这里给大家提供一个方法在不同平台上解决这个问题。

Windows/Mac平台:
cocos/platform/desktop/CCGLViewImpl.cpp 然后在GLViewImpl::initWithRect函数中添加下面语句
glfwWindowHint(GLFW_SAMPLES,4);4是一个采样的等级,你可以输入2,4,8 数值越大抗锯齿的效果越好,不过经过本人测试一般的android机器也就支持到4再高就无法运行了。

IOS平台:
找到工程目录下proj.ios_mac/ios/AppController.mm文件,然后找到下面这行代码
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
修改 multiSampling: NO 为 multiSampling: YES
修改 numberOfSamples: 0 为 numberOfSamples: 4

Android平台:
首先,在cocos\platform\android\java\src\org\cocos2dx\lib目录下找到Cocos2dxActivity.java文件,在文件头包含下面几项:
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLDisplay;
import android.opengl.GLSurfaceView.EGLConfigChooser;
接下来,在Cocos2dxActivity类的内部添加一个类 如下: 
public class BaseConfigChooser implements EGLConfigChooser {
public BaseConfigChooser(){
}
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display){
int CONFIG_ATTRIBS[] =  
{  
EGL10.EGL_RED_SIZE,       5,  
EGL10.EGL_GREEN_SIZE,     6,  
EGL10.EGL_BLUE_SIZE,      5,  
EGL10.EGL_DEPTH_SIZE,     16,  
EGL10.EGL_ALPHA_SIZE,     EGL10.EGL_DONT_CARE,  
EGL10.EGL_STENCIL_SIZE,   EGL10.EGL_DONT_CARE,  
EGL10.EGL_SURFACE_TYPE,   EGL10.EGL_WINDOW_BIT,
EGL10.EGL_SAMPLES, 4,
EGL10.EGL_NONE 
};  

        int[] num_config = new int[1];  
        egl.eglChooseConfig(display, CONFIG_ATTRIBS, null, 0, num_config);  
        int numConfigs = num_config[0];  
        if (numConfigs <= 0) {  
            throw new IllegalArgumentException("No configs match configSpec");  
        }  
        EGLConfig[] configs = new EGLConfig[numConfigs];  
        egl.eglChooseConfig(display, CONFIG_ATTRIBS, configs, numConfigs, num_config);  
        EGLConfig config = configs[0];  
        if (config == null) {  
            throw new IllegalArgumentException("No config chosen");  
        }  
        return config;
}
}


然后,再找到 onCreateView 函数,在函数内添加下面代码:


glSurfaceView.setEGLConfigChooser(new BaseConfigChooser());


最后,打开Cocos2dxRenderer.java文件找到onSurfaceCreated函数添加下面代码:


pGL10.glEnable(pGL10.GL_MULTISAMPLE);

   

抗锯齿其实就是边缘羽化,没有特别清晰的边缘,不过这种抗锯齿对性能要求是很高的,使用也是需要慎重考虑的。



原创粉丝点击