基於glew,freeglut的imshow

OpenGL顯示圖片,這篇博客使用glew + freeglut + gdal來實現imshow。html

主要修改:git

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

  • 純C,去掉C++相關的函數

  • 去掉GDAL,使用stb image讀取圖像oop

  • 提供了完整的CMakeLists.txt腳本測試

glew-2.1.0:編譯安裝

glew-2.1.0/build/vs2017-x64.batui

@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.batcode

@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便可。htm

主函數代碼

#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)

相關文章
相關標籤/搜索