Thread: glDrawElements()

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    glDrawElements()

    I can't seem to get the glDrawElement to work correctly. Could someone tell me what I am doing wrong? Thanks
    Code:
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <GL/glut.h>
    #include <stdlib.h>
    
    void init(void){
            glClearColor(0.0,0.0,0.0,0.0);
            glShadeModel(GL_SMOOTH);
    }
    
    void reshape(GLint w,GLint h){
            glViewport(0,0,(GLsizei)w,(GLsizei)h);
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);
    }
    
    void display(void){
            GLdouble mix[]={25.0,25.0,0.0,
                            35.0,25.0,0.0,
                            35.0,35.0,0.0,
                            25.0,35.0,0.0};
            glClear(GL_COLOR_BUFFER_BIT);
            glEnableClientState(GL_VERTEX_ARRAY);
            glVertexPointer(3,GL_DOUBLE,6*sizeof(GLdouble),mix);
            glDrawElements(GL_LINE_STRIP,3,GL_DOUBLE,mix);
            glDisableClientState(GL_VERTEX_ARRAY);
            glutSwapBuffers();
    }
    
    int main(int argc,char **argv){
            glutInit(&argc,argv);
            glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
            glutInitWindowSize(350,350);
            glutCreateWindow(argv[0]);
            init();
            glutDisplayFunc(display);
            glutReshapeFunc(reshape);
            glutMainLoop();
            return EXIT_SUCCESS;
    }

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    I think you want a stride of 0.

    NAME
    glVertexPointer - define an array of vertex data

    C SPECIFICATION
    void glVertexPointer( GLint size,
    GLenum type,
    GLsizei stride,
    const GLvoid *pointer )

    PARAMETERS
    size Specifies the number of coordinates per vertex; must be 2, 3,
    or 4. The initial value is 4.

    type Specifies the data type of each coordinate in the array. Sym-
    bolic constants GL_SHORT, GL_INT, GL_FLOAT, and GL_DOUBLE are
    accepted. The initial value is GL_FLOAT.

    stride Specifies the byte offset between consecutive vertices. If
    stride is 0, the vertices are understood to be tightly packed
    in the array. The initial value is 0.

    pointer Specifies a pointer to the first coordinate of the first ver-
    tex in the array. The initial value is 0.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    this was true, I did forget to change the value from a previous code example I was doing. But it didn't fix the problem. I think i am misinerpreting how glDrawElements works.

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    glEnableClientState(GL_VERTEX_ARRAY);

    edit: do'h, there it is.
    hmmmmm... have you tried glFlush()ing at the end of your draw routine?

  5. #5
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    You're not using glDrawElements correctly. It should be used like this:

    Code:
            glEnableClientState(GL_VERTEX_ARRAY);
            glVertexPointer(3, GL_DOUBLE, 0, mix);
    
            unsigned int indices[] = { 0, 1, 2, 3 };
            glDrawElements(GL_LINE_STRIP, 4, GL_UNSIGNED_INT, indices);
    
            glDisableClientState(GL_VERTEX_ARRAY);
    The last parameter specifies which elements of mix[] should be used. I think it's easier to use glDrawArrays() in your program:

    Code:
            glEnableClientState(GL_VERTEX_ARRAY);
            glVertexPointer(3, GL_DOUBLE, 0, mix);
    
            // glDrawArrays(type, begin, number_of_elements)
            glDrawArrays(GL_LINE_STRIP, 0, 4);
    
            glDisableClientState(GL_VERTEX_ARRAY);

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    ahhh thank you so much. I knew I was misunderstanding something!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. glDrawElements()
    By Unregistered in forum Game Programming
    Replies: 3
    Last Post: 10-24-2004, 11:56 AM