2015-07-19 48 views
-1

我想获得一个OpenGL纹理在2D中在屏幕上呈现。我知道这看起来很简单,但我是OpenGL新手,这给我带来了一些麻烦。为什么我的OpenGL纹理显示为白色?

无论我尝试什么,它都显示出白色。

我已经看到其他问题,如OpenGL renders texture all white,并检查了清单上的所有内容,但我似乎无法弄清楚源代码,我非常感谢任何帮助。

我很确定我在某个地方犯了一些愚蠢的错误,并且试图废弃我的代码并重写了几次。

以下是与主要方法运行的窗口相关的代码。

//Window is a custom wrapper for GLFWwindow 
Window thirdwindow(window_width, window_height, "Three"); 
thirdwindow.show(); 
thirdwindow.setPosition(300, 300); 

ThirdLoop(thirdwindow); 

这里是ThirdLoop:

static void ThirdLoop(Window& win) 
{ 
    GLuint tex = loadBMP("cat.bmp"); 
    auto size = win.getWindowSize(); 
    win.beginDraw(); 
    while (running) 
    { 
     if (win.isClosing()) 
      running = false; 

     glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   // Clear the color buffer (background) 

     DrawImage(size, tex); 
     win.render(); 

     Update(); 
    } 
} 

和的DrawImage(对于奇缩进道歉这里,一个复制粘贴的东西)

void DrawImage(const Size<int>& size, GLuint texture) { 
glMatrixMode(GL_PROJECTION); 
glPushMatrix(); 
glLoadIdentity(); 
glOrtho(0.0, size.width, 0.0, size.height, -1.0, 1.0); 
glMatrixMode(GL_MODELVIEW); 
glPushMatrix(); 

glLoadIdentity(); 
glDisable(GL_LIGHTING); 

glEnable(GL_TEXTURE_2D); 
glBindTexture(GL_TEXTURE_2D, texture); 

// Draw a textured quad 
glBegin(GL_QUADS); 
glTexCoord2f(0, 0); glVertex3f(0, 0, 0); 
glTexCoord2f(0, 1); glVertex3f(0, 100, 0); 
glTexCoord2f(1, 1); glVertex3f(100, 100, 0); 
glTexCoord2f(1, 0); glVertex3f(100, 0, 0); 
glEnd(); 

glDisable(GL_TEXTURE_2D); 
glPopMatrix(); 

glMatrixMode(GL_PROJECTION); 
glPopMatrix(); 

glMatrixMode(GL_MODELVIEW); 
} 

loadBMPhttp://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/拍摄。

+0

您是否尝试过通过GPU调试运行呢?如果您使用的是AMD卡,则可能与[GPU Perf Studio](http://developer.amd.com/tools-and-sdks/graphics-development/gpu-perfstudio/)类似;我认为NVIDIA有相同的东西。 – Nitori

回答

1

你可以试试这个:

unsigned int CreateTexture(unsigned int Width, unsigned int Height, unsigned char* Pixels) 
{ 
    unsigned int ID = 0; 
    glGenTextures(1, &ID); 
    glBindTexture(GL_TEXTURE_2D, ID); 
    glPixelStorei(GL_UNPACK_ROW_LENGTH, Width); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_BGRA, GL_UNSIGNED_BYTE, Pixels); 
    glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    return ID; 
} 

void DestroyTexture(unsigned int ID) 
{ 
    glDeleteTextures(1, &ID); 
} 

void DrawTexture(unsigned int ID, float X1, float Y1) 
{ 
    glEnable(GL_TEXTURE_2D); 
    glBindTexture(GL_TEXTURE_2D, ID); 
    glColor4ub(0xFF, 0xFF, 0xFF, 0xFF); 
    glBegin(GL_QUADS); 
    glTexCoord2f(0.0f, 1.0f); 
    glVertex2f(X1, Y1); 
    glTexCoord2f(0.0f, 0.0f); 
    glVertex2f(X1, Y2); 
    glTexCoord2f(1.0f, 0.0f); 
    glVertex2f(X2, Y2); 
    glTexCoord2f(1.0f, 1.0f); 
    glVertex2f(X2, Y1); 
    glEnd(); 
    glDisable(GL_TEXTURE_2D); 
} 

我用下面的设置窗口:

static float angle = 0.0f; 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 

int w = 800; 
int h = 600; 
glOrtho(0.0f, w, h, 0.0f, 0.0f, -1.0f); 

glMatrixMode(GL_MODELVIEW); 
glShadeModel(GL_SMOOTH); 
glClearColor(0.0f, 40.0f/256.0f, 100.0f/256.0f, 1.0f); 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
glClearDepth(1.0f); 

glEnable(GL_BLEND); 
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 


glPushMatrix(); 
glTranslatef(400, 300, 0); 
glScalef(100, 100, 0); 
glRotatef(angle, 0.0f, 0.0f, 1.0f); 

if (TextureID == 0) 
{ 
    TextureID = CreateTexture(Width, Height, Pixels); //I used the ImageLoader from the comments that I posted. 
} 
DrawTexture(TextureID, 0.0f, 0.0f); 

glPopMatrix(); 
++angle; 

SwapBuffers(DC); 
Sleep(1); 

enter image description here

+0

谢谢,但它仍然显示为白色。 – russellsayshi

+0

无论您传递的是像素可能不正确还是格式与BGRA不匹配。该网站上的位图加载功能不会响应像素偏移量。它也不处理24位格式。 – Brandon

+0

尝试加载位图:http://stackoverflow.com/questions/20595340/loading-a-tga-bmp-file-in-c-opengl它不处理24位位图,但至少它寻求向右像素的偏移量。 – Brandon