org.lwjgl.opengl.PixelFormat Java Examples

The following examples show how to use org.lwjgl.opengl.PixelFormat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Window.java    From LowPolyWater with The Unlicense 6 votes vote down vote up
protected Window(Context context, WindowBuilder settings) {
	this.fpsCap = settings.getFpsCap();
	try {
		getSuitableFullScreenModes();
		DisplayMode resolution = getStartResolution(settings);
		Display.setInitialBackground(1, 1, 1);
		this.aspectRatio = (float) resolution.getWidth() / resolution.getHeight();
		setResolution(resolution, settings.isFullScreen());
		if (settings.hasIcon()) {
			Display.setIcon(settings.getIcon());
		}
		Display.setVSyncEnabled(settings.isvSync());
		Display.setTitle(settings.getTitle());
		Display.create(new PixelFormat().withDepthBits(24).withSamples(4), context.getAttribs());
		GL11.glViewport(0, 0, resolution.getWidth(), resolution.getHeight());
	} catch (LWJGLException e) {
		e.printStackTrace();
	}
}
 
Example #2
Source File: DisplayManager.java    From OpenGL-Animation with The Unlicense 6 votes vote down vote up
public static void createDisplay() {
	try {
		Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
		ContextAttribs attribs = new ContextAttribs(3, 2).withProfileCore(true).withForwardCompatible(true);
		Display.create(new PixelFormat().withDepthBits(24).withSamples(4), attribs);
		Display.setTitle(TITLE);
		Display.setInitialBackground(1, 1, 1);
		GL11.glEnable(GL13.GL_MULTISAMPLE);
	} catch (LWJGLException e) {
		e.printStackTrace();
		System.err.println("Couldn't create display!");
		System.exit(-1);
	}
	GL11.glViewport(0, 0, WIDTH, HEIGHT);
	lastFrameTime = getCurrentTime();
}
 
Example #3
Source File: OpenGL3_TheQuadTextured.java    From ldparteditor with MIT License 6 votes vote down vote up
private void setupOpenGL() {
    // Setup an OpenGL context with API version 3.2
    try {
        PixelFormat pixelFormat = new PixelFormat();
        ContextAttribs contextAtrributes = new ContextAttribs(3, 2)
            .withForwardCompatible(true)
            .withProfileCore(true);
         
        Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
        Display.setTitle(WINDOW_TITLE);
        Display.create(pixelFormat, contextAtrributes);
         
        GL11.glViewport(0, 0, WIDTH, HEIGHT);
    } catch (LWJGLException e) {
        e.printStackTrace();
        System.exit(-1);
    }
     
    // Setup an XNA like background color
    GL11.glClearColor(0.4f, 0.6f, 0.9f, 0f);
     
    // Map the internal OpenGL coordinate system to the entire screen
    GL11.glViewport(0, 0, WIDTH, HEIGHT);
     
    this.exitOnGLError("setupOpenGL");
}
 
Example #4
Source File: SerializableDisplayMode.java    From tribaltrouble with GNU General Public License v2.0 6 votes vote down vote up
private final static void createWindow() throws LWJGLException {
	int[] depth_array = new int[]{24, 16};
	int[] samples_array = new int[]{/*Settings.getSettings().samples, */0};
	LWJGLException last_exception = new LWJGLException("Could not find a suitable pixel format");
	for (int d = 0; d < depth_array.length; d++)
		for (int s = 0; s < samples_array.length; s++) {
			int depth = depth_array[d];
			int samples = samples_array[s];
			try {
				Display.create(new PixelFormat(0, depth, 0, samples));
				return;
			} catch (LWJGLException e) {
				last_exception = e;
				System.out.println("Failed window: depthbits = " + depth + " | samples = " + samples + " with exception " + e);
			}
		}
	throw last_exception;
}
 
Example #5
Source File: PbufferRenderer.java    From tribaltrouble with GNU General Public License v2.0 6 votes vote down vote up
PbufferRenderer(int width, int height, PixelFormat format, boolean use_copyteximage, OffscreenRendererFactory factory) throws LWJGLException {
	super(width, height, use_copyteximage);
	this.factory = factory;
	pbuffer = new Pbuffer(width, height, format, null, null);
	GLStateStack state_stack = new GLStateStack();
	pbuffer.makeCurrent();
	GLStateStack.setCurrent(state_stack);
	try {
		pbuffer.makeCurrent();
		Renderer.dumpWindowInfo();
		init();
		if (!GLUtils.getGLBoolean(GL11.GL_DOUBLEBUFFER)) {
			GL11.glReadBuffer(GL11.GL_FRONT);
			GL11.glDrawBuffer(GL11.GL_FRONT);
		}
	} catch (LWJGLException e) {
		pbuffer.destroy();
		throw e;
	}
}
 
