- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个代码,最初有一个着色器 (ourShader)、一个 VAO 和一个 VBO。它将使用 Stencil 测试渲染带有纹理立方体的场景及其在地面上的反射。现在,我想为后处理添加一个帧缓冲区,所以我需要第二个着色器。我添加了第二个着色器 (screenShader) 和第二个 VBO。我还没有在我的绘图中使用它们,但我添加它们这一简单的事实使我的代码渲染了一个黑色的红色三角形,而不是通常的场景。
我的代码是这样的:
Shader ourShader(string("core.vs"), string("core.frag")); // shader class creating a shader program from vertex shader and fragment shader source files.
Shader screenShader(string("core2.vs"), string("core2.frag"));
// Setting up attributes, VAO, VBO
GLuint VAO;
array<GLuint, 2> VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(2, &VBO[0]);
glBindVertexArray(VAO);
// Data for first shader and first VBO
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesCube), verticesCube, GL_STATIC_DRAW);
GLint posAttribLoc = glGetAttribLocation(ourShader.Program, "position");
glVertexAttribPointer(posAttribLoc, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0); // specify (to the active VAO) how to retrieve the values for the attribute "position" from the data stored ("vertices" here) on the active VBO (GPU)
glEnableVertexAttribArray(posAttribLoc); // enable attribute for rendering
GLint colAttribLoc = glGetAttribLocation(ourShader.Program, "color");
glVertexAttribPointer(colAttribLoc, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(colAttribLoc);
GLint texAttribLoc = glGetAttribLocation(ourShader.Program, "texCoord");
glVertexAttribPointer(texAttribLoc, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(texAttribLoc);
// ##### PART GIVING A WEIRD RESULT #####
// Data for second shader and second VBO
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesRectangle), verticesRectangle, GL_STATIC_DRAW);
GLint posAttribLoc2 = glGetAttribLocation(screenShader.Program, "position");
glVertexAttribPointer(posAttribLoc2, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0); // specify (to the active VAO) how to retrieve the values for the attribute "position" from the data stored ("vertices" here) on the active VBO (GPU)
glEnableVertexAttribArray(posAttribLoc2); // enable attribute for rendering
GLint texAttribLoc2 = glGetAttribLocation(screenShader.Program, "texCoord");
glVertexAttribPointer(texAttribLoc2, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)(2 * sizeof(GLfloat)));
glEnableVertexAttribArray(texAttribLoc2);
// ##### END #####
// Setting up texture that will be used for the first shader
GLuint texture;
int width, height;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture); // makes "texture" the current texture and attaches it to texture unit 0
// Set the wrapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set the filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
unsigned char* image = SOIL_load_image("res/images/image1.jpg", &width, &height, 0, SOIL_LOAD_RGBA);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
// Unbind VBO, texture before main loop
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glfwSwapInterval(1);
glfwSetKeyCallback(window, Input::keyCallback); // Input is a singleton class handling inputs. It works well.
glfwSetCursorPosCallback(window, Input::mouseCallback);
glfwSetScrollCallback(window, Input::scrollCallback);
while (glfwWindowShouldClose(window) == GLFW_FALSE) {
// MAIN LOOP
// ...
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(2, &VBO[0]);
我认为主循环并不重要。我想说的是,如果我删除处理第二个 VBO 的几行代码,那么场景渲染得很好。否则,我会得到一个颜色怪异的三角形。
我的两个着色器将这些源文件用于它们各自的顶点着色器和片段着色器。顺便说一下,我的着色器没有编译错误。
核心对比:
#version 330 core
in vec3 position;
in vec2 texCoord;
in vec3 color;
out vec2 TexCoord;
out vec3 Color;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection*view*model*vec4(position, 1.0);
TexCoord = vec2(texCoord.x, 1.0 - texCoord.y);
Color = color;
}
核心片段:
#version 330 core
in vec2 TexCoord;
in vec3 Color;
out vec4 outColor;
uniform sampler2D ourTexture0;
void main()
{
outColor = vec4(Color, 1)*texture(ourTexture0, TexCoord);
}
核心2.vs:
#version 330 core
in vec2 position;
in vec2 texCoord;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
TexCoord = texCoord;
}
核心2.片段:
#version 330 core
in vec2 TexCoord;
out vec4 outColor;
uniform sampler2D texFramebuffer;
void main()
{
outColor = texture(texFramebuffer, TexCoord);
}
顶点看起来像这样(但只有阅读它们的方式很重要):
GLfloat verticesRectangle[] = {
// position // texture coordinates
-0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 1.0f,
0.5f, 0.5f, 1.0f, 1.0f
};
GLfloat verticesCube[] = {
// position // color // texture coordinates
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f,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,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // reflection surface // not a part of the cube itself
-1.0f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
1.0f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
1.0f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-1.0f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
1.0f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f
};
我已经看过 Binding a second vertex buffer seems to spoil my first vertex buffer, OpenGL OES ios 5.1但是这个人有这个问题是因为他没有正确使用他的 VAO。
最佳答案
很可能您的两个属性位置具有相同的值。由于您只使用一个 VAO,因此您覆盖了一些绑定(bind)。使用多个独立几何体的正确方法是在每个几何体上使用 VAO。
正确的代码应该是这样的:
glBindVertexArray(vao1);
glBindBuffer(VBO[0])
glVertexAttribPointer...
glEnableVertexAttribArray...
//Setup all attributes for first VBO
glBindVertexArray(vao2);
glBindBuffer(VBO[1])
glVertexAttribPointer...
glEnableVertexAttribArray...
//Setup all attributes for second VBO
渲染时执行以下操作:
glBindVertexArray(vao1);
glDraw*... //Draw VAO1
glBindVertexArray(vao2);
glDraw*.... //Draw VAO2
关于c++ - OpenGL : second VBO spoiling first VBO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45946940/
目前,在我的渲染引擎中,每个网格数据都有一个 VBO(1 个 VBO 用于顶点,1 个 VBO 用于法线,1 个 VBO 用于纹理坐标,1 个 VBO 用于切线,1 个 VBO 用于副切线)并且它们都
我有一个代码,最初有一个着色器 (ourShader)、一个 VAO 和一个 VBO。它将使用 Stencil 测试渲染带有纹理立方体的场景及其在地面上的反射。现在,我想为后处理添加一个帧缓冲区,所以
因此,我使用两个 VBO 成功绘制了一系列线条,一个用于顶点,一个用于索引。 Python 列表是: vertices = [ [0,1],[0,2],[0,3], # Axe
因此,我需要使用一次绘制调用来渲染多个对象(而非实例)的方法。实际上我知道如何做到这一点,只是将数据放入单个 vbo/ibo 并使用 glDrawElements 进行渲染。 问题是:使用 glUni
大家好。我正在尝试呈现如下所示的两种方法。 RenderA() 使用的是 VBO 而 RenderB() 不是。当它到达 RenderB() 中的 glDrawArrays() 时,我收到一个 EXC
我正在尝试制作一个类似于《我的世界》的 opengl 体素引擎。 我创建了一个 block 类,我想将整个 block 的所有顶点放入单个 VBO 中。我之前只是将每个 block 放入 vbo 中并
我最近将当前项目中的绘图从内存阵列的标准绘图更改为 VBO。令我惊讶的是,帧速率从 60fps 显着下降到 30fps,绘制了 1200verts 的模型 8 次。进一步的分析表明,与从内存中绘制相比
所以我了解如何使用顶点缓冲区对象,并且它比立即模式绘图提供了很大的性能提升。我将绘制很多 2D 四边形( Sprite ),我想知道我是否应该为每个四边形创建一个 VBO,或者创建一个 VBO 来保存
我正在编写一个在 OpenGL 中渲染一些网格的插件。 我在一个 VBO 中有一个 Vertices 数组,在另一个 VBO 中有一个 Indices 数组。我现在想存储法线。由于多个面之间共享的顶点
你能渲染一个由四边形组成的 VBO,每个四边形都具有不同的纹理吗?现在,我读到了有关按纹理或纹理图集排序的内容,但这仍然不能回答我的问题。我正在开发一款 2d 游戏。现在我的动画 Sprite 将拥有
我有一个 2D VBO 对象,它表示 2D 空间中的点。此时绘制任意形状的最佳方法是什么?假设我想在每个位置画一个红色的“X”。 我可以使用着色器来做到这一点吗? 最佳答案 您不一定需要特殊的着色器,
如果我有一个顶点不断变化的模型,因此我需要在每一帧上重新绑定(bind) VBO 上的所有信息,使用即时模式是否会提高性能?或者,所有数据同时传递到 GFX 卡的事实仍然是 VBO 的救赎因素吗? 根
我想在交错的 OpenGL vbo 中存储三个浮点值和两个字节值。不幸的是,渲染的数据显然不正确。当我使用两个不同的 VBO 渲染相同的数据时,一切都工作正常,因此我不认为我的着色器存在问题。 /*
此代码工作正常,呈现正确(没有发布所有相关代码,因为我认为这些部分有问题): std::vector vboId; std::vector > verts; ...初始化: verts[num].pu
现在我正在使用索引,所以我在让我的 VBO 正确渲染法线时遇到了一些麻烦。我很确定我对普通指针的偏移有问题,但数学似乎加起来对我来说是正确的。 我如何存储数据: struct MyVertex {
我正在尝试使用 OpenGL 中的顶点缓冲区对象绘制两个四边形。他们应该用不同的颜色绘制。如您所见,第一个四边形具有红色、绿色、蓝色和黄色顶点。第二个四边形有不同的颜色,但问题是第二个四边形被完全绘制
我正在尝试在我的原生黑莓 10 应用程序中使用 VBO 绘制一个正方形。我的实现是, glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
在我的项目中,我想使用 vbo 显示许多对象(球体)。我设法毫无问题地显示 1 个对象,但是当涉及到 2 个或更多对象时,所有对象 (vbos) 都被最后定义的对象 (vbo) 替换。 CosmicB
我已经学习 VBO 几个星期了,有人告诉我 here VBO 可以渲染“约 100 万个顶点,每秒数百帧”。然而,我当前的 VBO 测试程序只能获得大约 50 FPS 的渲染速度和 100 万个顶点。
我正在使用 Haskell 制作一个渲染引擎,并且正在优化我的代码以使用 VBO。我想稍微抽象一下几何类型,因为有相当数量,并且想把它放在一个函数和一些参数中。 例如,如果我有一组几何体,它是一对顶点
我是一名优秀的程序员,十分优秀!