- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在 mac os x (10.12) 上开发一个简单的离屏渲染应用程序,但总是黑屏。我确认shader编译链接没问题,把贴图改成单色也ok。然后我注释了有关深度信息相关的代码,但没有效果。单步调试时,没有发现glGetError
返回错误。以下是代码,请问有什么问题?
GLuint program;
if (compile_link_shader(vertex_shader_source, fragment_shader_source, &program) < 0) {
base_error_log("call compile_link_shader failed\n");
return -1;
}
glViewport(0, 0, target->width, target->height);
GLuint vao, vbo, ibo, loc_attr;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glGenBuffers(1, &ibo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ARRAY_BUFFER, sizeof(pos_coord), pos_coord, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(element_index), element_index, GL_STATIC_DRAW);
loc_attr = _static_cast(GLuint) glGetAttribLocation(program, "vertexPosition");
glVertexAttribPointer(loc_attr, 2, GL_FLOAT, GL_FALSE,
4 * sizeof(GLfloat), _static_cast(GLvoid*) (0 * 2 * sizeof(GLfloat)));
glEnableVertexAttribArray(loc_attr);
loc_attr = (GLuint) glGetAttribLocation(program, "textureCoordinate");
glVertexAttribPointer(loc_attr, 2, GL_FLOAT, GL_FALSE,
4 * sizeof(GLfloat), (GLvoid*) (1 * 2 * sizeof(GLfloat)));
glEnableVertexAttribArray(loc_attr);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
GLuint texture;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
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_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
/* glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); // Removed from GL 3.1 and above */
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
GLenum format = origin->channels == 3 ? GL_RGB : GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, 0, format, origin->width, origin->height, 0,
format, GL_UNSIGNED_BYTE, origin->image);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
GLuint fbo;
GLuint depthBufferName;
glGenRenderbuffers(1, &depthBufferName);
glBindRenderbuffer(GL_RENDERBUFFER, depthBufferName);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, origin->width, origin->height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBufferName);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
base_error_log("glCheckFramebufferStatus not GL_FRAMEBUFFER_COMPLETE\n");
return -1;
}
glUseProgram(program);
GLint loc_uniform = glGetUniformLocation(program, "texture");
glUniform1i(loc_uniform, texture);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glBindTexture(GL_TEXTURE_2D, texture);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLES, 2 * 3, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glReadBuffer(GL_COLOR_ATTACHMENT0);
target->image = malloc(_static_cast(size_t) (target->width * target->height * target->channels));
if (!target->image) {
base_error_log("malloc target->image failed\n");
return -1;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glReadPixels(0, 0, target->width, target->height, (target->channels == 3 ? GL_RGB : GL_RGBA),
GL_UNSIGNED_BYTE, target->image);
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
另一部分是:
#define GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED
#define GLEW_STATIC
#include <GL/glew.h>
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl3.h>
#include <OpenGL/gl3ext.h>
static GLfloat pos_coord[] = {
// Vertex Positions // Texture Coordinates
-1.0f, 1.0f, 0.0f, 1.0f, // Left Top
1.0f, 1.0f, 1.0f, 1.0f, // Right Top
1.0f, -1.0f, 1.0f, 0.0f, // Right Bottom
-1.0f, -1.0f, 0.0f, 0.0f, // Left Bottom
};
static GLuint element_index[] = { // Note that we start from 0 with GL_CCW!
0, 3, 2, // First Triangle
0, 2, 1, // Second Triangle
};
static const char *vertex_shader_source = ""
"#version 330 core\n"
"precision mediump float;\n"
"in vec2 vertexPosition;\n"
"in vec2 textureCoordinate;\n"
"out vec2 varyingTextureCoordinate;\n"
"void main() {\n"
" gl_Position = vec4(vertexPosition, 0.0, 1.0);\n"
" varyingTextureCoordinate = textureCoordinate;\n"
"}";
static const char *default_fragment_shader_source = ""
"#version 330 core\n"
"precision mediump float;\n"
"in vec2 varyingTextureCoordinate;\n"
"out vec4 fragmentColor;\n"
"uniform sampler2D texture;\n"
"void main() {\n"
" fragmentColor = texture(texture, varyingTextureCoordinate);\n"
/* " fragmentColor = vec4(1.0, 1.0, 0.0, 1.0);\n" */ // this line work fine
"}";
以下代码具有相同的效果:黑屏。是同一个问题吗?
glViewport(0, 0, target->width, target->height);
GLuint vao, vbo, ibo, loc_attr;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glGenBuffers(1, &ibo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ARRAY_BUFFER, sizeof(pos_coord), pos_coord, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(element_index), element_index, GL_STATIC_DRAW);
loc_attr = _static_cast(GLuint) glGetAttribLocation(program, "vertexPosition");
glVertexAttribPointer(loc_attr, 2, GL_FLOAT, GL_FALSE,
4 * sizeof(GLfloat), _static_cast(GLvoid*) (0 * 2 * sizeof(GLfloat)));
glEnableVertexAttribArray(loc_attr);
loc_attr = (GLuint) glGetAttribLocation(program, "textureCoordinate");
glVertexAttribPointer(loc_attr, 2, GL_FLOAT, GL_FALSE,
4 * sizeof(GLfloat), (GLvoid*) (1 * 2 * sizeof(GLfloat)));
glEnableVertexAttribArray(loc_attr);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glActiveTexture(GL_TEXTURE0);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
/* glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); // Removed from GL 3.1 and above */
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
GLenum format = origin->channels == 3 ? GL_RGB : GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, 0, format, origin->width, origin->height, 0,
format, GL_UNSIGNED_BYTE, origin->image);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
GLuint fbo;
GLuint colorBufferName, depthBufferName;
glGenRenderbuffers(1, &colorBufferName);
glBindRenderbuffer(GL_RENDERBUFFER, colorBufferName);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, origin->width, origin->height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glGenRenderbuffers(1, &depthBufferName);
glBindRenderbuffer(GL_RENDERBUFFER, depthBufferName);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, origin->width, origin->height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBufferName);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBufferName);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
base_error_log("glCheckFramebufferStatus not GL_FRAMEBUFFER_COMPLETE\n");
return -1;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glUseProgram(program);
GLint loc_uniform = glGetUniformLocation(program, "texture");
glUniform1i(loc_uniform, texture);
glBindTexture(GL_TEXTURE_2D, texture);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLES, 2 * 3, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glReadBuffer(GL_COLOR_ATTACHMENT0);
target->image = malloc(_static_cast(size_t) (target->width * target->height * target->channels));
if (!target->image) {
base_error_log("malloc target->image failed\n");
return -1;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glReadPixels(0, 0, target->width, target->height, (target->channels == 3 ? GL_RGB : GL_RGBA),
GL_UNSIGNED_BYTE, target->image);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
最佳答案
texture
是写入帧缓冲区的颜色附件,纹理绑定(bind)到从中读取的纹理采样器。这是未定义的行为。
Khronos OpenGL 文档 Framebuffer Object - Feedback Loops说:
It is possible to bind a texture to an FBO, bind that same texture to a shader, and then try to render with it at the same time.
It is perfectly valid to bind one image from a texture to an FBO and then render with that texture, as long as you prevent yourself from sampling from that image. If you do try to read and write to the same image, you get undefined results. Meaning it may do what you want, the sampler may get old data, the sampler may get half old and half new data, or it may get garbage data. Any of these are possible outcomes.
Do not do this. What you will get is undefined behavior.
由于 texture
是事件帧缓冲区的颜色附件,因此 glClear
命令将清除纹理:
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
.....
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
如果要渲染到纹理,则必须创建第二个纹理,将其附加到帧缓冲区颜色平面并写入。
另见:
您必须将纹理单元的索引设置为纹理采样器统一,而不是纹理对象本身。纹理单元由 glActiveTexture
设置和 glBindTexture
将纹理绑定(bind)到目标(例如 GL_TEXTURE_2D
)和事件纹理单元。
顺便说一句,你必须重命名统一texture
的名称,因为它与GLSL函数texture
的名称相同。在下面的代码中,我使用了 u_texture
。
GLint loc_uniform = glGetUniformLocation( program, "u_texture" );
.....
int textureUnitIndex = 0; // 0, 1, 2 ...
glActiveTexture( GL_TEXTURE0 + textureUnitIndex );
glBindTexture( GL_TEXTURE_2D, texture );
.....
glUseProgram( program );
glUniform1i( loc_uniform, textureUnitIndex ); // <-- index of the texture unit
// NOT the texture object
关于c - 使用 fbo 和纹理的离屏渲染得到黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46291313/
在我的 OpenGL 程序中,我按顺序执行以下操作: // Drawing filled polyhedrons // Drawing points using GL_POINTS // Displa
我想传递一个包含原始页面的局部变量,这个变量只包含一个带有值的符号。 当我使用此代码时,它运行良好,可以在部分中访问 origin 变量: render :partial => "products",
为什么这个 HTML/脚本(来自“JavaScript Ninja 的 secret ”)不渲染? http://jsfiddle.net/BCL54/
我想在阅读完 View 后返回到特定的网页位置(跳转到页内 anchor )。换句话说,在 views.py 中,我想做类似的事情: context={'form':my_form} return r
我有一个包含单条折线的 PathGeometry,并以固定的间隔向该线添加一个新点(以绘制波形)。使用 Perforator 工具时,我可以看到每次向直线添加一个点时,WPF 都会将整个 PathGe
尝试了解如何消除或最小化网站上不同 JavaScript 库的渲染延迟。 例如,如果我想加载来自许多社交网络的“即时”关注按钮,它们似乎会相互阻止渲染,并且您会收到令人不快的弹出窗口。 (func
我有以 xyz 点格式表示 3D 表面(即地震断层平面)的数据。我想创建这些表面的 3D 表示。我使用 rgl 和 akima 取得了一些成功,但是它无法真正处理可能会自行折叠或在同一 x,y 点具有
我正在用 Libgdx 编写一个小游戏。 我有一个 Render[OpenGL] 线程,它不断对所有对象调用 render() 和一个更新线程不断对所有对象调用 update(double delta
我有一个 .Rmd 文件包含: ```{r, echo=FALSE, message=FALSE, results='asis'} library(xtable) print(xtable(group
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
请不要评判我,我只是在学习 Swift。 最近我安装了 MetalPetal 框架,并按照说明操作: https://github.com/MetalPetal/MetalPetal#example-
如果您尝试渲染 Canvas 宽度和高度之外的图像,计算机是否仍会尝试渲染它并使用资源来尝试渲染它?我只是想找出在尝试渲染图像之前检查图像是否在 Canvas 内是否更好。 最佳答案 我相信它仍然在无
我在 safari 中渲染时遇到问题。 在 firefox、chrome 和 IE 上。如下图所示: input.searchbox{-webkit-border-radius:10px;-moz-b
我正在尝试通过远程桌面在 Windows7 下运行我在 RHEL7 服务器中制作的 java 程序。 服务器中的所有java程序都无法通过远程桌面呈现。如果我在服务器位置访问服务器本身,它们看起来没问
我正处于一个新项目的设计阶段,该项目将采用数据集并将其加载到文档中,然后围绕模板呈现文档。呈现的文件可以是 CSV 数据集、PDF 营销信函、电子邮件……很多东西。数据不会是数学方程式,我只是在寻找一
有没有办法在不同的 div 下渲染 React 组件的子组件? ... ... ... ... ...
使用以下代码: import numpy as np from plotly.offline import iplot, init_notebook_mode import plotly.graph_
截至最近, meteor 的所有文档都指出 onRendered是一种在模板完成渲染时获取回调的新方法。和 rendered只是为了向后兼容。 但是,这似乎对我不起作用。 onRendered永远不会
所以在我的基本模板中,我有:{% render "EcsCrmBundle:Module:checkClock" %} 然后我创建了 ModuleController.php ... getDoctr
我正在使用 vue-mathjax 来编译我的 vue 项目中的数学方程。它正在编译第一个括号 () 之间的文本。我想防止编译括号内的字符串。在文档中我发现,对于$符号,如果我们想逃避编译,我们需要使
我是一名优秀的程序员,十分优秀!