Detatch client logic from opengl/openal calls

This commit is contained in:
austin 2023-04-11 21:27:09 -04:00
parent b167062585
commit 2fd8e77a2a
9 changed files with 199 additions and 150 deletions

View File

@ -30,7 +30,7 @@ public class ControlCallback implements GLFWKeyCallbackI {
} }
/** /**
* !!!WARNING!!!, will silently fail if * !!!WARNING!!!, will silently fail if opengl elementsn ot defined or keycode is outside of key value array size or is not in main rendering thread
* @param keycode * @param keycode
* @return * @return
*/ */

View File

@ -1167,14 +1167,14 @@ public class ControlHandler {
public void runHandlers(List<Control> controls){ public void runHandlers(List<Control> controls){
//construct mouse event //construct mouse event
glfwGetCursorPos(Globals.window, mouse_X_Buffer, mouse_Y_Buffer);
xpos = mouse_X_Buffer[0]; //Fills the buffer
ypos = mouse_Y_Buffer[0]; getMousePositionInBuffer();
float xoffset = (float) (xpos - mouse_lastX); float xoffset = (float) (xpos - mouse_lastX);
float yoffset = (float) (mouse_lastY - ypos); float yoffset = (float) (mouse_lastY - ypos);
mouse_lastX = (float) xpos; mouse_lastX = (float) xpos;
mouse_lastY = (float) ypos; mouse_lastY = (float) ypos;
MouseEvent currentMouseEvent = new MouseEvent((int)xpos,(int)ypos,(int)mouse_lastX,(int)mouse_lastY,(int)xoffset,(int)yoffset,Globals.mouseCallback.getButton(GLFW_MOUSE_BUTTON_1),Globals.mouseCallback.getButton(GLFW_MOUSE_BUTTON_2)); MouseEvent currentMouseEvent = new MouseEvent((int)xpos,(int)ypos,(int)mouse_lastX,(int)mouse_lastY,(int)xoffset,(int)yoffset,getButton1Raw(),getButton2Raw());
boolean mouseMoveEvent = xoffset != 0 || yoffset != 0; boolean mouseMoveEvent = xoffset != 0 || yoffset != 0;
@ -1238,6 +1238,31 @@ public class ControlHandler {
} }
} }
void getMousePositionInBuffer(){
//only if not headless, gather position
if(!Globals.HEADLESS){
glfwGetCursorPos(Globals.window, mouse_X_Buffer, mouse_Y_Buffer);
xpos = mouse_X_Buffer[0];
ypos = mouse_Y_Buffer[0];
}
}
boolean getButton1Raw(){
if(Globals.HEADLESS){
return false;
} else {
return Globals.mouseCallback.getButton(GLFW_MOUSE_BUTTON_1);
}
}
boolean getButton2Raw(){
if(Globals.HEADLESS){
return false;
} else {
return Globals.mouseCallback.getButton(GLFW_MOUSE_BUTTON_2);
}
}
public Control getControl(String controlName){ public Control getControl(String controlName){
return controls.get(controlName); return controls.get(controlName);
} }

View File

@ -23,7 +23,7 @@ public class MouseCallback extends GLFWMouseButtonCallback {
} }
/** /**
* !!!WARNING!!!, will silently fail if * !!!WARNING!!!, will silently fail if opengl elementsn ot defined or keycode is outside of key value array size or is not in main rendering thread
* @param keycode * @param keycode
* @return * @return
*/ */

View File

