让颜色使用顶点缓冲对象在OpenGL元 [英] Giving colour to primitives in OpenGL using Vertex Buffer Objects

查看:263
本文介绍了让颜色使用顶点缓冲对象在OpenGL元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C写一个程序,用OpenGL,画一些图元(线,圆等)。我已经成功地利用缓冲器的对象来存储元的顶点,因此,在绘制它们。但是,我现在似乎被卡住,因为我不知道如何设置自己的颜色。该计划的一部分是给我以我的老师,所以我知道的颜色属性必须采用这种结构指定:

I am writing a program in C, using OpenGL, to draw some primitives (lines, circles, etc.). I have succeeded in using the buffer objects to store the vertices of the primitives, and hence, in drawing them. But I seem to be stuck now since I don't know how to set their colour. Part of the program was given to me by my teacher, so I know that the colour attributes must be specified using this structure:

typedef struct {
    float r;
    float g;
    float b;
    float a;
} vec4;

我知道我应该使用一个vec4阵列来存储颜色值,所以我宣布一个这样的变量。第一个是一个VEC2阵列将被用于存储原始顶点

I know I am supposed to use a vec4 array to store colour values so I declared one such variable. The first one is a vec2 array which will be used to store the primitive vertices.

vec2 vertices[10000];
vec4 colours[10000];

这(据我所知)用于设置顶点缓冲区的限制功能如下:

The function which (as I understand) is used to set the limit of the vertex buffer looks like this:

void initShaders(void) 
{
    GLuint buffer;
    GLuint loc;
    GLuint vao;

    /*Create and initialize a program object with shaders */ 
  idProgram = initProgram("ass1vshader.glsl", "ass1fshader.glsl");

    /*installs the program object as part of current rendering state */ 
   glUseProgram(idProgram);

   /*Creat a vertex array object */ 
   glGenVertexArrays(1, &vao);
   glBindVertexArray(vao);

    /*Create buffer in the shared display list space and */ 
     /* bind it as GL_ARRAY_BUFFER */ 
    glGenBuffers( 1, &buffer);
    glBindBuffer( GL_ARRAY_BUFFER, buffer);
    glBufferData( GL_ARRAY_BUFFER, sizeof(vertex)+sizeof(colours), NULL,GL_STATIC_DRAW);

    /*Initialize attribute vPosition in program */ 
    loc = glGetAttribLocation( idProgram, "vPosition");
    glEnableVertexAttribArray(loc);
    glVertexAttribPointer(loc, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)BUFFER_OFFSET(0));

    /*Get location of the uniform attribute 'u_proj_matrix' */ 
    idProjection = glGetUniformLocation(idProgram, "u_proj_matrix");

    /*Set graphics attributes */ 
    glLineWidth(3.0);
    glPointSize(1.0);
    glClearColor(1.0, 1.0, 1.0, 1.0);

}

正如你所看到的,我已经初始化顶点缓冲区大小为顶点和色彩的组合大小。

As you can see, I have initialized the vertex buffer size to the combined size of "vertex" and "colours".

我还给出明白,片段着色器起着着色过程中起重要作用。它看起来是这样的:

I am also given to understand that the fragment shader plays an important role in the colouring process. It looks like this:

#version 140

in vec4 color;
out vec4  fColor;

void
main()
{    
    fColor = color;
}

有人可以告诉我怎么可以去给颜色我的原语?我希望我提供的信息就足够了。

Can somebody tell me how I can go about giving colour to my primitives? I hope the information I have provided is sufficient.

推荐答案

一做的方法是使用一个彩色阵列。你可以通过调用 glColorPointer() 作为你做 glVertexPointer()

One way to do it is to use a color array. You can do this by calling glColorPointer() in the same way as you do for glVertexPointer().

另外,我不知道这只是一个错字,或者如果你确实有这个错误,但你写的:

Also, I don't know if this is just a typo, or if you actually have this error, but you wrote:

glBufferData( GL_ARRAY_BUFFER, sizeof(vertex)+sizeof(colours), NULL,GL_STATIC_DRAW);

不过,你的顶点数组被命名为顶点而不是顶点(至少根据你刚才的发言)。如果它编译OK,那么它可能意味着你有一个名为顶点另一个变量或数据结构,但它不是做你的想法。

However, your vertex array is named "vertices" not "vertex" (at least according to your earlier statement). If it compiles OK, then it probably means you have another variable or data structure named vertex, but it's not doing what you think.

此外,由于你的数据不是交错(你有独立的顶点和颜色数据),我想你会想使用独立的缓冲区他们。如果他们在一个结构,你可以使用一个单一的阵列,并设置适当的步伐,但由于他们都没有交错的,我不知道,如果你试图做会工作的。

And since your data isn't interleaved (you have separate vertex and color data), I think you'll want to use separate buffers for them. If they were in one structure, you could use a single array, and set the stride appropriately, but since they aren't interleaved, I'm not sure if what you're attempting to do will work.

这篇关于让颜色使用顶点缓冲对象在OpenGL元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