简体   繁体   English

OpenGL复制顶点数组

[英]OpenGL Duplicating Vertex Arrays

I am looking for some clarification and some guidance regarding duplicating indices that are already in an array.I need to from what I gather duplicate indices so that I can render a cube (each side a different colour). 我正在寻找一些有关重复数组中已有索引的说明和指导,我需要从收集重复索引的内容中进行渲染,以便渲染一个立方体(每边各有不同的颜色)。 My cube is read in from a "OBJ" file. 我的多维数据集是从“ OBJ”文件中读取的。

Current Code 当前代码

using std::vector;

vector<GLfloat>vertex;
vector<GLuint>faces;

GLubyte color1[] =
{
   255,255,0,
   255,255,0,
   255,255,0,
   255,255,0,

   255,0,255,
   255,0,255,
   255,0,255,
   255,0,255

};

struct OBJVertex
{
   GLint f1;
   GLint f2;
   GLint f3;

   GLfloat x;
   GLfloat y;
   GLfloat z;

}obj;

int OBJLoader::LoadOBJData(string filename)
{
   ifstream f_obj;

   string line;

   f_obj.open(filename, ios::in);

while (!f_obj.eof())
{
    getline(f_obj, line);

    if (line.find("v") != line.npos)
    {
        sscanf_s(line.c_str(), "v %f %f %f ", &obj.x, &obj.y, &obj.z);
        vertex.push_back(obj.x);
        vertex.push_back(obj.y);
        vertex.push_back(obj.z);
    }
    if (line.find("f ")!= line.npos)
    {
        sscanf_s(line.c_str(), "f %d %d %d ", &obj.f1, &obj.f2, &obj.f3);
        obj.f1 = obj.f1 - 1;
        obj.f2 = obj.f2 - 1;
        obj.f3 = obj.f3 - 1;

        faces.push_back(obj.f1);
        faces.push_back(obj.f2);
        faces.push_back(obj.f3);
    }
}
return 0;
}
void OBJLoader::RenderOBJ()
{
   glEnable(GL_DEPTH_TEST);
   glEnableClientState(GL_COLOR_ARRAY);
   glEnableClientState(GL_VERTEX_ARRAY);

   glColorPointer(3, GL_UNSIGNED_BYTE, 0, color1);
   glVertexPointer(3,GL_FLOAT, 0, &vertex[0]);

   glDrawElements(GL_TRIANGLE_STRIP,faces.size(),GL_UNSIGNED_INT,&faces[0]);

   glDisableClientState(GL_VERTEX_ARRAY);
   glDisableClientState(GL_COLOR_ARRAY);
}

Am I correct in thinking I need to bind both the vertex and colour array using glBindBuffer? 我是否认为需要使用glBindBuffer绑定顶点和颜色数组是否正确?

glBindBuffer(GL_ARRAY_BUFFER,vertex[0]); 

I currently have a colour array setup named "color1",this colours the top and bottom faces of the cube after binding the first vertex and colour array am I correct in thinking I can create two more colour arrays for the remaining sides of the cube? 我目前有一个名为“ color1”的颜色阵列设置,该颜色在绑定第一个顶点和颜色阵列后为立方体的顶面和底面着色

Idea? 理念?

glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);

glColorPointer(3, GL_UNSIGNED_BYTE, 0, color1);    // first colour array //
glVertexPointer(3,GL_FLOAT, 0, &vertex[0]);

glDrawElements(GL_TRIANGLE_STRIP, faces.size(), GL_UNSIGNED_INT,&faces[0]);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER,vertex[0]); 



glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);

glColorPointer(3, GL_UNSIGNED_BYTE, 0, color2);    // second colour array //
glVertexPointer(3,GL_FLOAT, 0, &vertex[0]);

glDrawElements(GL_TRIANGLE_STRIP, faces.size(), GL_UNSIGNED_INT,&faces[0]);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER,vertex[0]); 

----Update---- ----更新----

Sorry, but I made a colour array per face but it is just overwriting the previous array.Can someone explain what I have not done right? 抱歉,但是我每张脸都做了一个彩色阵列,但它只是覆盖了以前的阵列,有人可以解释我做得不好吗?

GLubyte color1[] =
{
    255,255,0,
    255,255,0,
    255,255,0,
    255,255,0,

    255,0,255,
    255,0,255,
    255,0,255,
    255,0,255

    };

GLubyte color2[] =
{
    255,0,0,
    255,0,0,
    255,0,0,
    255,0,0,

    0,0,255,
    0,0,255,
    0,0,255,
    0,0,255
};

