[英]Why does my OpenGL texture appear white?
我正在嘗試獲取OpenGL紋理以2D形式在屏幕上渲染。 我知道這看起來真的很簡單,但是我是OpenGL新手,這給我帶來了一些麻煩。
不管我嘗試什么,它都會顯示白色。
我已經看到了其他問題,例如OpenGL將紋理渲染為白色並檢查了清單上的所有內容,但是我似乎無法弄清楚源代碼,非常感謝您的幫助。
我敢肯定我只是在某個地方犯了一些愚蠢的錯誤,並且嘗試過廢棄我的代碼並重寫了幾次。
這是與從main方法運行的窗口相關的代碼。
//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);
}
loadBMP
來自http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/ 。
您可以嘗試以下方法:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.