use of com.jme3.renderer.opengl.GL in project jmonkeyengine by jMonkeyEngine.
the class GLRenderer method updateTexImageData.
/**
* Uploads the given image to the GL driver.
*
* @param img The image to upload
* @param type How the data in the image argument should be interpreted.
* @param unit The texture slot to be used to upload the image, not important
* @param scaleToPot If true, the image will be scaled to power-of-2 dimensions
* before being uploaded.
*/
public void updateTexImageData(Image img, Texture.Type type, int unit, boolean scaleToPot) {
int texId = img.getId();
if (texId == -1) {
// create texture
gl.glGenTextures(intBuf1);
texId = intBuf1.get(0);
img.setId(texId);
objManager.registerObject(img);
statistics.onNewTexture();
}
// bind texture
int target = convertTextureType(type, img.getMultiSamples(), -1);
bindTextureAndUnit(target, img, unit);
if (!img.hasMipmaps() && img.isGeneratedMipmapsRequired()) {
if (!caps.contains(Caps.FrameBuffer) && gl2 != null) {
gl2.glTexParameteri(target, GL2.GL_GENERATE_MIPMAP, GL.GL_TRUE);
img.setMipmapsGenerated(true);
} else {
// For OpenGL3 and up.
// We'll generate mipmaps via glGenerateMipmapEXT (see below)
}
} else if (img.hasMipmaps()) {
// Image already has mipmaps, set the max level based on the
// number of mipmaps we have.
gl.glTexParameteri(target, GL.GL_TEXTURE_MAX_LEVEL, img.getMipMapSizes().length - 1);
} else {
// Image does not have mipmaps and they are not required.
// Specify that that the texture has no mipmaps.
gl.glTexParameteri(target, GL.GL_TEXTURE_MAX_LEVEL, 0);
}
int imageSamples = img.getMultiSamples();
if (imageSamples > 1) {
if (img.getFormat().isDepthFormat()) {
img.setMultiSamples(Math.min(limits.get(Limits.DepthTextureSamples), imageSamples));
} else {
img.setMultiSamples(Math.min(limits.get(Limits.ColorTextureSamples), imageSamples));
}
}
// Check if graphics card doesn't support multisample textures
if (!caps.contains(Caps.TextureMultisample)) {
if (img.getMultiSamples() > 1) {
throw new RendererException("Multisample textures are not supported by the video hardware");
}
}
// Check if graphics card doesn't support depth textures
if (img.getFormat().isDepthFormat() && !caps.contains(Caps.DepthTexture)) {
throw new RendererException("Depth textures are not supported by the video hardware");
}
if (target == GL.GL_TEXTURE_CUBE_MAP) {
// Check max texture size before upload
int cubeSize = limits.get(Limits.CubemapSize);
if (img.getWidth() > cubeSize || img.getHeight() > cubeSize) {
throw new RendererException("Cannot upload cubemap " + img + ". The maximum supported cubemap resolution is " + cubeSize);
}
if (img.getWidth() != img.getHeight()) {
throw new RendererException("Cubemaps must have square dimensions");
}
} else {
int texSize = limits.get(Limits.TextureSize);
if (img.getWidth() > texSize || img.getHeight() > texSize) {
throw new RendererException("Cannot upload texture " + img + ". The maximum supported texture resolution is " + texSize);
}
}
Image imageForUpload;
if (scaleToPot) {
imageForUpload = MipMapGenerator.resizeToPowerOf2(img);
} else {
imageForUpload = img;
}
if (target == GL.GL_TEXTURE_CUBE_MAP) {
List<ByteBuffer> data = imageForUpload.getData();
if (data.size() != 6) {
logger.log(Level.WARNING, "Invalid texture: {0}\n" + "Cubemap textures must contain 6 data units.", img);
return;
}
for (int i = 0; i < 6; i++) {
texUtil.uploadTexture(imageForUpload, GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, i, linearizeSrgbImages);
}
} else if (target == GLExt.GL_TEXTURE_2D_ARRAY_EXT) {
if (!caps.contains(Caps.TextureArray)) {
throw new RendererException("Texture arrays not supported by graphics hardware");
}
List<ByteBuffer> data = imageForUpload.getData();
// -1 index specifies prepare data for 2D Array
texUtil.uploadTexture(imageForUpload, target, -1, linearizeSrgbImages);
for (int i = 0; i < data.size(); i++) {
// upload each slice of 2D array in turn
// this time with the appropriate index
texUtil.uploadTexture(imageForUpload, target, i, linearizeSrgbImages);
}
} else {
texUtil.uploadTexture(imageForUpload, target, 0, linearizeSrgbImages);
}
if (img.getMultiSamples() != imageSamples) {
img.setMultiSamples(imageSamples);
}
if (caps.contains(Caps.FrameBuffer) || gl2 == null) {
if (!img.hasMipmaps() && img.isGeneratedMipmapsRequired() && img.getData(0) != null) {
glfbo.glGenerateMipmapEXT(target);
img.setMipmapsGenerated(true);
}
}
img.clearUpdateNeeded();
}
use of com.jme3.renderer.opengl.GL in project jmonkeyengine by jMonkeyEngine.
the class KTXLoader method getImageFormat.
/**
* returns the JME image format from gl formats and types.
* @param glFormat
* @param glInternalFormat
* @param glType
* @return
*/
private Image.Format getImageFormat(int glFormat, int glInternalFormat, int glType) {
EnumSet<Caps> caps = EnumSet.allOf(Caps.class);
GLImageFormat[][] formats = GLImageFormats.getFormatsForCaps(caps);
for (GLImageFormat[] format : formats) {
for (int j = 0; j < format.length; j++) {
GLImageFormat glImgFormat = format[j];
if (glImgFormat != null) {
if (glImgFormat.format == glFormat && glImgFormat.dataType == glType) {
if (glFormat == glInternalFormat || glImgFormat.internalFormat == glInternalFormat) {
return Image.Format.values()[j];
}
}
}
}
}
return null;
}
use of com.jme3.renderer.opengl.GL in project jmonkeyengine by jMonkeyEngine.
the class LwjglContext method initContextFirstTime.
protected void initContextFirstTime() {
final GLCapabilities capabilities = createCapabilities(settings.getRenderer().equals(AppSettings.LWJGL_OPENGL3));
if (!capabilities.OpenGL20) {
throw new RendererException("OpenGL 2.0 or higher is required for jMonkeyEngine");
}
if (settings.getRenderer().equals(AppSettings.LWJGL_OPENGL2) || settings.getRenderer().equals(AppSettings.LWJGL_OPENGL3)) {
GL gl = new LwjglGL();
GLExt glext = new LwjglGLExt();
GLFbo glfbo;
if (capabilities.OpenGL30) {
glfbo = new LwjglGLFboGL3();
} else {
glfbo = new LwjglGLFboEXT();
}
if (settings.getBoolean("GraphicsDebug")) {
gl = new GLDebugDesktop(gl, glext, glfbo);
glext = (GLExt) gl;
glfbo = (GLFbo) gl;
}
if (settings.getBoolean("GraphicsTiming")) {
GLTimingState timingState = new GLTimingState();
gl = (GL) GLTiming.createGLTiming(gl, timingState, GL.class, GL2.class, GL3.class, GL4.class);
glext = (GLExt) GLTiming.createGLTiming(glext, timingState, GLExt.class);
glfbo = (GLFbo) GLTiming.createGLTiming(glfbo, timingState, GLFbo.class);
}
if (settings.getBoolean("GraphicsTrace")) {
gl = (GL) GLTracer.createDesktopGlTracer(gl, GL.class, GL2.class, GL3.class, GL4.class);
glext = (GLExt) GLTracer.createDesktopGlTracer(glext, GLExt.class);
glfbo = (GLFbo) GLTracer.createDesktopGlTracer(glfbo, GLFbo.class);
}
renderer = new GLRenderer(gl, glext, glfbo);
renderer.initialize();
} else {
throw new UnsupportedOperationException("Unsupported renderer: " + settings.getRenderer());
}
if (capabilities.GL_ARB_debug_output && settings.getBoolean("GraphicsDebug")) {
ARBDebugOutput.glDebugMessageCallbackARB(new LwjglGLDebugOutputHandler(), 0);
}
renderer.setMainFrameBufferSrgb(settings.isGammaCorrection());
renderer.setLinearizeSrgbImages(settings.isGammaCorrection());
// Init input
if (keyInput != null) {
keyInput.initialize();
}
if (mouseInput != null) {
mouseInput.initialize();
}
if (joyInput != null) {
joyInput.initialize();
}
renderable.set(true);
}
use of com.jme3.renderer.opengl.GL in project jmonkeyengine by jMonkeyEngine.
the class OpaqueComparatorTest method testSort.
/**
* Given a correctly sorted list of materials, check if the
* opaque comparator can sort a reversed list of them.
*
* Each material will be cloned so that none of them will be equal to each other
* in reference, forcing the comparator to compare the material sort ID.
*
* E.g. for a list of materials A, B, C, the following list will be generated:
* <pre>C, B, A, C, B, A, C, B, A</pre>, it should result in
* <pre>A, A, A, B, B, B, C, C, C</pre>.
*
* @param materials The pre-sorted list of materials to check sorting for.
*/
private void testSort(Material... materials) {
GeometryList gl = new GeometryList(comparator);
for (int g = 0; g < 5; g++) {
for (int i = materials.length - 1; i >= 0; i--) {
Geometry geom = new Geometry("geom", mesh);
Material clonedMaterial = materials[i].clone();
if (materials[i].getActiveTechnique() != null) {
String techniqueName = materials[i].getActiveTechnique().getDef().getName();
clonedMaterial.selectTechnique(techniqueName, renderManager);
} else {
clonedMaterial.selectTechnique(TechniqueDef.DEFAULT_TECHNIQUE_NAME, renderManager);
}
geom.setMaterial(clonedMaterial);
gl.add(geom);
}
}
gl.sort();
for (int i = 0; i < gl.size(); i++) {
Material mat = gl.get(i).getMaterial();
String sortId = Integer.toHexString(mat.getSortId()).toUpperCase();
System.out.print(sortId + "\t");
System.out.println(mat);
}
Set<String> alreadySeen = new HashSet<String>();
Material current = null;
for (int i = 0; i < gl.size(); i++) {
Material mat = gl.get(i).getMaterial();
if (current == null) {
current = mat;
} else if (!current.getName().equals(mat.getName())) {
assert !alreadySeen.contains(mat.getName());
alreadySeen.add(current.getName());
current = mat;
}
}
for (int i = 0; i < materials.length; i++) {
for (int g = 0; g < 5; g++) {
int index = i * 5 + g;
Material mat1 = gl.get(index).getMaterial();
Material mat2 = materials[i];
assert mat1.getName().equals(mat2.getName()) : mat1.getName() + " != " + mat2.getName() + " for index " + index;
}
}
}
use of com.jme3.renderer.opengl.GL in project jmonkeyengine by jMonkeyEngine.
the class JoglContext method determineMaxSamples.
protected int determineMaxSamples(int requestedSamples) {
GL gl = GLContext.getCurrentGL();
if (gl.hasFullFBOSupport()) {
return gl.getMaxRenderbufferSamples();
} else {
if (gl.isExtensionAvailable("GL_ARB_framebuffer_object") || gl.isExtensionAvailable("GL_EXT_framebuffer_multisample")) {
IntBuffer intBuf1 = IntBuffer.allocate(1);
gl.glGetIntegerv(GL2GL3.GL_MAX_SAMPLES, intBuf1);
return intBuf1.get(0);
} else {
return Integer.MAX_VALUE;
}
}
}
Aggregations