Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: df2af759d652af54aa2ca7c3c7ef682a61e96d87 (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
/*
   render/state.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include <string>
#include <sstream>

#include "SDL/SDL.h"

#include "render/state.h"
#include "render/gl.h"
#include "render/render.h"

namespace render
{

int	State::render_width = 0;
int 	State::render_height = 0;
float	State::render_aspect = 0;
bool	State::render_has_generate_mipmaps = false;
bool	State::render_has_vbo = false;
GLuint	State::render_vbo = 0;

void State::init(int width, int height)
{
	resize(width, height);

	render_has_generate_mipmaps = false;

	std::string version(gl::version());
	for (size_t i = 0; i < version.size(); i++) {
		if (version[i] == '.')
			version[i] = ' ';
	}

	std::stringstream versionstream(version);
	int major, minor;
	if (versionstream >> major >> minor) {

		if (major > 1) {
			render_has_generate_mipmaps = true;
		} else if (major == 1) {
			if (minor > 3)
				render_has_generate_mipmaps = true;
		}

	} else {
		con_warn << "Could not determine OpenGL version!" << std::endl;
	}
	
	con_print << "  Hardware generate mipmaps ";
	if (render_has_generate_mipmaps)
		con_print << "enabled" << std::endl;
	else
		con_print << "disabled" << std::endl;
	
	// initialize gl functions
	render_has_vbo = true;
	gl::genbuffers = (gl::genbuffers_func) SDL_GL_GetProcAddress("glGenBuffers");
	if (!gl::genbuffers) {
		con_debug << "  glGenBuffers not available" << std::endl;
		render_has_vbo = false;
	}
	
	gl::deletebuffers = (gl::deletebuffers_func) SDL_GL_GetProcAddress("glDeleteBuffers");
	if (!gl::deletebuffers) {
		con_debug << "  glDeleteBuffers not available" << std::endl;
		render_has_vbo = false;
	}

	gl::bindbuffer = (gl::bindbuffer_func) SDL_GL_GetProcAddress("glBindBuffer");
	if (!gl::bindbuffer) {
		con_debug << "  glBindBuffer not available" << std::endl;
		render_has_vbo = false;
	}
	
	gl::bufferdata = (gl::bufferdata_func) SDL_GL_GetProcAddress("glBufferData");
	if (!gl::bufferdata) {
		con_debug << "  glBufferData not available" << std::endl;
		render_has_vbo = false;
	}

	con_print << "  Vertex bufer objects ";
	if (render_has_vbo)
		con_print << "enabled" << std::endl;
	else
		con_print << "disabled" << std::endl;

	// Generate VBO
	if (render_has_vbo)
		gl::genbuffers(1, &render_vbo);
}

void State::shutdown()
{
	// Delete VBO
	if (render_has_vbo)
		gl::deletebuffers(1, &render_vbo);
}

void State::resize(int width, int height)
{
	render_width = width;
	render_height = height;

	render_aspect = (float) width / (float) height;

	clear();
}

void State::clear()
{
	// set viewport
	gl::viewport(0, 0, render_width, render_height);

	// set clear color
	gl::clearcolor(0.0f, 0.0f, 0.0f, 1.0f);

	// load identity matrices
	gl::matrixmode(GL_PROJECTION);
	gl::loadidentity();

	gl::matrixmode(GL_MODELVIEW);
	gl::loadidentity();

	// shading model: Gouraud (smooth, the default)
	gl::shademodel(GL_SMOOTH);
	//gl::shademodel(GL_FLAT);

	// color tracking
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

	// material settings
	GLfloat specular_reflectance[] = { 0.2f, 0.2f, 0.2f, 1.0f };
	glMaterialfv(GL_FRONT, GL_SPECULAR, specular_reflectance);
	glMateriali(GL_FRONT, GL_SHININESS, 128); // shininess 1-128

	// alpha blending function
	gl::blendfunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	gl::disable(GL_LIGHTING);
	gl::disable(GL_COLOR_MATERIAL);

	gl::cullface(GL_BACK);
	gl::frontface(GL_CCW);
	gl::disable(GL_CULL_FACE);
	gl::disable(GL_DEPTH_TEST);
	gl::disable(GL_BLEND);

	gl::disable(GL_TEXTURE_2D);
}

} // namespace render