OpenGL显示图片,这篇博客使用glew + freeglut + gdal来实现imshow。

主要修改:

  • 使用BGR而不是RGB,保持和opencv行为一致

  • 纯C,去掉C++相关的

  • 去掉GDAL,使用stb image读取图像

  • 提供了完整的CMakeLists.txt脚本

glew-2.1.0:编译安装

glew-2.1.0/build/vs2017-x64.bat

@echo off

set BUILD_DIR=vs2017-x64

if not exist %BUILD_DIR% md %BUILD_DIR%

cd %BUILD_DIR%

cmake -G "Visual Studio 15 2017 Win64" ^
-D CMAKE_INSTALL_PREFIX=%cd%/install ^
../cmake cd .. pause

freeglut-3.0.0:编译安装

freeglut-3.0.0/build/vs2017-x64.bat

@echo off

set BUILD_DIR=vs2017-x64

if not exist %BUILD_DIR% md %BUILD_DIR%

cd %BUILD_DIR%

cmake -G "Visual Studio 15 2017 Win64" ^
-D CMAKE_INSTALL_PREFIX=%cd%/install ^
../.. cd .. pause

读取图像:使用stb image替代gdal

参考的博客中,gdal用来读取图像,官方repo拒绝提供CMakeLists,有人维护了带CMake支持的版本。但下载仍然不方便,也没有必要使用。自行封装stb image即可,参照基于GDI的imshow:使用stb_image读取图像并修正绘制,使用fc_image.hfc_image.c即可。

主函数代码

#include <stdio.h>
#include <gl/glew.h> // 包含最新的gl.h,glu.h库
#include <gl/freeglut.h> // 包含OpenGL实用库
#include <assert.h>
#include "fc_image.h" unsigned int texture; // 纹理对象
unsigned char* imgBuf = NULL;
int imgWidth;
int imgHeight; void ReadImage()
{
const char* im_pth = "F:/zhangzhuo/dev/libfc/imgs/fruits.jpg";
FcImage im = fc_load_image(im_pth);
int buf_sz = im.c * im.h * im.w;
imgWidth = im.w;
imgHeight = im.h;
imgBuf = (unsigned char*)malloc(buf_sz);
memcpy(imgBuf, im.data, buf_sz);
} void InitGL()
{
glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_SMOOTH); //平滑着色
glEnable(GL_DEPTH_TEST); //深度测试
glEnable(GL_CULL_FACE); //只渲染某一面
glFrontFace(GL_CCW); //逆时针正面 glEnable(GL_TEXTURE_2D); //启用2D纹理映射 //载入纹理图像:
ReadImage(); //生成纹理对象:
glGenTextures(1, &texture);
} void DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindTexture(GL_TEXTURE_2D, texture); //绑定纹理: glPixelStorei(GL_UNPACK_ALIGNMENT, 1); //支持4字节对齐 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); //S方向上贴图
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); //T方向上贴图
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //放大纹理过滤方式
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //缩小纹理过滤方式 glTexImage2D(GL_TEXTURE_2D, 0, 3, imgWidth, imgHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, imgBuf); //载入纹理: glMatrixMode(GL_MODELVIEW); // 选择模型观察矩阵
glLoadIdentity(); // 重置模型观察矩阵
glMatrixMode(GL_PROJECTION); // 选择投影矩阵
glLoadIdentity(); glEnable(GL_TEXTURE_2D); //启用2D纹理映射
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(0.5f, 0.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-0.5f, 0.5f, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D); glutSwapBuffers();
} GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // 重置OpenGL窗口大小
{
glViewport(0, 0, width, height);
} int main(int argc, char* argv[], char* envp[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitContextProfile(GLUT_CORE_PROFILE);
glutInitWindowSize(600, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("opengl"); InitGL();
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);
//glutKeyboardFunc(keyboard);
//glutMouseWheelFunc(mouse_wheel);
//glutIdleFunc(idle); glutMainLoop(); return 0;
}

CMakeLists.txt


cmake_minimum_required(VERSION 3.14) project(opengl_imshow) include_directories(
"F:/zhangzhuo/dev/glew-2.1.0/build/vs2017-x64/install/include"
"F:/zhangzhuo/dev/freeglut-3.0.0/build/vs2017-x64/install/include"
)
link_directories(
"F:/zhangzhuo/dev/glew-2.1.0/build/vs2017-x64/install/lib"
"F:/zhangzhuo/dev/freeglut-3.0.0/build/vs2017-x64/install/lib"
) add_executable(imshow
src/opengl_imshow.c
src/stb_image.h
src/fc_image.h
src/fc_image.c
)
target_link_libraries(imshow glew32 freeglutd)

基于glew,freeglut的imshow-LMLPHP

05-11 13:32