@ -113,8 +113,8 @@ public class Globals {
// //
public static ControlHandler controlHandler; public static ControlHandler controlHandler;
public static boolean updateCamera = true; public static boolean updateCamera = true;
public static ControlCallback controlCallback; public static ControlCallback controlCallback = new ControlCallback();
public static MouseCallback mouseCallback; public static MouseCallback mouseCallback = new MouseCallback();
// //

View File

@ -264,11 +264,11 @@ public class Main {
} }
if(Globals.RUN_CLIENT){ if(Globals.RUN_CLIENT && !Globals.HEADLESS){
Globals.renderingEngine.drawScreen(); Globals.renderingEngine.drawScreen();
} }
if(Globals.ENGINE_SHUTDOWN_FLAG || (Globals.RUN_CLIENT && glfwWindowShouldClose(Globals.window))){ if(Globals.ENGINE_SHUTDOWN_FLAG || (!Globals.HEADLESS && Globals.RUN_CLIENT && glfwWindowShouldClose(Globals.window))){
running = false; running = false;
} }
@ -289,7 +289,7 @@ public class Main {
// S H U T D O W N // S H U T D O W N
// //
//Terminate the program. //Terminate the program.
if(Globals.RUN_CLIENT){ if(!Globals.HEADLESS && Globals.RUN_CLIENT){
glfwTerminate(); glfwTerminate();
} }
//used to signal threads to stop //used to signal threads to stop
@ -299,7 +299,7 @@ public class Main {
Globals.serverThread.interrupt(); Globals.serverThread.interrupt();
} }
//shut down audio engine //shut down audio engine
if(Globals.RUN_CLIENT){ if(!Globals.HEADLESS && Globals.RUN_CLIENT){
Globals.audioEngine.shutdown(); Globals.audioEngine.shutdown();
} }
} }

View File

