Loading .obj file in OpenGL

Ok so I have made a basic parser and loader for a 3d .obj file made in Blender. I know that the code isn't great, I made this pretty quickly and it certainly could be more efficient. However my problem is that OpenGL lights it as if it were all one polygon, and it makes it look artificial and it is hard to make out details. How could I make it so OpenGL recognises the vertices as multiple polygons so it can properly light them? Thanks.

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
//main.cpp

#include <GL/freeglut.h>
#include <GL/gl.h>
#include <stdio.h>

#include "reader.h"

Reader obj;

float angle;

void init(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize(800, 600);
    glutInitWindowPosition(150, 150);
    glutCreateWindow("Generic RPG 3D");

    AllocConsole();
    freopen( "CON", "wt", stdout );
    freopen( "CON", "wt", stderr );

    obj.load("cube.obj");

    glEnable (GL_DEPTH_TEST);
    glEnable (GL_LIGHTING);
    glEnable (GL_LIGHT0);
}

void display(void)
{
    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glTranslatef(0.0f, -2.75f, -5.0f);
    obj.draw("cube.obj");

    glutSwapBuffers();
    angle += .1f;
}

void reshape( int width, int height )
{
    glViewport(0, 0, (GLsizei)width, (GLsizei)height );
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char **argv)
{

    init(argc, argv);

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(display);
    glutMainLoop();

    return 0;
}


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
156
157
158
159
160
161
162
163
164
165
166
167
168
//reader.cpp

#include <GL/freeglut.h>
#include <fstream>

#include "reader.h"

using namespace std;

Reader::Reader()
{
    for(int i = 0; i < 256; i++ ) {
        vertex[i].x = 0;
        vertex[i].y = 0;
        vertex[i].z = 0;
    }
}

Reader::~Reader()
{

}

void Reader::load(char* filename)
{
    ifstream file;
    file.open(filename);

    string str;

    while(!file.eof()) //while we are still in the file
    {
        getline(file,str); //move one line down
        if(str[0] == 'v') break; //if we have a vertex line, stop
    }

    int v = 0;

    while(str[0] == 'v') {

        int i = 0;

        while(true)
        {
            while(str[i] == ' ' )
            {
                i++; //move a space over
            }
            i++;
            i++;
            int j = i, k = i;
            while(str[i] != ' ') {  
                i++;
                k = i;
            }
            vertex[v].x = atof(str.substr(j, k-j).c_str());

            //moving on to the y coord

            while(str[i] == ' ' ) {
                i++;
            }

            int q = i, w = i;
            while(str[i] != ' ' ) {
                i++;
                w = i;
            }
            vertex[v].y = atof(str.substr(q, w-q).c_str());

            while(str[i] == ' ' ) {
                i++;
            }

            int a = i, s = i;
            while(str[i] != ' ' ) {
                i++;
                s = i;
            }
            vertex[v].z = atof(str.substr(a, s-a).c_str());

            break;
        }
        v++; 
        getline(file, str);  
    }
}

void Reader::draw(char *filename)
{
    ifstream file;
    file.open(filename);

    string str;

    while(true)
    {
        getline(file, str);
        if(str[0] == 'f' ) {
            break;
        }
    }

    int i = 0;

    while(str[0] == 'f')
    {

        while(str[i] == 'f') i++;
        while(str[i] == ' ') i++;
        int j = i, k = i;

        while(str[i] != ' ') {  
            i++;
            k = i;
        }

        int one = atof(str.substr(j, k - j).c_str());

        i +=1;

        j = i;
        k = i;

        while(str[i] != ' ') { 
            i++;
            k = i;
        }

        int two = atof(str.substr(j, k - j).c_str());

        i+=1;

        j = i;
        k = i;

        while(str[i] != ' ') {  
            i++;
            k = i;
        }

        int three = atof(str.substr(j, k - j).c_str());

        i+=1;

        j = i;
        k = i;

        while(str[i] != ' ') {  
            i++;
            k = i;
        }

        int four = atof(str.substr(j, k - j).c_str());

        glBegin(GL_POLYGON);
        glVertex3d(vertex[one - 1].x, vertex[one - 1].y, vertex[one - 1].z);
        glVertex3d(vertex[two - 1].x, vertex[two - 1].y, vertex[two - 1].z);
        glVertex3d(vertex[three - 1].x, vertex[three - 1].y, vertex[three - 1].z);
        glVertex3d(vertex[four - 1].x, vertex[four - 1].y, vertex[four - 1].z);
        glEnd();

        getline(file, str);
        i = 0;
    }

}


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
//reader.h

#ifndef READER_H
#define READER_H

#define MAX_VERT 100000

struct Vertex {

    float x;
    float y;
    float z;

};


class Reader {
    private:
        Vertex vertex[256];

    public:
        Reader();
        ~Reader();

        void load(char* filename);
        void draw(char* filename);


};

#endif

Here is the .obj file that I am using. Sorry about not commenting the code above, it was before but I had to take the comments out in order for it all to fit.


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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//cube.obj

