audio-development
Harald Christian Joachim Wolff 2017-05-01 01:33:33 +02:00
parent f3d0fd6932
commit c299431480
27 changed files with 248227 additions and 178 deletions

View File

@ -11,8 +11,7 @@ using OpenTK.Audio;
using ImageSharp;
using org.nhengine.graphics;
using org.niclasundharald.engine.graphics;
namespace nhengine
{
@ -46,31 +45,28 @@ namespace nhengine
public void bootGraphics(){
glWindow = new OpenGLWindow();
glWindow.MakeCurrent();
GlobalDefaults.instance();
//graphics = new Graphics();
}
public void bootAudio(){
AL.Enable(ALCapability.Invalid);
}
public void bootMap(){
public void bootMap(){
ImageSharp.Image i = ImageSharp.Image.Load( new FileStream("M3-wip.png",FileMode.Open));
i.Flip(ImageSharp.Processing.FlipType.Vertical);
map = new SquaredMap(i);
glWindow.addGLObject(map);
glWindow.Scene.RootObject.addChild( map );
}
public void run(){
glWindow.MakeCurrent();
glWindow.Run(30,30);
}

View File

@ -6,8 +6,9 @@ using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK;
using org.nhengine.graphics;
using org.nhengine.graphics.primitives;
using org.niclasundharald.engine;
using org.niclasundharald.engine.graphics;
using org.niclasundharald.engine.graphics.primitives;
namespace nhengine
{
@ -20,15 +21,16 @@ namespace nhengine
private float arcUpDown,
arcLeftRight;
private Vector3 lookAt;
private float lookDistance;
private Cube cube,
c1, c2, c3;
GraphicsMode gmode = new GraphicsMode();
GLCamera scene;
List<GLObject> glObjects;
GLCamera sceneCamera;
GLScene scene;
public OpenGLWindow()
:base(800, 600,
GraphicsMode.Default,
@ -38,15 +40,15 @@ namespace nhengine
3, 3,
GraphicsContextFlags.ForwardCompatible)
{
glObjects = new List<GLObject>();
VSync = VSyncMode.On;
scene = new GLCamera();
sceneCamera = new GLCamera();
scene = new GLScene(sceneCamera);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GL.ClearColor(0.1f, 0.2f, 0.5f, 0.0f);
GL.ClearColor(0.05f, 0.05f, 0.05f, 0.0f);
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.Normalize);
GL.Enable(EnableCap.Blend);
@ -56,16 +58,23 @@ namespace nhengine
c1 = new Cube(new Vector3(512,0,512), 128.0f);
c2 = new Cube(new Vector3(0,512,512), 128.0f);
c3 = new Cube(new Vector3(512,512,512), 128.0f);
addGLObject( cube );
addGLObject( c1 );
addGLObject( c2 );
addGLObject( c3 );
scene.Position = new Vector3(16000,-5000,8000);
scene.View = new Vector3(0f,1f,-0.250f);
scene.Top = new Vector3(0,0,1);
scene.setFoV(MathHelper.PiOver4);
lookAt = new Vector3(0,0,0);
lookDistance = 2048;
arcUpDown = MathHelper.PiOver6;
arcLeftRight = MathHelper.PiOver4;
// sceneCamera.Position = new Vector3(1000,-1000,1000);
// sceneCamera.View = new Vector3(0f,1.0f,0.0f);
sceneCamera.Top = new Vector3(0,0,1);
sceneCamera.setFoV(MathHelper.PiOver4);
setupCamera();
scene.RootObject.addChild( cube );
scene.RootObject.addChild( c1 );
scene.RootObject.addChild( c2 );
scene.RootObject.addChild( c3 );
}
@ -74,29 +83,19 @@ namespace nhengine
base.OnResize(e);
GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
scene.setViewport(ClientRectangle.Width, ClientRectangle.Height);
sceneCamera.setViewport(ClientRectangle.Width, ClientRectangle.Height);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
foreach (GLObject glo in glObjects){
glo.paint(scene);
}
scene.draw();
SwapBuffers();
}
public void addGLObject(GLObject glObject){
this.glObjects.Add( glObject );
}
public void removeGLObject(GLObject glObject){
this.glObjects.Remove( glObject );
}
private void captureMouse(){
mouseCapturePosition = new Point(Mouse.X, Mouse.Y);
mouseCaptured = true;
@ -118,8 +117,8 @@ namespace nhengine
}
protected virtual void onMouseCapturedMove(Point delta){
rotateLeftRight(((float)delta.X) / 10.0f);
rotateUpDown(((float)delta.Y) / 10.0f);
rotateLeftRight(((float)delta.X) / 20.0f);
rotateUpDown(((float)delta.Y) / 20.0f);
}
protected override void OnMouseDown(OpenTK.Input.MouseButtonEventArgs e)
@ -140,7 +139,9 @@ namespace nhengine
protected override void OnMouseWheel(OpenTK.Input.MouseWheelEventArgs e)
{
walk(e.ValuePrecise);
lookDistance *= 1.0f - ( e.DeltaPrecise / 16 );
MathHelper.Clamp( lookDistance, 512, 8192 );
setupCamera();
}
protected override void OnKeyPress(KeyPressEventArgs e)
@ -151,10 +152,10 @@ namespace nhengine
{
switch (e.Key){
case OpenTK.Input.Key.Up:
rotateUpDown(MathHelper.Pi / 90.0f);
rotateUpDown(-MathHelper.Pi / 180.0f);
break;
case OpenTK.Input.Key.Down:
rotateUpDown(-MathHelper.Pi / 90.0f);
rotateUpDown(MathHelper.Pi / 180.0f);
break;
case OpenTK.Input.Key.Left:
rotateLeftRight(MathHelper.Pi / 90.0f);
@ -163,10 +164,10 @@ namespace nhengine
rotateLeftRight(-MathHelper.Pi / 90.0f);
break;
case OpenTK.Input.Key.W:
walk(100.0f);
walk(250.0f);
break;
case OpenTK.Input.Key.S:
walk(-100.0f);
walk(-250.0f);
break;
}
@ -175,25 +176,39 @@ namespace nhengine
public void rotateUpDown(float arc)
{
arcUpDown += arc;
applyRotation();
arcUpDown = MathHelper.Clamp(arcUpDown,MathHelper.Pi / 8,MathHelper.PiOver2-0.00001f);
setupCamera();
}
public void rotateLeftRight(float arc)
{
arcLeftRight += arc;
applyRotation();
arcLeftRight %= MathHelper.TwoPi;
setupCamera();
}
public void walk(float distance){
scene.Position += scene.View * distance;
}
private void applyRotation(){
Console.WriteLine("Rotation: {0} {1}", arcLeftRight, arcUpDown);
lookAt += (new Vector3(sceneCamera.View.Xy)).Normalized() * distance;
setupCamera();
}
protected override void OnKeyUp(OpenTK.Input.KeyboardKeyEventArgs e)
{
}
private void setupCamera(){
Vector3 view = (Matrix4.CreateRotationZ(arcLeftRight) * Matrix4.CreateRotationX(arcUpDown) * Vector4.UnitY).Xyz;
view.Normalize();
Vector3 pos = lookAt - (view * lookDistance);
sceneCamera.View = view;
sceneCamera.Position = pos;
}
public GLScene Scene {
get { return this.scene; }
}
}
}

