user49335
user49335

Reputation: 13

Why is the third vertex of my first triangle always at the origin and the rest of my triangles not being drawn?

I've checked the results of everything and I've tidied up multiple bugs in my draw function already, but I still can't find the reason for the behavior described in the question title. I'm using OpenGL 1.4, 3D textures, vertex arrays, texture coordinate arrays, and glDrawArrays to draw models (from my model API) with textures (from my texture API) to the screen. Through looking at the results of everything (printfs), I've concluded the problem has to be in the block of code that actually draws everything, and not my code that fills these arrays with post animating vertex data (so I'm only posting the former to save on bloating this post).

The current color is used to achieve a per current window brightness effect. The variable msindex is already set to the number of model draw specifications before the loop featured begins. Vertex data and texture coordinate data for every model being drawn are actually all stuffed into one segment, and as you can see below there are glVertexPointer and glTexCoordPointer calls on different parts of the start of it to register this data. The contents of this segment are tightly packed, with three floats for the position of a vertex first and then three floats following for its texture coordinates. There is multitexturing (up to two textures specified much earlier in the model), but both textures share the same texture coordinates (which is why both calls to glTexCoordPointer specify the same location in memory). The while loop is meant to draw each individual specified model according to information for the model draw specification in the miaptr segment. Start is, in my code, the starting 6 float wide index into the overall vertex data segment for the first vertex of the model to be drawn, and count is the number of vertices. In my example case these are just 0 for start and 6 for count (attempted to draw one model with two triangles). Type can be multiple things depending on the model, but in this case it is GL_TRIANGLES. I've tried this with other primitive types, but they all suffer from the same problem. Additionally, the texture being drawn is entirely opaque (and green), the brightness of the target window is always 1, and all the primitives are front facing.

The following is my broken source code:

    /* Enable/set global things. */
    jgl.Viewport(
    (GLint) x, (GLint) y, (GLsizei) width, (GLsizei) height
    );
    fvals[0] = (jWindowsptr + jGetCurrentWindow())->brightness;
    jgl.Color4f(
    (GLfloat) fvals[0],
    (GLfloat) fvals[0],
    (GLfloat) fvals[0],
    1
    );
    jgl.Enable(GL_ALPHA_TEST);
    jgl.Enable(GL_CULL_FACE);
    jgl.CullFace(GL_BACK);
    jgl.Enable(GL_DEPTH_TEST);
    jgl.Enable(GL_POINT_SPRITE_ARB);
    jgl.EnableClientState(GL_VERTEX_ARRAY);
    const GLvoid *vaptrc = vaptr;
    jgl.VertexPointer(3, GL_FLOAT, 12, vaptrc);
    /* Color clearing is in here so I could see better while testing. */
    jgl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    /* Enable/set per texture unit things. */
    jgl.ActiveTexture(GL_TEXTURE0 + 1);
    jgl.TexEnvi(
    GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE
    );
    jgl.ClientActiveTexture(GL_TEXTURE0 + 1);
    jgl.EnableClientState(GL_TEXTURE_COORD_ARRAY);
    jgl.TexCoordPointer(3, GL_FLOAT, 12, (vaptrc + 3));
    jgl.Enable(GL_TEXTURE_3D);
    jgl.ActiveTexture(GL_TEXTURE0);
    jgl.TexEnvi(
    GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE
    );
    jgl.ClientActiveTexture(GL_TEXTURE0);
    jgl.EnableClientState(GL_TEXTURE_COORD_ARRAY);
    jgl.TexCoordPointer(3, GL_FLOAT, 12, (vaptrc + 3));
    jgl.Enable(GL_TEXTURE_3D);

    /* Pass #1. */
    jgl.MatrixMode(GL_TEXTURE);
    jgl.DepthFunc(GL_LESS);
    jgl.AlphaFunc(GL_EQUAL, 1);
    const GLfloat *tctm;
    while (msindex > 0) {
        msindex = msindex - 1;
        jgl.ActiveTexture(GL_TEXTURE0);
        jgl.BindTexture(
        GL_TEXTURE_3D, (miaptr + msindex)->textureids[0]
        );
        if ((miaptr + msindex)->textureids[0] != 0) {
            tctm
            = (miaptr
            + msindex)->transformationmatrices[0];
            jgl.LoadMatrixf(tctm);
        }
        jgl.ActiveTexture(GL_TEXTURE0 + 1);
        jgl.BindTexture(
        GL_TEXTURE_3D, (miaptr + msindex)->textureids[1]
        );
        if ((miaptr + msindex)->textureids[1] != 0) {
            tctm
            = (miaptr
            + msindex)->transformationmatrices[1];
            jgl.LoadMatrixf(tctm);
        }
        jgl.DrawArrays(
        (miaptr + msindex)->type,
        (GLint) (miaptr + msindex)->start,
        (GLsizei) (miaptr + msindex)->count
        );
    }

    /* WIP */

    /* Disable per texture unit things. */
    jgl.ActiveTexture(GL_TEXTURE0 + 1);
    jgl.ClientActiveTexture(GL_TEXTURE0 + 1);
    jgl.DisableClientState(GL_TEXTURE_COORD_ARRAY);
    jgl.Disable(GL_TEXTURE_3D);
    jgl.ActiveTexture(GL_TEXTURE0);
    jgl.ClientActiveTexture(GL_TEXTURE0);
    jgl.DisableClientState(GL_TEXTURE_COORD_ARRAY);
    jgl.Disable(GL_TEXTURE_3D);

    /* WIP */

    /* Disable global things. */
    jgl.DisableClientState(GL_VERTEX_ARRAY);
    jgl.Disable(GL_POINT_SPRITE_ARB);
    jgl.Disable(GL_DEPTH_TEST);
    jgl.Disable(GL_CULL_FACE);
    jgl.Disable(GL_ALPHA_TEST);

Upvotes: 0

Views: 99

Answers (1)

Reto Koradi
Reto Koradi

Reputation: 54592

Your description says that you have interleaved vertex attributes, with 3 floats for the position and 3 floats for the texture coordinates per vertex. This also matches the code you posted.

The values you pass as stride to the glVertexPointer() and glTexCoordPointer() does not match this, though. With 6 floats (3 for position + 3 for texture coordinate) per vertex, and a float being 4 bytes large, the stride should be 6 * 4 = 24. So all these calls need to use 24 for the stride:

jgl.VertexPointer(3, GL_FLOAT, 24, vaptrc);
jgl.TexCoordPointer(3, GL_FLOAT, 24, ...);

Upvotes: 1

Related Questions