简体   繁体   English

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

[英]How to use OpenGL and GLSL to change color (C++)?

I am working on an OpenGL project in C++, and I want to learn how to use GLSL shaders. 我正在用C ++开发OpenGL项目,我想学习如何使用GLSL着色器。 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). 问题是,虽然我可以不用自己的着色器来完成程序,但我想编写自己的着色器(此时,我没有加载任何着色器-不知道OpenGL是否默认)。

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. 要在图像上绘制一束线(其顶点存储在矢量中,颜色值存储在另一个矢量中),我使用以下代码来遍历并在帧更新时渲染它们-我使用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();

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. 这也使glColor3f什么也不做。

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? 我的问题是,假设我可以使用着色器执行此操作,那么着色器必须具有什么样的外观,如何加载它们?

Apparently it cannot be done using glColor3f. 显然,它无法使用glColor3f完成。 Instead, I had to make a pre-determined OpenGL buffer, and send the buffer data to the shaders. 相反,我必须制作一个预定的OpenGL缓冲区,然后将缓冲区数据发送到着色器。 It can't be done "on the fly" as I hoped. 正如我希望的那样,它不可能“即时”完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM