Advertisement Jump to content
Sign in to follow this  
sadrius

OpenGL Weird Vertex Array problem

This topic is 4916 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm an opengl newbie, so bear with me. Ok, so I have this function that renders text using textured quads. It works fine in immediate mode, but when I use vertex arrays, it causes really awful looking artifacts. The artifacts don't appear when I'm rendering just text. However, when I add a cube, the artifacts appear immediately. Like I said before, if I render the text using immediate mode, everything looks normal. Here is what the screen should look like (immediate mode): Here is what the screen looks like using vertex arrays: Now here's the relevant code:
//... 
   glOrtho(0.0f, 400, 300, 0.0, 100.0, -100.0);
//...

void Display::update()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();

    glTranslatef(200.0f, 150.0f, 0.0f);
    glRotatef(Logic::xRotate, 1.0f, 0.0f, 0.0f);
    glRotatef(Logic::yRotate, 0.0f, 1.0f, 0.0f);
    glRotatef(Logic::zRotate, 0.0f, 0.0f, 1.0f);
    glScalef(16.0f, 16.0f, 16.0f);

    glDisable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);
        glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, 1.0f, -1.0f);
        glVertex3f(1.0f, 1.0f, -1.0f);

        glColor4f(0.0f, 1.0f, 1.0f, 1.0f);
        glVertex3f(-1.0f, -1.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
        glVertex3f(1.0f, 1.0f, 1.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);

        glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
        glVertex3f(-1.0f, 1.0f, -1.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, -1.0f, 1.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);

        glColor4f(1.0f, 0.0f, 1.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(1.0f, 1.0f, -1.0f);
        glVertex3f(1.0f, 1.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);

        glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
        glVertex3f(-1.0f, -1.0f, 1.0f);

        glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
        glVertex3f(1.0f, 1.0f, -1.0f);
        glVertex3f(-1.0f, 1.0f, -1.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);
        glVertex3f(1.0f, 1.0f, 1.0f);
    glEnd();

    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glEnable(GL_TEXTURE_2D);
    glColor3f(1.0f, 1.0f, 1.0f);
    std::stringstream message;
    message << "X: " << Logic::xRotate;
    Text::write(font, 0, 0, message.str());
    message.str("");
    message << "Y: " << Logic::yRotate;
    Text::write(font, 0, 10, message.str());
    message.str("");
    message << "Z: " << Logic::zRotate;
    Text::write(font, 0, 20, message.str());
    message.str("");
    std::cerr << "\n";
    glFlush();
    SDL_GL_SwapBuffers();
}

and
void Text::write(boost::shared_ptr<Font> font, GLfloat x, GLfloat y, std::string
    text)
{
    glPushMatrix();

    Uint8 character;
    Uint32 length = text.length() * 8;

/* Vertex Array Version
    GLfloat vertices[length];
    GLfloat textureCoordinates[length];

    for (Uint32 i = 0, c = 0; i < length; i += 8, ++c) {
        character = text[c];

        vertices = x + font->characterWidth[character];
        vertices[i + 1] = y;
        vertices[i + 2] = x;
        vertices[i + 3] = y;
        vertices[i + 4] = x;
        vertices[i + 5] = y + font->lineHeight;
        vertices[i + 6] = x + font->characterWidth[character];
        vertices[i + 7] = y + font->lineHeight;

        textureCoordinates = font->xCoordinates[character].second;
        textureCoordinates[i + 1] = font->yCoordinates.first;
        textureCoordinates[i + 2] = font->xCoordinates[character].first;
        textureCoordinates[i + 3] = font->yCoordinates.first;
        textureCoordinates[i + 4] = font->xCoordinates[character].first;
        textureCoordinates[i + 5] = font->yCoordinates.second;
        textureCoordinates[i + 6] = font->xCoordinates[character].second;
        textureCoordinates[i + 7] = font->yCoordinates.second;

        x += font->characterWidth[character] + font->tracking;
    }

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glBindTexture(GL_TEXTURE_2D, font->texture->name);
    glVertexPointer(2, GL_FLOAT, 0, &vertices[0]);
    glTexCoordPointer(2, GL_FLOAT, 0, &textureCoordinates[0]);
    glDrawArrays(GL_QUADS, 0, length);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
*/

/*  Immediate Mode Version
    glBegin(GL_QUADS);
    for (Uint32 i = 0; i < text.length(); ++i) {
        character = text;
        glTexCoord2f(font->xCoordinates[character].second, font->yCoordinates.first);
        glVertex2f(x + font->characterWidth[character], y);
        glTexCoord2f(font->xCoordinates[character].first, font->yCoordinates.first);
        glVertex2f(x, y);
        glTexCoord2f(font->xCoordinates[character].first, font->yCoordinates.second);
        glVertex2f(x, y + font->lineHeight);
        glTexCoord2f(font->xCoordinates[character].second, font->yCoordinates.second);
        glVertex2f(x + font->characterWidth[character], y + font->lineHeight);
        x += font->characterWidth[character] + font->tracking;
    }
    glEnd();
*/

    glPopMatrix();
}

I have tested to make sure all texture coordinates and whatnot are correct... they should be, as the immediate mode works perfectly. I have no idea what the problem is... please help!

Share this post


Link to post
Share on other sites
Advertisement
glDrawArrays(GL_QUADS, 0, length);

You tell OpenGL to draw "length" vertices, but you actually only have length/2.. (Half of them are x, half of them are y). ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by TomasH
glDrawArrays(GL_QUADS, 0, length);

You tell OpenGL to draw "length" vertices, but you actually only have length/2.. (Half of them are x, half of them are y). ;)


Thank you so much! I've been trying to figure this out for days, and completely overlooked that. I appreciate it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!