برنامج SDL_IMAGE / C ++ OPENGL البرنامج: IMG_Load () ينتج صورا غامضة

StackOverflow https://stackoverflow.com/questions/1726122

  •  19-09-2019
  •  | 
  •  

سؤال

أحاول تحميل ملف صورة واستخدامه كملمس للمكعب. أنا أستخدم SDL_Image للقيام بذلك.

original image

استخدمت هذه الصورة لأنني وجدت ذلك في تنسيقات الملفات المختلفة (TGA، TIF، JPG، PNG، BMP)

الرمز :

SDL_Surface * texture;

//load an image to an SDL surface (i.e. a buffer)

texture = IMG_Load("/Users/Foo/Code/xcode/test/lena.bmp");

if(texture == NULL){
    printf("bad image\n");
    exit(1);
}

//create an OpenGL texture object 
glGenTextures(1, &textureObjOpenGLlogo);

//select the texture object you need
glBindTexture(GL_TEXTURE_2D, textureObjOpenGLlogo);

//define the parameters of that texture object
//how the texture should wrap in s direction
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//how the texture should wrap in t direction
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//how the texture lookup should be interpolated when the face is smaller than the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//how the texture lookup should be interpolated when the face is bigger than the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

//send the texture image to the graphic card
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGB, GL_UNSIGNED_BYTE, texture-> pixels);

//clean the SDL surface
SDL_FreeSurface(texture);

الكود يجمع دون أخطاء أو تحذيرات!

لقد تعبت من جميع تنسيقات الملفات، لكن هذا ينتج دائما أن النتيجة القبيحة:

result

أنا أستخدم: SDL_IMAGE 1.2.9 و SDL 1.2.14 مع xcode 3.2 أقل من 10.6.2

هل يعرف أحد كيفية إصلاح هذا؟

هل كانت مفيدة؟

المحلول

السبب في تشويه الصورة لأنه ليس في تنسيق RGBA الذي حددته. افحص ال texture->format لمعرفة التنسيق الذي يوجد به وحدد المناسب GL_ ثابت يمثل التنسيق. (أو، تحويلها بنفسك إلى تنسيق اختيارك.)

نصائح أخرى

أظن و Greyfade. لديه الإجابة الصحيحة، ولكن شيء آخر يجب أن تكون على دراية به هو الحاجة إلى قفل الأسطح. ربما يكون هذا هو الحال، لأنك تعمل مع سطح في الذاكرة، ولكن عادة ما تحتاج إلى قفل الأسطح قبل الوصول إلى بيانات البكسل الخاصة بهم SDL_LockSurface(). وبعد علي سبيل المثال:

bool lock = SDL_MUSTLOCK(texture);
if(lock)
    SDL_LockSurface(texture);  // should check that return value == 0
// access pixel data, e.g. call glTexImage2D
if(lock)
    SDL_UnlockSUrface(texture);

إذا كان لديك Alpha Chanel، فكل بكسل هو 4 بايت غير موقعة، إذا لم تكن 3 بايت غير موقعة. هذه الصورة لا تحتوي على شوكية وعند محاولة حفظها، it .jpg.

يتغيرون

glteximage2d (gl_texture_2d، 0، gl_rgba، texture-> w، texture-> h، 0، gl_rggb، gl_unsigned_byte، الملمس-> بكسل)؛

ل

glteximage2d (gl_texture_2d، 0، gl_rgb، الملمس-> w، texture-> h، 0، gl_rggb، gl_unsigned_byte، الملمس-> بكسل)؛

يجب أن إصلاحه.

للحصول على .png باستخدام قناة ألفا

glteximage2d (gl_texture_2d، 0، gl_rgba، texture-> w، texture-> h، 0، gl_rgba، gl_unsigned_byte، الملمس-> بكسل)؛

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top