0

I know it is very basic question but I have array of vertices and texture coordinates and corresponding buffers:

glBindBuffer(GL_ARRAY_BUFFER,vertexbuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(float)*Loader.verticesFixed.size(),&Loader.verticesFixed[0],GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER,texturebuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(float)*Loader.texturesFixed.size(),&Loader.texturesFixed[0],GL_STATIC_DRAW);

and I wonder how to send data for rendering.

I know that I can merge array of vertices and textures so that I have one array that has x1,y1,z1,u1,v1,x2,y2,z2,u2,v2... then create buffer for this array, bind it and use glVertexAttribPointer with particular stride and offset and attributes 0 and 1 for vertices and texture coordinates and glDrawArrays. However I wonder if it is possible to send data from multiple buffers without merging arrays.

I am asking this because I want to add textures to 3d model and if I understood everything correctly to do it efficiently I need to send data to shader using glVertexAttribPointer with different indices (locations) together with uniform texture.

2
  • 1
    Are you asking how to upload two arrays of non-interleaved data into an OpenGL buffer of interleaved data? You'll have to do the interleaving yourself, either on your own buffer with glBufferData or by glMapBuffer'ing the GL buffer. Commented Apr 30, 2016 at 1:38
  • 1
    "I need to send data to shader using glVertexAttribPointer with different indices (locations) together with uniform texture." What do you mean by "different indices"? Do you mean to use an index list for positions and a separate index list for texture coordinates? Commented Apr 30, 2016 at 4:24

1 Answer 1

1

I'm guessing you want to send your data like this : x1,y1,z1,x2,y2,z2....u1,v1,u2,v2... I actually don't really know if that's possible but I think it would be rather inefficient anyway. When you have your data interleaved, you have everything you need for one vertex in one place, you have the position and right next to it you have the texture information, you dont have to "jump" to find the texture coordinates. I don't know if openGL actually works this way, it's just an assumption.

However, for sending texture information to a shader, using the "normal interleaved" way is just fine. You would then have to bind your texture with glBindTexture(GL_TEXTURE_2D,texture_id); before making you draw call.

In the vertex shader you would then access the texture position of the vertex just like you would access the position information with layout (location = 1) in vec2 TexCoord;

For example if your data is arranged like x1,y1,z1,u1,v1,x2,y2... then you set the "location" yourself when you call glVertexAttribPointer(location,2,GL_FLOAT,GL_FALSE,stride,(GLvoid*)(3*sizeof(GLfloat))); glEnableVertexAttribArray(location);

I hope I cleared some things up. If you are new to texturing in OpenGL, or OpenGL in general, I recommend http://learnopengl.com/#!Getting-started/Textures

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.