简体   繁体   English

OpenGL纹理渲染

[英]OpenGL texture rendering

I'm having problem rendering a texture in OpenGL. 我在OpenGL中渲染纹理时遇到问题。 I'm getting a white color instead of the texture image but my rectangle and the whole context is working fine. 我得到的是白色而不是纹理图像,但是我的矩形和整个上下文都正常工作。 I'm using the stbi image loading library and OpenGL 3.3. 我正在使用stbi图像加载库和OpenGL 3.3。

Before rendering: 渲染之前:

glViewport(0, 0, ScreenWidth, ScreenHeight);

glClearColor(0.2f, 0.0f, 0.0f, 1.0f);

int Buffer;
glGenBuffers(1, &Buffer);
glBindBuffer(GL_ARRAY_BUFFER, Buffer);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

Loading the Texture: 加载纹理:

unsigned char *ImageData = stbi_load(file_name, 1024, 1024, 0, 4); //not sure about the "0" parameter

int Texture;
glGenTextures(1, &Texture);
glBindTexture(GL_TEXTURE_2D, Texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, x, y, 0, GL_RGB, GL_UNSIGNED_BYTE, ImageData);

stbi_image_free(ImageData);

Rendering: 渲染:

glClear(GL_COLOR_BUFFER_BIT);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Texture);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 6 * 3); //a rectangle which is rendered fine
glDisableVertexAttribArray(0);

I am looking for a very minimalistic and simple solution. 我正在寻找一个非常简单和简单的解决方案。

First of all, take a second to look at the function declaration for stbi_load (...) : 首先,花点时间看一下stbi_load (...)的函数声明:

unsigned char *stbi_load (char const *filename,
                          int        *x,    // POINTER!!
                          int        *y,    // POINTER!!
                          int        *comp, // POINTER!!
                          int         req_comp)

Now, consider the arguments you have passed it: 现在,考虑传递给它的参数:

file_name (presumably a pointer to a C string)
1024      (definitely NOT a pointer)
1024      (definitely NOT a pointer)
0         (definitely NOT a pointer)

The whole point of passing x , y and comp as pointers is so that stbi can update their values after it loads the image. xycomp作为指针传递的重点是,这样stbi可以在加载图像后更新其值。 This is the C equivalent of passing by reference in a language like C++ or Java, but you have opted instead to pass integer constants that the compiler then treats as addresses. 这与在C ++或Java之类的语言中按引用传递等效于C语言,但是您选择了传递整数常量,然后将其视为编译器。

Because of the way you call this function, stbi attempts to store the width of the image at the address: 1024 , the height of the image at address: 1024 and the number of components at address: 0 . 因为你的方式调用这个函数,stbi试图将图像的宽度存储在地址:1024,图像的地址的高度:1024,并在地址组件的数量:0。 You are actually lucky that this does not cause an access violation or other bizarre behavior at run-time, storing things at arbitrary addresses is dangerous. 您实际上很幸运,这不会在运行时引起访问冲突或其他奇怪的行为,将内容存储在任意地址是危险的。

Instead, consider this: 相反,请考虑以下情况:

GLuint ImageX,
       ImageY,
       ImageComponents;

GLubyte *ImageData = stbi_load(file_name, &ImageX, &ImageY, &ImageComponents, 4);

[...]

glTexImage2D (GL_TEXTURE_2D,    0, GL_RGBA8,
                ImageX, ImageY, 0, GL_RGBA, GL_UNSIGNED_BYTE, ImageData);

// ~~~~ You are requesting 4 components per-pixel from stbi, so this is RGBA!

It's been a while since I've done OpenGL but aside from a few arbitrary calls, the main issue I'd guess is that you're not calling glEnable(GL_TEXTURE_2D); 自从我完成OpenGL以来已经有一段时间了,除了一些随意的调用,我猜的主要问题是您没有调用glEnable(GL_TEXTURE_2D); before you make the call to draw your function. 在进行调用以绘制函数之前。 Afterwards it is wise to call glDisable(GL_TEXTURE_2D); 然后,最好调用glDisable(GL_TEXTURE_2D); as well to avoid potential future conflicts. 以及避免将来可能发生的冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM