[英]Show a png Image with lodePng and OpenGL
我正在嘗試使用lodepng( http://lodev.org/lodepng/ )加載png圖像並使用openGl進行繪制,但出現錯誤,我認為我正在嘗試訪問不可訪問的矢量ID。 但是我不知道為什么。
主要代碼:
#include <iostream>
#include <glut.h>
#include <vector>
#include "lodepng.h"
using namespace std;
std::vector<unsigned char> img;
unsigned w, h;
void decodeOneStep(const char* filename)
{
std::vector<unsigned char> image;
unsigned width, height;
//decode
unsigned error = lodepng::decode(image, width, height, filename);
cout << "w: " << width << " " << "h: " << height << endl;
//if there's an error, display it
if (error) std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
else
{
img = image;
w = width;
h = height;
cout << "Success" << endl;
}
}
void display(void)
{
/* clear all pixels */
glClear (GL_COLOR_BUFFER_BIT);
glRasterPos2i(0,0);
glDrawPixels(w,h, GL_RGBA, GL_UNSIGNED_INT, &img);
glFlush ();
}
void init (void)
{
/* select clearing (background) color */
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
decodeOneStep("eleTest.png");
cout << img->size();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (800, 800);
glutInitWindowPosition (300, 0);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
看來您的數據與glDrawPixels
中的類型限定符不匹配
std::vector<unsigned char> img;
glDrawPixels(w,h, GL_RGBA, GL_UNSIGNED_INT, &img);
img每個通道包含1個字節的數據,但告訴OpenGL每個通道應讀取4個字節(一個整數)。 嘗試將GL_UNSIGNED_INT切換到GL_UNSIGNED_BYTE。
由於我不知道導入程序庫:您將必須確保圖像確實具有Alpha通道。 否則,您可能會遇到類似問題。
注意, &img
不一定是向量中第一個元素的地址。 您至少應使用&img[0]
,如LodePNG的opengl示例中所示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.