[DEV] add base of physic engine and basic of display of aabb box...

This commit is contained in:
Edouard DUPIN 2020-05-29 18:32:08 +02:00
parent 49aae1461e
commit e72f6b3449
33 changed files with 3952 additions and 656 deletions

Binary file not shown.

12
res/person_-xfw_zup.mtl Normal file
View File

@ -0,0 +1,12 @@
# Blender MTL File: 'person.blend'
# Material Count: 1
newmtl Material
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2

1082
res/person_-xfw_zup.obj Normal file

File diff suppressed because it is too large Load Diff

12
res/person_-yfw_zup.mtl Normal file
View File

@ -0,0 +1,12 @@
# Blender MTL File: 'person.blend'
# Material Count: 1
newmtl Material
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2

1082
res/person_-yfw_zup.obj Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.3 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View File

@ -1,6 +1,10 @@
package org.atriaSoft.etk;
public class Color {
@Override
public String toString() {
return "Color [r=" + r + ", g=" + g + ", b=" + b + ", a=" + a + "]";
}
public float r;
public float g;
public float b;

View File

@ -1,13 +1,16 @@
package org.atriaSoft.etk.math;
public class Vector3f {
public float x = 0;
public float y = 0;
public float z = 0;
public float x;
public float y;
public float z;
/**
* @brief Default contructor
*/
public Vector3f() {
this.x = 0;
this.y = 0;
this.z = 0;
}
/**
* @brief Constructor from scalars

View File

@ -15,6 +15,7 @@ import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.GL40;
import org.atriaSoft.etk.math.Matrix4f;
import org.atriaSoft.etk.math.Vector2f;
import org.atriaSoft.etk.math.Vector2i;
@ -492,13 +493,13 @@ public class OpenGL {
/**
* @brief draw a specific array == > this enable mode difference ...
*/
// public static void drawArrays(RenderMode mode, int first, int count) {
// if (this.programId >= 0) {
// updateAllFlags();
// GL20.glDrawArrays(convertRenderMode.get(mode), first, count);
// checkGlError("glDrawArrays");
// }
// }
public static void drawArrays(RenderMode mode, int first, int count) {
if (programId >= 0) {
updateAllFlags();
GL20.glDrawArrays(convertRenderMode.get(mode), first, count);
checkGlError("glDrawArrays");
}
}
// public static void drawElements(RenderMode mode, List<Integer> indices) {
// if (this.programId >= 0) {
// updateAllFlags();
@ -837,7 +838,7 @@ public class OpenGL {
}
public static void programLoadUniformColor(int location, Color value) {
GL20.glUniform3f(location, value.r, value.g, value.b);
GL20.glUniform4f(location, value.r, value.g, value.b, value.a);
}
public static void programLoadUniformVector(int location, Vector3f value) {
@ -868,5 +869,8 @@ public class OpenGL {
public static void drawElements(RenderMode mode, int vertexCount) {
GL11.glDrawElements(convertRenderMode.get(mode), vertexCount, GL11.GL_UNSIGNED_INT, 0);
}
public static void setDeathMask(boolean state) {
GL40.glDepthMask(state);
}
}

View File

@ -531,7 +531,7 @@ public abstract class Context {
public void stop() {
Log.warning("stop: NOT implemented for this platform...");
}
protected Vector2f windowsSize; //!< current size of the system
protected Vector2f windowsSize = new Vector2f(0,0); //!< current size of the system
/**
* @brief get the current windows size
* @return the current size ...
@ -568,12 +568,16 @@ public abstract class Context {
public void setSize( Vector2f _size){
Log.info("setSize: NOT implemented ...");
};
protected boolean fullscreen = false;
/**
* @brief The application request a change of his current size force the fullscreen mode.
* @param[in] _status status of the fullscreen mode.
*/
public void setFullScreen(boolean _status){
Log.info("setFullScreen: NOT implemented ...");
fullscreen = _status;
};
public boolean getFullScreen(){
return fullscreen;
};
protected Vector2f windowsPos; //!< current size of the system
/**

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,559 @@
package org.atriaSoft.gale.resource;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;
import org.atriaSoft.etk.Color;
import org.atriaSoft.etk.Uri;
import org.atriaSoft.etk.math.Matrix4f;
import org.atriaSoft.etk.math.Vector3f;
import org.atriaSoft.gale.Log;
import org.atriaSoft.gale.backend3d.OpenGL;
import org.atriaSoft.gale.backend3d.OpenGL.RenderMode;
import org.atriaSoft.gameEngine.resource.ResourceListTexturedMesh;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL40;
public class ResourceColored3DObject extends Resource {
private ResourceProgram program;
private int GLPosition;
private int GLColor;
private int GLMatrixTransformation;
private int GLMatrixProjection;
private int GLMatrixView;
protected ResourceColored3DObject() {
super();
addResourceType("ResourceColored3DObject");
// get the shader resource :
this.GLPosition = 0;
this.program = ResourceProgram.create(new Uri("DATA_EGE", "simple3D.vert"), new Uri("DATA_EGE", "simple3D.frag"));
if (this.program != null) {
this.GLMatrixTransformation = this.program.getUniform("in_matrixTransformation");
this.GLMatrixProjection = this.program.getUniform("in_matrixProjection");
this.GLMatrixView = this.program.getUniform("in_matrixView");
this.GLPosition = this.program.getAttribute("in_position");
this.GLColor = this.program.getUniform("in_colors");
}
}
public void draw(List<Vector3f> vertices,
Color color,
boolean updateDepthBuffer,
boolean depthtest) {
if (vertices.size() <= 0) {
return;
}
if (this.program == null) {
Log.error("No shader ...");
return;
}
if (true == depthtest) {
OpenGL.enable(OpenGL.Flag.flag_depthTest);
if (false == updateDepthBuffer) {
OpenGL.setDeathMask(false);
}
}
//Log.debug(" display " << this.coord.size() << " elements" );
this.program.use();
Matrix4f projectionMatrix = OpenGL.getMatrix();
Matrix4f viewMatrix = OpenGL.getCameraMatrix();
Matrix4f transformationMatrix = Matrix4f.identity();
this.program.uniformMatrix(this.GLMatrixView, viewMatrix);
this.program.uniformMatrix(this.GLMatrixProjection, projectionMatrix);
this.program.uniformMatrix(this.GLMatrixTransformation, transformationMatrix);
// color :
this.program.uniformColor(this.GLColor, color);
// position :
FloatBuffer buffer = storeDataInFloatBuffer(convertInFloat(vertices));
this.program.sendAttribute(this.GLPosition, 3, buffer, 3);
if (color.a < 1.0f) {
OpenGL.enable(OpenGL.Flag.flag_blend);
}
// Request the draw of the elements:
OpenGL.drawArrays(OpenGL.RenderMode.triangle, 0, vertices.size());
if (color.a < 1.0f) {
OpenGL.disable(OpenGL.Flag.flag_blend);
}
this.program.unUse();
// Request the draw od the elements:
//glDrawArrays(GLLINES, 0, vertices.size());
//this.GLprogram.UnUse();
if (true == depthtest) {
if (false == updateDepthBuffer) {
OpenGL.setDeathMask(true);
}
OpenGL.disable(OpenGL.Flag.flag_depthTest);
}
}
private float[] convertInFloat(List<Vector3f> data) {
float[] out = new float[data.size()*3];
for (int iii=0; iii<data.size(); iii++) {
out[iii*3] = data.get(iii).x;
out[iii*3+1] = data.get(iii).y;
out[iii*3+2] = data.get(iii).z;
}
return out;
}
private FloatBuffer storeDataInFloatBuffer(float[] data) {
FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
buffer.put(data);
buffer.flip();
return buffer;
}
public void draw(List<Vector3f> vertices,
Color color,
Matrix4f transformationMatrix,
boolean updateDepthBuffer,
boolean depthtest) {
if (vertices.size() <= 0) {
return;
}
if (this.program == null) {
Log.error("No shader ...");
return;
}
if (true == depthtest) {
OpenGL.enable(OpenGL.Flag.flag_depthTest);
if (false == updateDepthBuffer) {
OpenGL.setDeathMask(false);
}
}
//Log.debug(" display " << this.coord.size() << " elements" );
this.program.use();
// set Matrix: translation/positionMatrix
Matrix4f projectionMatrix = OpenGL.getMatrix();
Matrix4f viewMatrix = OpenGL.getCameraMatrix();
this.program.uniformMatrix(this.GLMatrixView, viewMatrix);
this.program.uniformMatrix(this.GLMatrixProjection, projectionMatrix);
this.program.uniformMatrix(this.GLMatrixTransformation, transformationMatrix);
// position :
FloatBuffer buffer = storeDataInFloatBuffer(convertInFloat(vertices));
this.program.sendAttribute(this.GLPosition, 3, buffer, 3);
// color :
Log.info("color= " + color + " " + this.GLPosition);
this.program.uniformColor(this.GLColor, color);
if (color.a < 1.0f) {
OpenGL.enable(OpenGL.Flag.flag_blend);
}
// Request the draw of the elements:
OpenGL.drawArrays(OpenGL.RenderMode.triangle, 0, vertices.size());
if (color.a < 1.0f) {
OpenGL.disable(OpenGL.Flag.flag_blend);
}
this.program.unUse();
if (true == depthtest) {
if (false == updateDepthBuffer) {
OpenGL.setDeathMask(true);
}
OpenGL.disable(OpenGL.Flag.flag_depthTest);
}
}
public void drawLine(List<Vector3f> vertices,
Color color,
Matrix4f transformationMatrix,
boolean updateDepthBuffer,
boolean depthtest) {
if (vertices.size() <= 0) {
return;
}
if (this.program == null) {
Log.error("No shader ...");
return;
}
if (true == depthtest) {
OpenGL.enable(OpenGL.Flag.flag_depthTest);
if (false == updateDepthBuffer) {
OpenGL.setDeathMask(false);
}
}
//Log.debug(" display " << this.coord.size() << " elements" );
this.program.use();
// set Matrix: translation/positionMatrix
Matrix4f projectionMatrix = OpenGL.getMatrix();
Matrix4f viewMatrix = OpenGL.getCameraMatrix();
this.program.uniformMatrix(this.GLMatrixView, viewMatrix);
this.program.uniformMatrix(this.GLMatrixProjection, projectionMatrix);
this.program.uniformMatrix(this.GLMatrixTransformation, transformationMatrix);
// position :
FloatBuffer buffer = storeDataInFloatBuffer(convertInFloat(vertices));
this.program.sendAttribute(this.GLPosition, 3, buffer, 3);
// color :
this.program.uniformColor(this.GLColor, color);
if (color.a < 1.0f) {
OpenGL.enable(OpenGL.Flag.flag_blend);
}
// Request the draw od the elements:
OpenGL.drawArrays(OpenGL.RenderMode.line, 0, vertices.size());
if (color.a < 1.0f) {
OpenGL.disable(OpenGL.Flag.flag_blend);
}
this.program.unUse();
if (true == depthtest) {
if (false == updateDepthBuffer) {
OpenGL.setDeathMask(true);
}
OpenGL.disable(OpenGL.Flag.flag_depthTest);
}
}
public void drawCubeLine(Vector3f min,
Vector3f max,
Color color,
Matrix4f transformationMatrix,
boolean updateDepthBuffer,
boolean depthtest) {
List<Vector3f> vertices = new ArrayList<Vector3f>();
vertices.add(new Vector3f(min.x, min.y,min.z));
vertices.add(new Vector3f(max.x, min.y,min.z));
vertices.add(new Vector3f(max.x, min.y,min.z));
vertices.add(new Vector3f(max.x, min.y,max.z));
vertices.add(new Vector3f(max.x, min.y,max.z));
vertices.add(new Vector3f(min.x, min.y,max.z));
vertices.add(new Vector3f(min.x, min.y,max.z));
vertices.add(new Vector3f(min.x, min.y,min.z));
vertices.add(new Vector3f(min.x, max.y,min.z));
vertices.add(new Vector3f(max.x, max.y,min.z));
vertices.add(new Vector3f(max.x, max.y,min.z));
vertices.add(new Vector3f(max.x, max.y,max.z));
vertices.add(new Vector3f(max.x, max.y,max.z));
vertices.add(new Vector3f(min.x, max.y,max.z));
vertices.add(new Vector3f(min.x, max.y,max.z));
vertices.add(new Vector3f(min.x, max.y,min.z));
vertices.add(new Vector3f(min.x, min.y,min.z));
vertices.add(new Vector3f(min.x, max.y,min.z));
vertices.add(new Vector3f(max.x, min.y,min.z));
vertices.add(new Vector3f(max.x, max.y,min.z));
vertices.add(new Vector3f(max.x, min.y,max.z));
vertices.add(new Vector3f(max.x, max.y,max.z));
vertices.add(new Vector3f(min.x, min.y,max.z));
vertices.add(new Vector3f(min.x, max.y,max.z));
drawLine(vertices, color, transformationMatrix, updateDepthBuffer, depthtest);
}
public void drawSquare(Vector3f size,
Matrix4f transformationMatrix,
Color tmpColor) {
List<Vector3f> tmpVertices = new ArrayList<Vector3f>();
int indices[] = { 0,1,2, 3,2,1, 4,0,6,
6,0,2, 5,1,4, 4,1,0,
7,3,1, 7,1,5, 5,4,7,
7,4,6, 7,2,3, 7,6,2};
Vector3f vertices[]={ new Vector3f(size.x,size.y,size.z),
new Vector3f(-size.x,size.y,size.z),
new Vector3f(size.x,-size.y,size.z),
new Vector3f(-size.x,-size.y,size.z),
new Vector3f(size.x,size.y,-size.z),
new Vector3f(-size.x,size.y,-size.z),
new Vector3f(size.x,-size.y,-size.z),
new Vector3f(-size.x,-size.y,-size.z)};
tmpVertices.clear();
for (int iii=0 ; iii<36 ; iii+=3) {
// normal calculation :
//btVector3 normal = (vertices[indices[iii+2]]-vertices[indices[iii]]).cross(vertices[indices[iii+1]]-vertices[indices[iii]]);
//normal.normalize ();
tmpVertices.add(vertices[indices[iii]]);
tmpVertices.add(vertices[indices[iii+1]]);
tmpVertices.add(vertices[indices[iii+2]]);
}
draw(tmpVertices, tmpColor, transformationMatrix, true, true);
}
public void drawSphere(float radius,
int lats,
int longs,
Matrix4f transformationMatrix,
Color tmpColor) {
List<Vector3f> tmpVertices = new ArrayList<Vector3f>();;
for(int iii=0; iii<=lats; ++iii) {
float lat0 = (float)Math.PI * (-0.5f + (float)(iii - 1) / lats);
float z0 = radius*(float)Math.sin(lat0);
float zr0 = radius*(float)Math.cos(lat0);
float lat1 = (float)Math.PI * (-0.5f + (float)(iii) / lats);
float z1 = radius*(float)Math.sin(lat1);
float zr1 = radius*(float)Math.cos(lat1);
for(int jjj=0; jjj<longs; ++jjj) {
float lng = 2.0f * (float)Math.PI * (float)(jjj - 1) / longs;
float x = (float)Math.cos(lng);
float y = (float)Math.sin(lng);
Vector3f v1 = new Vector3f(x * zr1, y * zr1, z1);
Vector3f v4 = new Vector3f(x * zr0, y * zr0, z0);
lng = 2 * (float)Math.PI * (float)(jjj) / longs;
x = (float)Math.cos(lng);
y = (float)Math.sin(lng);
Vector3f v2 = new Vector3f(x * zr1, y * zr1, z1);
Vector3f v3 = new Vector3f(x * zr0, y * zr0, z0);
tmpVertices.add(v1);
tmpVertices.add(v2);
tmpVertices.add(v3);
tmpVertices.add(v1);
tmpVertices.add(v3);
tmpVertices.add(v4);
}
}
draw(tmpVertices, tmpColor, transformationMatrix, true, true);
}
public void drawCylinder(float radius,
float size,
int lats,
int longs,
Matrix4f transformationMatrix,
Color tmpColor) {
List<Vector3f> tmpVertices = new ArrayList<Vector3f>();;
// center to border (TOP)
// center to border (TOP)
for(int jjj=0; jjj<longs; ++jjj) {
float lng = 2.0f * (float)Math.PI * (float)(jjj - 1) / longs;
float z = size*0.5f;
Vector3f v1 = new Vector3f(0.0f, 0.0f, z);
float x = (float) Math.cos(lng)*radius;
float y = (float) Math.sin(lng)*radius;
Vector3f v2 = new Vector3f(x, y, z);
lng = 2.0f * (float)Math.PI * (float)(jjj) / longs;
x = (float) Math.cos(lng)*radius;
y = (float)Math.sin(lng)*radius;
Vector3f v3 = new Vector3f(x, y, z);
tmpVertices.add(v1);
tmpVertices.add(v3);
tmpVertices.add(v2);
}
// Cylinder
for(int jjj=0; jjj<longs; ++jjj) {
float lng = 2.0f * (float)Math.PI * (float)(jjj - 1) / longs;
float z = size*0.5f;
float x = (float) Math.cos(lng)*radius;
float y = (float) Math.sin(lng)*radius;
Vector3f v2 = new Vector3f(x, y, z);
Vector3f v2b = new Vector3f(x, y, -z);
lng = 2.0f * (float)Math.PI * (float)(jjj) / longs;
x = (float) Math.cos(lng)*radius;
y = (float) Math.sin(lng)*radius;
Vector3f v3 = new Vector3f(x, y, z);
Vector3f v3b = new Vector3f(x, y, -z);
tmpVertices.add(v2);
tmpVertices.add(v3);
tmpVertices.add(v3b);
tmpVertices.add(v2);
tmpVertices.add(v3b);
tmpVertices.add(v2b);
}
// center to border (BUTTOM)
for(int jjj=0; jjj<longs; ++jjj) {
float lng = 2.0f * (float)Math.PI * (float)(jjj - 1) / longs;
float z = size*-0.5f;
Vector3f v1 = new Vector3f(0.0f, 0.0f, z);
float x = (float)Math.cos(lng)*radius;
float y = (float)Math.sin(lng)*radius;
Vector3f v2 = new Vector3f(x, y, z);
lng = 2.0f * (float)Math.PI * (float)(jjj) / longs;
x = (float)Math.cos(lng)*radius;
y = (float)Math.sin(lng)*radius;
Vector3f v3 = new Vector3f(x, y, z);
tmpVertices.add(v1);
tmpVertices.add(v2);
tmpVertices.add(v3);
}
draw(tmpVertices, tmpColor, transformationMatrix, true, true);
}
public void drawCapsule(float radius,
float size,
int lats,
int longs,
Matrix4f transformationMatrix,
Color tmpColor) {
List<Vector3f> tmpVertices = new ArrayList<Vector3f>();;
lats = (int)(lats / 2)*2;
// center to border (TOP)
float offset = size*0.5f;
for(int iii=lats/2+1; iii<=lats; ++iii) {
float lat0 = (float)Math.PI * (-0.5f + (float)(iii - 1) / lats);
float z0 = radius*(float)Math.sin(lat0);
float zr0 = radius*(float)Math.cos(lat0);
float lat1 = (float)Math.PI * (-0.5f + (float)(iii) / lats);
float z1 = radius*(float)Math.sin(lat1);
float zr1 = radius*(float)Math.cos(lat1);
for(int jjj=0; jjj<longs; ++jjj) {
float lng = 2.0f * (float)Math.PI * (float)(jjj - 1) / longs;
float x = (float)Math.cos(lng);
float y = (float)Math.sin(lng);
Vector3f v1 = new Vector3f(x * zr1, y * zr1, z1+offset);
Vector3f v4 = new Vector3f(x * zr0, y * zr0, z0+offset);
lng = 2 * (float)Math.PI * (float)(jjj) / longs;
x = (float)Math.cos(lng);
y = (float)Math.sin(lng);
Vector3f v2 = new Vector3f(x * zr1, y * zr1, z1+offset);
Vector3f v3 = new Vector3f(x * zr0, y * zr0, z0+offset);
tmpVertices.add(v1);
tmpVertices.add(v2);
tmpVertices.add(v3);
tmpVertices.add(v1);
tmpVertices.add(v3);
tmpVertices.add(v4);
}
}
// Cylinder
for(int jjj=0; jjj<longs; ++jjj) {
float lng = 2.0f * (float)Math.PI * (float)(jjj - 1) / longs;
float z = size*0.5f;
float x = (float)Math.cos(lng)*radius;
float y = (float)Math.sin(lng)*radius;
Vector3f v2 = new Vector3f(x, y, z);
Vector3f v2b = new Vector3f(x, y, -z);
lng = 2.0f * (float)Math.PI * (float)(jjj) / longs;
x = (float)Math.cos(lng)*radius;
y = (float)Math.sin(lng)*radius;
Vector3f v3 = new Vector3f(x, y, z);
Vector3f v3b = new Vector3f(x, y, -z);
tmpVertices.add(v2);
tmpVertices.add(v3);
tmpVertices.add(v3b);
tmpVertices.add(v2);
tmpVertices.add(v3b);
tmpVertices.add(v2b);
}
// center to border (BUTTOM)
offset = -size*0.5f;
for(int iii=0; iii<=lats/2; ++iii) {
float lat0 = (float)Math.PI * (-0.5f + (float)(iii - 1) / lats);
float z0 = radius*(float)Math.sin(lat0);
float zr0 = radius*(float)Math.cos(lat0);
float lat1 = (float)Math.PI * (-0.5f + (float)(iii) / lats);
float z1 = radius*(float)Math.sin(lat1);
float zr1 = radius*(float)Math.cos(lat1);
for(int jjj=0; jjj<longs; ++jjj) {
float lng = 2.0f * (float)Math.PI * (float)(jjj - 1) / longs;
float x = (float)Math.cos(lng);
float y = (float)Math.sin(lng);
Vector3f v1 = new Vector3f(x * zr1, y * zr1, z1+offset);
Vector3f v4 = new Vector3f(x * zr0, y * zr0, z0+offset);
lng = 2 * (float)Math.PI * (float)(jjj) / longs;
x = (float)Math.cos(lng);
y = (float)Math.sin(lng);
Vector3f v2 = new Vector3f(x * zr1, y * zr1, z1+offset);
Vector3f v3 = new Vector3f(x * zr0, y * zr0, z0+offset);
tmpVertices.add(v1);
tmpVertices.add(v2);
tmpVertices.add(v3);
tmpVertices.add(v1);
tmpVertices.add(v3);
tmpVertices.add(v4);
}
}
draw(tmpVertices, tmpColor, transformationMatrix, true, true);
}
public void drawCone(float radius,
float size,
int lats,
int longs,
Matrix4f transformationMatrix,
Color tmpColor) {
List<Vector3f> tmpVertices = new ArrayList<Vector3f>();
// center to border (TOP)
for(int jjj=0; jjj<longs; ++jjj) {
float lng = 2.0f * (float)Math.PI * (float)(jjj - 1) / longs;
Vector3f v1 = new Vector3f(0.0f, 0.0f, -size/2);
float x = (float)Math.cos(lng)*radius;
float y = (float)Math.sin(lng)*radius;
Vector3f v2 = new Vector3f(x, y, size/2);
lng = 2.0f * (float)Math.PI * (float)(jjj) / longs;
x = (float)Math.cos(lng)*radius;
y = (float)Math.sin(lng)*radius;
Vector3f v3 = new Vector3f(x, y, size/2);
tmpVertices.add(v1);
tmpVertices.add(v3);
tmpVertices.add(v2);
}
// center to border (BUTTOM)
for(int jjj=0; jjj<longs; ++jjj) {
float lng = 2.0f * (float)Math.PI * (float)(jjj - 1) / longs;
Vector3f v1 = new Vector3f(0.0f, 0.0f, size/2);
float x = (float)Math.cos(lng)*radius;
float y = (float)Math.sin(lng)*radius;
Vector3f v2 = new Vector3f(x, y, size/2);
lng = 2.0f * (float)Math.PI * (float)(jjj) / longs;
x = (float)Math.cos(lng)*radius;
y = (float)Math.sin(lng)*radius;
Vector3f v3 = new Vector3f(x, y, size/2);
tmpVertices.add(v1);
tmpVertices.add(v2);
tmpVertices.add(v3);
}
draw(tmpVertices, tmpColor, transformationMatrix, true, true);
}
public void drawTriangles(List<Vector3f> vertex,
List<Integer> indice,
Matrix4f transformationMatrix,
Color tmpColor,
Vector3f offset) {// = new Vector3f(0,0,0.1f)
List<Vector3f> tmpVertices = new ArrayList<Vector3f>();;
for (int iii=0; iii<indice.size()/3; ++iii) {
tmpVertices.add(vertex.get(indice.get(iii*3 + 0)).add_new(offset));
tmpVertices.add(vertex.get(indice.get(iii*3 + 1)).add_new(offset));
tmpVertices.add(vertex.get(indice.get(iii*3 + 2)).add_new(offset));
//Log.info(" indices " << indice[iii*3 + 0] << " " << indice[iii*3 + 1] << " " << indice[iii*3 + 2]);
//Log.info(" triangle " << vertex[indice[iii*3 + 0]] << " " << vertex[indice[iii*3 + 1]] << " " << vertex[indice[iii*3 + 2]]);
}
//Log.info("display " << tmpVertices.size() << " vertices form " << indice.size());
draw(tmpVertices, tmpColor, transformationMatrix, true, true);
}
@Override
public void cleanUp() {
// TODO Auto-generated method stub
}
public static ResourceColored3DObject create() {
ResourceColored3DObject resource = new ResourceColored3DObject();
if (resource.resourceHasBeenCorectlyInit() == false) {
Log.critical("resource Is not correctly init: ResourceColored3DObject");
}
getManager().localAdd(resource);
return resource;
}
}

View File

@ -14,14 +14,8 @@ import org.atriaSoft.etk.math.Vector3f;
import org.atriaSoft.etk.math.Vector3i;
import org.atriaSoft.gale.Log;
import org.atriaSoft.gale.backend3d.OpenGL;
import org.atriaSoft.gale.backend3d.OpenGL.Usage;
import org.atriaSoft.gameEngine.Material;
//import org.lwjgl.BufferUtils;
//import org.lwjgl.opengl.GL11;
//import org.lwjgl.opengl.GL13;
//import org.lwjgl.opengl.GL15;
//import org.lwjgl.opengl.GL20;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL40;
class ProgAttributeElement {
@ -38,9 +32,9 @@ public class ResourceProgram extends Resource {
private ResourceShader shaderVertex = null;
private ResourceShader shaderFragment = null;
private List<ProgAttributeElement> elementList = new ArrayList<ProgAttributeElement>(); //!< List of all the attribute requested by the user
private List<Integer> listOfVBOUsed = new ArrayList<Integer>(); //!< retain the list of VBO used to disable it when unuse program ...
private boolean hasTexture = false; //!< A texture has been set to the current shader
private boolean hasTexture1 = false; //!< A texture has been set to the current shader
// private List<Integer> listOfVBOUsed = new ArrayList<Integer>(); //!< retain the list of VBO used to disable it when unuse program ...
// private boolean hasTexture = false; //!< A texture has been set to the current shader
// private boolean hasTexture1 = false; //!< A texture has been set to the current shader
/**
* @brief Contructor of an opengl Program.
* @param[in] uri Uri of the file
@ -85,8 +79,8 @@ public class ResourceProgram extends Resource {
this.shaderVertex = null;
}
this.elementList.clear();
this.hasTexture = false;
this.hasTexture1 = false;
// this.hasTexture = false;
// this.hasTexture1 = false;
}
/**
* @brief Check If an Id is valid in the shader or not (sometime the shader have not some attribute, then we need to display some error)
@ -240,18 +234,24 @@ public class ResourceProgram extends Resource {
// GL20.glVertexAttribPointer(attributeNumber, coordinateSize, GL11.GL_FLOAT, false, 0, 0);
// GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
// }
private float[] convertInFloat(List<Vector3f> data) {
float[] out = new float[data.size()*3];
for (int iii=0; iii<data.size(); iii++) {
out[iii*3] = data.get(iii).x;
out[iii*3+1] = data.get(iii).y;
out[iii*3+2] = data.get(iii).z;
}
return out;
}
/**
* @brief Send attribute table to the spefified ID attribure (not send if does not really exist in the openGL program).
* @param[in] idElem Id of the Attribute that might be sended.
* @brief Send attribute table to the specified ID attribute (not send if does not really exist in the openGL program).
* @param[in] idElem Id of the Attribute that might be sent.
* @param[in] nbElement Specifies the number of elements that are to be modified.
* @param[in] pointer Pointer on the data that might be sended.
* @param[in] jumpBetweenSample Number of byte to jump between 2 vertex (this permit to enterlace informations)
* @param[in] pointer Pointer on the data that might be sent.
* @param[in] jumpBetweenSample Number of byte to jump between 2 vertex (this permit to interlace informations)
*/
// public void sendAttribute(int idElem,
// int nbElement,
// void* pointer,
// int jumpBetweenSample=0) {
//
// public void sendAttribute3fv(int idElem, float[] data) {
// if (this.exist == false) {
// return;
// }
@ -263,23 +263,51 @@ public class ResourceProgram extends Resource {
// if (this.elementList.get(idElem).isLinked == false) {
// return;
// }
// //Log.error("[" + this.elementList.get(idElem).name + "] send " + nbElement + " element");
// glVertexAttribPointer(this.elementList.get(idElem).elementId, // attribute ID of openGL
// nbElement, // number of elements per vertex, here (r,g,b,a)
// GLFLOAT, // the type of each element
// GLFALSE, // take our values as-is
// jumpBetweenSample, // no extra data between each position
// pointer); // Pointer on the buffer
// checkGlError("glVertexAttribPointer", LINE, idElem);
// glEnableVertexAttribArray(this.elementList.get(idElem).elementId);
// checkGlError("glEnableVertexAttribArray", LINE, idElem);
// FloatBuffer buffer = storeDataInFloatBuffer(data);
// //GL40.glBindVertexArray(this.elementList.get(idElem).elementId);
// Log.error("[" + this.elementList.get(idElem).name + "] send " + data.length + " element");
// GL40.glVertexAttribPointer(
// this.elementList.get(idElem).elementId,
// data.length,
// GL40.GL_FLOAT,
// false,
// 0,
// buffer);
// //checkGlError("glVertexAttribPointer", LINE, idElem);
// GL40.glEnableVertexAttribArray(this.elementList.get(idElem).elementId);
// //checkGlError("glEnableVertexAttribArray", LINE, idElem);
// }
// public void sendAttribute(int idElem, etk::Vector<Vector2f> data) {
// sendAttribute(idElem, 2/*u,v / x,y*/, data[0]);
// }
// public void sendAttribute(int idElem, etk::Vector<Vector3f> data) {
// sendAttribute(idElem, 3/*x,y,z,unused*/, data[0], 4*sizeof(btScalar));
//// public void sendAttribute(int idElem, etk::Vector<Vector2f> data) {
//// sendAttribute(idElem, 2/*u,v / x,y*/, data[0]);
//// }
// public void sendAttribute(int idElem, List<Vector3f> data) {
// sendAttribute3fv(idElem, convertInFloat(data));
// }
public void sendAttribute(int idElem, int nbElement, FloatBuffer data, int jumpBetweenSample) {
if (this.exist == false) {
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 == false) {
return;
}
//GL40.glBindVertexArray(this.elementList.get(idElem).elementId);
Log.error("[" + this.elementList.get(idElem).name + "] send " + 3 + " element");
GL40.glVertexAttribPointer(
this.elementList.get(idElem).elementId,
nbElement,
GL40.GL_FLOAT,
false,
jumpBetweenSample*4, /* 4 is the size of float in the generic system...*/
data);
//checkGlError("glVertexAttribPointer", LINE, idElem);
GL40.glEnableVertexAttribArray(this.elementList.get(idElem).elementId);
//checkGlError("glEnableVertexAttribArray", LINE, idElem);
}
// public void sendAttribute(int idElem, etk::Vector<etk::Color<float>> data) {
// sendAttribute(idElem, 4/*r,g,b,a*/, data[0]);
// }

View File

@ -42,6 +42,7 @@ public class ResourceVirtualArrayObject extends Resource {
GL30.glBindVertexArray(vaoID);
if (positions != null) {
GL20.glEnableVertexAttribArray(INDICE_VBO_POSITIONS);
//Log.info("unbind POSITION");
}
if (textureCoordinates != null) {
GL20.glEnableVertexAttribArray(INDICE_VBO_TEXTURE_COORDINATES);

View File

@ -18,17 +18,16 @@ import renderEngine.DisplayManager;
public class ControlCameraPlayer implements ControlInterface {
private Camera camera;
private float distanceFromCenter = 20;
private float angleZ = 0;
private float pitch = 0;
private float distanceFromCenter = 2.5f;
private boolean fpsMode = false;
private Entity playerEntity;
private Vector2f lastMousePosition = null;
private ComponentPositionPlayer playerPosition;
private ComponentPlayer player;
private boolean moveUp = false;
private boolean moveDown = false;
private boolean moveLeft = false;
private boolean moveRight = false;
private boolean walk = false;
public ControlCameraPlayer(Camera camera, Entity playerEntity) {
this.camera = camera;
@ -47,18 +46,36 @@ public class ControlCameraPlayer implements ControlInterface {
}
@Override
public boolean onEventEntry(EventEntry event) {
if(event.getType() == KeyKeyboard.up) {
if(event.getType() == KeyKeyboard.up
|| (event.getType() == KeyKeyboard.character && (event.getChar() == 'z' || event.getChar() == 'Z' ))) {
moveUp = getState(event.getStatus(), moveUp);
}
if(event.getType() == KeyKeyboard.left) {
if(event.getType() == KeyKeyboard.left
|| (event.getType() == KeyKeyboard.character && (event.getChar() == 'q' || event.getChar() == 'Q' ))) {
moveLeft = getState(event.getStatus(), moveLeft);
}
if(event.getType() == KeyKeyboard.right) {
if(event.getType() == KeyKeyboard.right
|| (event.getType() == KeyKeyboard.character && (event.getChar() == 'd' || event.getChar() == 'D' ))) {
moveRight = getState(event.getStatus(), moveRight);
}
if(event.getType() == KeyKeyboard.down) {
if(event.getType() == KeyKeyboard.down
|| (event.getType() == KeyKeyboard.character && (event.getChar() == 's' || event.getChar() == 'S' ))) {
moveDown = getState(event.getStatus(), moveDown);
}
if(event.getType() == KeyKeyboard.shiftLeft || event.getType() == KeyKeyboard.shiftRight) {
walk = event.getSpecialKey().getShift();
}
if(event.getType() == KeyKeyboard.f10) {
if (event.getStatus() == KeyStatus.up) {
if (fpsMode == false) {
fpsMode = true;
distanceFromCenter = 0;
} else {
fpsMode = false;
distanceFromCenter = 2.5f;
}
}
}
return false;
}
@ -66,16 +83,26 @@ public class ControlCameraPlayer implements ControlInterface {
public boolean onEventInput(EventInput event, Vector2f relativePosition) {
// Log.info("" + event);
// TODO Auto-generated method stub
if (event.getInputId() == 4) {
if (event.getStatus() == KeyStatus.down) {
distanceFromCenter -= 1;
if (fpsMode == false) {
if (event.getInputId() == 4) {
if (event.getStatus() == KeyStatus.down) {
distanceFromCenter -= 0.2;
}
if (distanceFromCenter < 0.0) {
distanceFromCenter = 0.0f;
}
return true;
} else if (event.getInputId() == 5) {
if (event.getStatus() == KeyStatus.down) {
distanceFromCenter += 0.2;
}
if (distanceFromCenter < 0.3) {
distanceFromCenter = 0.3f;
}
return true;
}
return true;
} else if (event.getInputId() == 5) {
if (event.getStatus() == KeyStatus.down) {
distanceFromCenter += 1;
}
return true;
} else {
distanceFromCenter = 0;
}
// TODO: check if grabbing is enable ...
// in grabbing mouse only:
@ -86,14 +113,15 @@ public class ControlCameraPlayer implements ControlInterface {
Vector2f delta = event.getPosition().clone();
//angleZ += delta.x;
//this.camera.setYaw(this.camera.getYaw() + (float)Math.toRadians(delta.x));
this.camera.setPitch(this.camera.getPitch() + (float)Math.toRadians(delta.y));
this.camera.setPitch(this.camera.getPitch() + (float)Math.toRadians(delta.y * this.player.getTurnSpeed()));
if (this.camera.getPitch()>0) {
this.camera.setPitch(0);
}
if (this.camera.getPitch()<-Math.PI) {
this.camera.setPitch((float)-Math.PI);
}
this.camera.setRoll(this.camera.getRoll() - (float)Math.toRadians(delta.x));
/*
this.camera.setRoll(this.camera.getRoll() - (float)Math.toRadians(delta.x * this.player.getTurnSpeed()));
Log.info("Change camera: " + this.camera.getYaw() + " " + this.camera.getPitch());
if (this.camera.getRoll()>Math.PI) {
this.camera.setRoll(this.camera.getRoll()-(float)Math.PI*2.0f);
@ -101,7 +129,19 @@ public class ControlCameraPlayer implements ControlInterface {
if (this.camera.getRoll()<-Math.PI) {
this.camera.setRoll(this.camera.getRoll()+(float)Math.PI*2.0f);
}
this.playerPosition.setAngles(new Vector3f(0,0,this.camera.getRoll()));
this.playerPosition.setAngles(new Vector3f(0,0,-this.camera.getRoll()));
*/
float tmpAngle = this.playerPosition.getAngles().z + (float)Math.toRadians(delta.x * this.player.getTurnSpeed());
if (tmpAngle > Math.PI) {
tmpAngle -= (float)Math.PI*2.0f;
}
if (tmpAngle < -Math.PI) {
tmpAngle += (float)Math.PI*2.0f;
}
this.playerPosition.setAngles(new Vector3f(0,0,tmpAngle));
this.camera.setRoll(-this.playerPosition.getAngles().z);
Log.info("Change camera: " + this.camera.getYaw() + " " + this.camera.getPitch());
}
return false;
}
@ -109,6 +149,11 @@ public class ControlCameraPlayer implements ControlInterface {
@Override
public void periodicCall(EventTime event) {
float speed = 0;
float walkFactor = 1;
if (this.walk == true) {
walkFactor = this.player.getWalkFactor();
}
//distanceFromCenter = 6;
if (moveUp != moveDown) {
if (moveUp) {
speed = this.player.getRunSpeed();
@ -116,16 +161,36 @@ public class ControlCameraPlayer implements ControlInterface {
speed = -this.player.getRunSpeed();
}
}
float distance = speed * event.getTimeDeltaCallSecond();
float dx = (float) (distance * Math.sin(this.playerPosition.getAngles().z));
float distance = speed * walkFactor * event.getTimeDeltaCallSecond();
float dx = -(float) (distance * Math.sin(this.playerPosition.getAngles().z));
float dy = (float) (distance * Math.cos(this.playerPosition.getAngles().z));
Log.error("update position ..." + dx + " " + dy);
this.playerPosition.getTransform().getPosition().x += dx;
this.playerPosition.getTransform().getPosition().y += dy;
this.camera.getPosition().x = this.playerPosition.getTransform().getPosition().x;
this.camera.getPosition().y = this.playerPosition.getTransform().getPosition().y;
this.camera.getPosition().z = this.playerPosition.getTransform().getPosition().z;
speed = 0;
if (moveRight != moveLeft) {
if (moveRight) {
speed = this.player.getStrafSpeed();
} else {
speed = -this.player.getStrafSpeed();
}
}
distance = speed * walkFactor * event.getTimeDeltaCallSecond();
float dxStraf = (float) (distance * Math.sin((float)Math.PI*0.5f + this.playerPosition.getAngles().z));
float dyStraf = -(float) (distance * Math.cos((float)Math.PI*0.5f + this.playerPosition.getAngles().z));
//Log.error("update position ..." + dx + " " + dy);
this.playerPosition.getTransform().getPosition().x += dx + dxStraf;
this.playerPosition.getTransform().getPosition().y += dy + dyStraf;
// here the camera is behind the player, we need to move the camera ...
//Log.info(" pitch: " + Math.toDegrees(this.camera.getPitch()) + " " + Math.toDegrees(this.playerPosition.getAngles().z));
float horinzontalDistance = (float) (distanceFromCenter * Math.sin(this.camera.getPitch()));
float verticalDistance = (float) (distanceFromCenter * Math.cos(this.camera.getPitch()));
//Log.info(" distanceFromCenter " + distanceFromCenter);
float tmp = -horinzontalDistance;
float theta = (float)Math.PI + this.playerPosition.getAngles().z;// - (float)Math.PI*0.5f;
float offsetX = (float) (tmp * Math.sin(-theta));
float offsetY = (float) (tmp * Math.cos(-theta));
//Log.info(" res" + offsetX + " " + offsetY);
this.camera.getPosition().x = this.playerPosition.getTransform().getPosition().x + offsetX;
this.camera.getPosition().y = this.playerPosition.getTransform().getPosition().y + offsetY;
this.camera.getPosition().z = this.playerPosition.getTransform().getPosition().z + 1.6f + verticalDistance;
}
}

View File

@ -0,0 +1,128 @@
package org.atriaSoft.gameEngine;
import org.atriaSoft.etk.math.Vector2f;
import org.atriaSoft.etk.math.Vector3f;
import org.atriaSoft.gale.Gale;
import org.atriaSoft.gale.event.EventEntry;
import org.atriaSoft.gale.event.EventInput;
import org.atriaSoft.gale.event.EventTime;
import org.atriaSoft.gale.key.KeyKeyboard;
import org.atriaSoft.gale.key.KeyStatus;
import org.atriaSoft.gameEngine.camera.Camera;
import org.atriaSoft.gameEngine.components.ComponentPlayer;
import org.atriaSoft.gameEngine.components.ComponentPosition;
import org.atriaSoft.gameEngine.components.ComponentPositionPlayer;
import renderEngine.DisplayManager;
public class ControlCameraPlayerFPS implements ControlInterface {
private Camera camera;
private Entity playerEntity;
private ComponentPositionPlayer playerPosition;
private ComponentPlayer player;
private boolean moveUp = false;
private boolean moveDown = false;
private boolean moveLeft = false;
private boolean moveRight = false;
public ControlCameraPlayerFPS(Camera camera, Entity playerEntity) {
this.camera = camera;
this.playerEntity = playerEntity;
this.playerPosition = (ComponentPositionPlayer)this.playerEntity.getComponent("position");
this.player = (ComponentPlayer)this.playerEntity.getComponent("player");
}
private boolean getState(KeyStatus state, boolean previousState) {
if (state == KeyStatus.down) {
return true;
}
if (state == KeyStatus.up) {
return false;
}
return previousState;
}
@Override
public boolean onEventEntry(EventEntry event) {
if(event.getType() == KeyKeyboard.up
|| (event.getType() == KeyKeyboard.character && (event.getChar() == 'z' || event.getChar() == 'z' ))) {
moveUp = getState(event.getStatus(), moveUp);
}
if(event.getType() == KeyKeyboard.left
|| (event.getType() == KeyKeyboard.character && (event.getChar() == 'q' || event.getChar() == 'Q' ))) {
moveLeft = getState(event.getStatus(), moveLeft);
}
if(event.getType() == KeyKeyboard.right
|| (event.getType() == KeyKeyboard.character && (event.getChar() == 'd' || event.getChar() == 'D' ))) {
moveRight = getState(event.getStatus(), moveRight);
}
if(event.getType() == KeyKeyboard.down
|| (event.getType() == KeyKeyboard.character && (event.getChar() == 's' || event.getChar() == 'S' ))) {
moveDown = getState(event.getStatus(), moveDown);
}
return false;
}
@Override
public boolean onEventInput(EventInput event, Vector2f relativePosition) {
// Log.info("" + event);
// in grabbing mouse only:
if (Gale.getContext().isGrabPointerEvents() == false) {
return false;
}
if (event.getStatus() == KeyStatus.move) {
Vector2f delta = event.getPosition().clone();
//angleZ += delta.x;
//this.camera.setYaw(this.camera.getYaw() + (float)Math.toRadians(delta.x));
this.camera.setPitch(this.camera.getPitch() + (float)Math.toRadians(delta.y * this.player.getTurnSpeed()));
if (this.camera.getPitch()>0) {
this.camera.setPitch(0);
}
if (this.camera.getPitch()<-Math.PI) {
this.camera.setPitch((float)-Math.PI);
}
this.camera.setRoll(this.camera.getRoll() - (float)Math.toRadians(delta.x * this.player.getTurnSpeed()));
Log.info("Change camera: " + this.camera.getYaw() + " " + this.camera.getPitch());
if (this.camera.getRoll()>Math.PI) {
this.camera.setRoll(this.camera.getRoll()-(float)Math.PI*2.0f);
}
if (this.camera.getRoll()<-Math.PI) {
this.camera.setRoll(this.camera.getRoll()+(float)Math.PI*2.0f);
}
this.playerPosition.setAngles(new Vector3f(0,0,this.camera.getRoll()));
}
return false;
}
@Override
public void periodicCall(EventTime event) {
float speed = 0;
if (moveUp != moveDown) {
if (moveUp) {
speed = this.player.getRunSpeed();
} else {
speed = -this.player.getRunSpeed();
}
}
float distance = speed * event.getTimeDeltaCallSecond();
float dx = (float) (distance * Math.sin(this.playerPosition.getAngles().z));
float dy = (float) (distance * Math.cos(this.playerPosition.getAngles().z));
speed = 0;
if (moveRight != moveLeft) {
if (moveRight) {
speed = this.player.getStrafSpeed();
} else {
speed = -this.player.getStrafSpeed();
}
}
distance = speed * event.getTimeDeltaCallSecond();
float dxStraf = (float) (distance * Math.sin((float)Math.PI*0.5f + this.playerPosition.getAngles().z));
float dyStraf = (float) (distance * Math.cos((float)Math.PI*0.5f + this.playerPosition.getAngles().z));
//Log.error("update position ..." + dx + " " + dy);
this.playerPosition.getTransform().getPosition().x += dx + dxStraf;
this.playerPosition.getTransform().getPosition().y += dy + dyStraf;
this.camera.getPosition().x = this.playerPosition.getTransform().getPosition().x;
this.camera.getPosition().y = this.playerPosition.getTransform().getPosition().y;
this.camera.getPosition().z = this.playerPosition.getTransform().getPosition().z;
}
}

View File

@ -43,10 +43,10 @@ public class Environement {
public Environement() {
// we add the 4 classical engines (the order is used to the global rendering cycle ...
addEngine(new EnginePlayer(this));
addEngine(new EnginePhysics(this));
addEngine(new EngineAI(this));
addEngine(new EngineDynamicMeshs(this));
addEngine(new EngineRender(this));
addEngine(new EnginePhysics(this));
addEngine(new EngineParticle(this));
addEngine(new EngineLight(this));
}

View File

@ -0,0 +1,73 @@
package org.atriaSoft.gameEngine.components;
import java.util.ArrayList;
import java.util.List;
import org.atriaSoft.etk.Color;
import org.atriaSoft.etk.math.Matrix4f;
import org.atriaSoft.gale.backend3d.OpenGL;
import org.atriaSoft.gale.resource.ResourceColored3DObject;
import org.atriaSoft.gale.test.sample2.Log;
import org.atriaSoft.gameEngine.Component;
import org.atriaSoft.gameEngine.physics.PhysicCollisionAABB;
import org.atriaSoft.gameEngine.physics.PhysicShape;
import entities.Entity;
public class ComponentPhysics extends Component {
private PhysicCollisionAABB aabb;
private List<PhysicShape> shapes = new ArrayList<PhysicShape>();
private ComponentPosition position;
@Override
public String getType() {
return "physics";
}
@Override
public void addFriendComponent(Component component) {
if (component.getType().contentEquals("position")) {
position = (ComponentPosition)component;
}
}
@Override
public void removeFriendComponent(Component component) {
// nothing to do.
}
public void updateAABB() {
if (position == null) {
Log.info("No position in Entity ");
return;
}
// TODO: Add a flag to check if it is needed to update the AABB...
PhysicCollisionAABB aabb_new = PhysicCollisionAABB.beforeCalculated();
for (PhysicShape shape : shapes) {
shape.updateAABB(position.getTransform(), aabb_new);
}
aabb = aabb_new;
}
public PhysicCollisionAABB getcurrentAABB() {
return aabb;
}
public void applyForces(float timeStep) {
// TODO Auto-generated method stub
}
public void renderDebug(ResourceColored3DObject debugDrawProperty) {
if (aabb != null) {
debugDrawProperty.drawCubeLine(aabb.getMin(), aabb.getMax(), new Color(1,1,1,1), Matrix4f.identity(), true, true);
//debugDrawProperty.drawCubeLine(new Vector3f(0,0,0), new Vector3f(32,32,32), new Color(1,0,1,1), Matrix4f.identity(), true, true);
} else {
Log.error("no AABB");
}
for (PhysicShape shape : shapes) {
shape.renderDebug(position.getTransform(), debugDrawProperty);
}
}
public void addShape(PhysicShape shape) {
shapes.add(shape);
}
public void clearShape() {
shapes.clear();
}
}

View File

@ -5,8 +5,10 @@ import org.atriaSoft.gameEngine.engines.EnginePlayer;
public class ComponentPlayer extends Component {
private float runSpeed = 35;
private float turnSpeed = (float)Math.toRadians(120);
private float strafSpeed = 25;
private float turnSpeed = 0.45f;
private float jumpPower = 30;
private float walkFactor = 0.37f;
public ComponentPlayer() {
@ -46,5 +48,21 @@ public class ComponentPlayer extends Component {
this.jumpPower = jumpPower;
}
public float getStrafSpeed() {
return strafSpeed;
}
public void setStrafSpeed(float strafSpeed) {
this.strafSpeed = strafSpeed;
}
public float getWalkFactor() {
return walkFactor;
}
public void setWalkFactor(float walkFactor) {
this.walkFactor = walkFactor;
}
}

View File

@ -21,7 +21,7 @@ public class ComponentPositionPlayer extends ComponentPosition {
}
public void setAngles(Vector3f angles) {
this.angles = angles;
this.angles = angles.clone();
// TODO: update transform3D
this.transform.getOrientation().setEulerAngles(this.angles);
}

View File

@ -1,12 +0,0 @@
package org.atriaSoft.gameEngine.components;
import org.atriaSoft.gameEngine.Component;
public class CoponentPhysics extends Component {
@Override
public String getType() {
// TODO Auto-generated method stub
return "physics";
}
}

View File

@ -0,0 +1,16 @@
#version 400 core
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
in vec4 io_color;
// output:
out vec4 out_Color;
void main(void) {
out_Color = io_color;
}

View File

@ -0,0 +1,21 @@
#version 400 core
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
// Input :
in vec3 in_position;
uniform vec4 in_colors;
uniform mat4 in_matrixTransformation;
uniform mat4 in_matrixProjection;
uniform mat4 in_matrixView;
// output :
out vec4 io_color;
void main(void) {
gl_Position = in_matrixProjection * in_matrixView * in_matrixTransformation * vec4(in_position, 1.0);
io_color = vec4(in_colors.xyz, 0.3);
}

View File

@ -1,13 +1,27 @@
package org.atriaSoft.gameEngine.engines;
import java.util.Vector;
import org.atriaSoft.etk.Color;
import org.atriaSoft.etk.math.Matrix4f;
import org.atriaSoft.etk.math.Vector3f;
import org.atriaSoft.gale.resource.ResourceColored3DObject;
import org.atriaSoft.gale.test.sample2.Log;
import org.atriaSoft.gameEngine.Component;
import org.atriaSoft.gameEngine.Engine;
import org.atriaSoft.gameEngine.Environement;
import org.atriaSoft.gameEngine.camera.Camera;
import org.atriaSoft.gameEngine.components.ComponentAI;
import org.atriaSoft.gameEngine.components.ComponentPhysics;
import org.atriaSoft.gameEngine.components.ComponentRender;
public class EnginePhysics extends Engine {
public static final String ENGINE_NAME = "physics";
private float accumulator = 0;
private static float TIME_STEP = 0.02f;
private Vector<ComponentPhysics> components = new Vector<ComponentPhysics>();
private ResourceColored3DObject debugDrawProperty = ResourceColored3DObject.create();
public EnginePhysics(Environement env) {
super(env);
// TODO Auto-generated constructor stub
@ -15,26 +29,62 @@ public class EnginePhysics extends Engine {
@Override
public void componentRemove(Component ref) {
// TODO Auto-generated method stub
components.remove(ref);
}
@Override
public void componentAdd(Component ref) {
// TODO Auto-generated method stub
if (ref instanceof ComponentPhysics == false) {
return;
}
components.add((ComponentPhysics)ref);
}
@Override
public void update(long deltaMili) {
// TODO Auto-generated method stub
// Add the time difference in the accumulator
accumulator += (float)deltaMili*0.0001f;
// While there is enough accumulated time to take one or several physics steps
while (accumulator >= TIME_STEP) {
Log.info("update physic ... " + accumulator);
applyForces(TIME_STEP);
updateAABB(TIME_STEP);
updateCollisions(TIME_STEP);
GenerateResultCollisionsForces(TIME_STEP);
// Decrease the accumulated time
accumulator -= TIME_STEP;
}
}
private void applyForces(float timeStep) {
for (ComponentPhysics it: components) {
it.applyForces(TIME_STEP);
}
}
private void updateAABB(float timeStep) {
for (ComponentPhysics it: components) {
it.updateAABB();
}
}
private void updateCollisions(float timeStep) {
}
private void GenerateResultCollisionsForces(float timeStep) {
}
@Override
public void render(long deltaMili, Camera camera) {
// TODO Auto-generated method stub
for (ComponentPhysics it: this.components) {
//Log.info("Render " + it);
it.renderDebug(debugDrawProperty);
}
//debugDrawProperty.drawCone(2, 5, 9, 12, Matrix4f.identity(), new Color(1,1,0,1));
//debugDrawProperty.drawSquare(new Vector3f(1,1,1), Matrix4f.identity(), new Color(1,1,0,1));
//debugDrawProperty.drawCubeLine(new Vector3f(1,1,1), new Vector3f(5,5,5), new Color(1,0,1,1), Matrix4f.identity(), true, true);
//debugDrawProperty.drawCubeLine(new Vector3f(0,0,0), new Vector3f(32,32,32), new Color(1,0,1,1), Matrix4f.identity(), true, true);
}
@Override

View File

@ -14,6 +14,7 @@ import org.atriaSoft.gameEngine.Material;
import org.atriaSoft.gameEngine.components.ComponentLight;
import org.atriaSoft.gameEngine.components.ComponentLightSun;
import org.atriaSoft.gameEngine.components.ComponentMaterials;
import org.atriaSoft.gameEngine.components.ComponentPhysics;
import org.atriaSoft.gameEngine.components.ComponentPosition;
import org.atriaSoft.gameEngine.components.ComponentRenderTexturedMaterialsDynamicMeshs;
import org.atriaSoft.gameEngine.components.ComponentRenderTexturedMaterialsStaticMeshs;
@ -24,6 +25,8 @@ import org.atriaSoft.gameEngine.components.ComponentTexture;
import org.atriaSoft.gameEngine.components.ComponentTextures;
import org.atriaSoft.gameEngine.engines.EngineLight;
import org.atriaSoft.gameEngine.engines.EngineMap;
import org.atriaSoft.gameEngine.physics.PhysicBox;
import org.atriaSoft.gameEngine.physics.PhysicMapVoxel;
public class MapVoxel extends EngineMap {
//List<VoxelChunk> listOfChunks = new ArrayList<VoxelChunk>();
@ -71,6 +74,10 @@ public class MapVoxel extends EngineMap {
new Uri("DATA", "basicMaterial.vert"),
new Uri("DATA", "basicMaterial.frag"),
(EngineLight)env.getEngine(EngineLight.ENGINE_NAME)));
ComponentPhysics physics = new ComponentPhysics();
PhysicMapVoxel box = new PhysicMapVoxel(tmpVoxelChunk);
physics.addShape(box);
tmpEntity.addComponent(physics);
this.env.addEntity(tmpEntity);
}

View File

@ -0,0 +1,36 @@
package org.atriaSoft.gameEngine.physics;
import org.atriaSoft.etk.Color;
import org.atriaSoft.etk.math.Transform3D;
import org.atriaSoft.etk.math.Vector3f;
import org.atriaSoft.gale.resource.ResourceColored3DObject;
public class PhysicBox extends PhysicShape {
// Box size property in X, Y and Z
private Vector3f size = new Vector3f(1,1,1);
public PhysicBox() {
super(PhysicShapeType.BOX);
}
public Vector3f getSize() {
return size;
}
public void setSize(Vector3f size) {
this.size = size;
}
@Override
public void updateAABB(Transform3D transform, PhysicCollisionAABB aabb) {
// TODO Auto-generated method stub
aabb.update(transform.multiply(this.transform.multiply(new Vector3f(this.size.x*0.5f,this.size.y*0.5f,this.size.z*0.5f))));
aabb.update(transform.multiply(this.transform.multiply(new Vector3f(-this.size.x*0.5f,this.size.y*0.5f,this.size.z*0.5f))));
aabb.update(transform.multiply(this.transform.multiply(new Vector3f(-this.size.x*0.5f,-this.size.y*0.5f,this.size.z*0.5f))));
aabb.update(transform.multiply(this.transform.multiply(new Vector3f(this.size.x*0.5f,-this.size.y*0.5f,this.size.z*0.5f))));
aabb.update(transform.multiply(this.transform.multiply(new Vector3f(this.size.x*0.5f,this.size.y*0.5f,-this.size.z*0.5f))));
aabb.update(transform.multiply(this.transform.multiply(new Vector3f(-this.size.x*0.5f,this.size.y*0.5f,-this.size.z*0.5f))));
aabb.update(transform.multiply(this.transform.multiply(new Vector3f(-this.size.x*0.5f,-this.size.y*0.5f,-this.size.z*0.5f))));
aabb.update(transform.multiply(this.transform.multiply(new Vector3f(this.size.x*0.5f,-this.size.y*0.5f,-this.size.z*0.5f))));
}
@Override
public void renderDebug(Transform3D transform, ResourceColored3DObject debugDrawProperty) {
debugDrawProperty.drawSquare(this.size.multiply_new(0.5f), this.transform.getOpenGLMatrix().multiply_new(transform.getOpenGLMatrix()), new Color(0,1,0,0.25f));
}
}

View File

@ -0,0 +1,74 @@
package org.atriaSoft.gameEngine.physics;
import org.atriaSoft.etk.math.Vector3f;
public class PhysicCollisionAABB {
public float minX;
public float minY;
public float minZ;
public float maxX;
public float maxY;
public float maxZ;
public PhysicCollisionAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
super();
this.minX = minX;
this.minY = minY;
this.minZ = minZ;
this.maxX = maxX;
this.maxY = maxY;
this.maxZ = maxZ;
}
public boolean intersect(PhysicCollisionAABB other) {
if (minX > other.maxX) {
return false;
}
if (maxX < other.minX) {
return false;
}
if (minY > other.maxY) {
return false;
}
if (maxY < other.minY) {
return false;
}
if (minZ > other.maxZ) {
return false;
}
if (maxZ < other.minZ) {
return false;
}
return true;
}
public void update(Vector3f point) {
if (minX > point.x) {
minX = point.x;
}
if (maxX < point.x) {
maxX = point.x;
}
if (minY > point.y) {
minY = point.y;
}
if (maxY < point.y) {
maxY = point.y;
}
if (minZ > point.z) {
minZ = point.z;
}
if (maxZ < point.z) {
maxZ = point.z;
}
}
public Vector3f getMin() {
return new Vector3f(minX, minY, minZ);
}
public Vector3f getMax() {
return new Vector3f(maxX, maxY, maxZ);
}
public static PhysicCollisionAABB beforeCalculated() {
// TODO Auto-generated method stub
return new PhysicCollisionAABB(999999,999999,999999,-999999,-999999,-999999);
}
}

View File

@ -0,0 +1,31 @@
package org.atriaSoft.gameEngine.physics;
import org.atriaSoft.etk.math.Transform3D;
import org.atriaSoft.etk.math.Vector3f;
import org.atriaSoft.gale.resource.ResourceColored3DObject;
import org.atriaSoft.gameEngine.map.VoxelChunk;
public class PhysicMapVoxel extends PhysicShape {
// Box size property in X, Y and Z
private VoxelChunk chunk;
public PhysicMapVoxel(VoxelChunk chunk) {
super(PhysicShapeType.MAP_VOXEL);
this.chunk = chunk;
}
@Override
public void updateAABB(Transform3D transform, PhysicCollisionAABB aabb) {
if (this.chunk == null) {
return;
}
aabb.update(new Vector3f(this.chunk.getPosition().x,this.chunk.getPosition().y,this.chunk.getPosition().z));
aabb.update(new Vector3f(
this.chunk.getPosition().x + VoxelChunk.VOXEL_CHUNK_SIZE,
this.chunk.getPosition().y + VoxelChunk.VOXEL_CHUNK_SIZE,
this.chunk.getPosition().z + VoxelChunk.VOXEL_CHUNK_SIZE));
}
@Override
public void renderDebug(Transform3D transform, ResourceColored3DObject debugDrawProperty) {
}
}

View File

@ -0,0 +1,59 @@
package org.atriaSoft.gameEngine.physics;
import org.atriaSoft.etk.math.Quaternion;
import org.atriaSoft.etk.math.Transform3D;
import org.atriaSoft.etk.math.Vector3f;
import org.atriaSoft.gale.resource.ResourceColored3DObject;
public abstract class PhysicShape {
// protected Quaternion quaternion;
// protected Vector3f origin;
protected Transform3D transform;
protected float mass = 0;
protected final PhysicShapeType type;
public PhysicShape(PhysicShapeType type) {
this.type = type;
this.transform = new Transform3D();
// this.quaternion = Quaternion.identity();
// this.origin = Vector3f.zero();
this.mass = 0;
}
public PhysicShape(PhysicShapeType type, Quaternion quaternion, Vector3f origin, float mass) {
this.type = type;
this.transform = new Transform3D(origin, quaternion);
// this.quaternion = quaternion;
// this.origin = origin;
this.mass = mass;
}
public Quaternion getQuaternion() {
return transform.getOrientation();
}
public void setQuaternion(Quaternion quaternion) {
this.transform.setOrientation(quaternion);
}
public Vector3f getOrigin() {
return this.transform.getPosition();
}
public void setOrigin(Vector3f origin) {
this.transform.setPosition(origin);
}
public Transform3D getTransform() {
return transform;
}
public void setTransform(Transform3D transform) {
this.transform = transform;
}
public float getMass() {
return mass;
}
public void setMass(float mass) {
this.mass = mass;
}
public PhysicShapeType getType() {
return type;
}
public abstract void updateAABB(Transform3D transform, PhysicCollisionAABB aabb);
public abstract void renderDebug(Transform3D transform, ResourceColored3DObject debugDrawProperty);
}

View File

@ -0,0 +1,13 @@
package org.atriaSoft.gameEngine.physics;
public enum PhysicShapeType {
UNKNOWN,
BOX,
CAPSULE,
CONE,
CONVEXHULL,
CYLINDER,
SPHERE,
CONCAVE,
MAP_VOXEL
}

View File

@ -0,0 +1,34 @@
package org.atriaSoft.gameEngine.physics;
import org.atriaSoft.etk.Color;
import org.atriaSoft.etk.math.Transform3D;
import org.atriaSoft.etk.math.Vector3f;
import org.atriaSoft.gale.resource.ResourceColored3DObject;
public class PhysicSphere extends PhysicShape {
// Box size property in X, Y and Z
private float size;
public PhysicSphere() {
super(PhysicShapeType.SPHERE);
}
public float getSize() {
return size;
}
public void setSize(float size) {
this.size = size;
}
@Override
public void updateAABB(Transform3D transform, PhysicCollisionAABB aabb) {
aabb.update(transform.multiply(this.transform.getPosition()).add_new(new Vector3f(this.size,0,0)));
aabb.update(transform.multiply(this.transform.getPosition()).add_new(new Vector3f(-this.size,0,0)));
aabb.update(transform.multiply(this.transform.getPosition()).add_new(new Vector3f(0,this.size,0)));
aabb.update(transform.multiply(this.transform.getPosition()).add_new(new Vector3f(0,-this.size,0)));
aabb.update(transform.multiply(this.transform.getPosition()).add_new(new Vector3f(0,0,this.size)));
aabb.update(transform.multiply(this.transform.getPosition()).add_new(new Vector3f(0,0,-this.size)));
}
@Override
public void renderDebug(Transform3D transform, ResourceColored3DObject debugDrawProperty) {
debugDrawProperty.drawSphere(this.size, 9, 9, this.transform.getOpenGLMatrix().multiply_new(transform.getOpenGLMatrix()), new Color(0,1,0,1));
}
}

View File

@ -1,7 +1,7 @@
package org.atriaSoft.gameEngine.sample.LoxelEngine;
public class Log {
private static String LIBNAME = "Sample1";
private static String LIBNAME = "LoxelEngine";
public static void print(String data) {
System.out.println(data);
}

View File

@ -23,6 +23,7 @@ import org.atriaSoft.gameEngine.camera.Camera;
import org.atriaSoft.gameEngine.components.ComponentLight;
import org.atriaSoft.gameEngine.components.ComponentLightSun;
import org.atriaSoft.gameEngine.components.ComponentMaterial;
import org.atriaSoft.gameEngine.components.ComponentPhysics;
import org.atriaSoft.gameEngine.components.ComponentPlayer;
import org.atriaSoft.gameEngine.components.ComponentPosition;
import org.atriaSoft.gameEngine.components.ComponentPositionPlayer;
@ -33,6 +34,7 @@ import org.atriaSoft.gameEngine.components.ComponentStaticMesh;
import org.atriaSoft.gameEngine.components.ComponentTexture;
import org.atriaSoft.gameEngine.engines.EngineLight;
import org.atriaSoft.gameEngine.map.MapVoxel;
import org.atriaSoft.gameEngine.physics.PhysicBox;
import org.atriaSoft.gameEngine.tools.MeshGenerator;
import org.atriaSoft.gale.key.KeyKeyboard;
import org.atriaSoft.gale.key.KeySpecial;
@ -46,7 +48,6 @@ public class LoxelApplication extends Application {
private Quaternion basicRotation2 = Quaternion.identity();
private boolean creationDone;
private ControlCameraPlayer simpleControl;
private Material materialCube;
private ComponentPosition lightPosition;
private float angleLight = 0;
private MapVoxel map;
@ -92,17 +93,24 @@ public class LoxelApplication extends Application {
env.addEntity(gird);
Entity player = new Entity(this.env);
objectPosition = new ComponentPositionPlayer(new Transform3D(new Vector3f(16,16,15)));
objectPosition = new ComponentPositionPlayer(new Transform3D(new Vector3f(5,5,13)));
player.addComponent(objectPosition);
objectPlayer = new ComponentPlayer();
player.addComponent(objectPlayer);
player.addComponent(new ComponentMaterial(new Material()));
player.addComponent(new ComponentStaticMesh(new Uri("RES", "person.obj")));
//player.addComponent(new ComponentStaticMesh(new Uri("RES", "person.obj")));
player.addComponent(new ComponentStaticMesh(new Uri("RES", "person_-yfw_zup.obj")));
player.addComponent(new ComponentTexture(new Uri("RES", "playerTexture.png")));
// player.addComponent(new ComponentRenderTexturedMaterialsStaticMesh(
// new Uri("DATA", "basicMaterial.vert"),
// new Uri("DATA", "basicMaterial.frag"),
// (EngineLight)env.getEngine(EngineLight.ENGINE_NAME)));
player.addComponent(new ComponentRenderTexturedMaterialsStaticMesh(
new Uri("DATA", "basicMaterial.vert"),
new Uri("DATA", "basicMaterial.frag"),
(EngineLight)env.getEngine(EngineLight.ENGINE_NAME)));
ComponentPhysics physics = new ComponentPhysics();
PhysicBox box = new PhysicBox();
box.setSize(new Vector3f(0.6f,0.6f,1.8f));
box.setOrigin(new Vector3f(0,0,0.9f));
physics.addShape(box);
player.addComponent(physics);
env.addEntity(player);
@ -130,10 +138,6 @@ public class LoxelApplication extends Application {
if (this.creationDone == false) {
return;
}
//materialCube.setAmbientFactor(new Vector3f(1.0f,1.0f,1.0f));
// apply a little rotation to show the element move
//objectPosition.getTransform().applyRotation(basicRotation);
//objectPosition.getTransform().applyRotation(basicRotation2);
angleLight += 0.01;
lightPosition.getTransform().getPosition().x = 5 + (float)Math.cos(angleLight) * 7.0f;
lightPosition.getTransform().getPosition().y = 5 + (float)Math.sin(angleLight) * 7.0f;
@ -169,7 +173,6 @@ public class LoxelApplication extends Application {
OpenGL.enable(Flag.flag_depthTest);
env.render(20, "default");
// Restore context of matrix
OpenGL.pop();
}
@ -189,6 +192,12 @@ public class LoxelApplication extends Application {
if (type == KeyKeyboard.f1 ) {
Gale.getContext().grabPointerEvents(false, new Vector2f(0,0));
}
if (type == KeyKeyboard.f2 ) {
Gale.getContext().grabPointerEvents(true, new Vector2f(0,0));
}
if (type == KeyKeyboard.f12 ) {
Gale.getContext().setFullScreen(!Gale.getContext().getFullScreen());
}
env.onKeyboard(special, type, value, state);
}
}