Results 1 to 7 of 7

Thread: Question about VBO and textures

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2011
    Posts
    6

    Question about VBO and textures

    Hello everyone,

    I`m trying to learn something about opengl, but I can`t 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?

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,649

    Re: Question about VBO and textures

    Quote Originally Posted by errfet
    I can`t understand how does textures match witch vbo. ... Unfortunately, this is not working. How should I define texture coordinates to achieve my goal?
    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.

  3. #3
    Super Moderator OpenGL Guru imported_dorbie's Avatar
    Join Date
    Jul 2000
    Location
    Bay Area, CA, USA
    Posts
    3,966

    Re: Question about VBO and textures

    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.

  4. #4
    Junior Member Newbie
    Join Date
    Oct 2011
    Posts
    6

    Re: Question about VBO and textures

    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

    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(C);
    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, &amp;id_VBO_vertices);
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, id_VBO_vertices);
    glBufferDataARB(GL_ARRAY_BUFFER_ARB, xNODES*yNODES*3*sizeof(GLfloat),_nodes, GL_STATIC_DRAW_ARB );

    //texture
    glGenBuffersARB(1, &amp;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

  5. #5
    V-man
    Guest

    Re: Question about VBO and textures

    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

  6. #6
    Junior Member Newbie
    Join Date
    Oct 2011
    Posts
    6

    Re: Question about VBO and textures

    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}

  7. #7
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,672

    Re: Question about VBO and textures

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

Similar Threads

  1. 2D textures & API question
    By Kullgen in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 11-11-2004, 12:17 AM
  2. Textures question
    By Chuffy_345 in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 02-18-2004, 05:09 AM
  3. Question About Textures
    By jleffelm in forum OpenGL: Basic Coding
    Replies: 11
    Last Post: 04-09-2003, 04:35 PM
  4. Question about textures
    By PopeKetric in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 01-22-2003, 02:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean