annotate mde/gl/texture.d @ 55:f3d8c0441408

Implemented gl.texture (without testing) & fixed log options adjusted previously. Implemented gl.texture module to load textures from file (untested). Fixed log level/option setting in Init.
author Diggory Hardy <diggory.hardy@gmail.com>
date Tue, 10 Jun 2008 17:35:13 +0100
parents
children 960206198cbd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
55
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
1 /* LICENSE BLOCK
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
2 Part of mde: a Modular D game-oriented Engine
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
3 Copyright © 2007-2008 Diggory Hardy
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
4
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify it under the terms
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
6 of the GNU General Public License as published by the Free Software Foundation, either
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
7 version 2 of the License, or (at your option) any later version.
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
8
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
11 See the GNU General Public License for more details.
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
12
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
13 You should have received a copy of the GNU General Public License
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
15
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
16 /** Simple module for loading images.
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
17 */
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
18 module mde.resource.image;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
19
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
20 import mde.resource.exception;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
21
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
22 import tango.std.stringz;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
23
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
24 import derelict.sdl.sdl;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
25 import derelict.sdl.image;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
26 import derelict.opengl.gl;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
27
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
28 /// The usual texture....
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
29 alias Texture!(GL_TEXTURE_2D) Texture2D;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
30
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
31 /** Represents a texture. */
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
32 class Texture(TARGET)
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
33 {
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
34 static assert (TARGET == GL_TEXTURE_1D ||
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
35 TARGET == GL_TEXTURE_2D ||
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
36 TARGET == GL_TEXTURE_3D ||
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
37 TARGET == GL_TEXTURE_CUBE_MAP);
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
38
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
39 /** Create a new Texture.
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
40 *
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
41 * The Texture is not associated with an OpenGL texture name until load is called.
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
42 *
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
43 * Params:
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
44 * components = The number of components per pixel, 3 (RGB) or 4 (RGBA), or 0 in which case
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
45 * components is derived from the image loaded.
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
46 */
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
47 this (ubyte components) {
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
48 components_ = components;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
49 }
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
50 /// Free the texture ID.
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
51 ~this () {
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
52 glDeleteTextures(1, &texID);
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
53 }
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
54
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
55 /// Bind as current opengl texture.
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
56 void bind () {
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
57 assert (texID != 0, "No texture loaded yet!");
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
58 glBindTexture (TARGET, texID);
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
59 }
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
60
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
61 /// Load from a file
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
62 void load (char[] file) {
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
63 static assert (TARGET == GL_TEXTURE_2D); // no other support
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
64
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
65 assert (texID == 0, "Texture already loaded.");
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
66 glGenTextures (1, &texID);
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
67 bind;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
68
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
69 SDL_Surface* image;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
70 image = IMG_Load (toStringz(file));
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
71 if (image is null)
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
72 throw new ImageException ("Unable to load "~file);
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
73 // SDL_Surfaces sometimes need locking... really just for spec compliance:
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
74 assert (!SDL_MUSTLOCK(image), "Didn't expect to have to lock a surface loaded from a file; no locks used!");
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
75
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
76 GLenum format;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
77 if (image.format.BytesPerPixel == 3)
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
78 format = GL_RGB;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
79 else if (image.format.BytesPerPixel == 4)
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
80 format = GL_RGBA;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
81 else
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
82 throw new ImageException ("Only 8-bit-per-channel RGB/RGBA images are supported");
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
83
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
84 // Assume format is RGB(A); i.e. don't bother checking what
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
85 // image.format.[RGBA]mask/shift/loss are.
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
86
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
87 if (components_ == 0)
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
88 components_ = image.format.BytesPerPixel;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
89
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
90 if (image.pitch != image.w)
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
91 throw new ImageException ("pitch != width; this is unsupported");
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
92
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
93 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
94 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
95 glTexImage2D(TARGET, 0, components_,
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
96 w, h, 0,
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
97 format, GL_UNSIGNED_BYTE, image.pixels);
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
98
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
99 SDL_FreeSurface (image);
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
100 }
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
101
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
102 int width () { return w; }
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
103 int height () { return h; }
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
104 /// 3 for RGB, 4 for RGBA, 0 if no image loaded and format will be derived from the image.
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
105 ubyte components () { return components_; }
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
106
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
107 private {
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
108 int w, h; // size
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
109 ubyte components_ = 0; // 3 for RGB, 4 for RGBA
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
110 uint texID = 0;
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
111 }
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
112 }
f3d8c0441408 Implemented gl.texture (without testing) & fixed log options adjusted previously.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
113