@ -113,8 +113,11 @@ public class Mesh {
// //
// VAO // VAO
// //
rVal.vertexArrayObject = glGenVertexArrays(); //Check for headless to not call gl functions when not running with gpu
glBindVertexArray(rVal.vertexArrayObject); if(!Globals.HEADLESS){
rVal.vertexArrayObject = glGenVertexArrays();
glBindVertexArray(rVal.vertexArrayObject);
}
@ -370,19 +373,20 @@ public class Mesh {
boneIndexDataBuffer.flip(); boneIndexDataBuffer.flip();
boneWeightDataBuffer.flip(); boneWeightDataBuffer.flip();
if(!Globals.HEADLESS){
rVal.boneWeightBuffer = glGenBuffers(); rVal.boneWeightBuffer = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, rVal.boneWeightBuffer); glBindBuffer(GL_ARRAY_BUFFER, rVal.boneWeightBuffer);
GL15.glBufferData(GL_ARRAY_BUFFER, boneWeightDataBuffer, GL_STATIC_DRAW); GL15.glBufferData(GL_ARRAY_BUFFER, boneWeightDataBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(2, 4, GL_FLOAT, false, 0, 0); glVertexAttribPointer(2, 4, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
rVal.boneIndexBuffer = glGenBuffers(); rVal.boneIndexBuffer = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, rVal.boneIndexBuffer); glBindBuffer(GL_ARRAY_BUFFER, rVal.boneIndexBuffer);
GL15.glBufferData(GL_ARRAY_BUFFER, boneIndexDataBuffer, GL_STATIC_DRAW); GL15.glBufferData(GL_ARRAY_BUFFER, boneIndexDataBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(3); glEnableVertexAttribArray(3);
}
} else { } else {
rVal.hasBones = false; rVal.hasBones = false;
} }
@ -397,8 +401,9 @@ public class Mesh {
if(!Globals.HEADLESS){
rVal.shader = ShaderProgram.smart_assemble_shader(has_bones, apply_lighting); rVal.shader = ShaderProgram.smart_assemble_shader(has_bones, apply_lighting);
}
@ -414,8 +419,9 @@ public class Mesh {
if(!Globals.HEADLESS){
glBindVertexArray(0); glBindVertexArray(0);
}
@ -435,58 +441,72 @@ public class Mesh {
public void buffer_vertices(FloatBuffer verticies, int vertexDimension){ public void buffer_vertices(FloatBuffer verticies, int vertexDimension){
vertexBuffer = glGenBuffers(); if(!Globals.HEADLESS){
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); vertexBuffer = glGenBuffers();
GL15.glBufferData(GL_ARRAY_BUFFER, verticies, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, vertexDimension, GL_FLOAT, false, 0, 0); GL15.glBufferData(GL_ARRAY_BUFFER, verticies, GL_STATIC_DRAW);
glEnableVertexAttribArray(0); glVertexAttribPointer(0, vertexDimension, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(0);
}
} }
public void buffer_normals(FloatBuffer normals, int normalDimension){ public void buffer_normals(FloatBuffer normals, int normalDimension){
normalBuffer = glGenBuffers(); if(!Globals.HEADLESS){
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer); normalBuffer = glGenBuffers();
GL15.glBufferData(GL_ARRAY_BUFFER, normals, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
glVertexAttribPointer(1, normalDimension, GL_FLOAT, false, 0, 0); GL15.glBufferData(GL_ARRAY_BUFFER, normals, GL_STATIC_DRAW);
glEnableVertexAttribArray(1); glVertexAttribPointer(1, normalDimension, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(1);
}
} }
public void buffer_faces(IntBuffer faces){ public void buffer_faces(IntBuffer faces){
elementArrayBuffer = glGenBuffers(); if(!Globals.HEADLESS){
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementArrayBuffer); elementArrayBuffer = glGenBuffers();
GL15.glBufferData(GL_ELEMENT_ARRAY_BUFFER, faces, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementArrayBuffer);
elementCount = faces.capacity(); GL15.glBufferData(GL_ELEMENT_ARRAY_BUFFER, faces, GL_STATIC_DRAW);
elementCount = faces.capacity();
}
} }
public void buffer_texture_coords(FloatBuffer coords, int textureDimension){ public void buffer_texture_coords(FloatBuffer coords, int textureDimension){
textureCoordBuffer = glGenBuffers(); if(!Globals.HEADLESS){
glBindBuffer(GL_ARRAY_BUFFER, textureCoordBuffer); textureCoordBuffer = glGenBuffers();
GL15.glBufferData(GL_ARRAY_BUFFER, coords, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, textureCoordBuffer);
glVertexAttribPointer(4, textureDimension, GL_FLOAT, false, 0, 0); GL15.glBufferData(GL_ARRAY_BUFFER, coords, GL_STATIC_DRAW);
glEnableVertexAttribArray(4); glVertexAttribPointer(4, textureDimension, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(4);
}
} }
public void bufferCustomFloatAttribArray(FloatBuffer buffer, int bufferDimension, int attribIndex){ public void bufferCustomFloatAttribArray(FloatBuffer buffer, int bufferDimension, int attribIndex){
int customBuffer = glGenBuffers(); if(!Globals.HEADLESS){
glBindBuffer(GL_ARRAY_BUFFER, customBuffer); int customBuffer = glGenBuffers();
GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, customBuffer);
glVertexAttribPointer(attribIndex, bufferDimension, GL_FLOAT, false, 0, 0); GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
glEnableVertexAttribArray(attribIndex); glVertexAttribPointer(attribIndex, bufferDimension, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(attribIndex);
}
} }
public void bufferCustomIntAttribArray(IntBuffer buffer, int bufferDimension, int attribIndex){ public void bufferCustomIntAttribArray(IntBuffer buffer, int bufferDimension, int attribIndex){
int customBuffer = glGenBuffers(); if(!Globals.HEADLESS){
glBindBuffer(GL_ARRAY_BUFFER, customBuffer); int customBuffer = glGenBuffers();
GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, customBuffer);
glVertexAttribPointer(attribIndex, bufferDimension, GL_INT, false, 0, 0); GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
glEnableVertexAttribArray(attribIndex); glVertexAttribPointer(attribIndex, bufferDimension, GL_INT, false, 0, 0);
glEnableVertexAttribArray(attribIndex);
}
} }
public void bufferCustomUIntAttribArray(IntBuffer buffer, int bufferDimension, int attribIndex){ public void bufferCustomUIntAttribArray(IntBuffer buffer, int bufferDimension, int attribIndex){
int customBuffer = glGenBuffers(); if(!Globals.HEADLESS){
glBindBuffer(GL_ARRAY_BUFFER, customBuffer); int customBuffer = glGenBuffers();
GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, customBuffer);
glVertexAttribPointer(attribIndex, bufferDimension, GL_UNSIGNED_INT, false, 0, 0); GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
glEnableVertexAttribArray(attribIndex); glVertexAttribPointer(attribIndex, bufferDimension, GL_UNSIGNED_INT, false, 0, 0);
glEnableVertexAttribArray(attribIndex);
}
} }
public void setTextureMask(ActorTextureMask textureMask){ public void setTextureMask(ActorTextureMask textureMask){

View File

@ -231,12 +231,13 @@ public class RenderingEngine {
Globals.WINDOW_WIDTH = bufferWidth; Globals.WINDOW_WIDTH = bufferWidth;
Globals.WINDOW_HEIGHT = bufferHeight; Globals.WINDOW_HEIGHT = bufferHeight;
//
// Attack controls callbacks
//
//set key callback //set key callback
Globals.controlCallback = new ControlCallback();
GLFW.glfwSetKeyCallback(Globals.window, Globals.controlCallback); GLFW.glfwSetKeyCallback(Globals.window, Globals.controlCallback);
//set mouse callback //set mouse callback
Globals.mouseCallback = new MouseCallback();
GLFW.glfwSetMouseButtonCallback(Globals.window, Globals.mouseCallback); GLFW.glfwSetMouseButtonCallback(Globals.window, Globals.mouseCallback);
//get title bar dimensions //get title bar dimensions

View File

@ -5,6 +5,7 @@
*/ */
package electrosphere.renderer.texture; package electrosphere.renderer.texture;
import electrosphere.engine.Globals;
import electrosphere.engine.Main; import electrosphere.engine.Main;
import electrosphere.util.FileUtils; import electrosphere.util.FileUtils;
import java.awt.Color; import java.awt.Color;
@ -42,93 +43,95 @@ public class Texture {
public Texture(String path){ public Texture(String path){
this.path = path; this.path = path;
//generate the texture object on gpu if(!Globals.HEADLESS){
texturePointer = glGenTextures(); //generate the texture object on gpu
//bind the new texture texturePointer = glGenTextures();
glBindTexture(GL_TEXTURE_2D, texturePointer); //bind the new texture
//how are we gonna wrap the texture?? glBindTexture(GL_TEXTURE_2D, texturePointer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); //how are we gonna wrap the texture??
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
//set the border color to black glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
float borderColor[] = { 0.0f, 0.0f, 0.0f, 1.0f }; //set the border color to black
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); float borderColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
//set magnification and minification operation sampling strategies glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //set magnification and minification operation sampling strategies
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//load the image here glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
ByteBuffer data; //load the image here
width = 1; ByteBuffer data;
height = 1; width = 1;
try { height = 1;
BufferedImage image_data = ImageIO.read(FileUtils.getAssetFile(path)); try {
if ( BufferedImage image_data = ImageIO.read(FileUtils.getAssetFile(path));
image_data.getType() == BufferedImage.TYPE_3BYTE_BGR || if (
image_data.getType() == BufferedImage.TYPE_INT_RGB image_data.getType() == BufferedImage.TYPE_3BYTE_BGR ||
){ image_data.getType() == BufferedImage.TYPE_INT_RGB
hasTransparency = false; ){
} else if( hasTransparency = false;
image_data.getType() == BufferedImage.TYPE_4BYTE_ABGR || } else if(
image_data.getType() == BufferedImage.TYPE_INT_ARGB image_data.getType() == BufferedImage.TYPE_4BYTE_ABGR ||
){ image_data.getType() == BufferedImage.TYPE_INT_ARGB
hasTransparency = true; ){
} hasTransparency = true;
width = image_data.getWidth();
height = image_data.getHeight();
if(hasTransparency){
data = BufferUtils.createByteBuffer(width * height * 4);
} else {
data = BufferUtils.createByteBuffer(width * height * 3);
}
/*
imgBuffer = BufferUtils.createByteBuffer(4 * dimX * dimY);
for(int x = 0; x < dimX; x++){
for(int y = 0; y < dimY; y++){
Color temp = new Color(image_data.getRGB(x, y));
data.put((byte)(temp.getRed());
data.put((byte)(temp.getGreen());
data.put((byte)(temp.getBlue());
data.put((byte)(temp.getAlpha());
} }
} width = image_data.getWidth();
imgBuffer.flip(); height = image_data.getHeight();
*/ if(hasTransparency){
for(int y = height - 1; y > -1; y--){ data = BufferUtils.createByteBuffer(width * height * 4);
for(int x = 0; x < width; x++){ } else {
Color temp = new Color(image_data.getRGB(x, y), true); data = BufferUtils.createByteBuffer(width * height * 3);
}
data.put((byte)temp.getRed()); /*
data.put((byte)temp.getGreen()); imgBuffer = BufferUtils.createByteBuffer(4 * dimX * dimY);
data.put((byte)temp.getBlue()); for(int x = 0; x < dimX; x++){
if(hasTransparency){ for(int y = 0; y < dimY; y++){
data.put((byte)temp.getAlpha()); Color temp = new Color(image_data.getRGB(x, y));
data.put((byte)(temp.getRed());
data.put((byte)(temp.getGreen());
data.put((byte)(temp.getBlue());
data.put((byte)(temp.getAlpha());
} }
// data[x * y * 3 + 0] = temp.getRed();
// data[x * y * 3 + 1] = temp.getGreen();
// data[x * y * 3 + 2] = temp.getBlue();
} }
imgBuffer.flip();
*/
for(int y = height - 1; y > -1; y--){
for(int x = 0; x < width; x++){
Color temp = new Color(image_data.getRGB(x, y), true);
data.put((byte)temp.getRed());
data.put((byte)temp.getGreen());
data.put((byte)temp.getBlue());
if(hasTransparency){
data.put((byte)temp.getAlpha());
}
// data[x * y * 3 + 0] = temp.getRed();
// data[x * y * 3 + 1] = temp.getGreen();
// data[x * y * 3 + 2] = temp.getBlue();
}
}
} catch (IOException ex) {
ex.printStackTrace();
hasTransparency = false;
data = BufferUtils.createByteBuffer(3);
data.put((byte)0);
data.put((byte)0);
data.put((byte)0);
} }
} catch (IOException ex) { data.flip();
ex.printStackTrace(); //call if width != height so opengl figures out how to unpack it properly
hasTransparency = false; if(width != height){
data = BufferUtils.createByteBuffer(3); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
data.put((byte)0); }
data.put((byte)0); //buffer the texture information
data.put((byte)0); if(hasTransparency){
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
} else {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
glGenerateMipmap(GL_TEXTURE_2D);
//OPTIONAL free the original image data now that it's on the gpu
// System.gc();
} }
data.flip();
//call if width != height so opengl figures out how to unpack it properly
if(width != height){
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
//buffer the texture information
if(hasTransparency){
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
} else {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
glGenerateMipmap(GL_TEXTURE_2D);
//OPTIONAL free the original image data now that it's on the gpu
System.gc();
} }
public void bind(){ public void bind(){

View File

@ -15,7 +15,7 @@ public class SpawningCreaturesTest {
@Before @Before
public void initEngine(){ public void initEngine(){
System.out.println("[Test] Spawn many creatures"); System.out.println("[Test] Spawn many creatures");
Globals.RUN_CLIENT = false; Globals.RUN_CLIENT = true;
Globals.RUN_SERVER = true; Globals.RUN_SERVER = true;
Globals.HEADLESS = true; Globals.HEADLESS = true;
NetUtils.setPort(0); NetUtils.setPort(0);