纹理未显示
Texture Not Showing Up
我试图在我的三角形上添加纹理,但它只是显示为一个黑色三角形。这是我被调用的起始函数:
GLuint vao;
GLuint shader_programme;
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API createSimpleWindow()
{
if (std::this_thread::get_id() == MAIN_THREAD_ID)
{
oldContext = glfwGetCurrentContext();
newContext = glfwCreateWindow(640, 480, "window", NULL, oldContext);
glfwMakeContextCurrent(newContext);
// start GLEW extension handler
glewExperimental = GL_TRUE;
glewInit();
glEnable(GL_DEPTH_TEST); // enable depth-testing
glDepthFunc(GL_LESS); // depth-testing interprets a smaller value as "closer"
//triangle pts
float points[] = {
0.0f, 0.5f, 0.0f, 0.5f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f
};
GLuint vbo = 0;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW);
vao = 0;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertex_shader, NULL);
glCompileShader(vs);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragment_shader, NULL);
glCompileShader(fs);
shader_programme = glCreateProgram();
glAttachShader(shader_programme, fs);
glAttachShader(shader_programme, vs);
glLinkProgram(shader_programme);
glUseProgram(shader_programme);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
int width, height, nrComponents;
unsigned char *data = stbi_load("/Users/roma/Desktop/Escape Tech/BitBucketRepos/blankpluginGLFW/BlankPlugin/PluginSource/source/container2.png", &width, &height, &nrComponents, 0);
writeToLog("before data");
if (data) {
writeToLog("data contained!!!!!!");
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
glUniform1i(glGetUniformLocation(shader_programme, "texture1"), 0);
//glfwCreateWindow(640, 480, "My Title", NULL, NULL);
glfwSetKeyCallback(newContext, key_callback);
}
else
{
writeToLog("not main thread");
}
}
下面是在 while 循环中调用的函数:
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API windowMainLoop()
{
writeToLog("render loop");
glfwMakeContextCurrent(newContext);
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
// loop until the window closed
if (!glfwWindowShouldClose(newContext)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (std::this_thread::get_id() == MAIN_THREAD_ID)
{
// bind Texture
glBindTexture(GL_TEXTURE_2D, texture);
glUseProgram(shader_programme);
glBindVertexArray(vao);
// draw points 0-3 from the currently bound VAO with current in-use shader
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(newContext);
// poll the events
glfwPollEvents();
}
//switch back to old context
glfwMakeContextCurrent(oldContext);
writeToLog("finished render loop");
}
}
我的"if(数据("语句导致文本开始写入日志,因此我知道图像正在正确加载,但我无法弄清楚为什么三角形是黑色的。
任何帮助将不胜感激!
问题是通用顶点属性数据的设置数组。每个属性元组由 5 个组件组成(x、y、z、u、v(:
float points[] = { 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f };
因此,步幅参数必须5 * siezof(GLfloat)
而不是6 * siezof(GLfloat)
:
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glBufferData
的第二个参数是缓冲区的大小(以字节为单位(。缓冲区由 15 (5 * 3( 个类型 GLfloat
的元素组成。所以大小是15 * sizeof(float)
而不是9 * sizeof(float)
:
glBufferData(GL_ARRAY_BUFFER, 15 * sizeof(float), points, GL_STATIC_DRAW);
由于文件的格式是.png stbi_load
的最后一个参数应设置为4,以确保获得纹理的4通道:
unsigned char *data = stbi_load("?.png", &width, &height, &nrComponents, 4);
glTexImage2D
的格式和内部格式必须GL_RGBA
。
默认情况下,纹理缩小功能(GL_TEXTURE_MIN_FILTER
(是GL_NEAREST_MIPMAP_LINEAR
(见glTexParameteri
(。由于不使用 mip 映射,因此必须将参数更改为 GL_LINEAR
:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
相关文章:
- dx11 渲染到纹理仅显示透明颜色
- 无法获取要在 OpenGL ES2 中显示的 RGB 纹理
- 如何显示具有 opengl 纹理的灰度图像
- SDL + OpenGL ES 2.0 使用着色器显示纹理
- 如何使用 openGL 在屏幕上显示加载的纹理
- 在 SFML 中不使用动态内存时显示白色纹理代替实际纹理
- 纹理显示为黑色
- 使用两个纹理 SDL2 显示视频
- 纹理未显示
- 渲染到框架/纹理显示为空
- 无法使用 OpenGL 显示纹理
- OpenGL纹理未显示
- 使用OpenGL显示两个对象.纹理不像预期的那样行为
- 试图显示纹理,但只能使用OpenGL获得纯色
- OpenGL:无法显示纹理
- 在openGL(版本>3.3)的屏幕上显示纹理时如何更改纹理的大小?
- QT和OpenGL如何正确集成和显示纹理
- 无法在SDL 2中显示纹理
- 用c++在OpenGL ES中显示纹理有问题
- 为什么我不能在OpenGL中显示纹理