View File

@ -7,48 +7,49 @@ using ImageSharp.PixelFormats;
using OpenTK.Graphics.OpenGL4;
using OpenTK;
using org.nhengine.graphics;
using org.niclasundharald.engine;
using org.niclasundharald.engine.graphics;
namespace nhengine
{
public class SquaredMap : GLObject
public class SquaredMap : WorldObject
{
private static float edge = 64;
private int width,
height;
private float[] heightMap;
private MapModel3D model;
public SquaredMap(int width,int height)
:base( 12 * width * height )
{
this.width = width;
this.height = height;
this.heightMap = new float[ (width + 1) * (height + 1) ];
normalsEnabled = true;
generateHeightMap();
computeGL();
updateGL();
computeModel();
}
public SquaredMap(Image heightMap)
:base(12 * heightMap.Width * heightMap.Height)
public override Model3D getModel3D()
{
this.width = heightMap.Width;
this.height = heightMap.Height;
return this.model;
}
public SquaredMap(Image heightMap)
{
this.width = heightMap.Width-1;
this.height = heightMap.Height-1;
this.heightMap = new float[ (width + 1) * (height + 1) ];
normalsEnabled = true;
loadHeightMap(heightMap);
computeGL();
updateGL();
computeModel();
}
@ -73,14 +74,14 @@ namespace nhengine
{
for (int x = 0; x <= width; x++)
{
Rgba32 pixel = pixels[ x + (y * _heightMap.Width)];
float h = (pixel.R + pixel.G + pixel.B);
heightMap[x + (y * width)] = (float)(h);
Rgba32 pixel = pixels[ x + (y * (width + 1))];
float h = (pixel.R + pixel.G + pixel.B) / 3;
heightMap[x + (y * (width + 1))] = (float)(h);
}
}
}
public void computeGL(){
public void computeModel(){
float left,
right,
bottom,
@ -88,6 +89,8 @@ namespace nhengine
vcenter,
hcenter;
Vector3[] vertexes = new Vector3[ 18 * width * height ];
Console.WriteLine("Creating geometry");
for (int x = 0; x < width; x++)
@ -105,34 +108,38 @@ namespace nhengine
float ah = (h[0] + h[1] + h[2] + h[3]) / 4.0f;
left = 128 * x;
right = 128 * (x + 1);
bottom = 128 * y;
top = 128 * (y + 1);
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));
setTriangle(
Model3D.setTriangle(
vertexes,
b,
new Vector3(left, top, h[2]),
new Vector3(right, top, h[3]),
new Vector3(hcenter,vcenter,ah)
);
setTriangle(
Model3D.setTriangle(
vertexes,
b + 1,
new Vector3(right, top, h[3]),
new Vector3(right, bottom, h[1]),
new Vector3(hcenter, vcenter, ah)
);
setTriangle(
Model3D.setTriangle(
vertexes,
b + 2,
new Vector3(right, bottom, h[1]),
new Vector3(left, bottom, h[0]),
new Vector3(hcenter, vcenter, ah)
);
setTriangle(
Model3D.setTriangle(
vertexes,
b + 3,
new Vector3(left, bottom, h[0]),
new Vector3(left, top, h[2]),
@ -140,25 +147,64 @@ namespace nhengine
);
}
}
/*
Console.WriteLine("Coloring...");
for (int n = 0; n < vertices.Length / 3;n++){
float _h = vertices[(3 * n) + 2] / 512.0f;
setColor(
n,
new Vector4(
0.3f + (0.7f * _h),
0.2f + (0.8f * _h),
0.4f + (0.6f * _h),
1.0f
)
)
}
*/
this.model = new MapModel3D( vertexes );
this.Position = new Vector3(-(width)*edge,-(height)*edge,0);
}
}
class MapModel3D : Model3D {
int nDraw;
public MapModel3D(Vector3[] vertexes){
Vector4[] colors = new Vector4[ vertexes.Length ];
for (int n=0; n< vertexes.Length; n++){
if (vertexes[n].Z >= 256 ){
Console.WriteLine("Max. Height : {0}",vertexes[n].Z / 256);
}
colors[n] = new Vector4(
0.50f + (0.2f * vertexes[n].Z / 256),
0.25f + (0.50f * vertexes[n].Z / 256),
0.10f + (0.80f * vertexes[n].Z / 256),
1.0f
);
}
bind(vertexes, colors);
}
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);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -76,11 +76,12 @@
<None Include="M3-wip.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="models\alfa147.obj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\org.nhengine\org.nhengine.csproj">
<ProjectReference Include="..\org.niclasundharald.engine\org.niclasundharald.engine.csproj">
<Project>{3E812F66-D5F3-4599-8360-97F355B6CC1B}</Project>
<Name>org.nhengine</Name>
<Name>org.niclasundharald.engine</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

View File

@ -15,7 +15,7 @@
<package id="System.IO.Compression" version="4.3.0" targetFramework="net45" />
<package id="System.Linq" version="4.3.0" targetFramework="net45" />
<package id="System.Linq.Expressions" version="4.3.0" targetFramework="net45" />
<package id="System.Net.Http" version="4.3.0" targetFramework="net45" />
<package id="System.Net.Http" version="4.3.1" targetFramework="net45" />
<package id="System.Net.Primitives" version="4.3.0" targetFramework="net45" />
<package id="System.Numerics.Vectors" version="4.3.0" targetFramework="net45" />
<package id="System.ObjectModel" version="4.3.0" targetFramework="net45" />

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nhengine", "NHEngine\nhengine.csproj", "{42BCFEF7-3F24-469A-BD46-E0C9C2C20D21}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "org.nhengine", "org.nhengine\org.nhengine.csproj", "{3E812F66-D5F3-4599-8360-97F355B6CC1B}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "org.niclasundharald.engine", "org.niclasundharald.engine\org.niclasundharald.engine.csproj", "{3E812F66-D5F3-4599-8360-97F355B6CC1B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -1,50 +0,0 @@
using System;
using OpenTK;
namespace org.nhengine.graphics.primitives
{
public class Cube : GLObject
{
public Cube(Vector3 position, float width)
:base(36)
{
Vector3 dx, dy, dz;
Vector3 a, b, c, d, e, f, g, h;
dx = (Vector3.UnitX * width / 2);
dy = (Vector3.UnitY * width / 2);
dz = (Vector3.UnitZ * width / 2);
a = - dx - dy - dz;
b = dx - dy - dz;
c = dx + dy - dz;
d = - dx + dy - dz;
e = - dx - dy + dz;
f = dx - dy + dz;
g = dx + dy + dz;
h = - dx + dy + dz;
normalsEnabled = true;
setTriangle(0, a, b, c);
setTriangle(1, c, d, a);
setTriangle(2, f, e, h);
setTriangle(3, h, g, f);
setTriangle(4, e, a, d);
setTriangle(5, d, h, e);
setTriangle(6, b, f, g);
setTriangle(7, g, c, b);
setTriangle(8, a, e, f);
setTriangle(9, f, b, a);
setTriangle(10, g, c, d);
setTriangle(11, d, h, g);
setColor(new Vector4(0.8f, 0.3f, 0.5f, 1.0f));
Position = position;
updateGL();
}
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.IO;
namespace org.niclasundharald.engine
{
public static class FileHelper
{
public static string findFile(string filename,string[] searchPaths){
foreach (string searchPath in searchPaths){
string fullname = String.Format("{0}{1}{2}",searchPath,Path.PathSeparator,filename);
if (File.Exists(fullname)){
return fullname;
}
}
return null;
}
}
}

View File

@ -0,0 +1,8 @@
Gameplay Object Tree
--------------------
NHWorld Boxes all Objects and Scenery of the gameplay
.Scenery[] A list of all objects that are part of the world

View File

@ -4,11 +4,11 @@ using System.Runtime.CompilerServices;
// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.
[assembly: AssemblyTitle("org.nhengine")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyTitle("org.niclasundharald.engine")]
[assembly: AssemblyDescription("Gaming Engine created by Niclas and Harald")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCompany("Niclas&Harald")]
[assembly: AssemblyProduct("GameEngine by Niclas and Harald")]
[assembly: AssemblyCopyright("${AuthorCopyright}")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using OpenTK;
using org.niclasundharald.engine.graphics;
namespace org.niclasundharald.engine
{
public abstract class WorldObject
{
private Matrix4 transformation;
private List<WorldObject> children;
public WorldObject()
{
transformation = Matrix4.Identity;
children = new List<WorldObject>();
}
public virtual Model3D getModel3D(){
return null;
}
public Vector3 Position{
get { return this.transformation.ExtractTranslation(); }
set { this.transformation += Matrix4.CreateTranslation(value - this.transformation.ExtractTranslation()); }
}
public Matrix4 Transformation {
get { return this.transformation; }
set { this.transformation = value; }
}
public void addChild(WorldObject wo){
if (!children.Contains(wo)){
children.Add(wo);
}
}
public void removeChild(WorldObject wo){
if (children.Contains(wo)){
children.Remove(wo);
}
}
public WorldObject[] Children{
get { return this.children.ToArray(); }
}
}
public class ObjectGroup : WorldObject {
}
}

View File

@ -4,7 +4,7 @@ using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;
using OpenTK;
namespace org.nhengine.graphics
namespace org.niclasundharald.engine.graphics
{
public class GLCamera : GLSceneOrientation
{
@ -60,6 +60,7 @@ namespace org.nhengine.graphics
get { return new Vector3(vView); }
set { this.vView = new Vector3(value).Normalized(); buildMatrices(); }
}
public Vector3 Top
{
get { return new Vector3(vTop); }

View File

@ -0,0 +1,29 @@
using System;
using System.IO;
using org.niclasundharald.engine.graphics;
namespace org.niclasundharald.engine
{
public class GLModel : GLObject
{
private static char[] whitespace = { };
public GLModel(String filename)
{
FileStream fs = new FileStream(filename,FileMode.Open);
load(fs);
fs.Close();
}
private void load(Stream stream){
StreamReader sr = new StreamReader(stream);
string line;
while ( (line = sr.ReadLine()) != null){
String[] tokens = line.Split(null);
}
}
}
}

View File

@ -1,11 +1,11 @@
using System;
using System;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;
using OpenTK;
namespace org.nhengine.graphics
namespace org.niclasundharald.engine.graphics
{
public class GLObject
{
@ -34,7 +34,17 @@ namespace org.nhengine.graphics
public GLObject(int vertexes)
{
shaderProgram = GlobalDefaults.instance().DefaultShaderProgram;
init(vertexes);
prepareGL();
}
public GLObject(){
shaderProgram = GlobalDefaults.instance().DefaultShaderProgram;
init(0);
prepareGL();
}
private void init(int vertexes){
this.vertices = new float[3 * vertexes];
this.colors = new float[4 * vertexes];
this.normals = new float[3 * vertexes];
@ -46,8 +56,7 @@ namespace org.nhengine.graphics
this.position = new Vector3();
this.matrix = Matrix4.Identity;
prepareGL();
}
protected void prepareGL()
@ -111,7 +120,7 @@ namespace org.nhengine.graphics
if (normalsEnabled)
{
GL.BindBuffer(BufferTarget.ArrayBuffer, nbo);
GL.BufferData(BufferTarget.ArrayBuffer, 4 * this.normals.Length, this.normals, BufferUsageHint.StaticDraw);
GL.BufferData(BufferTarget.ArrayBuffer, 3 * this.normals.Length, this.normals, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(2);
}
@ -190,8 +199,6 @@ namespace org.nhengine.graphics
public void paint(GLSceneOrientation scene)
{
shaderProgram.use(Matrix,scene);
GL.BindVertexArray(this.vao);
GL.DrawArrays(PrimitiveType.Triangles, 0, vertices.Length);

View File

@ -0,0 +1,64 @@
using System;
using OpenTK;
namespace org.niclasundharald.engine.graphics
{
public class GLScene
{
GLSceneOrientation sceneOrientation;
ShaderProgram shader;
WorldObject root;
public GLScene()
{
sceneOrientation = GlobalDefaults.instance().ActiveScene;
shader = GlobalDefaults.instance().DefaultShaderProgram;
root = new ObjectGroup();
}
public GLScene(GLSceneOrientation sceneOrientation)
{
this.sceneOrientation = sceneOrientation;
shader = GlobalDefaults.instance().DefaultShaderProgram;
root = new ObjectGroup();
}
public WorldObject RootObject {
get { return this.root; }
set { this.root = value; }
}
public void draw(){
Matrix4 objectMatrix = Matrix4.Identity;
shader.use();
if (root != null){
draw( objectMatrix, root );
}
}
public void draw(Matrix4 baseMatrix,WorldObject wo){
Matrix4 objectMatrix = baseMatrix * wo.Transformation;
Console.WriteLine("draw(): o={0}\noM=\n{1}",wo,objectMatrix);
shader.setup(objectMatrix, sceneOrientation);
Model3D m3d = wo.getModel3D();
if (m3d != null){
m3d.draw();
}
foreach (WorldObject child in wo.Children){
draw( objectMatrix, child );
}
}
}
}

View File

@ -3,7 +3,7 @@
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace org.nhengine.graphics
namespace org.niclasundharald.engine.graphics
{
public class GLSceneOrientation
{

View File

@ -2,7 +2,7 @@
using OpenTK.Graphics.OpenGL;
namespace org.nhengine.graphics
namespace org.niclasundharald.engine.graphics
{
public class GlobalDefaults
{
@ -68,19 +68,32 @@ out vec4 color;
out vec3 norm;
out vec2 uv;
out float shading;
void main()
{
gl_Position = mScene * vec4( iv_position, 1 );
norm = normalize( ( mObjectCameraIT * vec4( iv_normal, 1 ) ).xyz );
vec3 camray = normalize( -gl_Position.xyz );
float cosTheta = dot( norm , camray );
float fading = 1.0;
float visibility = 1.0;
if (cosTheta < 0){
}
color = vec4( iv_color.xyz * (0.5 + (0.5 * cosTheta * cosTheta)), visibility );
norm = normalize( ( mObjectCameraIT * vec4( iv_normal , 1 ) ).xyz );
vec3 camray = (mObjectCamera * vec4( iv_position , 1 )).xyz;
vec3 ncamray = normalize( camray );
float cosTheta = abs( clamp(dot( norm , ncamray ), -1, 1) );
float distance = length(camray);
float idist = 25000 / distance;
float sight = clamp( idist * idist * idist, 0.1, 1.0);
//sight = 1;
if (norm.z > 0){
fading = 0;
} else {
};
shading = clamp(0.3 + (cosTheta * 0.7), 0.1, 0.9);
color = clamp( vec4( iv_color.xyz * shading * sight, fading ), 0, 1);
}
";
@ -91,6 +104,8 @@ in vec4 color;
in vec3 norm;
in vec2 uv;
in float shading;
out vec4 _color;
void main()

View File

@ -0,0 +1,171 @@
using System;
using OpenTK.Graphics.OpenGL;
using OpenTK;
namespace org.niclasundharald.engine.graphics {
public class Model3D {
protected int vao,
vbo, // Vertex Buffer
cbo, // Color Buffer
nbo; // Normals Buffer
protected int nTriangles;
protected Model3D(){
vao = GL.GenVertexArray();
nTriangles = 0;
prepare();
}
protected void prepare(){
vbo = GL.GenBuffer();
cbo = GL.GenBuffer();
nbo = GL.GenBuffer();
GL.BindVertexArray(vao);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.VertexAttribPointer(0,
3,
VertexAttribPointerType.Float,
false,
0,
0);
GL.BindBuffer(BufferTarget.ArrayBuffer, cbo);
GL.VertexAttribPointer(1,
4,
VertexAttribPointerType.Float,
false,
0,
0);
GL.BindBuffer(BufferTarget.ArrayBuffer, nbo);
GL.VertexAttribPointer(2,
3,
VertexAttribPointerType.Float,
false,
0,
0);
GL.BindVertexArray(0);
}
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;
}
GL.BindVertexArray(vao);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BufferData(BufferTarget.ArrayBuffer, 4 * fv.Length, fv, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(0);
nTriangles = vertexes.Length / 3;
if (colors != null){
float[] fc = new float[ colors.Length * 4 ];
for (int n=0;n < colors.Length; n++){
fc[ (4*n) ] = colors[n].X;
fc[ (4*n) + 1 ] = colors[n].Y;
fc[ (4*n) + 2 ] = colors[n].Z;
fc[ (4*n) + 3 ] = colors[n].W;
}
GL.BindBuffer(BufferTarget.ArrayBuffer, cbo);
GL.BufferData(BufferTarget.ArrayBuffer, 4 * fc.Length, fc, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(1);
} else {
float[] fc = new float[ vertexes.Length * 4 ];
for (int n=0;n<fc.Length;n++){
fc[n] = n % 4 == 3 ? 1.0f : 0.5f;
}
GL.BindBuffer(BufferTarget.ArrayBuffer, cbo);
GL.BufferData(BufferTarget.ArrayBuffer, 4 * fc.Length, fc, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(1);
}
if (normals != null){
float[] fn = new float[ normals.Length * 3];
for (int n=0;n<normals.Length;n++){
fn[(n * 3) ] = normals[n].X;
fn[(n * 3) + 1] = normals[n].Y;
fn[(n * 3) + 2] = normals[n].Z;
}
GL.BindBuffer(BufferTarget.ArrayBuffer, nbo);
GL.BufferData(BufferTarget.ArrayBuffer, 3 * fn.Length, fn, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(2);
} else {
float[] fn = new float[fv.Length];
for (int n=0;n<vertexes.Length;n+=3){
Vector3 normal = Vector3.Cross( vertexes[n+1] - vertexes[n], vertexes[n+2] - vertexes[n] );
normal.Normalize();
fn[(n * 3) ] = normal.X;
fn[(n * 3) + 1] = normal.Y;
fn[(n * 3) + 2] = normal.Z;
fn[(n * 3) + 3] = normal.X;
fn[(n * 3) + 4] = normal.Y;
fn[(n * 3) + 5] = normal.Z;
fn[(n * 3) + 6] = normal.X;
fn[(n * 3) + 7] = normal.Y;
fn[(n * 3) + 8] = normal.Z;
}
GL.BindBuffer(BufferTarget.ArrayBuffer, nbo);
GL.BufferData(BufferTarget.ArrayBuffer, 3 * fn.Length, fn, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(2);
}
}
public virtual void draw(){
GL.BindVertexArray(this.vao);
GL.DrawArrays(PrimitiveType.Triangles, 0, nTriangles * 3);
GL.BindVertexArray(0);
}
public static void setTriangle(Vector3[] vertexes,int n, Vector3 a, Vector3 b, Vector3 c)
{
vertexes[(3 * n)] = a;
vertexes[(3 * n)+1] = b;
vertexes[(3 * n)+2] = c;
}
}
}

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
namespace org.niclasundharald.engine.graphics
{
public class ModelManager
{
Dictionary<string, Model3D> knownModels;
List<string> searchPaths;
public ModelManager()
{
knownModels = new Dictionary<string, Model3D>();
searchPaths = new List<string>();
searchPaths.Add(".");
}
public void addSearchPath(string sPath){
searchPaths.Add(sPath);
}
public void removeSearchPath(string sPath){
searchPaths.Remove(sPath);
}
public Model3D loadModel(String mName){
string fullName = FileHelper.findFile(String.Format("{0}.obj",mName),searchPaths.ToArray());
if (fullName != null){
}
return null;
}
public Model3D findModel(String mName){
if (!knownModels.ContainsKey(mName)){
return loadModel(mName);
} else {
return knownModels[mName];
}
}
}
}

View File

@ -3,7 +3,7 @@ using System.IO;
using OpenTK.Graphics.OpenGL;
namespace org.nhengine.graphics
namespace org.niclasundharald.engine.graphics
{
public class Shader
{

View File

@ -45,7 +45,7 @@ using OpenTK;
**/
namespace org.nhengine.graphics
namespace org.niclasundharald.engine.graphics
{
public class ShaderProgram
{
@ -162,14 +162,11 @@ namespace org.nhengine.graphics
}
}
public void use(GLSceneOrientation scene){
use(Matrix4.Identity, scene);
}
public void use(Matrix4 _mObject,GLSceneOrientation scene){
public void use(){
GL.UseProgram(this.id);
}
public void setup(Matrix4 _mObject,GLSceneOrientation scene){
Matrix4 mObject,
mCamera,
mObjectCamera,

View File

@ -0,0 +1,84 @@
using System;
using OpenTK;
namespace org.niclasundharald.engine.graphics.primitives
{
public class Cube : WorldObject
{
public Cube(Vector3 position, float width)
{
Transformation = Matrix4.CreateScale(width) * Matrix4.CreateTranslation( position );
}
public override Model3D getModel3D()
{
return CubeModel.inst;
}
}
class CubeModel : Model3D {
public static Vector4[] colors = 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),
new Vector4(1.0f,1.0f,0.0f,1.0f),
new Vector4(0.0f,0.9f,0.0f,1.0f),
new Vector4(0.0f,0.9f,0.0f,1.0f),
new Vector4(0.0f,0.9f,0.0f,1.0f),
new Vector4(0.0f,0.9f,1.0f,1.0f),
new Vector4(0.0f,0.0f,1.0f,1.0f),
new Vector4(0.0f,0.0f,1.0f,1.0f),
new Vector4(0.0f,0.0f,1.0f,1.0f),
new Vector4(1.0f,0.0f,1.0f,1.0f)
};
public static CubeModel inst = new CubeModel();
public CubeModel(){
Vector3 dx, dy, dz;
Vector3 a, b, c, d, e, f, g, h;
dx = (Vector3.UnitX / 2);
dy = (Vector3.UnitY / 2);
dz = (Vector3.UnitZ / 2);
a = - dx - dy - dz;
b = dx - dy - dz;
c = dx + dy - dz;
d = - dx + dy - dz;
e = - dx - dy + dz;
f = dx - dy + dz;
g = dx + dy + dz;
h = - dx + dy + dz;
Vector3[] vertexes = new Vector3[36];
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, 4, e, a, d);
Model3D.setTriangle(vertexes, 5, d, h, e);
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 ];
for (int n=0;n<colors.Length;n++){
colors[n] = CubeModel.colors[ n / 3 ];
}
bind( vertexes, colors );
}
}
}

View File

@ -1,9 +1,9 @@
using System;
using OpenTK;
using org.nhengine.graphics;
using org.niclasundharald.engine.graphics;
namespace org.nhengine.graphics.primitives
namespace org.niclasundharald.engine.graphics.primitives
{
public class Triangle : GLObject
{

View File

@ -5,7 +5,7 @@
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{3E812F66-D5F3-4599-8360-97F355B6CC1B}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>org.nhengine</RootNamespace>
<RootNamespace>org.niclasundharald.engine</RootNamespace>
<AssemblyName>org.nhengine</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
@ -31,6 +31,9 @@
<Reference Include="OpenTK">
<HintPath>..\packages\OpenTK.2.0.0\lib\net20\OpenTK.dll</HintPath>
</Reference>
<Reference Include="OpenTK.GLControl">
<HintPath>..\packages\OpenTK.GLControl.1.1.2349.61993\lib\NET40\OpenTK.GLControl.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
@ -42,10 +45,17 @@
<Compile Include="graphics\primitives\Cube.cs" />
<Compile Include="graphics\primitives\Triangle.cs" />
<Compile Include="graphics\GLSceneOrientation.cs" />
<Compile Include="graphics\GLModel.cs" />
<Compile Include="graphics\GLScene.cs" />
<Compile Include="WorldObject.cs" />
<Compile Include="graphics\Model3D.cs" />
<Compile Include="graphics\ModelManager.cs" />
<Compile Include="FileHelper.cs" />
</ItemGroup>
<ItemGroup>
<None Include="OpenTK.dll.config" />
<None Include="packages.config" />
<None Include="Gameplay Object Tree.txt" />
</ItemGroup>
<ItemGroup>
<Folder Include="graphics\" />

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="OpenTK" version="2.0.0" targetFramework="net45" />
<package id="OpenTK.GLControl" version="1.1.2349.61993" targetFramework="net45" />
</packages>