51拾忆
51拾忆

Mac下OpenGL超级宝典第七版纹理坐标代码问题

问题

  • 原文P107的simpletexcoords这个例子,配套的sb7code下源码在Mac下运行后系统错误:
    https://blog.51time.top/wp-content/uploads/2020/11/截屏2020-11-27-上午10.29.48-300x240.png
  • 从图上可以看出是object类下的render_sub_object这个函数的问题。但是这个函数是被封装在libsb7.dylib库里的,需要修改源代码并重新编译。

原因分析

  • 打开sb7object.cpp源文件,看到render_sub_object这个函数里,用了glDrawElementsInstancedBaseInstance 和 glDrawArraysInstancedBaseInstance 这两个GL函数。查询OpenGL文档,发现这两个函数是OpenGL 4.2版本才出现的。而Mac最高支持到4.1版本,所以上面报系统错误原因就在于此。

  • 具体版本和功能,参考如下链接:
    https://www.shuzhiduo.com/A/VGzlVb415b/

解决方法

  1. 修改sb7object.cpp和object.h文件,将4.2版本的这两个函数,改成支持4.1的glDrawElementsInstanced 和 glDrawArraysInstanced 。前者多一个baseinstance参数,分别删除掉即可。
void glDrawElementsInstancedBaseInstance(   GLenum mode,
    GLsizei count,
    GLenum type,
    const void * indices,
    GLsizei instancecount,
    GLuint baseinstance);

改成

void glDrawElementsInstanced(   GLenum mode,
    GLsizei count,
    GLenum type,
    const void * indices,
    GLsizei instancecount);
void glDrawArraysInstancedBaseInstance( GLenum mode,
    GLint first,
    GLsizei count,
    GLsizei instancecount,
    GLuint baseinstance);

改成

void glDrawArraysInstanced( GLenum mode,
    GLint first,
    GLsizei count,
    GLsizei instancecount);
  1. 重新编译库文件。
$git clone https://github.com/shawhen/sb7framework.git
$cd sb7framework
$mkdir build
$cd build
$cmake ..
$make
  1. 将库文件和头文件放入相应位置。
$cp lib/libsb7.dylib /usr/local/lib/
$cd ..
$cp include/object.h /usr/local/include/
  1. 重新编译运行simpletexcoords代码,看到了结果。
$cd simpletexcoords/build/
$cmake ..
$./simpletexcoords

https://blog.51time.top/wp-content/uploads/2020/11/截屏2020-11-27-上午10.32.34-300x224.png

https://blog.51time.top/wp-content/uploads/2020/11/截屏2020-11-27-上午10.32.52-300x224.png

没有标签
首页      技术文章      OpenGL      Mac下OpenGL超级宝典第七版纹理坐标代码问题

发表评论

textsms
account_circle
email

51拾忆

Mac下OpenGL超级宝典第七版纹理坐标代码问题
问题 原文P107的simpletexcoords这个例子,配套的sb7code下源码在Mac下运行后系统错误: 从图上可以看出是object类下的render_sub_object这个函数的问题。但是这个函数是被封装在libsb7…
扫描二维码继续阅读
2020-11-27