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
* @return
*/

View File

@ -1167,14 +1167,14 @@ public class ControlHandler {
public void runHandlers(List<Control> controls){
//construct mouse event
glfwGetCursorPos(Globals.window, mouse_X_Buffer, mouse_Y_Buffer);
xpos = mouse_X_Buffer[0];
ypos = mouse_Y_Buffer[0];
//Fills the buffer
getMousePositionInBuffer();
float xoffset = (float) (xpos - mouse_lastX);
float yoffset = (float) (mouse_lastY - ypos);
mouse_lastX = (float) xpos;
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;
@ -1237,6 +1237,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){
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
* @return
*/

View File

@ -113,8 +113,8 @@ public class Globals {
//
public static ControlHandler controlHandler;
public static boolean updateCamera = true;
public static ControlCallback controlCallback;
public static MouseCallback mouseCallback;
public static ControlCallback controlCallback = new ControlCallback();
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();
}
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;
}
@ -289,7 +289,7 @@ public class Main {
// S H U T D O W N
//
//Terminate the program.
if(Globals.RUN_CLIENT){
if(!Globals.HEADLESS && Globals.RUN_CLIENT){
glfwTerminate();
}
//used to signal threads to stop
@ -299,7 +299,7 @@ public class Main {
Globals.serverThread.interrupt();
}
//shut down audio engine
if(Globals.RUN_CLIENT){
if(!Globals.HEADLESS && Globals.RUN_CLIENT){
Globals.audioEngine.shutdown();
}
}

View File

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

View File

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

View File

@ -5,6 +5,7 @@
*/
package electrosphere.renderer.texture;
import electrosphere.engine.Globals;
import electrosphere.engine.Main;
import electrosphere.util.FileUtils;
import java.awt.Color;
@ -42,93 +43,95 @@ public class Texture {
public Texture(String path){
this.path = path;
//generate the texture object on gpu
texturePointer = glGenTextures();
//bind the new texture
glBindTexture(GL_TEXTURE_2D, texturePointer);
//how are we gonna wrap the texture??
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
//set the border color to black
float borderColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
//set magnification and minification operation sampling strategies
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//load the image here
ByteBuffer data;
width = 1;
height = 1;
try {
BufferedImage image_data = ImageIO.read(FileUtils.getAssetFile(path));
if (
image_data.getType() == BufferedImage.TYPE_3BYTE_BGR ||
image_data.getType() == BufferedImage.TYPE_INT_RGB
){
hasTransparency = false;
} else if(
image_data.getType() == BufferedImage.TYPE_4BYTE_ABGR ||
image_data.getType() == BufferedImage.TYPE_INT_ARGB
){
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());
if(!Globals.HEADLESS){
//generate the texture object on gpu
texturePointer = glGenTextures();
//bind the new texture
glBindTexture(GL_TEXTURE_2D, texturePointer);
//how are we gonna wrap the texture??
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
//set the border color to black
float borderColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
//set magnification and minification operation sampling strategies
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//load the image here
ByteBuffer data;
width = 1;
height = 1;
try {
BufferedImage image_data = ImageIO.read(FileUtils.getAssetFile(path));
if (
image_data.getType() == BufferedImage.TYPE_3BYTE_BGR ||
image_data.getType() == BufferedImage.TYPE_INT_RGB
){
hasTransparency = false;
} else if(
image_data.getType() == BufferedImage.TYPE_4BYTE_ABGR ||
image_data.getType() == BufferedImage.TYPE_INT_ARGB
){
hasTransparency = true;
}
}
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());
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());
}
// 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) {
ex.printStackTrace();
hasTransparency = false;
data = BufferUtils.createByteBuffer(3);
data.put((byte)0);
data.put((byte)0);
data.put((byte)0);
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();
}
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(){

View File

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