Example #6
Source File: PBufferUniqueGraphics.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Initialise the PBuffer that will be used to render to
 * 
 * @throws SlickException
 */
private void init() throws SlickException {
	try {
		Texture tex = InternalTextureLoader.get().createTexture(image.getWidth(), image.getHeight(), image.getFilter());

		pbuffer = new Pbuffer(screenWidth, screenHeight, new PixelFormat(8, 0, 0), null, null);
		// Initialise state of the pbuffer context.
		pbuffer.makeCurrent();

		initGL();
		image.draw(0,0);
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex.getTextureID());
		GL11.glCopyTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, 0, 0, 
							  tex.getTextureWidth(), 
							  tex.getTextureHeight(), 0);
		image.setTexture(tex);
		
		Display.makeCurrent();
	} catch (Exception e) {
		Log.error(e);
		throw new SlickException("Failed to create PBuffer for dynamic image. OpenGL driver failure?");
	}
}
 
Example #7
Source File: PBufferGraphics.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Initialise the PBuffer that will be used to render to
 * 
 * @throws SlickException
 */
private void init() throws SlickException {
	try {
		Texture tex = InternalTextureLoader.get().createTexture(image.getWidth(), image.getHeight(), image.getFilter());
		
		final RenderTexture rt = new RenderTexture(false, true, false, false, RenderTexture.RENDER_TEXTURE_2D, 0);
		pbuffer = new Pbuffer(screenWidth, screenHeight, new PixelFormat(8, 0, 0), rt, null);

		// Initialise state of the pbuffer context.
		pbuffer.makeCurrent();

		initGL();
		GL.glBindTexture(GL11.GL_TEXTURE_2D, tex.getTextureID());
		pbuffer.releaseTexImage(Pbuffer.FRONT_LEFT_BUFFER);
		image.draw(0,0);
		image.setTexture(tex);
		
		Display.makeCurrent();
	} catch (Exception e) {
		Log.error(e);
		throw new SlickException("Failed to create PBuffer for dynamic image. OpenGL driver failure?");
	}
}
 
Example #8
Source File: LwjglOffscreenBuffer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected void initInThread(){
    if ((Pbuffer.getCapabilities() & Pbuffer.PBUFFER_SUPPORTED) == 0){
        logger.severe("Offscreen surfaces are not supported.");
        return;
    }

    pixelFormat = new PixelFormat(settings.getBitsPerPixel(),
                                  0,
                                  settings.getDepthBits(),
                                  settings.getStencilBits(),
                                  settings.getSamples());
    
    width = settings.getWidth();
    height = settings.getHeight();
    try{
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            public void uncaughtException(Thread thread, Throwable thrown) {
                listener.handleError("Uncaught exception thrown in "+thread.toString(), thrown);
            }
        });

        pbuffer = new Pbuffer(width, height, pixelFormat, null, null, createContextAttribs());
        pbuffer.makeCurrent();

        renderable.set(true);

        logger.info("Offscreen buffer created.");
        printContextInitInfo();
    } catch (LWJGLException ex){
        listener.handleError("Failed to create display", ex);
    } finally {
        // TODO: It is possible to avoid "Failed to find pixel format"
        // error here by creating a default display.
    }
    super.internalCreate();
    listener.initialize();
}
 
Example #9
Source File: DisplayManager.java    From OpenGL-Tutorial-1 with The Unlicense 5 votes vote down vote up
/**
 * Creates a display window on which we can render our game. The dimensions
 * of the window are determined by setting the display mode. By using
 * "glViewport" we tell OpenGL which part of the window we want to render
 * our game onto. We indicated that we want to use the entire window.
 */
public static void createDisplay() {
	ContextAttribs attribs = new ContextAttribs(3, 2).withForwardCompatible(true).withProfileCore(true);
	try {
		Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
		Display.create(new PixelFormat(), attribs);
		Display.setTitle(TITLE);
	} catch (LWJGLException e) {
		e.printStackTrace();
	}
	GL11.glViewport(0, 0, WIDTH, HEIGHT);
}
 
