OpenGL Classes

Hi everyone. So I am starting up OpenGL and I am struggling to get some classes working. Have a look:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cmath>
#include <time.h>

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

using namespace std;

float x = -0.9f;
float y = -0.9f;

class UserInput{
public:
    bool* keyStates = new bool[256];
    void clearKeyStates(){
        for (char i=0; i<256; i++)
        {
            keyStates[i] = false;
        }
    }
    void quitProgram(void){
        if (keyStates[27])
        {
            exit(0);
        }
        glutPostRedisplay();
    }
    void keyPressed (unsigned char key, int x, int y){
        keyStates[key] = true; // Set the state of the current key to pressed
        quitProgram();
    }
    
    void keyUp (unsigned char key, int x, int y){
        keyStates[key] = false; // Set the state of the current key to not pressed
    }
};

class Shapes{
public:
    
    void background(){
        glBegin(GL_LINE_LOOP);
        glColor3f(0.22f, 0.22f, 0.22f);
        
        glVertex3f(-0.9f, -0.9f, 0.0f);
        glVertex3f(0.9f, -0.9f, 0.0f);
        glVertex3f(0.9f, 0.9f, 0.0f);
        glVertex3f(-0.9f, 0.9f, 0.0f);
        
        glEnd();
        
        float i = -0.9f;
        while (i < 0.9f) {
            glBegin(GL_LINES);
            glColor3f(0.22f, 0.22f, 0.22f);
            glVertex3f(0.9f, i, 0.0f);
            glVertex3f(-0.9f, i, 0.0f);
            i += 0.02;
            glEnd();
        }
        
        float j = -0.9f;
        while (j < 0.9f) {
            glBegin(GL_LINES);
            glColor3f(0.22f, 0.22f, 0.22f);
            glVertex3f(j, 0.9f, 0.0f);
            glVertex3f(j, -0.9f, 0.0f);
            j += 0.02;
            glEnd();
        }
    }
    void randomlyPlaceSquares(){
        Shapes shapesKey;
        
        int c = time(NULL);
        while (x < 0.88f && y < 0.88f) {
            //if random number % 2 = 0
            srand(c);
            int r = rand() % 5;
            if (r == 0)
            {
                shapesKey.greySquare();
            }

            //else
            x += 0.02;
            if (x > 0.88) {
                x = -0.9f;
                y += 0.02f;
            }
            c *= c + 1;
        }
    }
private:
    void greySquare(){
        glBegin(GL_POLYGON);
        glColor3f(0.22f, 0.22f, 0.22f);
        //Draw the square with respect to x and y
        glVertex3f(x, y, 0.0f);
        glVertex3f(x, y + 0.02f, 0.0f);
        glVertex3f(x + 0.02f, y + 0.02f, 0.0f);
        glVertex3f(x + 0.02f, y, 0.0f);
        glEnd();
    }

};

void initRendering(){
    //Makes 3D drawing work when something is in front of something else
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);

}

void drawScene(){
    //Clear information from last draw
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
    glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
    glLoadIdentity(); //Reset the drawing perspective
    
    Shapes shapesKey;
    shapesKey.background();
    
    x = -0.9f;
    y = -0.9f;
    shapesKey.randomlyPlaceSquares();
    
    glutSwapBuffers(); //Send the 3D scene to the screen
}

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 ("Randomly Generated Map"); // Set the title for the window

    glutDisplayFunc(drawScene); // Tell GLUT to use the method "display" for rendering
    
    UserInput userInputKey;
    glutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key presses
    glutKeyboardUpFunc(keyUp); // Tell GLUT to use the method "keyUp" for key up events

    glutMainLoop(); // Enter GLUT's main loop
}


The problem is here:

1
2
3
    UserInput userInputKey;
    glutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key presses
    glutKeyboardUpFunc(keyUp); // Tell GLUT to use the method "keyUp" for key up events 


I have tried doing this but it does not work because it is not referring to the proper class. I have also tried doing glutKeyboardFunc(userInputKey.keyPressed) but that also does not work. Could someone tell me how to get the classes working? Thanks!
1
2
3
4
5
6
7
class foo{
public:
   int a_method( int, int );
};

//equivalent
int a_method( foo *this, int, int );
Your method does not respect the callback prototype.


bool* keyStates = new bool[256]; ¿why are you allocating dynamically?
Sorry, I'm not quite sure what you mean by that! I haven't done much work with classes and I should have learnt them properly before moving onto OpenGL
how do you use opengl on a mac?
Xcode. It comes with mac
are there any tutorials online? Ive tried a million times but never got it to work, this is my first time using 3d programming
http://www.videotutorialsrock.com

I've just started too and have already produced some cool programs. That tutorial above is really easy to follow however it doesnt show you how to get opengl and glut on xcode. For that just search on youtube installing opengl and glut into xcode. It is really quick and easy and you dont have to download anything.

Mikeecb
I dont need to download it? It looks like it only needs to be set up. God this is hard. I wish there was one video that explained everything
Last edited on
Somebody, please help, spend just 5 minutes of your time explaining it and I will be quiet, I have been trying for 1 week!
when you are in Xcode, create a new project and then click file, add files to ..., then go into system, library, frameworks, then add opengl and glut framework.
I found opengl and glut framework but when I click to add it it opens the folders.
Last edited on
click on the folder and click add at the bottom. I cant do much more now. make a new post. Sorry
I think I added it. Does anybody have some sample code I can run?!
Topic archived. No new replies allowed.