Questions about TinyGL (was TinyGL and glMaterialv)
  • Order of the Butterfly
    Order of the Butterfly
    eliot
    Posts: 452 from 2004/4/15
    Hello,

    some little changes. I add fog to the scene and cleaned up the source.
    It's now looking like this on linux: landscape_fog_linux.png

    Next steps:
    - find out what's going wrong on mos
    - use Color arrays per vertex instead of materials (paint water blue, and so on, ...)
    - object groups
    - animation

    EDIT:
    I really could not find the error in my code.
    The vertex and normal arrays are ok, I also checked every gl function call with glGetError(), nothing.
    I also looked into the autodocs but I could not any usefull hint.
    The vertex and normal count for my landscape is 151686, could that be the problem?





    [ Edited by eliot 09.01.2017 - 04:11 ]
    regards
    eliot

    www.exception-dev.de
  • »08.01.17 - 18:08
    Profile Visit Website
  • Paladin of the Pegasos
    Paladin of the Pegasos
    Jupp3
    Posts: 1190 from 2003/2/24
    From: Helsinki, Finland
    Quote:

    The vertex and normal count for my landscape is 151686, could that be the problem?

    As long as the max. value can be specified with the used index type (GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT), it should work. If not, I'd say that's a bug in GL implementation (unless there's some other bug elsewhere).

    But another question, how are you drawing your heightmap in general?

    Probably the most efficient way is to specify each point in map in the vertex array once, then have series of GL_TRIANGLE_STRIP arrays (in same VBO), that specify the vertex id's in triangles used in that particular row. Of course you also lose control over "which way quads are split to triangles" (splitting this conditionally is way more complex) - oh, and using GL_QUADS isn't useful either, basically you just pretend that OpenGL wouldn't draw 2 triangles instead (which it does).

    Then just do a simple for loop, and draw the rows you want. This way you can also only draw specific rows, which you probably want later, so you only draw specific range in front of the camera.

    Example:
    Vertex array (number specifies the order points are specified in)
    00 04 08
    01 05 09
    02 06 10
    03 07 11

    Index array:
    00, 04, 01, 05, 02, 06, 03, 07 (first row done),
    04, 08, 05, 09, 06, 10, 07, 11 (second row done)

    -EDIT-

    One generic tip for "I'm not getting the graphics I expected to" cases: Draw as GL_POINTS or GL_LINE_STRIP instead. That way, you will get some (wrong) graphics, even if triangles (should you have drawn as them) were facing backwards (and you've enabled bfc, like you should). Then just compare if you get vertices in locations you don't, when drawing with the "correct" primitive.

    [ Edited by Jupp3 10.01.2017 - 23:30 ]
  • »10.01.17 - 13:08
    Profile Visit Website
  • Order of the Butterfly
    Order of the Butterfly
    eliot
    Posts: 452 from 2004/4/15
    Hello again,

    finally I found a solution for MorphOs.
    Screenshots:
    linux: landscape_linux_fog_working.png
    mos: landscape_mos_fog_missing.png

    And that's what I have done:
    1. I replaced std::vector by my own Array class (move away from stack to heap)
    That should be done anyway.
    2. Implemented splitting of 3d objects (vertex and normals arrays)
    3. Objects with a vertex count lesser than 4096 will rendered correctly on mos
    (any greater number will fail)

    Nice!

    But, while blending works well (transparent water), fog on mos fails.
    That is the code I am using:
    Code:

    glEnable(GL_FOG);
    glFogi(GL_FOG_MODE, GL_LINEAR);
    glFogf(GL_FOG_DENSITY, 1.0f);
    glFogf(GL_FOG_START, 0.f);
    glFogf(GL_FOG_END, range / 2.0f);
    glFogfv(GL_FOG_COLOR, scene->clearColor.rgba);


    I can remember that the guys of Wings Battlefield also had problems with glFog on mos.
    Sadly I do not know the solution.
    Any hints?
    regards
    eliot

    www.exception-dev.de
  • »12.01.17 - 18:10
    Profile Visit Website
  • Paladin of the Pegasos
    Paladin of the Pegasos
    Jupp3
    Posts: 1190 from 2003/2/24
    From: Helsinki, Finland
    Quote:

    eliot wrote:
    I can remember that the guys of Wings Battlefield also had problems with glFog on mos.
    Sadly I do not know the solution.
    Any hints?


    There has been major rewrite in some parts of 3D acceleration (on some GPU's), which sadly also resulted in some previously working features becoming broken. Not sure, if fog was one of them (texture matrix is, at least)
  • »12.01.17 - 23:57
    Profile Visit Website
  • Order of the Butterfly
    Order of the Butterfly
    BSzili
    Posts: 432 from 2012/6/8
    From: Hungary
    GL_FOG stuff is not implemented in TinyGL.
    I see the jimmies have been rustled.
  • »13.01.17 - 07:20
    Profile Visit Website
  • Order of the Butterfly
    Order of the Butterfly
    eliot
    Posts: 452 from 2004/4/15
    Quote:

    BSzili wrote:
    GL_FOG stuff is not implemented in TinyGL.


    Ok, what I can use instead?
    Or will it be implemented in near future?

    EDIT: Can somebody of the mods change this topic to a more general one like "Questions about TinyGL"?

    [ Edited by eliot 13.01.2017 - 06:42 ]
    regards
    eliot

    www.exception-dev.de
  • »13.01.17 - 07:37
    Profile Visit Website
  • Order of the Butterfly
    Order of the Butterfly
    eliot
    Posts: 452 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

    www.exception-dev.de
  • »23.01.17 - 14:45
    Profile Visit Website
  • Order of the Butterfly
    Order of the Butterfly
    eliot
    Posts: 452 from 2004/4/15
    Nice,

    cloring is now working (no glMaterial anymore). The Landscape generator can be parameterised
    (I am not a designer who can paint landscapes).

    Linux: linux_landscape_coloring.png
    Mos: mos_landscape_coloring.png

    Another question: currently I am using floats for rgba values. It would use less memory if I
    use chars instead of floats. Any disadvantages?

    Next to go: animations

    [ Edited by eliot 26.01.2017 - 19:17 ]
    regards
    eliot

    www.exception-dev.de
  • »25.01.17 - 15:28
    Profile Visit Website
  • Order of the Butterfly
    Order of the Butterfly
    eliot
    Posts: 452 from 2004/4/15
    Hello again,

    SDL1 is now working on all platforms. I am getting better keyboard and mouse events and
    fullscreen is also working. I also fixed lighting stuff (should look better now).

    Morphos with SDL:heightmap_sdl_mos.png

    I am focussing on learning Blender because I want to create some 3D objects like a helicopter, trees, buildings, ...



    [ Edited by eliot 25.02.2017 - 15:53 ]
    regards
    eliot

    www.exception-dev.de
  • »25.02.17 - 16:41
    Profile Visit Website