Question about VBO and textures

Hello everyone,

Im trying to learn something about opengl, but I cant understand how does textures match witch vbo. I want to create 3D terrain, and use some grass-texture. Terrain is a grid, for example it is 3x3 nodes:

2-5-8
|T|T|
1-4-7
|T|T|
0-3-6

so, there is an array of points, C_point nodes[3][3]. Next
thing I need is an array of indices, so I defined:
indices[] = {3,4,1,0,4,5,2,1,6,7,4,3,7,8,5,4} (later, to draw terrain I use glDrawElements with GL_QUADS as firs parameter)

I want to define one texture per quad. So i did something like this:

TEXCoords coords[16]; //TEXCoords keeps 2 GLfloats.

#define A 0.f, 0.f
#define B 0.f, 1.f
#define C 1.f, 1.f
#define D 1.f, 0.f

coords[0].set(A);
coords[1].set(B);
coords[2].set(D);
coords[3].set(C);

coords[4].set(A);
coords[5].set(B);
coords[6].set(D);
coords[7].set(C);

coords[8].set(A);
coords[9].set(B);
coords[10].set(D);
coords[11].set(C);

coords[12].set(A);
coords[13].set(B);
coords[14].set(D);
coords[15].set(C);

Unfortunately, this is not working. How should I define texture coordinates to achieve my goal? Or maybe am I totally wrong?

I think terminology may be a barrier to understanding your question.

Do you want to have a copy of the same texture on each QUAD? Or do you want a different texture on each QUAD?

Also, when you say “it’s not working”, please provide some details. Without that we have no idea what you’ve done and might have slipped up on. It may be something simple like you haven’t bound the texture, or have your transforms wrong.

That looks about right. Indices 3,4,1,0 gives you a coordinate order of:

1.f, 1.f
1.f, 0.f
0.f, 0.f
0.f, 1.f

That will need to correlate with your vertex numerical order and be sane for the primitive type. Of course your vertex issue order will match the indices, so coordinates need to correlate in some manner with position regardless of the primitive type.

You do not say what went wrong and don’t supply position so it is impossible to guess, but what you have MIGHT be correct depending on the rest of your code.

Ok, I will show you all steps I do, with results of program. I attached two files. One is the texture I want to use (it is used for testing purposes;)) and the another one is program result.

To answer the question DarkPhoton asked me, I want one texture on each quad. If this is wrong idea just tell me :wink:

First of all, used classes:

C_point{
public:
GLfloat x, y, z;
}

C_texcoord{
public:
GLfloat u,v;
}

Grid is defined as:
C_point nodes[xNodes][yNodes]; //where xNodes=yNodes=3

Next thing I need is array of indices. I`ve assumed grid points numeration as follows:
2-5-8
| | |
1-4-7
| | |
0-3-6
In this case indices is array of 16 size. Points are set as follows:
3,4,1,0 4,5,2,1 6,7,4,3 7,8,5,4

First of all, function that builds vbo:

#define A 0.f, 0.f
#define B 0.f, 1.f
#define C 1.f, 1.f
#define D 1.f, 0.f
C_texcoord coords[16];
BuildVBO(){
//setting texture coordinates
//according to the indices, I wonder if the texture coordinates shouldn`t be in order D, C, B, A, but looks even worse than the picure i attached.
coords[0].set(A);
coords[1].set(B);
coords[2].set©;
coords[3].set(D);

coords[4].set(A);
coords[5].set(B);
coords[6].set(C);
coords[7].set(D);

coords[8].set(A);
coords[9].set(B);
coords[10].set(C);
coords[11].set(D);

coords[12].set(A);
coords[13].set(B);
coords[14].set(C);
coords[15].set(D);

    C_point _nodes[9];

//this is to make sure that points are in order I want them to be
int i=0;
for (int x=0;x<xNodes;x++){
for (int y=0;y<yNodes;y++){
_nodes[i] = nodes[x][y];
i++;
}
}

//grid
glGenBuffersARB(1, &id_VBO_vertices);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, id_VBO_vertices);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, xNODESyNODES3*sizeof(GLfloat),_nodes, GL_STATIC_DRAW_ARB );

//texture
glGenBuffersARB(1, &id_VBO_texture);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, id_VBO_texture);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, _SIZE*sizeof(GLfloat),coords, GL_STATIC_DRAW_ARB );
}

And next - drawing function.
Draw(){

	glEnableClientState( GL_VERTEX_ARRAY );						// Enable Vertex Arrays
//	glEnableClientState( GL_COLOR_ARRAY );						// Enable Vertex Array
	glEnableClientState( GL_TEXTURE_COORD_ARRAY );         // Enable textures
	//BuildVBO();

// glBindBufferARB(GL_ARRAY_BUFFER,id_VBO_colors);
// glColorPointer( 3, GL_FLOAT, 0, NULL);

	glBindBufferARB(GL_ARRAY_BUFFER,id_VBO_vertices);
	glVertexPointer( 3, GL_FLOAT, 0, NULL);

	glBindBufferARB(GL_ARRAY_BUFFER_ARB, id_VBO_texture);
	glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL);

	//glDrawElements( GL_TRIANGLES, indices_size, GL_UNSIGNED_INT, indices );
	glDrawElements( GL_QUADS, indices_size, GL_UNSIGNED_INT, indices );

	glDisableClientState( GL_TEXTURE_COORD_ARRAY );
	glDisableClientState( GL_VERTEX_ARRAY );	// Disable Vertex Arrays
	//glDisableClientState( GL_COLOR_ARRAY );

	SDL_GL_SwapBuffers();

}

I hope I am clear enough :slight_smile:

It looks like you have 8 vertices and 16 texcoords. Change your code so that you have 1 texcoord per vertex.

You can declare a vertex struct like this if you want
http://www.opengl.org/wiki/VBO_-_just_examples

Sorry but I don`t get it. How can i decrease number of texcords while there are 4 squares, and each one must have texcord defined?

I have question, in regards to the http://www.opengl.org/wiki/VBO_-_just_examples. When a point is shared among few vertices, what should I do? Define two points with the same vertex coordinates and different texcoords?

Lets assume I have something like this:
1-3-5
| | |
0-2-4
struct MyVertex
{
float x, y, z; //Vertex
float s0, t0; //Texcoord0
};
MyVertex p2_in2310, p2_in4532;

p2_in2310 = {1.0f, 0.0, 0.0, 1.0f, 1.0f}
p2_in4532 = {2.0f, 0.0, 0.0, 0.0f, 0.0f}

Define two points with the same vertex coordinates and different texcoords?

Yes.