# Blender v2.64 (sub 0) OBJ File: ''
# www.blender.org
mtllib cube.mtl
o Grid_Grid.001
v 0.518272 1.131488 1.935897
v 0.384532 1.087422 1.877534
v 0.194592 1.131488 1.935897
v 0.055283 1.173685 2.018032
v -0.129088 1.131488 1.935897
v -0.290928 1.131488 1.935897
v -0.419401 1.162998 2.006013
v -0.614609 1.131488 1.935897
v -0.776449 1.131488 1.935897
v -0.938289 1.131488 1.935897
v 0.518272 1.131488 1.774056
v 0.356432 1.131488 1.774056
v 0.194592 1.131488 1.774056
v 0.032752 1.131488 1.774056
v -0.133140 1.187131 1.864440
v -0.283516 1.097859 1.722068
v -0.448529 1.120777 1.758496
v -0.614609 1.131488 1.774056
v -0.666732 1.323140 1.818890
v -0.938289 1.131488 1.774056
v 0.514737 1.219958 1.757466
v 0.341049 1.157256 1.646901
v 0.103853 1.259472 1.776878
v 0.032752 1.131488 1.612216
v -0.129088 1.131488 1.612216
v -0.290928 1.131488 1.612216
v -0.452769 1.131488 1.612216
v -0.614609 1.131488 1.612216
v -0.727726 1.183092 1.605145
v -0.906684 1.096586 1.570953
v 0.499078 1.181330 1.523074
v 0.333517 1.194935 1.543723
v 0.118743 1.190631 1.508461
v 0.032752 1.131488 1.450376
v -0.218255 1.271678 1.365329
v -0.395605 1.329857 1.326677
v -0.452769 1.131488 1.450376
v -0.615633 1.249461 1.546403
v -0.776449 1.131488 1.450376
v -0.970506 1.162958 1.485606
v 0.508798 1.195456 1.389886
v 0.356432 1.131488 1.288536
v 0.247507 1.102528 1.268469
v 0.032752 1.131488 1.288536
v -0.018010 1.286906 1.119142
v -0.442831 1.411379 1.114659
v -0.486016 1.159259 1.341606
v -0.614609 1.131488 1.288536
v -0.691108 1.214893 1.270527
v -0.938289 1.131488 1.288536
v 0.518272 1.131488 1.126696
v 0.343100 1.171472 1.186112
v 0.169748 1.177407 1.189869
v 0.032752 1.131488 1.126696
v -0.159585 1.154632 1.171387
v -0.313079 1.177570 1.209258
v -0.444593 1.193086 1.192916
v -0.531590 1.204827 1.102899
v -0.714528 1.170166 1.223892
v -0.909191 1.187848 1.143041
v 0.518272 1.131488 0.964856
v 0.356432 1.131488 0.964856
v 0.212689 1.112853 0.943475
v -0.002549 1.196509 1.082023
v -0.129088 1.131488 0.964856
v -0.170148 1.195811 0.896109
v -0.420159 1.077837 0.892947
v -0.614609 1.131488 0.964856
v -0.776449 1.131488 0.964856
v -0.938289 1.131488 0.964856
v 0.493473 1.178760 0.868463
v 0.356432 1.131488 0.803016
v 0.206293 1.161256 0.815311
v 0.032752 1.131488 0.803016
v -0.129088 1.131488 0.803016
v -0.290928 1.131488 0.803016
v -0.391409 1.198935 0.796091
v -0.614609 1.131488 0.803016
v -0.773828 1.202375 0.857486
v -1.008615 1.228812 0.852519
v 0.518272 1.131488 0.641176
v 0.356432 1.131488 0.641176
v 0.308955 1.244107 0.617727
v 0.032752 1.131488 0.641176
v -0.134324 1.238977 0.704124
v -0.290928 1.131488 0.641176
v -0.462566 1.216063 0.719063
v -0.614609 1.131488 0.641176
v -0.735736 1.090856 0.633541
v -0.938289 1.131488 0.641176
v 0.518272 1.131488 0.479336
v 0.356432 1.131488 0.479336
v 0.194592 1.131488 0.479336
v 0.032752 1.131488 0.479336
v -0.129088 1.131488 0.479336
v -0.290928 1.131488 0.479336
v -0.452769 1.131488 0.479336
v -0.614609 1.131488 0.479336
v -0.776449 1.131488 0.479336
v -0.992984 1.239501 0.561084
usemtl 
s off
f 19 20 10 9
f 14 15 5 4
f 15 16 6 5
f 16 17 7 6
f 2 1 11 12
f 17 18 8 7
f 12 13 3 2
f 18 19 9 8
f 13 14 4 3
f 25 26 16 15
f 26 27 17 16
f 21 22 12 11
f 27 28 18 17
f 22 23 13 12
f 28 29 19 18
f 23 24 14 13
f 29 30 20 19
f 24 25 15 14
f 37 38 28 27
f 32 33 23 22
f 38 39 29 28
f 33 34 24 23
f 39 40 30 29
f 34 35 25 24
f 35 36 26 25
f 36 37 27 26
f 31 32 22 21
f 44 45 35 34
f 45 46 36 35
f 46 47 37 36
f 41 42 32 31
f 47 48 38 37
f 42 43 33 32
f 48 49 39 38
f 43 44 34 33
f 49 50 40 39
f 56 57 47 46
f 51 52 42 41
f 57 58 48 47
f 52 53 43 42
f 58 59 49 48
f 53 54 44 43
f 59 60 50 49
f 54 55 45 44
f 55 56 46 45
f 63 64 54 53
f 69 70 60 59
f 64 65 55 54
f 65 66 56 55
f 66 67 57 56
f 61 62 52 51
f 67 68 58 57
f 62 63 53 52
f 68 69 59 58
f 75 76 66 65
f 76 77 67 66
f 71 72 62 61
f 77 78 68 67
f 72 73 63 62
f 78 79 69 68
f 73 74 64 63
f 79 80 70 69
f 74 75 65 64
f 82 83 73 72
f 88 89 79 78
f 83 84 74 73
f 89 90 80 79
f 84 85 75 74
f 85 86 76 75
f 86 87 77 76
f 81 82 72 71
f 87 88 78 77
f 94 95 85 84
f 95 96 86 85
f 96 97 87 86
f 91 92 82 81
f 97 98 88 87
f 92 93 83 82
f 98 99 89 88
f 93 94 84 83
f 99 100 90 89
Topic archived. No new replies allowed.