Example #10
Source File: AppGameContainer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Try creating a display with the given format
 * 
 * @param format The format to attempt
 * @throws LWJGLException Indicates a failure to support the given format
 */
private void tryCreateDisplay(PixelFormat format) throws LWJGLException {
	if (SHARED_DRAWABLE == null) 
	{
		Display.create(format);
	}
	else
	{
		Display.create(format, SHARED_DRAWABLE);
	}
}
 
Example #11
Source File: GameContainer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Enable shared OpenGL context. After calling this all containers created 
 * will shared a single parent context
 * 
 * @throws SlickException Indicates a failure to create the shared drawable
 */
public static void enableSharedContext() throws SlickException {
	try {
		SHARED_DRAWABLE = new Pbuffer(64, 64, new PixelFormat(8, 0, 0), null);
	} catch (LWJGLException e) {
		throw new SlickException("Unable to create the pbuffer used for shard context, buffers not supported", e);
	}
}
 
Example #12
Source File: AppletGameContainer.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Create the LWJGL display
 * 
 * @throws Exception Failure to create display
 */
private void createDisplay() throws Exception {
   try {
      // create display with alpha
      Display.create(new PixelFormat(8,8,GameContainer.stencil ? 8 : 0));
      alphaSupport = true;
   } catch (Exception e) {
      // if we couldn't get alpha, let us know
      alphaSupport = false;
       Display.destroy();
       // create display without alpha
      Display.create();
   }
}
 
Example #13
Source File: GLProgram.java    From LWJGL-OpenGL-Tutorials with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Initializes the context with its attributes and PixelFormat supplied.
 * 
 * This method does not return until the game loop ends.
 * 
 * @param format The PixelFormat specifying the buffers.
 * @param attribs The context attributes.
 */
public final void run(PixelFormat format, ContextAttribs attribs) {
	try {
		Display.create(format, attribs);
	} catch(Exception exc) {
		exc.printStackTrace();
		System.exit(1);
	}
	
	gameLoop();
}
 
Example #14
Source File: GameContainer.java    From opsu with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Enable shared OpenGL context. After calling this all containers created 
 * will shared a single parent context
 * 
 * @throws SlickException Indicates a failure to create the shared drawable
 */
public static void enableSharedContext() throws SlickException {
	try {
		SHARED_DRAWABLE = new Pbuffer(64, 64, new PixelFormat(8, 0, 0), null);
	} catch (LWJGLException e) {
		throw new SlickException("Unable to create the pbuffer used for shard context, buffers not supported", e);
	}
}
 
Example #15
Source File: Example3_14.java    From LWJGL-OpenGL-Tutorials with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void main(String[] args) {
	new Example3_14().run(new PixelFormat(0, 0, 1));
}
 
Example #16
Source File: Example16_3.java    From LWJGL-OpenGL-Tutorials with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void main(String[] args) {
	new Example16_3().run(true, new PixelFormat().withSRGB(true));
}
 
Example #17
Source File: OffscreenRendererFactory.java    From tribaltrouble with GNU General Public License v2.0 4 votes vote down vote up
public final OffscreenRenderer createRenderer(int width, int height, PixelFormat format, boolean use_copyteximage, boolean use_pbuffer, boolean use_fbo) {
		OffscreenRenderer renderer = doCreateRenderer(width, height, format, use_copyteximage, use_pbuffer, use_fbo);
System.out.println("Creating renderer = " + renderer);
		return renderer;
	}
 
