summaryrefslogtreecommitdiff
path: root/lib/glbase.cc
blob: 67b1b9cff6a8e10457563935d926a3eaca83da27 (plain)
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
#include <stdio.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include "glbase.h"
#include "engine.h"
#include "generic.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

int mogltk::glbase::width, mogltk::glbase::height, mogltk::glbase::inited = 0, mogltk::glbase::twoD = 0;
SDL_Surface * mogltk::glbase::surface = 0;

int mogltk::glbase::setup(int w, int h, int flags) throw(GeneralException) {
    if (inited) {
        printm(M_WARNING, "mogltk::glbase::setup called twice, ignoring second call...\n");
        return -1;
    }

    width = w;
    height = h;
    
    mogltk::engine::setup();
    if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
        throw GeneralException(String("Couldn't initialise Video SubSystem: ") + SDL_GetError());
    }

    if (!(surface = SDL_SetVideoMode(width, height, 0, flags | SDL_OPENGL))) {
        throw GeneralException(String("Couldn't set GL mode: ") + SDL_GetError());
    }

    float ratio = surface->w;
    ratio /= surface->h;

    printm(M_INFO, "Video resolution: %dx%dx%d (ratio = %3.2f)\n", surface->w, surface->h, surface->format->BitsPerPixel, ratio);
    printm(M_INFO, "\n");
    printm(M_INFO, "OpenGL infos\n");
    printm(M_INFO, "------------\n");
    printm(M_INFO, String("Vendor    : ") + (char *) glGetString(GL_VENDOR) + "\n");
    printm(M_INFO, String("Renderer  : ") + (char *) glGetString(GL_RENDERER) + "\n");
    printm(M_INFO, String("Version   : ") + (char *) glGetString(GL_VERSION) + "\n");
    printm(M_INFO, String("Extensions: ") + (char *) glGetString(GL_EXTENSIONS) + "\n");

    inited = 1;
    
    glViewport(0, 0, surface->w, surface->h);

    glCullFace(GL_BACK);
    glFrontFace(GL_CCW);
    glEnable(GL_CULL_FACE);

    glClearColor(0, 0, 0, 0);
    glShadeModel(GL_SMOOTH);
    
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, ratio, 1.0, 1024.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    SDL_ShowCursor(0);
    Flip();
    
    return 0;
}

int mogltk::glbase::GetWidth(void) {
    return width;
}

int mogltk::glbase::GetHeight(void) {
    return height;
}

int mogltk::glbase::GetInited(void) {
    return inited;
}

void mogltk::glbase::Enter2DMode(void) {
    if (twoD)
	return;
    
    glPushAttrib(GL_ENABLE_BIT);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0.0, width, height, 0.0, 0.0, 1.0);
    
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    
    twoD = 1;
}

void mogltk::glbase::Leave2DMode(void) {
    if (!twoD)
	return;
    
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    
    glPopAttrib();
    
    twoD = 0;
}

void mogltk::glbase::Flip() {
    SDL_GL_SwapBuffers();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    mogltk::engine::pollevents();
}

bool mogltk::glbase::is2D() {
    return twoD;
}

void mogltk::glbase::glVertex(GLshort x, GLshort y, GLshort z, GLshort w) {
#ifdef DEBUG
    printm(M_INFO, "Calling glVertex(%i, %i, %i, %i) (shorts)\n", x, y, z, w);
#endif
    glVertex4i(x, y, z, w);
}

void mogltk::glbase::glVertex(GLint x, GLint y, GLint z, GLint w) {
#ifdef DEBUG
    printm(M_INFO, "Calling glVertex(%i, %i, %i, %i) (ints)\n", x, y, z, w);
#endif
    glVertex4i(x, y, z, w);
}

void mogltk::glbase::glVertex(GLfloat x, GLfloat y, GLfloat z, GLfloat w) {
#ifdef DEBUG
    printm(M_INFO, "Calling glVertex(%f, %f, %f, %f) (floats)\n", x, y, z, w);
#endif
    glVertex4f(x, y, z, w);
}

void mogltk::glbase::glVertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w) {
#ifdef DEBUG
    printm(M_INFO, "Calling glVertex(%f, %f, %f, %f) (doubles)\n", x, y, z, w);
#endif
    glVertex4d(x, y, z, w);
}