OpenGL在不同的VBO中绘制具有不同颜色,顶点位置和顶点颜色的点?

如何解决OpenGL在不同的VBO中绘制具有不同颜色,顶点位置和顶点颜色的点?

我想用不同的颜色绘制不同的点。我将顶点位置数据和颜色数据放在不同的VBO中,如下所示:

这是我的C ++代码:

    m_Points.push_back(glm::vec3(4,0));
    m_Points.push_back(glm::vec3(0,2,3));
    m_Points.push_back(glm::vec3(0,6));

    m_Colors.push_back(glm::vec3(0,1.0,0));
    m_Colors.push_back(glm::vec3(1.0,0));
    m_Colors.push_back(glm::vec3(0,1.0));
    m_Colors.push_back(glm::vec3(1.0,0));

    glEnable(GL_PROGRAM_POINT_SIZE);


    glGenVertexArrays(1,&m_VAO);
    glBindVertexArray(m_VAO);

    // the first VBO,it is the coordinates
    glGenBuffers(1,&m_VBO);
    glBindBuffer(GL_ARRAY_BUFFER,m_VBO);
    glBufferData(GL_ARRAY_BUFFER,sizeof(glm::vec3) * sizeof(m_Points),&m_Points[0][0],GL_DYNAMIC_DRAW);

    // the second VBO,the color
    glGenBuffers(1,&m_VBO_Color);
    glBindBuffer(GL_ARRAY_BUFFER,m_VBO_Color);
    glBufferData(GL_ARRAY_BUFFER,sizeof(glm::vec3) * sizeof(m_Colors),&m_Colors[0][0],GL_DYNAMIC_DRAW);

    glEnableVertexAttribArray(0);
    // in file axis.vs,there is a statement
    // layout (location = 0) in vec3 vertex;
    // the first argument 0 means (location = 0)
    glVertexAttribPointer(0,// location = 0 in vertics shader file
                          3,// the position has X,Y,Z 3 elements
                          GL_FLOAT,// element type
                          GL_FALSE,// do not normalize
                          sizeof(glm::vec3),// Stride
                          (void *) nullptr); // an offset of into the array,it is 0


    glEnableVertexAttribArray(1);
    // in file axis.vs,there is a statement
    // layout (location = 1) in vec3 vertex;
    // the first argument 1 means (location = 1)
    glVertexAttribPointer(1,// location = 1 in vertics shader file
                          3,// the position has R,G,B 3 elements
                          GL_FLOAT,it is 0


    glBindVertexArray(0);

m_Points和m_Colors都是std::vector<glm::vec3>

这是我的顶点着色器和框架着色器:

layout (location = 0) in vec3 vertex;
layout (location = 1) in vec3 color;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;

out vec3 ObjectColor;

void main()
{
    gl_Position = projection * view * model * vec4(vertex,1.0);
    gl_PointSize = 10.0;
    ObjectColor = color;

}
#version 330 core

in vec3 ObjectColor;

out vec4 FragColor;

void main()
{
    FragColor = vec4(ObjectColor,1.0f);
}

绘制代码如下:

    shader.use();
    glBindVertexArray(m_VAO);
    glDrawArrays(GL_POINTS,m_Points.size());

但是我想通过AddPoint()函数动态添加一些点,因此这是在向量中添加顶点和颜色元素的代码:

void AddPoint() 
{
    glm::vec3 p(5.0,6.0,0.0);    
    m_Points.push_back(p);
    glBindBuffer(GL_ARRAY_BUFFER,sizeof(glm::vec3) * m_Points.size(),GL_DYNAMIC_DRAW);

    glm::vec3 b(1.0,0.0,0.0);
    m_Colors.push_back(b);
    glBindBuffer(GL_ARRAY_BUFFER,sizeof(glm::vec3) * m_Colors.size(),GL_DYNAMIC_DRAW);
}

问题是:最初的4个点可以用4种不同的颜色正确显示,但是AddPoint()永远无法工作,我看不到任何新的点被添加和渲染。

我知道,如果将顶点位置放在单个VBO中,

例如:

日期数组可以是:

x,y,z,r,g,b
x,b

它可以正常工作。

但是如果将位置和颜色放在不同的VBO中,为什么它不起作用?

有什么想法吗?谢谢。

这个问题(c++ - Storing different vertex attributes in different VBO's - Stack Overflow)是相关的,但我仍然不知道为什么我的问题仍然会发生。

编辑 Rabbid76指出glVertexAttribPointer()函数仅通过glBindBuffer附加(复制)来自先前绑定VBO的数据。哪个提示我不能将顶点位置数组和顶点颜色数组放在不同的VBO中?

EDIT2 我的AddPoint()函数在这里错误吗?

解决方法

glVertexAttribPointer将绑定到GL_ARRAY_BUFFER目标的缓冲区对象与指定属性相关联。规范存储在当前Vertex Array Object的状态向量中。
在调用glBindBuffer之前,正确的缓冲区对象必须由glVertexAttribPointer绑定:

// the first VBO,it is the coordinates
glGenBuffers(1,&m_VBO);
glBindBuffer(GL_ARRAY_BUFFER,m_VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(glm::vec3) * sizeof(m_Points),&m_Points[0][0],GL_DYNAMIC_DRAW);

// the second VBO,the color
glGenBuffers(1,&m_VBO_Color);
glBindBuffer(GL_ARRAY_BUFFER,m_VBO_Color);
glBufferData(GL_ARRAY_BUFFER,sizeof(glm::vec3) * sizeof(m_Colors),&m_Colors[0][0],GL_DYNAMIC_DRAW);

glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER,m_VBO); // <--- bind `m_VBO`

glVertexAttribPointer(0,// location = 0 in vertics shader file
                      3,// the position has X,Y,Z 3 elements
                      GL_FLOAT,// element type
                      GL_FALSE,// do not normalize
                      sizeof(glm::vec3),// Stride

glEnableVertexAttribArray(1);

glBindBuffer(GL_ARRAY_BUFFER,m_VBO_Color); // <--- bind `m_VBO_Color`

glVertexAttribPointer(1,// location = 1 in vertics shader file
                      3,// the position has R,G,B 3 elements
                      GL_FLOAT,// Stride
                      (void *) nullptr); // an offset of into the array,it is 0

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-