header and file problem using OpenGL

Hi everyone. I have just learnt how to include headers and links to other class files and am trying to rewrite this program I am working on using OpenGL and GLUT but it doesn't seem to be working. Take a lot at the code:

in main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <stdlib.h>
#include <stdio.h>

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

using namespace std;

#include "drawBackground.h"

void compileScene() {
    DrawBackground drawBackgroundObject;
    drawBackgroundObject.backgroundLines();
}

int main (int argc, char **argv){
    glutInit(&argc, argv); // Initialize GLUT
    glutInitDisplayMode (GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)
    glutInitWindowSize (800, 800); // Set the width and height of the window
    glutInitWindowPosition (0, 0); // Set the position of the window
    glutCreateWindow ("Box Express"); // Set the title for the window
    
    glutDisplayFunc(compileScene); // Tell GLUT to use the method "display" for rendering
    
    glutMainLoop(); // Enter GLUT's main loop
}


in drawBackground.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "drawBackground.h"

void DrawBackground::backgroundLines() {
    float j = -1.0f;
    while (j < 1.0f) {
        glBegin(GL_LINES);
        glColor3f(0.9f, 0.9f, 0.9f);
        glVertex3f(1.0f, j, 0.0f);
        glVertex3f(-1.0f, j, 0.0f);
        j += 0.02;
        glEnd();
    }
    
    float i = -1.0f;
    while (i < 1.0f) {
        glBegin(GL_LINES);
        glColor3f(0.9f, 0.9f, 0.9f);
        glVertex3f(i, 1.0f, 0.0f);
        glVertex3f(i, -1.0f, 0.0f);
        i += 0.02;
        glEnd();
    }
    cout << "test one";
}


in drawBackground.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef __BoxExpress__drawBackground__
#define __BoxExpress__drawBackground__

#include <iostream>
#include <stdlib.h>
#include <stdio.h>

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

using namespace std;

class DrawBackground {
public:
    void backgroundLines();
};

#endif /* defined(__BoxExpress__drawBackground__) */ 


output:
test onetest one
(Also a blank window pops up without the lines being drawn)

I don't know what is going wrong. It worked fine when I made a simple program not using OpenGL but not it doesn't seem to be working. Can anyone help? Thanks!
If I'm corrent you need to put 'glClear(GL_COLOR_BUFFER_BIT);'
Got it working. Thought it was a problem with the headers but it is in fact with that function. This is the updated function that works

1
2
3
4
5
6
7
8
void compileScene() {
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    DrawBackground drawBackgroundObject;
    drawBackgroundObject.backgroundLines();
    glutSwapBuffers();
}
Topic archived. No new replies allowed.