1 /*
2  * Copyright (c) 2002-2008 LWJGL Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'LWJGL' nor the names of
17  *   its contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.lwjgl.opengl;
33 
34 import java.nio.IntBuffer;
35 
36 import org.lwjgl.BufferUtils;
37 
38 import static org.lwjgl.opengl.GL11.*;
39 
40 /** This class represents the state necessary for render-to-texture. */
41 public final class RenderTexture {
42 
43 	// ----------------------------------------------------------------------------------
44 	// ----------------------------- WGL_ARB_render_texture -----------------------------
45 	// ----------------------------------------------------------------------------------
46 
47 	/*
48 	Accepted by the <piAttributes> parameter of wglGetPixelFormatAttribivARB,
49 	wglGetPixelFormatAttribfvARB, and the <piAttribIList> and <pfAttribIList>
50 	parameters of wglChoosePixelFormatARB:
51 	*/
52 	private static final int WGL_BIND_TO_TEXTURE_RGB_ARB = 0x2070;
53 	private static final int WGL_BIND_TO_TEXTURE_RGBA_ARB = 0x2071;
54 
55 	/*
56 	Accepted by the <piAttribList> parameter of wglCreatePbufferARB and
57 	by the <iAttribute> parameter of wglQueryPbufferARB:
58 	*/
59 	private static final int WGL_TEXTURE_FORMAT_ARB = 0x2072;
60 	private static final int WGL_TEXTURE_TARGET_ARB = 0x2073;
61 	private static final int WGL_MIPMAP_TEXTURE_ARB = 0x2074;
62 
63 	/*
64 	Accepted as a value in the <piAttribList> parameter of
65 	wglCreatePbufferARB and returned in the value parameter of
66 	wglQueryPbufferARB when <iAttribute> is WGL_TEXTURE_FORMAT_ARB:
67 	*/
68 	private static final int WGL_TEXTURE_RGB_ARB = 0x2075;
69 	private static final int WGL_TEXTURE_RGBA_ARB = 0x2076;
70 
71 	/*
72 	Accepted as a value in the <piAttribList> parameter of
73 	wglCreatePbufferARB and returned in the value parameter of
74 	wglQueryPbufferARB when <iAttribute> is WGL_TEXTURE_TARGET_ARB:
75 	*/
76 	private static final int WGL_TEXTURE_CUBE_MAP_ARB = 0x2078;
77 	private static final int WGL_TEXTURE_1D_ARB = 0x2079;
78 	private static final int WGL_TEXTURE_2D_ARB = 0x207A;
79 	private static final int WGL_NO_TEXTURE_ARB = 0x2077;
80 
81 	/*
82 	Accepted by the <piAttribList> parameter of wglSetPbufferAttribARB and
83 	by the <iAttribute> parameter of wglQueryPbufferARB:
84 	*/
85 	static final int WGL_MIPMAP_LEVEL_ARB = 0x207B;
86 	static final int WGL_CUBE_MAP_FACE_ARB = 0x207C;
87 
88 	/*
89 	Accepted as a value in the <piAttribList> parameter of
90 	wglSetPbufferAttribARB and returned in the value parameter of
91 	wglQueryPbufferARB when <iAttribute> is WGL_CUBE_MAP_FACE_ARB:
92 	*/
93 	static final int WGL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB = 0x207D;
94 	static final int WGL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB = 0x207E;
95 	static final int WGL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB = 0x207F;
96 	static final int WGL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB = 0x2080;
97 	static final int WGL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB = 0x2081;
98 	static final int WGL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB = 0x2082;
99 
100 	/*
101 	Accepted by the <iBuffer> parameter of wglBindTexImageARB and
102 	wglReleaseTexImageARB:
103 	*/
104 	static final int WGL_FRONT_LEFT_ARB = 0x2083;
105 	static final int WGL_FRONT_RIGHT_ARB = 0x2084;
106 	static final int WGL_BACK_LEFT_ARB = 0x2085;
107 	static final int WGL_BACK_RIGHT_ARB = 0x2086;
108 
109 	/*
110 	private static final int WGL_AUX0_ARB = 0x2087;
111 	private static final int WGL_AUX1_ARB = 0x2088;
112 	private static final int WGL_AUX2_ARB = 0x2089;
113 	private static final int WGL_AUX3_ARB = 0x208A;
114 	private static final int WGL_AUX4_ARB = 0x208B;
115 	private static final int WGL_AUX5_ARB = 0x208C;
116 	private static final int WGL_AUX6_ARB = 0x208D;
117 	private static final int WGL_AUX7_ARB = 0x208E;
118 	private static final int WGL_AUX8_ARB = 0x208F;
119 	private static final int WGL_AUX9_ARB = 0x2090;
120 	*/
121 
122 	// -------------------------------------------------------------------------------------------
123 	// ----------------------------- WGL_NV_render_texture_rectangle -----------------------------
124 	// -------------------------------------------------------------------------------------------
125 
126 	/*
127 	Accepted by the <piAttributes> parameter of wglGetPixelFormatAttribivARB,
128 	wglGetPixelFormatAttribfvARB, and the <piAttribIList> and <pfAttribIList>
129 	parameters of wglChoosePixelFormatARB:
130 	*/
131 	private static final int WGL_BIND_TO_TEXTURE_RECTANGLE_RGB_NV = 0x20A0;
132 	private static final int WGL_BIND_TO_TEXTURE_RECTANGLE_RGBA_NV = 0x20A1;
133 
134 	/*
135 	Accepted as a value in the <piAttribList> parameter of wglCreatePbufferARB
136 	and returned in the value parameter of wglQueryPbufferARB when
137 	<iAttribute> is WGL_TEXTURE_TARGET_ARB:
138 	*/
139 	private static final int WGL_TEXTURE_RECTANGLE_NV = 0x20A2;
140 
141 	// ---------------------------------------------------------------------------------------
142 	// ----------------------------- WGL_NV_render_depth_texture -----------------------------
143 	// ---------------------------------------------------------------------------------------
144 
145 	/*
146 	Accepted by the <piAttributes> parameter of wglGetPixelFormatAttribivARB,
147 	wglGetPixelFormatAttribfvARB, and the <piAttribIList> and <pfAttribIList>
148 	parameters of wglChoosePixelFormatARB:
149 	*/
150 	private static final int WGL_BIND_TO_TEXTURE_DEPTH_NV = 0x20A3;
151 	private static final int WGL_BIND_TO_TEXTURE_RECTANGLE_DEPTH_NV = 0x20A4;
152 
153 	/*
154 	Accepted by the <piAttribList> parameter of wglCreatePbufferARB and
155 	by the <iAttribute> parameter of wglQueryPbufferARB:
156 	*/
157 	private static final int WGL_DEPTH_TEXTURE_FORMAT_NV = 0x20A5;
158 
159 	/*
160 	Accepted as a value in the <piAttribList> parameter of wglCreatePbufferARB
161 	and returned in the value parameter of wglQueryPbufferARB when
162 	<iAttribute> is WGL_DEPTH_TEXTURE_FORMAT_NV:
163 	*/
164 	private static final int WGL_TEXTURE_DEPTH_COMPONENT_NV = 0x20A6;
165 
166 	/*
167 	Accepted by the <iBuffer> parameter of wglBindTexImageARB:
168 	*/
169 	static final int WGL_DEPTH_COMPONENT_NV = 0x20A7;
170 
171 	/** The TEXTURE_1D target. */
172 	public static final int RENDER_TEXTURE_1D = WGL_TEXTURE_1D_ARB;
173 
174 	/** The TEXTURE_2D target. */
175 	public static final int RENDER_TEXTURE_2D = WGL_TEXTURE_2D_ARB;
176 
177 	/** The TEXTURE_RECTANGLE target. */
178 	public static final int RENDER_TEXTURE_RECTANGLE = WGL_TEXTURE_RECTANGLE_NV;
179 
180 	/** The TEXTURE_CUBE_MAP target. */
181 	public static final int RENDER_TEXTURE_CUBE_MAP = WGL_TEXTURE_CUBE_MAP_ARB;
182 
183 	IntBuffer pixelFormatCaps;
184 	IntBuffer pBufferAttribs;
185 
186 	/**
187 	 * Creates a RenderTexture object for enabling render-to-texture on a P-buffer.
188 	 * <p/>
189 	 * NOTE: Only one of useRGB and useRGBA can be true at the same time.
190 	 * <p/>
191 	 * NOTE: useRGB(A) and useDepth can be true at the same time, thus allowing two different render textures.
192 	 * <p/>
193 	 * NOTE: The target parameter can be one of the following:
194 	 * <p/>
195 	 * RENDER_TEXTURE_1D RENDER_TEXTURE_2D RENDER_TEXTURE_RECTANGLE RENDER_TEXTURE_CUBE_MAP
196 	 *
197 	 * @param useRGB      - When true the P-buffer can be used as an RGB render texture.
198 	 * @param useRGBA     - When true the P-buffer can be used as an RGBA render texture.
199 	 * @param useDepth    - When true the P-buffer can be used as a depth render texture.
200 	 * @param isRectangle - When true rectangle textures will be allowed on the P-buffer.
201 	 * @param target      - The texture target of the render texture.
202 	 * @param mipmaps     - How many mipmap levels to allocate on the P-buffer.
203 	 */
RenderTexture(boolean useRGB, boolean useRGBA, boolean useDepth, boolean isRectangle, int target, int mipmaps)204 	public RenderTexture(boolean useRGB, boolean useRGBA, boolean useDepth, boolean isRectangle, int target, int mipmaps) {
205 		if ( useRGB && useRGBA )
206 			throw new IllegalArgumentException("A RenderTexture can't be both RGB and RGBA.");
207 
208 		if ( mipmaps < 0 )
209 			throw new IllegalArgumentException("The mipmap levels can't be negative.");
210 
211 		if ( isRectangle && target != RENDER_TEXTURE_RECTANGLE )
212 			throw new IllegalArgumentException("When the RenderTexture is rectangle the target must be RENDER_TEXTURE_RECTANGLE.");
213 
214 		pixelFormatCaps = BufferUtils.createIntBuffer(4);
215 		pBufferAttribs = BufferUtils.createIntBuffer(8);
216 
217 		if ( useRGB ) {
218 			pixelFormatCaps.put(isRectangle ? WGL_BIND_TO_TEXTURE_RECTANGLE_RGB_NV : WGL_BIND_TO_TEXTURE_RGB_ARB);
219 			pixelFormatCaps.put(GL_TRUE);
220 
221 			pBufferAttribs.put(WGL_TEXTURE_FORMAT_ARB);
222 			pBufferAttribs.put(WGL_TEXTURE_RGB_ARB);
223 		} else if ( useRGBA ) {
224 			pixelFormatCaps.put(isRectangle ? WGL_BIND_TO_TEXTURE_RECTANGLE_RGBA_NV : WGL_BIND_TO_TEXTURE_RGBA_ARB);
225 			pixelFormatCaps.put(GL_TRUE);
226 
227 			pBufferAttribs.put(WGL_TEXTURE_FORMAT_ARB);
228 			pBufferAttribs.put(WGL_TEXTURE_RGBA_ARB);
229 		}
230 
231 		if ( useDepth ) {
232 			pixelFormatCaps.put(isRectangle ? WGL_BIND_TO_TEXTURE_RECTANGLE_DEPTH_NV : WGL_BIND_TO_TEXTURE_DEPTH_NV);
233 			pixelFormatCaps.put(GL_TRUE);
234 
235 			pBufferAttribs.put(WGL_DEPTH_TEXTURE_FORMAT_NV);
236 			pBufferAttribs.put(WGL_TEXTURE_DEPTH_COMPONENT_NV);
237 		}
238 
239 		pBufferAttribs.put(WGL_TEXTURE_TARGET_ARB);
240 		pBufferAttribs.put(target);
241 
242 		if ( mipmaps != 0 ) {
243 			pBufferAttribs.put(WGL_MIPMAP_TEXTURE_ARB);
244 			pBufferAttribs.put(mipmaps);
245 		}
246 
247 		pixelFormatCaps.flip();
248 		pBufferAttribs.flip();
249 	}
250 
251 }