17 #warning The version of GLEW you have installed is missing \
18 some OpenGL 2.0 entry points.
19 #warning Please upgrade to GLEW 1.3.2 or higher.
20 #warning Until then, shaders are disabled.
59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
73 int mode = wrap ? GL_REPEAT : GL_CLAMP;
74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mode);
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mode);
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, mode);
80 GLint x, GLint y, GLint width, GLint height)
82 const GLint x2 = x + width;
83 const GLint y2 = y + height;
84 const GLfloat tx2 = tx + twidth;
85 const GLfloat ty2 = ty + theight;
87 glEnable(GL_TEXTURE_2D);
89 glTexCoord2f(tx, ty ); glVertex2i(x , y );
90 glTexCoord2f(tx2, ty ); glVertex2i(x2, y );
91 glTexCoord2f(tx2, ty2); glVertex2i(x2, y2);
92 glTexCoord2f(tx, ty2); glVertex2i(x, y2);
94 glDisable(GL_TEXTURE_2D);
127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
128 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
142 GLint x, GLint y, GLsizei width, GLsizei height, GLbyte* data)
160 static GLuint currentId = 0;
161 static std::vector<GLuint> stack;
170 glGenFramebuffersEXT(1, &bufferId);
171 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, bufferId);
172 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
173 GL_COLOR_ATTACHMENT0_EXT,
175 bool success = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) ==
176 GL_FRAMEBUFFER_COMPLETE_EXT;
177 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, currentId);
180 "Your OpenGL implementation support for "
181 "framebuffer objects is too limited.");
189 if (!bufferId)
return;
191 if (currentId == bufferId) {
194 glDeleteFramebuffersEXT(1, &bufferId);
199 stack.push_back(currentId);
200 currentId = bufferId;
201 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, currentId);
206 assert(currentId == bufferId);
207 assert(!stack.empty());
208 currentId = stack.back();
210 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, currentId);
220 #ifdef GL_VERSION_2_0
221 static string readTextFile(
const string& filename)
225 const byte* data = file.mmap(size);
226 return string(reinterpret_cast<const char*>(data), size);
233 #ifdef GL_VERSION_2_0
236 init(type,
"", filename);
239 Shader::Shader(GLenum type,
const string& header,
const string& filename)
241 init(type, header, filename);
244 void Shader::init(GLenum type,
const string& header,
const string& filename)
247 if (!GLEW_VERSION_2_0) {
253 string source = header;
255 source += readTextFile(
"shaders/" + filename);
256 }
catch (FileException& e) {
257 std::cerr <<
"Cannot find shader: " << e.getMessage() << std::endl;
263 handle = glCreateShader(type);
265 std::cerr <<
"Failed to allocate shader" << std::endl;
270 const char* sourcePtr = source.c_str();
271 glShaderSource(handle, 1, &sourcePtr,
nullptr);
274 glCompileShader(handle);
275 const bool ok =
isOK();
276 GLint infoLogLength = 0;
277 glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength);
280 VLA(GLchar, infoLog, infoLogLength);
281 glGetShaderInfoLog(handle, infoLogLength,
nullptr, infoLog);
282 fprintf(stderr,
"%s(s) compiling shader \"%s\":\n%s",
283 ok ?
"Warning" :
"Error", filename.c_str(),
284 infoLogLength > 1 ? infoLog :
"(no details available)\n");
296 #ifdef GL_VERSION_2_0
298 glDeleteShader(handle);
305 #ifdef GL_VERSION_2_0
309 GLint compileStatus = GL_FALSE;
310 glGetShaderiv(handle, GL_COMPILE_STATUS, &compileStatus);
311 return compileStatus == GL_TRUE;
320 #ifndef GL_VERSION_2_0
321 #ifndef GL_VERTEX_SHADER
322 #define GL_VERTEX_SHADER 0
338 #ifndef GL_VERSION_2_0
339 #ifndef GL_FRAGMENT_SHADER
340 #define GL_FRAGMENT_SHADER 0
358 #ifdef GL_VERSION_2_0
360 if (!GLEW_VERSION_2_0) {
367 handle = glCreateProgram();
369 std::cerr <<
"Failed to allocate program" << std::endl;
379 #ifdef GL_VERSION_2_0
381 glDeleteProgram(handle);
388 #ifdef GL_VERSION_2_0
392 GLint linkStatus = GL_FALSE;
393 glGetProgramiv(handle, GL_LINK_STATUS, &linkStatus);
394 return linkStatus == GL_TRUE;
400 #ifdef GL_VERSION_2_0
408 if (!shader.
isOK()) {
412 glAttachShader(handle, shader.handle);
422 #ifdef GL_VERSION_2_0
428 glLinkProgram(handle);
429 const bool ok =
isOK();
430 GLint infoLogLength = 0;
431 glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength);
434 VLA(GLchar, infoLog, infoLogLength);
435 glGetProgramInfoLog(handle, infoLogLength,
nullptr, infoLog);
436 fprintf(stderr,
"%s(s) linking shader program:\n%s\n",
437 ok ?
"Warning" :
"Error",
438 infoLogLength > 1 ? infoLog :
"(no details available)\n");
443 #ifdef GL_VERSION_2_0
451 GLint location = glGetUniformLocation(handle, name);
452 if (location == -1) {
453 fprintf(stderr,
"%s: \"%s\"\n",
454 strncmp(name,
"gl_", 3) == 0
455 ?
"Accessing built-in shader variables is not possible"
456 :
"Could not find shader variable",
470 #ifdef GL_VERSION_2_0
471 if (GLEW_VERSION_2_0) {
472 glUseProgram(handle);
479 #ifdef GL_VERSION_2_0
480 if (GLEW_VERSION_2_0) {
489 glValidateProgram(handle);
490 GLint validateStatus = GL_FALSE;
491 glGetProgramiv(handle, GL_VALIDATE_STATUS, &validateStatus);
492 GLint infoLogLength = 0;
493 glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &infoLogLength);
495 VLA(GLchar, infoLog, infoLogLength);
496 glGetProgramInfoLog(handle, infoLogLength,
nullptr, infoLog);
497 std::cout <<
"Validate "
498 << ((validateStatus == GL_TRUE) ?
string(
"OK") : string(
"FAIL"))
499 <<
": " << infoLog << std::endl;