[DEV] rework texture and shaders

This commit is contained in:
Edouard DUPIN 2021-05-24 13:46:19 +02:00
parent ccd988640d
commit 43ea0339f1
16 changed files with 499 additions and 28 deletions

View File

@ -32,4 +32,5 @@ open module org.atriasoft.gale {
requires transitive pngdecoder; requires transitive pngdecoder;
requires transitive lwjgl3.awt; requires transitive lwjgl3.awt;
requires io.scenarium.logger; requires io.scenarium.logger;
requires org.atriasoft.iogami;
} }

View File

@ -0,0 +1,5 @@
package org.atriasoft.gale;
public enum TextureFilter {
LINEAR, NEAREST
}

View File

@ -23,6 +23,7 @@ import org.atriasoft.etk.math.Vector2f;
import org.atriasoft.etk.math.Vector2i; import org.atriasoft.etk.math.Vector2i;
import org.atriasoft.etk.math.Vector3f; import org.atriasoft.etk.math.Vector3f;
import org.atriasoft.etk.math.Vector3i; import org.atriasoft.etk.math.Vector3i;
import org.atriasoft.etk.math.Vector4f;
import org.atriasoft.gale.internal.Log; import org.atriasoft.gale.internal.Log;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
@ -124,17 +125,17 @@ public class OpenGL {
} }
public enum RenderMode { public enum RenderMode {
point, line, lineStrip, // !< Not supported in GALE (TODO Later) POINT, LINE, LINE_STRIP, // !< Not supported in GALE (TODO Later)
lineLoop, triangle, triangleStrip, // !< Not supported in GALE (TODO Later) LINE_LOOP, TRIANGLE, TRIANGLE_STRIP, // !< Not supported in GALE (TODO Later)
triangleFan, // !< Not supported in GALE (TODO Later) TRIANGLE_FAN, // !< Not supported in GALE (TODO Later)
quad, // !< Not supported in OpenGL-ES2 QUAD, // !< Not supported in OpenGL-ES2
quadStrip, // !< Not supported in OpenGL-ES2 QUAD_STRIP, // !< Not supported in OpenGL-ES2
polygon // !< Not supported in OpenGL-ES2 POLYGON // !< Not supported in OpenGL-ES2
} }
/* Shader wrapping : */ /* Shader wrapping : */
public static enum ShaderType { public static enum ShaderType {
vertex, fragment VERTEX, FRAGMENT
}; };
public static class StateFlag { public static class StateFlag {
@ -169,9 +170,9 @@ public class OpenGL {
private static Matrix4f matrixCamera = Matrix4f.IDENTITY; private static Matrix4f matrixCamera = Matrix4f.IDENTITY;
private static int programId = 0; private static int programId = 0;
private static final Map<RenderMode, Integer> CONVERT_RENDER_MODE = Map.of(RenderMode.point, GL11.GL_POINTS, RenderMode.line, GL11.GL_LINES, RenderMode.lineStrip, GL11.GL_LINE_STRIP, private static final Map<RenderMode, Integer> CONVERT_RENDER_MODE = Map.of(RenderMode.POINT, GL11.GL_POINTS, RenderMode.LINE, GL11.GL_LINES, RenderMode.LINE_STRIP, GL11.GL_LINE_STRIP,
RenderMode.lineLoop, GL11.GL_LINE_LOOP, RenderMode.triangle, GL11.GL_TRIANGLES, RenderMode.triangleStrip, GL11.GL_TRIANGLE_STRIP, RenderMode.triangleFan, GL11.GL_TRIANGLE_FAN, RenderMode.LINE_LOOP, GL11.GL_LINE_LOOP, RenderMode.TRIANGLE, GL11.GL_TRIANGLES, RenderMode.TRIANGLE_STRIP, GL11.GL_TRIANGLE_STRIP, RenderMode.TRIANGLE_FAN, GL11.GL_TRIANGLE_FAN,
RenderMode.quad, GL11.GL_QUADS, RenderMode.quadStrip, GL11.GL_QUAD_STRIP, RenderMode.polygon, GL11.GL_POLYGON); RenderMode.QUAD, GL11.GL_QUADS, RenderMode.QUAD_STRIP, GL11.GL_QUAD_STRIP, RenderMode.POLYGON, GL11.GL_POLYGON);
private static final Map<Flag, Integer> BASIC_FLAG; private static final Map<Flag, Integer> BASIC_FLAG;
private static boolean flagsStatesChange = false; private static boolean flagsStatesChange = false;
@ -795,6 +796,10 @@ public class OpenGL {
GL20.glUniform3i(location, value.x(), value.y(), value.z()); GL20.glUniform3i(location, value.x(), value.y(), value.z());
} }
public static void programLoadUniformVector(final int location, final Vector4f value) {
GL20.glUniform4f(location, value.x(), value.y(), value.z(), value.w());
}
public static void programRemove(final int prog) { public static void programRemove(final int prog) {
if (prog < 0) { if (prog < 0) {
return; return;
@ -999,10 +1004,10 @@ public class OpenGL {
private static int shaderCreate(final ShaderType type) { private static int shaderCreate(final ShaderType type) {
int shaderId = 0; int shaderId = 0;
if (type == ShaderType.vertex) { if (type == ShaderType.VERTEX) {
Log.verbose("create shader: VERTEX"); Log.verbose("create shader: VERTEX");
shaderId = GL20.glCreateShader(GL20.GL_VERTEX_SHADER); shaderId = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
} else if (type == ShaderType.fragment) { } else if (type == ShaderType.FRAGMENT) {
Log.verbose("create shader: FRAGMENT"); Log.verbose("create shader: FRAGMENT");
shaderId = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER); shaderId = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
} else { } else {

View File

@ -37,6 +37,7 @@ public abstract class GaleContext {
/** /**
* From everyware in the program, we can get the context inteface. * From everyware in the program, we can get the context inteface.
* @return current reference on the instance. * @return current reference on the instance.
* @note For test create a ``` new GaleContextTest()``` ... this permit to run some test...
*/ */
public static GaleContext getContext() { public static GaleContext getContext() {
return globalContext; return globalContext;

View File

@ -0,0 +1,22 @@
package org.atriasoft.gale.context;
import org.atriasoft.gale.GaleApplication;
class GaleApplicationTest extends GaleApplication {
}
public class GaleContextTest extends GaleContext {
public GaleContextTest() {
super(new GaleApplicationTest(), new String[0]);
setContext(this);
}
@Override
public int run() {
// TODO Auto-generated method stub
return 0;
}
}

View File

@ -30,12 +30,20 @@ public abstract class Resource {
} }
protected Resource(final String name) { protected Resource(final String name) {
//if (name == null) {
// this.name = "---";
//} else {
this.name = name; this.name = name;
//}
getManager().localAdd(this); getManager().localAdd(this);
} }
protected Resource(final Uri uri) { protected Resource(final Uri uri) {
//if (uri == null) {
// this.name = "---";
//} else {
this.name = uri.toString(); this.name = uri.toString();
//}
getManager().localAdd(this); getManager().localAdd(this);
} }

View File

@ -87,7 +87,7 @@ public class ResourceColored3DObject extends Resource {
OpenGL.enable(OpenGL.Flag.flag_blend); OpenGL.enable(OpenGL.Flag.flag_blend);
} }
// Request the draw of the elements: // Request the draw of the elements:
OpenGL.drawArrays(OpenGL.RenderMode.triangle, 0, vertices.size()); OpenGL.drawArrays(OpenGL.RenderMode.TRIANGLE, 0, vertices.size());
if (color.a() < 1.0f) { if (color.a() < 1.0f) {
OpenGL.disable(OpenGL.Flag.flag_blend); OpenGL.disable(OpenGL.Flag.flag_blend);
} }
@ -135,7 +135,7 @@ public class ResourceColored3DObject extends Resource {
OpenGL.enable(OpenGL.Flag.flag_blend); OpenGL.enable(OpenGL.Flag.flag_blend);
} }
// Request the draw of the elements: // Request the draw of the elements:
OpenGL.drawArrays(OpenGL.RenderMode.triangle, 0, vertices.size()); OpenGL.drawArrays(OpenGL.RenderMode.TRIANGLE, 0, vertices.size());
if (color.a() < 1.0f) { if (color.a() < 1.0f) {
OpenGL.disable(OpenGL.Flag.flag_blend); OpenGL.disable(OpenGL.Flag.flag_blend);
} }
@ -425,7 +425,7 @@ public class ResourceColored3DObject extends Resource {
OpenGL.enable(OpenGL.Flag.flag_blend); OpenGL.enable(OpenGL.Flag.flag_blend);
} }
// Request the draw od the elements: // Request the draw od the elements:
OpenGL.drawArrays(OpenGL.RenderMode.line, 0, vertices.size()); OpenGL.drawArrays(OpenGL.RenderMode.LINE, 0, vertices.size());
if (color.a() < 1.0f) { if (color.a() < 1.0f) {
OpenGL.disable(OpenGL.Flag.flag_blend); OpenGL.disable(OpenGL.Flag.flag_blend);
} }

View File

@ -163,7 +163,6 @@ public class ResourceManager {
Log.error("Request update after application EXIT ..."); Log.error("Request update after application EXIT ...");
return; return;
} }
Log.debug("Update context : " + this.resourceListToUpdate.size());
// TODO Check the number of call this ... Log.info("update open-gl context ... "); // TODO Check the number of call this ... Log.info("update open-gl context ... ");
if (this.contextHasBeenRemoved) { if (this.contextHasBeenRemoved) {
// need to update all ... // need to update all ...
@ -174,7 +173,7 @@ public class ResourceManager {
synchronized (this.resourceList) { synchronized (this.resourceList) {
if (this.resourceList.size() != 0) { if (this.resourceList.size() != 0) {
for (long jjj = 0; jjj < MAX_RESOURCE_LEVEL; jjj++) { for (long jjj = 0; jjj < MAX_RESOURCE_LEVEL; jjj++) {
Log.warning(" updateContext level (D) : " + jjj + "/" + (MAX_RESOURCE_LEVEL - 1)); Log.verbose(" updateContext level (D) : " + jjj + "/" + (MAX_RESOURCE_LEVEL - 1));
for (final Resource it : this.resourceList) { for (final Resource it : this.resourceList) {
if (jjj == it.getResourceLevel()) { if (jjj == it.getResourceLevel()) {
//Log.debug("Update context named : " + lresourceList[iii].getName()); //Log.debug("Update context named : " + lresourceList[iii].getName());
@ -197,7 +196,7 @@ public class ResourceManager {
} }
if (resourceListToUpdate.size() != 0) { if (resourceListToUpdate.size() != 0) {
for (long jjj = 0; jjj < MAX_RESOURCE_LEVEL; jjj++) { for (long jjj = 0; jjj < MAX_RESOURCE_LEVEL; jjj++) {
Log.warning(" updateContext level (U) : " + jjj + "/" + (MAX_RESOURCE_LEVEL - 1)); Log.verbose(" updateContext level (U) : " + jjj + "/" + (MAX_RESOURCE_LEVEL - 1));
for (final Resource it : resourceListToUpdate) { for (final Resource it : resourceListToUpdate) {
if (jjj == it.getResourceLevel()) { if (jjj == it.getResourceLevel()) {
if (!it.updateContext()) { if (!it.updateContext()) {

View File

@ -11,6 +11,7 @@ import org.atriasoft.etk.math.Vector2f;
import org.atriasoft.etk.math.Vector2i; import org.atriasoft.etk.math.Vector2i;
import org.atriasoft.etk.math.Vector3f; import org.atriasoft.etk.math.Vector3f;
import org.atriasoft.etk.math.Vector3i; import org.atriasoft.etk.math.Vector3i;
import org.atriasoft.etk.math.Vector4f;
import org.atriasoft.gale.backend3d.OpenGL; import org.atriasoft.gale.backend3d.OpenGL;
import org.atriasoft.gale.internal.Log; import org.atriasoft.gale.internal.Log;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
@ -33,9 +34,9 @@ public class ResourceProgram extends Resource {
final String name = uriVertexShader.getValue() + "<-->" + uriFragmentShader.getValue(); final String name = uriVertexShader.getValue() + "<-->" + uriFragmentShader.getValue();
Resource resource2 = getManager().localKeep(name); Resource resource2 = getManager().localKeep(name);
if (resource2 != null) { if (resource2 != null) {
if (resource2 instanceof ResourceProgram ploppp) { if (resource2 instanceof ResourceProgram) {
resource2.keep(); resource2.keep();
return ploppp; return (ResourceProgram) resource2;
} }
Log.critical("Request resource file : '" + name + "' With the wrong type (dynamic cast error)"); Log.critical("Request resource file : '" + name + "' With the wrong type (dynamic cast error)");
return null; return null;
@ -805,6 +806,20 @@ public class ResourceProgram extends Resource {
OpenGL.programLoadUniformVector(this.elementList.get(idElem).elementId, value); OpenGL.programLoadUniformVector(this.elementList.get(idElem).elementId, value);
} }
public void uniformVector(final int idElem, final Vector4f value) {
if (!this.exist) {
return;
}
if (idElem < 0 || (long) idElem > this.elementList.size()) {
Log.error("idElem = " + idElem + " not in [0.." + (this.elementList.size() - 1) + "]");
return;
}
if (!this.elementList.get(idElem).isLinked) {
return;
}
OpenGL.programLoadUniformVector(this.elementList.get(idElem).elementId, value);
}
/** /**
* Stop the processing of this program * Stop the processing of this program
*/ */

View File

@ -47,12 +47,12 @@ public class ResourceShader extends Resource {
// load data from file "all the time ..." // load data from file "all the time ..."
if (uri.get().endsWith(".frag")) { if (uri.get().endsWith(".frag")) {
this.type = ShaderType.fragment; this.type = ShaderType.FRAGMENT;
} else if (uri.get().endsWith(".vert")) { } else if (uri.get().endsWith(".vert")) {
this.type = ShaderType.vertex; this.type = ShaderType.VERTEX;
} else { } else {
Log.error("File does not have extention '.vert' for Vertex Shader or '.frag' for Fragment Shader. but : \"" + uri + "\""); Log.error("File does not have extention '.vert' for Vertex Shader or '.frag' for Fragment Shader. but : \"" + uri + "\"");
this.type = ShaderType.vertex; this.type = ShaderType.VERTEX;
return; return;
} }
reload(); reload();

View File

@ -13,6 +13,7 @@ import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13; import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL30; import org.lwjgl.opengl.GL30;
@Deprecated
public class ResourceTexture extends Resource { public class ResourceTexture extends Resource {
public enum TextureColorMode { public enum TextureColorMode {
rgb, //!< red/green/blue data rgb, //!< red/green/blue data

View File

@ -0,0 +1,295 @@
/** @file
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
package org.atriasoft.gale.resource;
import org.atriasoft.egami.ImageByte;
import org.atriasoft.egami.ImageByteRGBA;
import org.atriasoft.etk.Tools;
import org.atriasoft.etk.Uri;
import org.atriasoft.etk.math.Vector2i;
import org.atriasoft.gale.TextureFilter;
import org.atriasoft.gale.backend3d.OpenGL;
import org.atriasoft.gale.internal.Log;
public class ResourceTexture2 extends Resource {
public enum TextureColorMode {
rgb, // !< red/green/blue data
rgba // !< red/green/blue/alpha data
}
public static ResourceTexture2 create() {
Log.verbose("KEEP: Resource Texture Dynamic: ");
return new ResourceTexture2();
}
public static ResourceTexture2 create(final Uri uri) {
Log.verbose("KEEP: Resource Texture: " + uri);
final Resource object2 = Resource.getManager().localKeep(uri);
if (object2 != null) {
if (object2 instanceof ResourceTexture2 tmpp) {
return tmpp;
}
Log.critical("Request resource file : '" + uri + "' With the wrong type (dynamic cast error)");
return null;
}
Log.debug("CREATE: new Texture: " + uri);
return new ResourceTexture2(uri);
}
public static ResourceTexture2 createNamed(final String uri) {
Log.verbose("KEEP: Resource Texture Named: " + uri);
final Resource object2 = Resource.getManager().localKeep(uri);
if (object2 != null) {
if (object2 instanceof ResourceTexture2 tmpp) {
return tmpp;
}
Log.critical("Request resource file : '" + uri + "' With the wrong type (dynamic cast error)");
return null;
}
Log.debug("CREATE: new Texture Named: " + uri);
return new ResourceTexture2(uri);
}
/*
* public static ResourceTexture2 createFromPng(final Uri uriTexture) { return
* createFromPng(uriTexture, 1); }
*
* public static ResourceTexture2 createFromPng(final Uri uriTexture, final int
* textureUnit) { ResourceTexture2 resource; Resource resource2; final String
* name = uriTexture.getValue(); if (name.isEmpty() == false && name != "---") {
* resource2 = getManager().localKeep(name); } else {
* Log.error("Can not create a shader without a filaname"); return null; } if
* (resource2 != null) { if (resource2 instanceof ResourceTexture2) {
* resource2.keep(); return (ResourceTexture2) resource2; }
* Log.critical("Request resource file : '" + name +
* "' With the wrong type (dynamic cast error)"); return null; } resource = new
* ResourceTexture2(uriTexture, textureUnit); final ImageRawData decodedData =
* ImageLoader.decodePngFile(uriTexture);
* resource.setTexture(decodedData.getBuffer(), new
* Vector2i(decodedData.getWidth(), decodedData.getHeight()),
* (decodedData.isHasAlpha() == true ? TextureColorMode.rgba :
* TextureColorMode.rgb), textureUnit); resource.flush(); return resource; }
*/
// openGl Context properties :
protected ImageByte data = new ImageByteRGBA(32, 32);
// !< Color space of the image.
private final TextureColorMode dataColorSpace = TextureColorMode.rgba;
// Filter apply at the image when rendering it
protected TextureFilter filter = TextureFilter.LINEAR;
// ! Last loaded size in the system openGL
protected Vector2i lastSize = new Vector2i(1, 1);
protected int lastSizeObject = 0;
protected int lastTypeObject = 0;
// internal state of the openGl system.
protected boolean loaded = false;
// ! some image are not square == > we need to sqared it to prevent some openGl
// api error the the displayable size is not all the time 0.0 . 1.0
protected Vector2i realImageSize = new Vector2i(1, 1);
// repeat mode of the image (repeat the image if out of range [0..1])
protected boolean repeat = false;
protected int texId = -1; // !< openGl textureID.
public ResourceTexture2() {}
public ResourceTexture2(final String filename) {
super(filename);
}
/*
* public void bindForRendering(final int idTexture) { if (this.loaded == false)
* { return; } GL13.glActiveTexture(textureIdBinding[idTexture]);
* GL11.glBindTexture(GL11.GLTEXTURE2D, this.texId); if (this.dataColorSpace
* == TextureColorMode.rgb) { OpenGL.enable(OpenGL.Flag.flagcullFace);
* OpenGL.enable(OpenGL.Flag.flagback); } }
*/
public ResourceTexture2(final Uri filename) {
super(filename);
}
public void bindForRendering(final int idTexture) {
if (!this.loaded) {
return;
}
OpenGL.activeTexture(idTexture);
OpenGL.bindTexture2D(this.texId);
if (this.dataColorSpace == TextureColorMode.rgb) {
OpenGL.enable(OpenGL.Flag.flag_cullFace);
OpenGL.enable(OpenGL.Flag.flag_back);
}
}
@Override
public void cleanUp() {
removeContext();
}
// Flush the data to send it at the openGl system
public synchronized void flush() {
// request to the manager to be call at the next update ...
Log.verbose("Request UPDATE of Element");
Resource.getManager().update(this);
}
// Get the reference on this image to draw something on it ...
public ImageByte get() {
return this.data;
}
public Vector2i getOpenGlSize() {
return this.data.getSize();
}
public int getRendererId() {
return this.texId;
}
public Vector2i getUsableSize() {
return this.realImageSize;
}
@Override
public synchronized void removeContext() {
if (this.loaded) {
// Request remove texture ...
Log.info("TEXTURE: Rm [" + getId() + "] texId=" + this.texId);
// TODO Check if we are in the correct thread
OpenGL.glDeleteTextures(this.texId);
this.loaded = false;
}
}
@Override
public synchronized void removeContextToLate() {
this.loaded = false;
this.texId = -1;
}
/**
* Set the image in the texture system
* @note It will resize in square2 if needed by the system.
* @param image Image to set.
*/
public synchronized void set(final ImageByte image) {
Log.debug("Set a new image in a texture:");
Log.debug(" size=" + image.getSize());
this.data = image;
this.realImageSize = this.data.getSize();
final Vector2i compatibilityHWSize = new Vector2i(Tools.nextP2(this.realImageSize.x()), Tools.nextP2(this.realImageSize.y()));
if (!this.realImageSize.equals(compatibilityHWSize)) {
Log.verbose("RESIZE Image for HArwareCompatibility:" + this.realImageSize + " => " + compatibilityHWSize);
this.data.resize(compatibilityHWSize.x(), compatibilityHWSize.y());
}
flush();
}
/**
* Set the Filter mode to apply at the image when display with a scale
* (not 1:1 ratio)
* @param filter Value of the new filter mode
*/
public void setFilterMode(final TextureFilter filter) {
this.filter = filter;
}
// You must set the size here, because it will be set in multiple of pow(2)
public synchronized void setImageSize(Vector2i newSize) {
newSize = new Vector2i(Tools.nextP2(newSize.x()), Tools.nextP2(newSize.y()));
this.data.resize(newSize.x(), newSize.y());
}
/**
* Set the repeate mode of the images if UV range is out of [0..1]
* @param value Value of the new repeate mode
*/
public void setRepeat(final boolean value) {
this.repeat = value;
}
public void unBindForRendering() {
if (!this.loaded) {
return;
}
if (this.dataColorSpace == TextureColorMode.rgb) {
OpenGL.disable(OpenGL.Flag.flag_cullFace);
OpenGL.disable(OpenGL.Flag.flag_back);
}
}
@Override
public synchronized boolean updateContext() {
Log.verbose("updateContext [START]");
//final Steady tic = Steady.now();
/*
* TODO : use unlockable synchronized ... if (lock.tryLock() == false) { //Lock
* error ==> try later ... return false; }
*/
final int typeObject = this.data.hasAlpha() ? OpenGL.GL_RGBA : OpenGL.GL_RGB;
final int sizeObject = OpenGL.GL_UNSIGNED_BYTE;
if (this.loaded) {
if (this.lastTypeObject != typeObject || this.lastSizeObject != sizeObject || !this.lastSize.equals(this.data.getSize())) {
Log.warning("TEXTURE: Rm [" + getId() + "] texId=" + this.texId);
OpenGL.glDeleteTextures(this.texId);
this.loaded = false;
}
}
if (!this.loaded) {
// Request a new texture at openGl :
this.texId = OpenGL.glGenTextures();
this.lastSize = this.data.getSize();
this.lastTypeObject = typeObject;
this.lastSizeObject = sizeObject;
Log.debug("TEXTURE: add [" + getId() + "]=" + this.data.getSize() + "=>" + this.data.getGPUSize() + " OGlId=" + this.texId + " type=" + this.data.getClass().getCanonicalName());
} else {
Log.debug("TEXTURE: update [" + getId() + "]=" + this.data.getSize() + "=>" + this.data.getGPUSize() + " OGlId=" + this.texId + " type=" + this.data.getClass().getCanonicalName());
}
// in all case we set the texture properties :
// TODO check error ???
OpenGL.bindTexture2D(this.texId);
if (!this.loaded) {
if (!this.repeat) {
OpenGL.setTexture2DWrapClampToEdge();
} else {
OpenGL.setTexture2DWrapRepeat();
}
if (this.filter == TextureFilter.LINEAR) {
OpenGL.setTexture2DFilterLinear();
} else {
OpenGL.setTexture2DFilterNearest();
}
}
// glPixelStorei(GLUNPACKALIGNMENT,1);
//final Steady toc1 = Steady.now();
//Log.verbose(" BIND ==> " + toc1.less(tic));
// egami::store(this.data, String("~/texture") + etk::toString(getId()) + ".bmp");
if (!this.loaded) {
OpenGL.glTexImage2D(0, // Level
typeObject, // Format internal
this.data.getWidth(), this.data.getHeight(), 0, // Border
typeObject, // format
sizeObject, // type
this.data.getRaw());
} else {
OpenGL.glTexSubImage2D(0, // Level
0, // x offset
0, // y offset
this.data.getWidth(), this.data.getHeight(), typeObject, // format
sizeObject, // type
this.data.getRaw());
}
// now the data is loaded
this.loaded = true;
// final Steady toc = Steady.now();
// Log.error(" updateContext [STOP] ==> " + (toc - toc1));
return true;
}
}

View File

@ -0,0 +1,101 @@
/** @file
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
package org.atriasoft.gale.resource;
import org.atriasoft.egami.ImageByte;
import org.atriasoft.etk.Tools;
import org.atriasoft.etk.Uri;
import org.atriasoft.etk.math.Vector2i;
import org.atriasoft.gale.internal.Log;
import org.atriasoft.iogami.IOgami;
// TODO : Change tis file name ...
public class ResourceTextureFile extends ResourceTexture2 {
public static Vector2i sizeAuto = new Vector2i(-1, -1);
public static Vector2i sizeDefault = new Vector2i(0, 0);
public static ResourceTextureFile create(final Uri filename) {
return ResourceTextureFile.create(filename, ResourceTextureFile.sizeAuto);
}
public static ResourceTextureFile create(final Uri filename, final Vector2i size) {
return ResourceTextureFile.create(filename, size, ResourceTextureFile.sizeAuto);
}
/**
* keep the resource pointer.
* @note Never free this pointer by your own...
* @param uri Name of the image file.
* @param size size of the image (usefull when loading .svg to
* automatic rescale)
* @param sizeRegister size register in named (When you preaload the images
* the size write here will be )
* @return pointer on the resource or null if an error occured.
*/
public static ResourceTextureFile create(final Uri uri, final Vector2i inSize, final Vector2i sizeRegister) {
Log.verbose("KEEP: TextureFile: '" + uri + "' size=" + inSize + " sizeRegister=" + sizeRegister);
Vector2i size = inSize;
if (uri == null) {
return new ResourceTextureFile();
}
if (size.x() == 0) {
size = size.withX(-1);
// Log.error("Error Request the image size.x() =0 ???");
}
if (size.y() == 0) {
size = size.withY(-1);
// Log.error("Error Request the image size.y() =0 ???");
}
if (!uri.getExtention().toLowerCase().contentEquals("svg")) {
size = ResourceTextureFile.sizeAuto;
}
if (size.x() > 0 && size.y() > 0) {
Log.verbose(" == > specific size : " + size);
size = new Vector2i(Tools.nextP2(size.x()), Tools.nextP2(size.y()));
if (!sizeRegister.equals(ResourceTextureFile.sizeAuto)) {
if (!sizeRegister.equals(ResourceTextureFile.sizeDefault)) {
// tmpFilename.getQuery().set("x", "" + size.x));
// tmpFilename.getQuery().set("y", "" + size.y));
}
}
}
Log.verbose("KEEP: TextureFile: '" + uri + "' new size=" + size);
final Resource object2 = Resource.getManager().localKeep(uri.toString());
if (object2 != null) {
if (object2 instanceof ResourceTextureFile out) {
object2.keep();
return out;
}
Log.critical("Request resource file : '" + uri + "' With the wrong type (dynamic cast error)");
return null;
}
Log.debug("CREATE: TextureFile: '" + uri + "' size=" + size);
// need to crate a new one ...
ResourceTextureFile object = new ResourceTextureFile(uri.toString(), uri, size);
Resource.getManager().localAdd(object);
return object;
}
protected ResourceTextureFile() {}
protected ResourceTextureFile(final String genName, final Uri uri, final Vector2i size) {
super(genName);
Log.debug("create a new resource::Image : genName=" + genName + " uri=" + uri + " size=" + size);
final ImageByte tmp = IOgami.load(uri, size);
if (tmp == null) {
Log.error("Can not load the file : " + uri);
return;
}
set(tmp);
}
public Vector2i getRealSize() {
return this.realImageSize;
}
}

View File

@ -299,6 +299,11 @@ public class ResourceVirtualArrayObject extends Resource {
this.colors = colors; this.colors = colors;
} }
public void setColors(final List<Color> colors) {
setColors(colors.toArray(Color[]::new));
}
public void setIndices(final int[] indices) { public void setIndices(final int[] indices) {
this.indices = indices; this.indices = indices;
} }
@ -312,6 +317,10 @@ public class ResourceVirtualArrayObject extends Resource {
this.normals = normals; this.normals = normals;
} }
public void setNormals(final List<Vector3f> normals) {
setNormals(normals.toArray(Vector3f[]::new));
}
public void setNormals(final Vector3f[] normals) { public void setNormals(final Vector3f[] normals) {
this.normals = normals; this.normals = normals;
} }
@ -320,6 +329,11 @@ public class ResourceVirtualArrayObject extends Resource {
this.positions = positions; this.positions = positions;
} }
public void setPosition(final List<Vector3f> outPosition) {
setPosition(outPosition.toArray(Vector3f[]::new));
}
public void setPosition(final Vector3f[] positions) { public void setPosition(final Vector3f[] positions) {
this.positions = positions; this.positions = positions;
} }
@ -328,6 +342,10 @@ public class ResourceVirtualArrayObject extends Resource {
this.textureCoordinates = textureCoordinates; this.textureCoordinates = textureCoordinates;
} }
public void setTextureCoordinate(final List<Vector2f> outTexturePosition) {
setTextureCoordinate(outTexturePosition.toArray(Vector2f[]::new));
}
public void setTextureCoordinate(final Vector2f[] textureCoordinates) { public void setTextureCoordinate(final Vector2f[] textureCoordinates) {
this.textureCoordinates = textureCoordinates; this.textureCoordinates = textureCoordinates;
} }

View File

@ -97,9 +97,9 @@ public class Sample1Application extends GaleApplication {
// Request the draw of the elements: // Request the draw of the elements:
if (TEST_STATIC_MODE) { if (TEST_STATIC_MODE) {
this.verticesVBO.render(OpenGL.RenderMode.triangle); this.verticesVBO.render(OpenGL.RenderMode.TRIANGLE);
} else { } else {
this.verticesVBO.renderArrays(OpenGL.RenderMode.triangle); this.verticesVBO.renderArrays(OpenGL.RenderMode.TRIANGLE);
} }
this.verticesVBO.unBindForRendering(); this.verticesVBO.unBindForRendering();
this.oGLprogram.unUse(); this.oGLprogram.unUse();

View File

@ -156,7 +156,7 @@ public class Sample2Application extends GaleApplication {
// update of flags is done asyncronously ==> need update befor drawing... // update of flags is done asyncronously ==> need update befor drawing...
OpenGL.updateAllFlags(); OpenGL.updateAllFlags();
// Request the draw od the elements: // Request the draw od the elements:
this.verticesVBO.render(OpenGL.RenderMode.triangle); this.verticesVBO.render(OpenGL.RenderMode.TRIANGLE);
this.verticesVBO.unBindForRendering(); this.verticesVBO.unBindForRendering();
this.texture.unBindForRendering(); this.texture.unBindForRendering();