Example #18
Source File: IslandGenerator.java    From tribaltrouble with GNU General Public License v2.0 4 votes vote down vote up
private final static Texture[][] blendTextures(OffscreenRendererFactory factory, int chunks_per_colormap, BlendInfo[] blend_infos, int alpha_size, int structure_size, int scale) {
		boolean use_pbuffer = Settings.getSettings().usePbuffer();
		boolean use_fbo = Settings.getSettings().useFBO();
		OffscreenRenderer offscreen = factory.createRenderer(TEXELS_PER_CHUNK, TEXELS_PER_CHUNK, new PixelFormat(Globals.VIEW_BIT_DEPTH, 0, 0, 0, 0), Settings.getSettings().use_copyteximage, use_pbuffer, use_fbo);
		GL11.glColor4f(1f, 1f, 1f, 1f);
		GL11.glDisable(GL11.GL_DEPTH_TEST);
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0f, TEXELS_PER_CHUNK, 0, TEXELS_PER_CHUNK, -1f, 1f);

		GL11.glMatrixMode(GL11.GL_TEXTURE);
		GLState.activeTexture(GL13.GL_TEXTURE1);
		GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_REPLACE);
		GL11.glDisable(GL11.GL_TEXTURE_GEN_S);
		GL11.glDisable(GL11.GL_TEXTURE_GEN_T);
		GL11.glLoadIdentity();
		GLState.activeTexture(GL13.GL_TEXTURE0);
		GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_REPLACE);
		GL11.glDisable(GL11.GL_TEXTURE_GEN_S);
		GL11.glDisable(GL11.GL_TEXTURE_GEN_T);
		GL11.glLoadIdentity();
		GL11.glMatrixMode(GL11.GL_MODELVIEW);

		float alpha_texel_size = 1f/(alpha_size*scale);
		float structure_texel_size = 1f/structure_size;
		float structure_texels_per_chunk_border = Globals.TEXELS_PER_CHUNK_BORDER*structure_texel_size;
		float structure_offset = TEXELS_PER_CHUNK*structure_texel_size - 2*structure_texels_per_chunk_border;
		float structure_length = structure_offset + 2*structure_texels_per_chunk_border;

		float alpha_texels_per_chunk_border = Globals.TEXELS_PER_CHUNK_BORDER*alpha_texel_size;
		float alpha_offset = TEXELS_PER_CHUNK*alpha_texel_size;
		float alpha_length = alpha_offset + 2*alpha_texels_per_chunk_border;

		Texture[][] chunk_maps = new Texture[chunks_per_colormap][chunks_per_colormap];
		FloatBuffer coordinates = BufferUtils.createFloatBuffer(4*3);
		coordinates.put(new float[]{0f, 0f, 0f,
									TEXELS_PER_CHUNK, 0f, 0f,
									TEXELS_PER_CHUNK, TEXELS_PER_CHUNK, 0f,
									0f, TEXELS_PER_CHUNK, 0f});
		coordinates.rewind();
		FloatBuffer structure_tex_coords = BufferUtils.createFloatBuffer(4*2);
		FloatBuffer alpha_tex_coords = BufferUtils.createFloatBuffer(4*2);
		GLStateStack.switchState(GLState.VERTEX_ARRAY | GLState.TEXCOORD0_ARRAY | GLState.TEXCOORD1_ARRAY);
		GL11.glVertexPointer(3, 0, coordinates);
		GL11.glTexCoordPointer(2, 0, structure_tex_coords);
		GLState.clientActiveTexture(GL13.GL_TEXTURE1);
		GL11.glTexCoordPointer(2, 0, alpha_tex_coords);
		GLState.clientActiveTexture(GL13.GL_TEXTURE0);
		for (int y = 0; y < chunk_maps.length; y++) {
			for (int x = 0; x < chunk_maps[y].length; x++) {
				chunk_maps[y][x] = new Texture(TEXELS_PER_CHUNK, TEXELS_PER_CHUNK, GL11.GL_LINEAR_MIPMAP_NEAREST, GL11.GL_LINEAR, GL12.GL_CLAMP_TO_EDGE, GL12.GL_CLAMP_TO_EDGE, Globals.NO_MIPMAP_CUTOFF);
				int mip_scale = 1;
				int mip_level = 0;
				int mip_size = TEXELS_PER_CHUNK;
				while (mip_size >= 1) {
					GL11.glLoadIdentity();
					GL11.glScalef(1f/mip_scale, 1f/mip_scale, 1f);
					GL11.glEnable(GL11.GL_BLEND);

					for (int i = 0; i < blend_infos.length; i++) {
						blend_infos[i].setup();
						float structure_offset_u = x*structure_offset - structure_texels_per_chunk_border;
						float structure_offset_v = y*structure_offset - structure_texels_per_chunk_border;
						float alpha_offset_u = x*alpha_offset - alpha_texels_per_chunk_border;
						float alpha_offset_v = y*alpha_offset - alpha_texels_per_chunk_border;
						structure_tex_coords.put(0, structure_offset_u).put(1, structure_offset_v);
						structure_tex_coords.put(2, structure_offset_u + structure_length).put(3, structure_offset_v);
						structure_tex_coords.put(4, structure_offset_u + structure_length).put(5, structure_offset_v + structure_length);
						structure_tex_coords.put(6, structure_offset_u).put(7, structure_offset_v + structure_length);
						alpha_tex_coords.put(0, alpha_offset_u).put(1, alpha_offset_v);
						alpha_tex_coords.put(2, alpha_offset_u + alpha_length).put(3, alpha_offset_v);
						alpha_tex_coords.put(4, alpha_offset_u + alpha_length).put(5, alpha_offset_v + alpha_length);
						alpha_tex_coords.put(6, alpha_offset_u).put(7, alpha_offset_v + alpha_length);
						GL11.glDrawArrays(GL11.GL_QUADS, 0, 4);
						blend_infos[i].reset();
					}
/*if (mip_level == 0)
offscreen.dumpToFile("colormap-" + x + "-" + y);*/
					offscreen.copyToTexture(chunk_maps[y][x], mip_level, Globals.COMPRESSED_RGB_FORMAT, 0, 0, mip_size, mip_size);
					mip_scale <<= 1;
					mip_level++;
					mip_size >>= 1;
				}
			}
		}
		boolean succeeded = offscreen.destroy();
		if (!succeeded) {
/*			for (int y = 0; y < chunk_maps.length; y++)
				for (int x = 0; x < chunk_maps[y].length; x++)
					chunk_maps[y][x].delete();*/
			return null;
		} else
			return chunk_maps;
	}
 
