• Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    eliot
    Posts: 564 from 2004/4/15
    Quote:

    Jupp3 wrote:
    I really hope it's just this simple test code, and you're not using immediate mode (glBegin() ...) in a "real" program in 2016 :-)


    Ok, ok, just to it right, here is beter example:

    Code:

    #ifdef __APPLE__
    #include <gl.h>
    #include <glu.h>
    #include <glut.h>
    #else

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

    #endif

    static char *TITLE = "test_materials";

    static GLint quadId = 0;
    static GLint triangleAId = 0;
    static GLint triangleBId = 0;

    static void objects() {
    GLfloat RA[] = {(GLfloat) 1,
    (GLfloat) 0,
    (GLfloat) 0,
    (GLfloat) 1};
    GLfloat RS[] = {(GLfloat) 1,
    (GLfloat) 0,
    (GLfloat) 0,
    (GLfloat) 1};
    GLfloat RD[] = {(GLfloat) 1,
    (GLfloat) 0,
    (GLfloat) 0,
    (GLfloat) 1};

    GLfloat GA[] = {(GLfloat) 0,
    (GLfloat) 1,
    (GLfloat) 0,
    (GLfloat) 1};
    GLfloat GS[] = {(GLfloat) 0,
    (GLfloat) 1,
    (GLfloat) 0,
    (GLfloat) 1};
    GLfloat GD[] = {(GLfloat) 0,
    (GLfloat) 1,
    (GLfloat) 0,
    (GLfloat) 1};

    GLfloat BA[] = {(GLfloat) 0,
    (GLfloat) 0,
    (GLfloat) 1,
    (GLfloat) 1};
    GLfloat BS[] = {(GLfloat) 0,
    (GLfloat) 0,
    (GLfloat) 1,
    (GLfloat) 1};
    GLfloat BD[] = {(GLfloat) 0,
    (GLfloat) 0,
    (GLfloat) 1,
    (GLfloat) 1};
    GLfloat Y = 0;

    quadId = glGenLists(1);
    glNewList(quadId, GL_COMPILE);
    glNormal3d(0, 0, 1);
    glMaterialfv(GL_FRONT, GL_AMBIENT, RA);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, RD);
    glMaterialfv(GL_FRONT, GL_SPECULAR, RS);
    glMaterialf(GL_FRONT, GL_SHININESS, Y);
    glBegin(GL_QUADS);
    glVertex3d(0, 0, 0);
    glVertex3d(1, 0, 0);
    glVertex3d(1, 1, 0);
    glVertex3d(0, 1, 0);
    glEnd();
    glEndList();

    triangleAId = glGenLists(1);
    glNewList(triangleAId, GL_COMPILE);
    glNormal3d(0, 0, 1);
    glMaterialfv(GL_FRONT, GL_AMBIENT, GA);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, GD);
    glMaterialfv(GL_FRONT, GL_SPECULAR, GS);
    glMaterialf(GL_FRONT, GL_SHININESS, Y);
    glBegin(GL_TRIANGLES);
    glVertex3d(1, 0, 0);
    glVertex3d(2, 0, 0);
    glVertex3d(2, 1, 0);
    glEnd();
    glEndList();

    triangleBId = glGenLists(1);
    glNewList(triangleBId, GL_COMPILE);
    glNormal3d(0, 0, 1);
    glMaterialfv(GL_FRONT, GL_AMBIENT, BA);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, BD);
    glMaterialfv(GL_FRONT, GL_SPECULAR, BS);
    glMaterialf(GL_FRONT, GL_SHININESS, Y);
    glBegin(GL_TRIANGLES);
    glVertex3d(0, 0, 0);
    glVertex3d(-1, 1, 0);
    glVertex3d(-1, 0, 0);
    glEnd();
    glEndList();
    }

    static void light() {
    GLfloat LP[] = {(GLfloat) 0,
    (GLfloat) 0,
    (GLfloat) 10,
    (GLfloat) 1};
    GLfloat A[] = {(GLfloat) 0.1,
    (GLfloat) 0.1,
    (GLfloat) 0.1};
    GLfloat S[] = {(GLfloat) 0.5,
    (GLfloat) 0.5,
    (GLfloat) 0.5};
    GLfloat D[] = {(GLfloat) 0.5,
    (GLfloat) 0.5,
    (GLfloat) 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);

    glCallList(quadId);
    glCallList(triangleAId);
    glCallList(triangleBId);

    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;
    }
    regards
    eliot
  • »15.10.16 - 08:25
    Profile