• Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    eliot
    Posts: 516 from 2004/4/15
    I am getting really crazy about using color arrays instead of glMaterial:

    Here is the code:

    Code:

    #ifdef __APPLE__

    #include <gl.h>
    #include <glu.h>
    #include <glut.h>

    #else
    #define GL_GLEXT_PROTOTYPES

    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <GL/glut.h>

    #endif

    #include <stdio.h>

    static char *TITLE = "test_materials_vbos";

    static GLuint verticesId = 0;
    static GLuint normalsId = 0;
    static GLuint redId = 0;
    static GLuint greenId = 0;

    static GLfloat VERTICES[] = {0, 0, 0,
    1, 0, 0,
    1, 1, 0};

    static GLfloat NORMALS[] = {0, 0, 1,
    0, 0, 1,
    0, 0, 1};

    static GLfloat RED[] = {1, 0, 0, 1,
    1, 0, 0, 1,
    1, 0, 0, 1};

    static GLfloat GREEN[] = {0, 1, 0, 1,
    0, 1, 0, 1,
    0, 1, 0, 1};

    static void objects() {
    /* vertices */
    glGenBuffers(1, &verticesId);
    glBindBuffer(GL_ARRAY_BUFFER, verticesId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), VERTICES, GL_STATIC_DRAW);

    /* normals */
    glGenBuffers(1, &normalsId);
    glBindBuffer(GL_ARRAY_BUFFER, normalsId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(NORMALS), NORMALS, GL_STATIC_DRAW);

    /* red */
    glGenBuffers(1, &redId);
    glBindBuffer(GL_ARRAY_BUFFER, redId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(RED), RED, GL_STATIC_DRAW);

    /* red */
    glGenBuffers(1, &greenId);
    glBindBuffer(GL_ARRAY_BUFFER, greenId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GREEN), GREEN, GL_STATIC_DRAW);

    }

    static void light() {
    GLfloat LP[] = {0, 0, 10, 1};
    GLfloat A[] = {0.1, 0.1, 0.1};
    GLfloat S[] = {0.5, 0.5, 0.5};
    GLfloat D[] = {0.5, 0.5, 0.5};

    glEnable(GL_LIGHT0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glLightfv(GL_LIGHT0, GL_POSITION, LP);
    glLightfv(GL_LIGHT0, GL_AMBIENT, A);
    glLightfv(GL_LIGHT0, GL_SPECULAR, S);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, D);
    }

    void init() {
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glEnable(GL_FRONT);
    glEnable(GL_LIGHTING);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT);

    glClearColor(0.f, 0.f, 0.f, 1.f);

    light();
    objects();
    }

    void reshape(int width, int height) {
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(65.0, (float) width / (float) height, 1.0, 100);
    }

    void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(0, 0, 5,
    0, 0, 0,
    0, 1, 0);

    /* red triangle */
    {
    glPushMatrix();

    glTranslated(-1, 0, 0);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, verticesId);
    glVertexPointer(3, GL_FLOAT, 0, (void *) 0);

    glBindBuffer(GL_ARRAY_BUFFER, normalsId);
    glNormalPointer(GL_FLOAT, 0, (void *) 0);

    glBindBuffer(GL_ARRAY_BUFFER, redId);
    glColorPointer(4, GL_FLOAT, 0, (void *) 0);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    glPopMatrix();
    }

    /* green triangle */
    {
    glPushMatrix();

    glTranslated(1, 0, 0);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, verticesId);
    glVertexPointer(3, GL_FLOAT, 0, (void *) 0);

    glBindBuffer(GL_ARRAY_BUFFER, normalsId);
    glNormalPointer(GL_FLOAT, 0, (void *) 0);

    glBindBuffer(GL_ARRAY_BUFFER, greenId);
    glColorPointer(4, GL_FLOAT, 0, (void *) 0);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    glPopMatrix();
    }
    glutSwapBuffers();
    }

    int main(int argc, char **argv) {
    int window = 0;

    /* init glut */
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(0, 0);

    /* create window */
    window = glutCreateWindow(TITLE);

    /* set callbacks */
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    /* init gl, light and objects */
    init();

    printf("sizeof RED=%dn", sizeof(RED));

    /* enter main loop */
    glutMainLoop();

    /* destroy window */
    glutDestroyWindow(window);
    return 0;
    }



    and this is the result:
    ogl_colors.png

    There should be one red triangle and another one in green.
    I could not find the error yet.

    EDIT: The example is now ok.
    In my other code I have found the problem:
    A really simple class (Color) like this one:

    Code:

    struct s{

    float f[4];
    };


    It is 24 bytes long (reported by sizeof) but should be 16 (four time float).
    I do not know where the extra bytes are coming from, but I now know where to search.

    [ Edited by eliot 23.01.2017 - 14:46 ]
    regards
    eliot
  • »23.01.17 - 14:45
    Profile