OpenGL SuperBible 5E, Triangle program background "scattered"

Sorry to have to dig up such an old book, and I hope this can be considered a beginner question. But I’m using the OpenGL SB 5E because my mac only supports OpenGL 3.3.

I’m currently working on the first sample program, Triangle.cpp. I have included all the necessary files, set up the correct path, and built and ran the program.

I am having an issue with the display of the program. It is supposed to display a red triangle on a blue background. I am able to see the red triangle, but the blue background displays other scattered images of my screen.

screenshot: imgur.com / mivJWpS

In all honesty I have no idea what could be causing this issue. I do however have 4 warnings that appear from the GLTools.h:

  • Lexical or Preprocessor Issue: ‘glGenVertexArrays’ macro redefined
  • Lexical or Preprocessor Issue: ‘glDeleteVertexArrays’ macro redefined
  • Lexical or Preprocessor Issue: ‘glBindVertexArray’ macro redefined
  • Lexical or Preprocessor Issue: ‘glGenerateMipmap’ macro redefined

Could these be a possible cause to the issue?

Macbook Pro 3,1 - 2.2GHz, 4GB Ram

Warnings like this probably aren’t causing you problem but you should resolve why you are getting them. How is the blue background being set?

The blue background is being drawn in SetupRC with ‘glClearColor’.

I’ve included the code so you know exactly what is going on.



#include <GLTools.h>
#include <GLShaderManager.h>

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#DEFINE FREEGLUT_STATIC
#include <GL/glut.h>
#endif


GLBatch triangleBatch;
GLShaderManager shaderManager;

void ChangeSize(int w, int h)
{
    glViewport(0, 0, w, h);
}

void SetupRC() {
    // blue background
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
    
    shaderManager.InitializeStockShaders();
    
    // load up triangle
    GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f,
                0.5f, 0.0f, 0.0f,
                0.0f, 0.5f, 0.0f };
    
    triangleBatch.Begin(GL_TRIANGLES, 3);
    triangleBatch.CopyVertexData3f(vVerts);
    triangleBatch.End();
}

void RenderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER | GL_STENCIL_BUFFER_BIT);
    
    GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
    triangleBatch.Draw();
    
    glutSwapBuffers();
}

int main(int argc, char* argv[])
{
    gltSetWorkingDirectory(argv[0]);
    
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Triangle");
    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);
    
    GLenum err = glewInit();
    if(GLEW_OK != err)
    {
        fprintf(stderr, "GLEW Error: %s
", glewGetErrorString(err));
        return 1;
    }
    
    SetupRC();
    
    glutMainLoop();
    return 0;
    
    
}


I solved the problem after reading the code about 10 times through.

The original RenderScene function, note ‘glClear’


void RenderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER | GL_STENCIL_BUFFER_BIT);
 
    GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
    triangleBatch.Draw();
 
    glutSwapBuffers();
}

The fixed and working just fine RenderScene function


void RenderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
                //                            ^ That's the error!
 
    GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
    triangleBatch.Draw();
 
    glutSwapBuffers();
}

Solved by simply adding _BIT to GL_DEPTH_BUFFER