Model3D improvements

audio-improve-offer
Harald Christian Joachim Wolff 2017-05-06 12:37:18 +02:00
parent 1f0b990989
commit 18b2099542
4 changed files with 297 additions and 210 deletions

View File

@ -27,7 +27,6 @@ namespace org.niclasundharald.engine.graphics
}
private void load(TextReader reader,float scale){
List<Vector3> vertexes,normals;
List<Vector4> colors;
List<triangle> triangles;
string line;
@ -36,7 +35,6 @@ namespace org.niclasundharald.engine.graphics
vertexes = new List<Vector3>();
normals = new List<Vector3>();
colors = new List<Vector4>();
triangles = new List<triangle>();
vertexes.Add(new Vector3());
@ -66,7 +64,7 @@ namespace org.niclasundharald.engine.graphics
max[2] = max[2] < z ? z : max[2];
break;
case "vn":
normals.Add( -(new Vector3(
normals.Add( (new Vector3(
float.Parse(fields[1],CultureInfo.InvariantCulture),
float.Parse(fields[2],CultureInfo.InvariantCulture),
float.Parse(fields[3],CultureInfo.InvariantCulture)
@ -107,8 +105,10 @@ namespace org.niclasundharald.engine.graphics
normals.Add( t.nc );
}
// bind(vertexes.ToArray(),null,normals.ToArray());
bind(vertexes.ToArray(),null,normals.ToArray());
this.vertexes = vertexes.ToArray();
this.normals = normals.ToArray();
bind();
}

View File

@ -3,6 +3,8 @@ using System;
using OpenTK.Graphics.OpenGL;
using OpenTK;
using org.niclasundharald.engine;
namespace org.niclasundharald.engine.graphics {
public class Model3D {
@ -14,9 +16,11 @@ namespace org.niclasundharald.engine.graphics {
protected int nTriangles;
Vector3[] vertexes;
Vector4[] colors;
Vector3[] normals;
public Vector3[] vertexes = null;
public Vector4[] colors = null;
public Vector3[] normals = null;
public Triangle[] triangles = new Triangle[0];
protected Model3D(){
@ -60,65 +64,96 @@ namespace org.niclasundharald.engine.graphics {
GL.BindVertexArray(0);
}
protected void rebind(){
public void prepareBuffers(int nTriangles){
this.vertexes = new Vector3[nTriangles * 3];
this.normals = new Vector3[nTriangles * 3];
this.colors = new Vector4[nTriangles * 3];
this.vertexes.Fill(new Vector3(0,0,0));
this.colors.Fill(new Vector4(0.5f,0.5f,0.5f,1.0f));
this.nTriangles = nTriangles;
rebind();
}
protected void bind(Vector3[] vertexes){
bind( vertexes, null, null, null );
}
protected void bind(Vector3[] vertexes,Vector4[] colors){
bind( vertexes, colors, null, null );
}
protected void bind(Vector3[] vertexes,Vector4[] colors,Vector3[] normals){
bind( vertexes, colors, normals, null );
}
protected void bind(Vector3[] vertexes,Vector4[] colors,Vector3[] normals,Vector2[] uvs){
/* float[] fv = new float[vertexes.Length * 3];
for (int n=0;n<vertexes.Length;n++){
fv[(n * 3) ] = vertexes[n].X;
fv[(n * 3) + 1] = vertexes[n].Y;
fv[(n * 3) + 2] = vertexes[n].Z;
public void bind(){
this.nTriangles = this.vertexes.Length / 3;
if (this.vertexes == null){
this.vertexes = new Vector3[nTriangles * 3];
this.vertexes.Fill(new Vector3(0,0,0));
}
*/
if (this.normals == null){
this.normals = new Vector3[nTriangles * 3];
}
if (this.colors == null){
this.colors = new Vector4[nTriangles * 3];
this.colors.Fill(new Vector4(0.5f,0.5f,0.5f,1.0f));
}
rebind();
}
public void rebind(){
bindVertexes();
bindNormals();
bindColors();
}
protected void bindVertexes(){
GL.BindVertexArray(vao);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
// GL.BufferData(BufferTarget.ArrayBuffer, 4 * fv.Length, fv, BufferUsageHint.StaticDraw);
GL.BufferData(BufferTarget.ArrayBuffer, 12 * vertexes.Length, vertexes, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(0);
nTriangles = vertexes.Length / 3;
if (this.triangles.Length != nTriangles){
int nt = this.triangles.Length;
if (colors == null){
colors = new Vector4[ vertexes.Length ];
Array.Resize<Triangle>(ref this.triangles,nTriangles);
for (int n=0;n<colors.Length;n++){
colors[n] = new Vector4(0.5f,0.9f,0.9f,1.0f);
for (int n=nt;n<nTriangles;n++){
this.triangles[n] = new Triangle(this,n);
}
}
GL.BindVertexArray(0);
}
protected void bindNormals(){
GL.BindVertexArray(vao);
GL.BindBuffer(BufferTarget.ArrayBuffer, nbo);
GL.BufferData(BufferTarget.ArrayBuffer, 12 * normals.Length, normals, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(2);
GL.BindVertexArray(0);
}
protected void bindColors(){
GL.BindVertexArray(vao);
GL.BindBuffer(BufferTarget.ArrayBuffer, cbo);
GL.BufferData(BufferTarget.ArrayBuffer, 16 * colors.Length, colors, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(1);
if (normals == null){
normals = computeNormals(vertexes);
}
/*
float[] fn = v3aToArray(normals);
*/
GL.BindBuffer(BufferTarget.ArrayBuffer, nbo);
GL.BufferData(BufferTarget.ArrayBuffer, 12 * normals.Length, normals, BufferUsageHint.StaticDraw);
GL.BindVertexArray(0);
}
GL.EnableVertexAttribArray(2);
protected void bind(Vector3[] vertexes,Vector4[] colors,Vector3[] normals,Vector2[] uvs){
if (vertexes != null){
this.vertexes = vertexes;
}
if (colors != null){
this.colors = colors;
}
if (normals != null){
this.normals = normals;
}
bindVertexes();
bindColors();
bindNormals();
}
public virtual void draw(){
@ -135,39 +170,23 @@ namespace org.niclasundharald.engine.graphics {
vertexes[(3 * n)+2] = c;
}
public static Vector3[] computeNormals(Vector3[] vertexes){
Vector3[] normals = new Vector3[ vertexes.Length ];
public void computeNormals(){
if ((normals == null)||(normals.Length != vertexes.Length)){
normals = new Vector3[vertexes.Length];
}
for (int n=0;n<vertexes.Length-2;n+=3){
Vector3 normal = Vector3.Cross( vertexes[n+1] - vertexes[n], vertexes[n+2] - vertexes[n] );
Vector3 normal = Vector3.Cross( vertexes[n+2] - vertexes[n], vertexes[n+1] - vertexes[n] );
normal.Normalize();
/*
Console.WriteLine("Normal Length: {0} Normal: {1}",normal.Length, normal);
if (normal.Length != 1){
Console.WriteLine("!!! Broken Normal !!!");
Console.WriteLine("Triangle: {0} {1} {2}",vertexes[n],vertexes[n+1],vertexes[n+2]);
}
*/
normals[(n) ] = normal;
normals[(n) + 1] = normal;
normals[(n) + 2] = normal;
}
return normals;
}
public static float[] v3aToArray(Vector3[] v3a){
float[] a = new float[ v3a.Length * 3];
for (int n=0;n<v3a.Length;n++){
a[ (3 * n) ] = v3a[n].X;
a[ (3 * n) + 1] = v3a[n].Y;
a[ (3 * n) + 2] = v3a[n].Z;
}
return a;
public Triangle[] Triangles {
get { return this.triangles; }
}
@ -175,5 +194,74 @@ namespace org.niclasundharald.engine.graphics {
return new LoadedModel(filename,scale);
}
public class Triangle{
Model3D model;
int n;
public Triangle(Model3D model,int n){
this.model = model;
this.n = n;
}
public void vertexes(ref Vector3 a,ref Vector3 b,ref Vector3 c){
a = model.vertexes[(3*n)];
b = model.vertexes[(3*n)+1];
c = model.vertexes[(3*n)+2];
}
public void vertexes(Vector3 a,Vector3 b,Vector3 c){
model.vertexes[(3*n)] = a;
model.vertexes[(3*n)+1] = b;
model.vertexes[(3*n)+2] = c;
}
public void vertex(int a,ref Vector3 v){
v = model.vertexes[(3*n) + a];
}
public void normal(int a,ref Vector3 n){
n = model.vertexes[(3*this.n) + a];
}
public void color(int a,ref Vector4 c){
c = model.colors[(3*n)+a];
}
public Vector3 VertexA {
get { return model.vertexes[(3*n)]; }
set { model.vertexes[(3*n)] = value; }
}
public Vector3 VertexB {
get { return model.vertexes[(3*n)+1]; }
set { model.vertexes[(3*n)+1] = value; }
}
public Vector3 VertexC {
get { return model.vertexes[(3*n)+2]; }
set { model.vertexes[(3*n)+2] = value; }
}
public Vector4 ColorA {
get { return model.colors[(3*n)]; }
set { model.colors[(3*n)] = value; }
}
public Vector4 ColorB {
get { return model.colors[(3*n)+1]; }
set { model.colors[(3*n)+1] = value; }
}
public Vector4 ColorC {
get { return model.colors[(3*n)+2]; }
set { model.colors[(3*n)+2] = value; }
}
public void updateNormals(){
Vector3 normal = Vector3.Cross( model.vertexes[(3*n)+1] - model.vertexes[(3*n)], model.vertexes[(3*n)+2] - model.vertexes[(3*n)] );
normal.Normalize();
model.normals[(3*n)] = normal;
model.normals[(3*n)+1] = normal;
model.normals[(3*n)+2] = normal;
}
}
}
}

View File

@ -17,14 +17,12 @@ namespace org.niclasundharald.engine.graphics
{
public static SquaredMap activeMap;
private static float edge = 5;
private static float maxHeight = 128.0f;
public static float edge = 5;
public static readonly float maxHeight = 128.0f;
private int width,
height;
private float[] heightMap;
private MapModel3D model;
public SquaredMap(int width,int height)
@ -34,11 +32,7 @@ namespace org.niclasundharald.engine.graphics
this.width = width;
this.height = height;
this.heightMap = new float[ (width + 1) * (height + 1) ];
generateHeightMap();
computeModel();
generateBaseMap();
}
public override Model3D getModel3D()
@ -53,148 +47,166 @@ namespace org.niclasundharald.engine.graphics
this.width = heightMap.Width-1;
this.height = heightMap.Height-1;
this.heightMap = new float[ (width + 1) * (height + 1) ];
generateBaseMap();
loadHeightMap( heightMap );
loadHeightMap(heightMap);
computeModel();
model.rebind();
}
public void generateHeightMap(){
Random rand = new Random();
for (int y = 0; y <= height; y++)
{
for (int x = 0; x <= width; x++)
{
float h = (float)rand.NextDouble();
heightMap[x + (y * width)] = (float)(maxHeight * (h * h));
protected void generateBaseMap(){
this.model = new MapModel3D(this,width,height);
for (int y=0;y<height;y++){
for (int x=0;x<width;x++){
int bi = 4 * (x + (y * width));
float cx = (edge * x) + (edge / 2);
float cy = (edge * y) + (edge / 2);
Vector3 a = new Vector3(),b = new Vector3(),c = new Vector3();
a.X = (edge * (x+1));
a.Y = (edge * y);
b.X = (edge * x);
b.Y = (edge * y);
c.X = cx;
c.Y = cy;
model.triangles[bi].vertexes(c,b,a);
a.X = (edge * (x+1));
a.Y = (edge * (y+1));
b.X = (edge * (x+1));
b.Y = (edge * y);
c.X = cx;
c.Y = cy;
model.triangles[bi+1].vertexes(c,b,a);
a.X = (edge * x);
a.Y = (edge * (y+1));
b.X = (edge * (x+1));
b.Y = (edge * (y+1));
c.X = cx;
c.Y = cy;
model.triangles[bi+2].vertexes(c,b,a);
a.X = (edge * x);
a.Y = (edge * y);
b.X = (edge * x);
b.Y = (edge * (y+1));
c.X = cx;
c.Y = cy;
model.triangles[bi+3].vertexes(c,b,a);
}
}
}
public void loadHeightMap(Image _heightMap){
Rgba32[] pixels = _heightMap.Pixels;
for (int y = 0; y <= height; y++)
for (int y = 0; y < height; y++)
{
for (int x = 0; x <= width; x++)
for (int x = 0; x < width; x++)
{
Rgba32 pixel = pixels[ x + (y * (width + 1))];
float h = (pixel.R + pixel.G + pixel.B) * maxHeight / 768;
heightMap[x + (y * (width + 1))] = (float)(h);
}
}
}
Rgba32[] pixel = new Rgba32[4];
float[] h = new float[4];
float ah = 0;
public void computeModel(){
float left,
right,
bottom,
top,
vcenter,
hcenter;
pixel[0] = pixels[ x + ((y+0) * (width + 1)) + 0 ];
pixel[1] = pixels[ x + ((y+0) * (width + 1)) + 1 ];
pixel[2] = pixels[ x + ((y+1) * (width + 1)) + 1 ];
pixel[3] = pixels[ x + ((y+1) * (width + 1)) + 0 ];
Vector3[] vertexes = new Vector3[ 12 * width * height ];
for (int n=0;n<4;n++){
h[n] = (pixel[n].R + pixel[n].G + pixel[n].B) * maxHeight / 768;
ah += h[n]/4;
}
Console.WriteLine("Creating geometry");
int bi = 12 * (x + (y * width));
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
int i = x + ((width + 1) * y);
int j = x + ((width + 1) * (y + 1));
model.vertexes[ bi++ ].Z = ah;
model.vertexes[ bi++ ].Z = h[0];
model.vertexes[ bi++ ].Z = h[1];
model.vertexes[ bi++ ].Z = ah;
model.vertexes[ bi++ ].Z = h[1];
model.vertexes[ bi++ ].Z = h[2];
model.vertexes[ bi++ ].Z = ah;
model.vertexes[ bi++ ].Z = h[2];
model.vertexes[ bi++ ].Z = h[3];
model.vertexes[ bi++ ].Z = ah;
model.vertexes[ bi++ ].Z = h[3];
model.vertexes[ bi++ ].Z = h[0];
float[] h = new float[4];
h[0] = heightMap[i];
h[1] = heightMap[i+1];
h[2] = heightMap[j];
h[3] = heightMap[j+1];
float ah = (h[0] + h[1] + h[2] + h[3]) / 4.0f;
left = edge * x;
right = edge * (x + 1);
bottom = edge * y;
top = edge * (y + 1);
vcenter = (top + bottom) / 2.0f;
hcenter = (left + right) / 2.0f;
int b = 4 * (x + (width * y));
Model3D.setTriangle(
vertexes,
b,
new Vector3(left, top, h[2]),
new Vector3(right, top, h[3]),
new Vector3(hcenter,vcenter,ah)
);
Model3D.setTriangle(
vertexes,
b + 1,
new Vector3(right, top, h[3]),
new Vector3(right, bottom, h[1]),
new Vector3(hcenter, vcenter, ah)
);
Model3D.setTriangle(
vertexes,
b + 2,
new Vector3(right, bottom, h[1]),
new Vector3(left, bottom, h[0]),
new Vector3(hcenter, vcenter, ah)
);
Model3D.setTriangle(
vertexes,
b + 3,
new Vector3(left, bottom, h[0]),
new Vector3(left, top, h[2]),
new Vector3(hcenter, vcenter, ah)
);
}
}
this.model = new MapModel3D( vertexes );
model.computeNormals();
model.colorMap();
}
private int indHeightMap(int x,int y){
return x + ((width + 1) * y);
private int tileIndex(int x,int y){
return (x + (width * y)) * 4;
}
public Vector3 ground(Vector2 _xy){
Vector2 xy = _xy / edge;
int i1 = indHeightMap((int)xy.X,(int)xy.Y);
int i2 = indHeightMap((int)xy.X,(int)xy.Y+1);
if ((xy.X < 0)||(xy.Y<0)||(xy.X>=width)||(xy.Y>=height)){
throw new OutOfWorldException();
}
float h = heightMap[i1] +
heightMap[i1 + 1] +
heightMap[i2] +
heightMap[i2 + 1];
int i1 = tileIndex((int)xy.X,(int)xy.Y);
h /= 4;
for (int n=0;n<4;n++){
Vector3 A,B,C,P,V;
Vector3 p = new Vector3();
Vector3 v3 = new Vector3(_xy.X,_xy.Y,h);
Console.WriteLine("Grounding: {0}",v3);
A = model.triangles[i1+n].VertexA;
B = model.triangles[i1+n].VertexB;
C = model.triangles[i1+n].VertexC;
return v3;
P = new Vector3(_xy);
V = Vector3.UnitZ;
if (Geometry.intersectTriangle(P,V,A,B,C,out p)){
Console.WriteLine("Grounded to: {0}",p);
return p;
} else {
Console.WriteLine("No Ground at triangle {0}",n);
}
}
throw new OutOfWorldException();
}
}
class MapModel3D : Model3D {
int nDraw;
public MapModel3D(Vector3[] vertexes){
SquaredMap map;
Vector4[] colors = new Vector4[ vertexes.Length ];
public MapModel3D(SquaredMap map,int width,int height){
this.map = map;
prepareBuffers(width * height * 4);
colorMap();
}
public void colorMap(){
for (int n=0; n < vertexes.Length; n++){
colors[n] = new Vector4(
0.50f + (0.2f * vertexes[n].Z / SquaredMap.maxHeight),
0.25f + (0.50f * vertexes[n].Z / SquaredMap.maxHeight),
0.10f + (0.80f * vertexes[n].Z / SquaredMap.maxHeight),
1.0f
);
}
}
/* Vector4[] colors = new Vector4[ vertexes.Length ];
for (int n=0; n< vertexes.Length; n++){
@ -240,20 +252,7 @@ namespace org.niclasundharald.engine.graphics
bind(vertexes, colors, normals);
}
public override void draw()
{
nDraw+= nTriangles >> 8;
if (nDraw > nTriangles){
nDraw = 0;
}
nDraw = nTriangles;
GL.BindVertexArray(this.vao);
GL.DrawArrays(PrimitiveType.Triangles, 0, nDraw * 3);
GL.BindVertexArray(0);
}
*/
}

View File

@ -21,7 +21,7 @@ namespace org.niclasundharald.engine.graphics.primitives
class CubeModel : Model3D {
public static Vector4[] colors = new Vector4[]{
public static Vector4[] ccolors = new Vector4[]{
new Vector4(1.0f,0.0f,0.0f,1.0f),
new Vector4(1.0f,0.0f,0.0f,1.0f),
new Vector4(1.0f,0.0f,0.0f,1.0f),
@ -55,30 +55,30 @@ namespace org.niclasundharald.engine.graphics.primitives
g = dx + dy + dz;
h = - dx + dy + dz;
Vector3[] vertexes = new Vector3[36];
prepareBuffers(12);
Model3D.setTriangle(vertexes, 0, a, b, c);
Model3D.setTriangle(vertexes, 1, c, d, a);
Model3D.setTriangle(vertexes, 2, f, e, h);
Model3D.setTriangle(vertexes, 3, h, g, f);
Model3D.setTriangle(vertexes, 0, c, b, a);
Model3D.setTriangle(vertexes, 1, a, d, c);
Model3D.setTriangle(vertexes, 2, h, e, f);
Model3D.setTriangle(vertexes, 3, f, g, h);
Model3D.setTriangle(vertexes, 4, e, a, d);
Model3D.setTriangle(vertexes, 5, d, h, e);
Model3D.setTriangle(vertexes, 6, b, f, g);
Model3D.setTriangle(vertexes, 7, g, c, b);
Model3D.setTriangle(vertexes, 4, d, a, e);
Model3D.setTriangle(vertexes, 5, e, h, d);
Model3D.setTriangle(vertexes, 6, g, f, b);
Model3D.setTriangle(vertexes, 7, b, c, g);
Model3D.setTriangle(vertexes, 8, a, e, f);
Model3D.setTriangle(vertexes, 9, f, b, a);
Model3D.setTriangle(vertexes, 10, g, h, d);
Model3D.setTriangle(vertexes, 11, d, c, g);
Vector4[] colors = new Vector4[ vertexes.Length ];
Model3D.setTriangle(vertexes, 8, f, e, a);
Model3D.setTriangle(vertexes, 9, a, b, f);
Model3D.setTriangle(vertexes, 10, d, h, g);
Model3D.setTriangle(vertexes, 11, g, c, d);
for (int n=0;n<colors.Length;n++){
colors[n] = CubeModel.colors[ n / 3 ];
this.colors[n] = CubeModel.ccolors[ n / 3 ];
}
bind( vertexes, colors );
computeNormals();
rebind();
}
}