Example #19
Source File: GLProgram.java    From LWJGL-OpenGL-Tutorials with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Initializes the context depending on the value of <code>core</code>, the supplied PixelFormat, and the specified
 * OpenGL version in the format: <code>major.minor</code>
 * 
 * This method does not return until the game loop ends.
 * 
 * @param major
 * @param minor
 * @param core <<code>True</code> requests the Core profile, while <code>false</code> requests the Compatibility profile.
 * @param format The PixelFormat specifying the buffers.
 */
public final void run(int major, int minor, boolean core, PixelFormat format) {
	run(format, core ? new ContextAttribs(major, minor).withProfileCore(core) : new ContextAttribs(major, minor));
}
 
Example #20
Source File: GLProgram.java    From LWJGL-OpenGL-Tutorials with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Initializes the context with default settings and the supplied PixelFormat.
 * 
 * This method does not return until the game loop ends.
 * 
 * @param format The PixelFormat specifying the buffers.
 */
public final void run(PixelFormat format) {
	run(format, null);
}
 
Example #21
Source File: GLProgram.java    From LWJGL-OpenGL-Tutorials with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Initializes the context with its attributes supplied.
 * 
 * This method does not return until the game loop ends.
 * 
 * @param attribs The context attributes.
 */
public final void run(ContextAttribs attribs) {
	run(new PixelFormat(), attribs);
}
 
Example #22
Source File: GLProgram.java    From LWJGL-OpenGL-Tutorials with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Initializes the context depending on the value of <code>core</code> and the specified OpenGL version in the format: <code>major.minor</code>
 * 
 * This method does not return until the game loop ends.
 * 
 * @param major
 * @param minor
 * @param core <code>True</code> requests the Core profile, while <code>false</code> requests the Compatibility profile.
 */
public final void run(int major, int minor, boolean core) {
	run(major, minor, core, new PixelFormat());
}
 
Example #23
Source File: GLProgram.java    From LWJGL-OpenGL-Tutorials with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Initializes the context depending on the value of <code>core</code> and the supplied PixelFormat.
 * 
 * This method does not return until the game loop ends.
 * 
 * @param core <code>True</code> requests the Core profile, while <code>false</code> keeps default settings.
 * @param format The PixelFormat specifying the buffers.
 */
public final void run(boolean core, PixelFormat format) {
	run(format, core ? new ContextAttribs(3, 2).withProfileCore(true) : null);
}
 
Example #24
Source File: GLProgram.java    From LWJGL-OpenGL-Tutorials with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Initializes the context depending on the value of <code>core</code>. Supplying <code>false</code> is equivalent to
 * simply calling <code>run()</code>, while <code>true</code> requests the Core profile if available.
 * 
 * This method does not return until the game loop ends.
 * 
 * @param core <code>True</code> requests the Core profile, while <code>false</code> keeps default settings.
 */
public final void run(boolean core) {
	run(core, new PixelFormat());
}