• Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    eliot
    Posts: 508 from 2004/4/15
    Finally I changed my code to VBOs.
    On MacOs and Linux everything is ok, sadly on MorphOs there is just a black window.

    Thats what I am doing:
    Code:

    // in method init objects
    // vertices
    glGenBuffers(1, &o.vertexId);
    glBindBuffer(GL_ARRAY_BUFFER, o.vertexId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * o.vertices.size(), o.vertices.data(), GL_STATIC_DRAW);

    // normals
    glGenBuffers(1, &o.normalId);
    glBindBuffer(GL_ARRAY_BUFFER, o.normalId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * o.normals.size(), o.normals.data(), GL_STATIC_DRAW);

    // in method render objects
    // set material
    glMaterialfv(GL_FRONT, GL_AMBIENT, m.ambient.rgba);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, m.diffuse.rgba);
    glMaterialfv(GL_FRONT, GL_SPECULAR, m.specular.rgba);
    glMaterialf(GL_FRONT, GL_SHININESS, m.shininess);

    glBindBuffer(GL_ARRAY_BUFFER, o.vertexId);
    glVertexPointer(3, GL_DOUBLE, sizeof(Vertex), NULL);
    glEnableClientState(GL_VERTEX_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, o.normalId);
    glNormalPointer(GL_DOUBLE, 0, NULL);
    glEnableClientState(GL_NORMAL_ARRAY);

    glDrawArrays(GL_TRIANGLES, 0, o.vertices.size());


    Anybody sees a mistake in this code?

    EDIT:

    Ok, I just worete a small example, an as usual, the mistake must be somewhere in my code.
    Here is a working example:
    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


    static char *TITLE = "test_materials_vbos";

    static GLuint verticesId = 0;
    static GLuint normalsId = 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 RA[] = {1, 0, 0, 1};
    static GLfloat RS[] = {1, 0, 0, 1};
    static GLfloat RD[] = {1, 0, 0, 1};

    static GLfloat GA[] = {0, 1, 0, 1};
    static GLfloat GS[] = {0, 1, 0, 1};
    static GLfloat GD[] = {0, 1, 0, 1};
    static GLfloat Y = 0;

    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);
    }

    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);

    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);

    glMaterialfv(GL_FRONT, GL_AMBIENT, RA);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, RD);
    glMaterialfv(GL_FRONT, GL_SPECULAR, RS);
    glMaterialf(GL_FRONT, GL_SHININESS, Y);

    glBindBuffer(GL_ARRAY_BUFFER, verticesId);
    glVertexPointer(3, GL_FLOAT, sizeof(GLfloat) * 3, NULL);
    glEnableClientState(GL_VERTEX_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, normalsId);
    glNormalPointer(GL_FLOAT, 0, NULL);
    glEnableClientState(GL_NORMAL_ARRAY);


    glDrawArrays(GL_TRIANGLES, 0, 3);
    glPopMatrix();
    }

    /* green triangle */
    {
    glPushMatrix();

    glTranslated(-1, 0, 0);

    glMaterialfv(GL_FRONT, GL_AMBIENT, GA);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, GD);
    glMaterialfv(GL_FRONT, GL_SPECULAR, GS);
    glMaterialf(GL_FRONT, GL_SHININESS, Y);

    glBindBuffer(GL_ARRAY_BUFFER, verticesId);
    glVertexPointer(3, GL_FLOAT, sizeof(GLfloat) * 3, NULL);
    glEnableClientState(GL_VERTEX_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, normalsId);
    glNormalPointer(GL_FLOAT, 0, NULL);
    glEnableClientState(GL_NORMAL_ARRAY);


    glDrawArrays(GL_TRIANGLES, 0, 3);
    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();

    /* enter main loop */
    glutMainLoop();

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


    result:
    test_materials_vbos.png

    I have to find the bug on my side.

    [ Edited by eliot 30.12.2016 - 09:38 ]
    regards
    eliot
  • »30.12.16 - 09:46
    Profile