日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ opengl 纹理生成

發(fā)布時間:2025/3/15 c/c++ 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ opengl 纹理生成 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

程序運行截圖如下:

?

看著很炫酷:其實是圖片炫酷

?

程序源碼如下:

void Init() {glMatrixMode(GL_PROJECTION);gluPerspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();int nFileSize = 0;unsigned char *bmpFileContent = LoadFileContent("Res/test.bmp", nFileSize);int bmpWidth = 0, bmpHeight = 0;unsigned char*pixelData = DecodeBMP(bmpFileContent, bmpWidth, bmpHeight);//最后一個參數(shù)傳GL_RGB因為BMP圖無α通道texture = CreateTexture2D(pixelData, bmpWidth, bmpHeight, GL_RGB); //創(chuàng)建OpenGL紋理對象 }void Draw() {glClearColor(0.0f, 0.0f, 0.0f, 1.0f);glClear(GL_COLOR_BUFFER_BIT);glEnable(GL_TEXTURE_2D); //開啟2D紋理glBindTexture(GL_TEXTURE_2D, texture);//將當前紋理設(shè)置為新創(chuàng)建的紋理對象glBegin(GL_QUADS);glColor4ub(255, 255, 255, 255);glTexCoord2f(0.0f, 0.0f);glVertex3f(-0.1f, -0.1f, -0.4f);glTexCoord2f(1.0f, 0.0f);glVertex3f(0.1f, -0.1f, -0.4f);glTexCoord2f(1.0f, 1.0f);glVertex3f(0.1f, 0.1f, -0.4f);glTexCoord2f(0.0f, 1.0f);glVertex3f(-0.1f, 0.1f, -0.4f);glEnd(); }

?

其中LoadFileContext函數(shù)如下:

unsigned char * LoadFileContent(const char *path, int &filesize) {unsigned char*fileContent = nullptr;filesize = 0;FILE*pFile = fopen(path, "rb");if (pFile) {fseek(pFile, 0, SEEK_END);int nLen = ftell(pFile);if (nLen > 0) {rewind(pFile);fileContent = new unsigned char[nLen + 1];fread(fileContent, sizeof(unsigned char), nLen, pFile);fileContent[nLen] = '\0';filesize = nLen;}fclose(pFile);}return fileContent; }

DecodeBMP函數(shù)如下:

unsigned char* DecodeBMP(unsigned char*bmpFileData, int&width, int&height) {if (0x4D42 == *((unsigned short*)bmpFileData)) {int pixelDataOffset = *((int*)(bmpFileData + 10));width = *((int*)(bmpFileData + 18));height = *((int*)(bmpFileData + 22));unsigned char*pixelData = bmpFileData + pixelDataOffset;for (int i = 0; i < width*height * 3; i += 3) {unsigned char temp = pixelData[i];pixelData[i] = pixelData[i + 2];pixelData[i + 2] = temp;}return pixelData;}return nullptr; }

?

createTexture2D函數(shù)如下:

GLuint CreateTexture2D(unsigned char*pixelData, int width, int height, GLenum type) {GLuint texture;glGenTextures(1, &texture);glBindTexture(GL_TEXTURE_2D, texture);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, type, GL_UNSIGNED_BYTE, pixelData);glBindTexture(GL_TEXTURE_2D, 0);return texture; }

?

總結(jié)

以上是生活随笔為你收集整理的C++ opengl 纹理生成的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。