GLubyte color3[] =
{
    0,255,255,
    0,255,255,
    0,255,255,
    0,255,255,

    255,0,255,
    255,0,255,
    255,0,255,
    255,0,255
};

GLubyte color4[] =
{
    255,255,255,
    255,255,255,
    255,255,255,
    255,255,255,

    0,255,0,
    0,255,0,
    0,255,0,
    0,255,0
};

void OBJLoader::RenderOBJ()
{
    glEnable(GL_DEPTH_TEST);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    glVertexPointer(3, GL_FLOAT, 0, &vertex[0]);

    glColorPointer(3, GL_UNSIGNED_BYTE, 0, color1);
    glColorPointer(3, GL_UNSIGNED_BYTE, 0, color2);
    glColorPointer(3, GL_UNSIGNED_BYTE, 0, color3);
    glColorPointer(3, GL_UNSIGNED_BYTE, 0, color4);

    glDrawElements(GL_TRIANGLE_STRIP, faces.size(), GL_UNSIGNED_INT, &faces[0]);
    glDrawElements(GL_TRIANGLE_STRIP, faces.size(), GL_UNSIGNED_INT, &faces[0]);
    glDrawElements(GL_TRIANGLE_STRIP, faces.size(), GL_UNSIGNED_INT, &faces[0]);
    glDrawElements(GL_TRIANGLE_STRIP, faces.size(), GL_UNSIGNED_INT, &faces[0]);

    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);
}

----OBJ---- ---- OBJ ----

v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v 1.000000 1.000000 1.000000
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
f 5 1 4
f 5 4 8
f 3 7 8
f 3 8 4
f 2 6 3
f 6 7 3
f 1 5 2
f 5 6 2
f 5 8 6
f 8 7 6
f 1 2 3
f 1 3 4

In your case you don't need to bind a buffer at all since you send the index of your buffer directly into glColorPointer() . 在您的情况下,您根本不需要绑定缓冲区,因为您将缓冲区的索引直接发送到glColorPointer()

Think of it as sending a pointer to your buffer directly to OpenGL to read from. 可以将其视为直接将指向缓冲区的指针发送到OpenGL进行读取。 You can create more color buffers and simply change your glColorPointer() . 您可以创建更多的颜色缓冲区,只需更改glColorPointer()

glColorPointer(3, GL_UNSIGNED_BYTE, 0, color2);
//...
glColorPointer(3, GL_UNSIGNED_BYTE, 0, color3);

alternatively you can have all colors in one buffer called color and simply pass the right offset. 另外,您也可以将所有颜色放在一个称为color的缓冲区中,然后简单地传递正确的偏移量。 assuming you had 72 components for three colors in a single buffer you can do. 假设您在一个缓冲区中有72种用于三种颜色的成分,则可以这样做。

glColorPointer(3, GL_UNSIGNED_BYTE, 0, &color[0]);
//...
glColorPointer(3, GL_UNSIGNED_BYTE, 0, &color[24]);
//...
glColorPointer(3, GL_UNSIGNED_BYTE, 0, &color[48]);

You only need to bind buffers if you pass a null pointer like this 仅当传递像这样的空指针时,才需要绑定缓冲区

//setup your array buffer
GLuint colorBuffID;
glGenBuffers(1, &colorBuffID);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffID);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(unsigned char), color1, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//...
//use your array buffer
glBindBuffer(GL_ARRAY_BUFFER, colorBuffID);
glColorPointer( 3, GL_UNSIGNED_BYTE, 0, (const void*)( 0 ) );
// draw the quad
glBindBuffer(GL_ARRAY_BUFFER, 0);

If a non-zero named buffer object is bound to the GL_ARRAY_BUFFER target (see glBindBuffer) while a color array is specified, pointer is treated as a byte offset into the buffer object's data store. 如果在指定颜色数组时将非零命名缓冲区对象绑定到GL_ARRAY_BUFFER目标(请参阅glBindBuffer),则将指针视为缓冲区对象数据存储区中的字节偏移量。 Also, the buffer object binding (GL_ARRAY_BUFFER_BINDING) is saved as color vertex array client-side state (GL_COLOR_ARRAY_BUFFER_BINDING). 另外,缓冲区对象绑定(GL_ARRAY_BUFFER_BINDING)被保存为颜色顶点数组客户端状态(GL_COLOR_ARRAY_BUFFER_BINDING)。

Refer https://www.opengl.org/sdk/docs/man2/xhtml/glColorPointer.xml 请参阅https://www.opengl.org/sdk/docs/man2/xhtml/glColorPointer.xml

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

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