如何使用OpenGL和GLSL改变颜色(C++)?

发布于 2024-12-20 20:22:01 字数 775 浏览 5 评论 0原文

我正在使用 C++ 开发 OpenGL 项目,我想学习如何使用 GLSL 着色器。问题是,虽然我可以在不使用自己的着色器的情况下完成我的程序,但我想编写自己的着色器(此时我自己没有加载任何着色器 - 不确定 OpenGL 是否默认有任何着色器)。

为了在图像上绘制一堆线(其顶点存储在一个向量中,颜色值存储在另一个向量中),我使用以下代码在帧更新时运行并渲染它们 - 我使用 GLUT 显示函数为了这。

glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);  
vector<point2>::iterator it;
int c_index = 0;
for(it=points.begin(); it<points.end(); it++){
    if(c_index % 2 == 0) //set color for every pair of points (each line)
        glColor3f(colors[c_index/2][0], colors[c_index/2][1], colors[c_index/2][2]);
    c_index++;

    glVertex2f(it->x, it->y); //set vertex
}
glEnd();
glFlush();

现在,问题是当我尝试使用在另一个示例程序中找到的着色器时,颜色全是红色(即使着色器没有明确定义任何颜色)。它还使 glColor3f 什么都不做。

我的问题是,假设我可以使用着色器来执行此操作,着色器必须是什么样子,以及如何加载它们?

I am working on an OpenGL project in C++, and I want to learn how to use GLSL shaders. The problem is that, while I can complete my program without using my own shaders, I want to write my own (at this time there are no shaders that I load myself - not sure if OpenGL defaults any).

To draw a bunch of lines on the image (whose vertices are stored in a vector and color values are stored in another vector), I use the following code to run through and render them whenever the frame is updated - I use the GLUT display function for this.

glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);  
vector<point2>::iterator it;
int c_index = 0;
for(it=points.begin(); it<points.end(); it++){
    if(c_index % 2 == 0) //set color for every pair of points (each line)
        glColor3f(colors[c_index/2][0], colors[c_index/2][1], colors[c_index/2][2]);
    c_index++;

    glVertex2f(it->x, it->y); //set vertex
}
glEnd();
glFlush();

Now, the problem is when I try to use shaders that I found in another example program, the color is all red (even though the shaders do not explicitly define any colors). It also makes glColor3f do nothing.

My question is, assuming I can use shaders to do this, what do the shaders have to look like, and how would I load them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

比忠 2024-12-27 20:22:01

显然,使用 glColor3f 无法完成此操作。相反,我必须创建一个预先确定的 OpenGL 缓冲区,并将缓冲区数据发送到着色器。它不能像我希望的那样“即时”完成。

Apparently it cannot be done using glColor3f. Instead, I had to make a pre-determined OpenGL buffer, and send the buffer data to the shaders. It can't be done "on the fly" as I hoped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文