I have an OpenGl program that is based on the Giawa tutorials numbers 1-5.Basically I create an image, display it on the screen and make it rotate. All I want to accomplish is add keyboard events to change certain parameters of the image. For instance, in my code I want to press F6 to make the image rotate faster:
using System;
using Tao.FreeGlut;
using OpenGL;
using System;
using System.Windows;
using System.Windows.Forms;
namespace OpenGLTutorial5
{
class Program
{
private static int width = 640, height = 480;
private static ShaderProgram program;
private static VBO<Vector3> top_pyramid, cube, bottom_pyramid;
private static VBO<Vector3> top_pyramidColor, cubeColor, bottom_pyramidColor;
private static VBO<int> top_pyramidElements, cubeElements, bottom_pyramidElements;
private static System.Diagnostics.Stopwatch watch;
private static float angle;
private static int rotate = 1;
static void Main(string[] args)
{
// create an OpenGL window
Glut.glutInit();
Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
Glut.glutInitWindowSize(width, height);
Glut.glutCreateWindow("OpenGL Tutorial");
// provide the Glut callbacks that are necessary for running this tutorial
Glut.glutIdleFunc(OnRenderFrame);
Glut.glutDisplayFunc(OnDisplay);
// Glut.glutKeyboardFunc(new Glut.KeyboardCallback(OnKeyPress));
Glut.glutSpecialFunc(new Glut.SpecialCallback(OnKeyPress));
Glut.glutCloseFunc(OnClose);
// enable depth testing to ensure correct z-ordering of our fragments
Gl.Enable(EnableCap.DepthTest);
// compile the shader program
program = new ShaderProgram(VertexShader, FragmentShader);
// set the view and projection matrix, which are static throughout this tutorial
program.Use();
program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f));
program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(10, 0, 10), Vector3.Zero, Vector3.Up));
top_pyramid = new VBO<Vector3>(new Vector3[]
{
new Vector3(-1.5, 0, -0.5), new Vector3(-0.5, 1, -0.5), new Vector3(0, 0, -1.5),
new Vector3(-0.5, 1, -0.5), new Vector3(0.5, 1, -0.5), new Vector3(0, 0, -1.5),
new Vector3(0.5, 1, -0.5), new Vector3(1.5, 0, -0.5), new Vector3(0, 0, -1.5),
new Vector3(1.5, 0, -0.5), new Vector3(0.5, -1, -0.5), new Vector3(0, 0, -1.5),
new Vector3(0.5, -1, -0.5), new Vector3(-0.5, -1, -0.5), new Vector3(0, 0, -1.5),
new Vector3(-0.5, -1, -0.5), new Vector3(-1.5, 0, -0.5), new Vector3(0, 0, -1.5)
});
cube = new VBO<Vector3>(new Vector3[]
{
new Vector3(-1.5, 0, -0.5), new Vector3(-0.5, 1, -0.5), new Vector3(-0.5, 1, 0.5), new Vector3(-1.5, 0, 0.5),
new Vector3(-0.5, 1, -0.5), new Vector3(0.5, 1, -0.5), new Vector3(0.5, 1, 0.5), new Vector3(-0.5, 1, 0.5),
new Vector3(0.5, 1, -0.5), new Vector3(1.5, 0, -0.5), new Vector3(1.5, 0, 0.5), new Vector3(0.5, 1, 0.5),
new Vector3(1.5, 0, -0.5), new Vector3(1.5, 0, 0.5), new Vector3(0.5, -1, 0.5), new Vector3(0.5, -1, -0.5),
new Vector3(0.5, -1, 0.5), new Vector3(0.5, -1, -0.5), new Vector3(-0.5, -1, -0.5), new Vector3(-0.5, -1, 0.5),
new Vector3(-0.5, -1, -0.5), new Vector3(-0.5, -1, 0.5), new Vector3(-1.5, 0, 0.5), new Vector3(-1.5, 0, -0.5)
});
bottom_pyramid = new VBO<Vector3>(new Vector3[]
{
new Vector3(-1.5, 0, 0.5), new Vector3(-0.5, 1, 0.5), new Vector3(0, 0, 1.5),
new Vector3(-0.5, 1, 0.5), new Vector3(0.5, 1, 0.5), new Vector3(0, 0, 1.5),
new Vector3(0.5, 1, 0.5), new Vector3(1.5, 0, 0.5), new Vector3(0, 0, 1.5),
new Vector3(1.5, 0, 0.5), new Vector3(0.5, -1, 0.5), new Vector3(0, 0, 1.5),
new Vector3(0.5, -1, 0.5), new Vector3(-0.5, -1, 0.5), new Vector3(0, 0, 1.5),
new Vector3(-0.5, -1, 0.5), new Vector3(-1.5, 0, 0.5), new Vector3(0, 0, 1.5)
});
top_pyramidColor = new VBO<Vector3>(new Vector3[]
{
new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0),
new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0),
new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0),
new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0),
new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0),
new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0)
});
cubeColor = new VBO<Vector3>(new Vector3[]
{
new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0,0,1),
new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0,0,1),
new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0,0,1),
new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0,0,1),
new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0,0,1),
new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0,0,1),
});
bottom_pyramidColor = new VBO<Vector3>(new Vector3[]
{
new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(1, 1, 0),
new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(1, 1, 0),
new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(1, 1, 0),
new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(1, 1, 0),
new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(1, 1, 0),
new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(1, 1, 0)
});
top_pyramidElements = new VBO<int>(new int[] {
0,1,2,
3,4,5,
6,7,8,
9,10,11,
12,13,14,
15,16,17}, BufferTarget.ElementArrayBuffer);
cubeElements = new VBO<int>(new int[]{
0,1,2,3,
4,5,6,7,
8,9,10,11,
12,13,14,15,
16,17,18,19,
20,21,22,23}, BufferTarget.ElementArrayBuffer);
bottom_pyramidElements = new VBO<int>(new int[] {
0,1,2,
3,4,5,
6,7,8,
9,10,11,
12,13,14,
15,16,17}, BufferTarget.ElementArrayBuffer);
watch = System.Diagnostics.Stopwatch.StartNew();
Glut.glutMainLoop();
}
public static void OnKeyPress(int theKey, int x, int y)
{
switch (theKey)
{
case Glut.GLUT_KEY_F6:
{
rotate += 3;
Console.WriteLine("Hallo!");
Console.ReadLine();
}
break;
}
Glut.glutPostRedisplay();
}
private static void OnClose()
{
top_pyramid.Dispose();
top_pyramidColor.Dispose();
top_pyramidElements.Dispose();
program.DisposeChildren = true;
program.Dispose();
}
private static void OnDisplay()
{
}
private static void OnRenderFrame()
{
// calculate how much time has elapsed since the last frame
watch.Stop();
float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;
watch.Restart();
// use the deltaTime to adjust the angle of the cube and pyramid
angle += deltaTime;
// set up the OpenGL viewport and clear both the color and depth bits
Gl.Viewport(0, 0, width, height);
Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
// use our shader program
Gl.UseProgram(program);
//top pyramid
program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle * rotate) * Matrix4.CreateTranslation(new Vector3(0.0f, 0, 1)));
Gl.BindBufferToShaderAttribute(top_pyramid, program, "vertexPosition");
Gl.BindBufferToShaderAttribute(top_pyramidColor, program, "vertexColor");
Gl.BindBuffer(top_pyramidElements);
//top pyramid
Gl.DrawElements(BeginMode.Triangles, top_pyramidElements.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
//cubes
program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle * rotate) * Matrix4.CreateTranslation(new Vector3(0.0f, 0, 1)));
Gl.BindBufferToShaderAttribute(cube, program, "vertexPosition");
Gl.BindBufferToShaderAttribute(cubeColor, program, "vertexColor");
Gl.BindBuffer(cubeElements);
//cubes
Gl.DrawElements(BeginMode.Quads, cubeElements.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
//bottom pyramid
program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle * rotate) * Matrix4.CreateTranslation(new Vector3(0.0f, 0, 1)));
Gl.BindBufferToShaderAttribute(bottom_pyramid, program, "vertexPosition");
Gl.BindBufferToShaderAttribute(bottom_pyramidColor, program, "vertexColor");
Gl.BindBuffer(bottom_pyramidElements);
//top pyramid
Gl.DrawElements(BeginMode.Triangles, bottom_pyramidElements.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
Glut.glutSwapBuffers();
}
public static string VertexShader = #"
#version 130
in vec3 vertexPosition;
in vec3 vertexColor;
out vec3 color;
uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;
void main(void)
{
color = vertexColor;
gl_Position = projection_matrix * view_matrix * model_matrix * vec4(vertexPosition, 1);
}
";
public static string FragmentShader = #"
#version 130
in vec3 color;
out vec4 fragment;
void main(void)
{
fragment = vec4(color, 1);
}
";
}
}
The image displays correctly, however when I do press F6 the image stops rotating at all. Any ideas on how to fix this?
I suspect that your problem has nothing to do with OpenGL. Looking at your handler for the F6 key, it has the following code:
rotate += 3;
Console.WriteLine("Hallo!");
Console.ReadLine();
The last of these lines waits for you to enter text on the standard input. I believe it will block until you enter something. Take out the ReadLine() call, and see if that fixes the problem.
Related
I'm triying to merge 2 meshes in EyeShot to calculate the volume of the intersecction and difference between this solid and another one. I need the resulting mesh to be closed.
I try this to merge the meshes.
List<Point3D> lpoint3D = new List<Point3D>();
lpoint3D.Add(new Point3D(0, 0, 10));
lpoint3D.Add(new Point3D(5,5, 10));
lpoint3D.Add(new Point3D(0, 5, 10));
lpoint3D.Add(new Point3D(5, 0, 10));
VAR_Glob.mesh2 = UtilityEx.Triangulate(lpoint3D);
List<Point3D> lpoint3D = new List<Point3D>();
lpoint3D.Add(new Point3D(0, 0, 5));
lpoint3D.Add(new Point3D(5, 5, 5));
lpoint3D.Add(new Point3D(0, 5, 5));
lpoint3D.Add(new Point3D(5, 0, 5));
VAR_Glob.mesh1 = UtilityEx.Triangulate(lpoint3D);
VAR_Glob.mesh2.MergeWith(VAR_Glob.mesh1, false, true);
But the resulting mesh is not closed. I transform the mesh to a solid but I only get the two planes separately.
Solid s2 = VAR_Glob.mesh2.ConvertToSolid();
The second parameter of this call is weldNow, please set it to true:
VAR_Glob.mesh2.MergeWith(mesh1, false, true);
Here is my code:
public static void ReduceScreenshot(string fileName)
{
var bmpSS = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
var gfxSS = Graphics.FromImage(bmpSS);
gfxSS.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] { 1.5f, 1.5f, 1.5f, 0, 0},
new float[] { 1.5f,1.5f, 1.5f, 0, 0},
new float[] {1.5f, 1.5f, 1.5f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {-1, -1, -1, 0, 1}
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
Rectangle abc= new Rectangle(-783, -383, bmpSS.Width, bmpSS.Height);
gfxSS.DrawImage(bmpSS, abc, -783, -383, bmpSS.Width, bmpSS.Height, GraphicsUnit.Pixel, attributes);
bmpSS.Save("ScreenshotGray.png", ImageFormat.Png);
}
}
It's how it work http://take.ms/6tzvU. How should i change area from rectangle to triangle?
Not sure just which points shall set up the triangle but the code to restrict DrawImage to it should look something like this:
using System.Drawing.Imaging;
..
..
Rectangle abc = new Rectangle(-783, -383, bmpSS.Width, bmpSS.Height);
GraphicsPath gp = new GraphicsPath();
// three points to make up a triangle
// repeat the first one as one way of closing the path
// use your own coordinates!
Point[] p = new Point[] { new Point(12, 34), new Point(56, 78),
new Point(90, 12), new Point(12, 34) };
gp.AddLines(p); // or AddPolygon
gfxSS.SetClip(gp); // now restrict the Graphics object to the interior of the path
gfxSS.DrawImage(bmpSS, abc, -783, -383, bmpSS.Width, bmpSS.Height,
GraphicsUnit.Pixel, attributes);
This question is based on the Giawa tutorials on OpenGL. I have completed tut6 & tut9 separately, but now I want to combine the two. I.e. I want that cube spinning while a bunch of stars is floating around. However, I'm pretty new two OpenGL but it looks like they use different types of shaders. So I wrote two shaders, but now when I try to run it my cube is transparent and the stars does not display. Is it possible to display both on screen at the same time correctly? Below is my code.
using System;
using Tao.FreeGlut;
using OpenGL;
using System.Windows;
using System.Windows.Forms;
using System.Collections.Generic;
namespace OpenGLTutorial6
{
class Program
{
private static int width = 1280, height = 720;
private static ShaderProgram program, program_2;
private static VBO<Vector3> cube, top_pyramid, bottom_pyramid, cubeNormals, bottom_pyramidNormals, top_pyramidNormals, star;
private static VBO<Vector2> cubeUV, top_pyramidUV, bottom_pyramidUV, starUV;
private static VBO<int> cubeQuads, top_pyramidTrianlges, bottom_pyramidTrianlges, starQuads;
private static bool fullscreen = false;
private static bool left, right, up, down;
private static List<Star> stars = new List<Star>();
private static Random generator = new Random(Environment.TickCount);
private static float theta = (float)Math.PI / 2, phi = (float)Math.PI / 2;
private static Texture crateTexture,
brickTexture,
cracked_glassTexture,
desert_surfaceTexture,
numbersTexture,
ziggyTexture,
starTexture;
private static System.Diagnostics.Stopwatch watch;
private static float angle;
private static int rotate = 1;
private static bool lighting = true;
private class Star
{
public float angle;
public float dist;
public Vector3 color;
public Star(float Angle, float Distance, Vector3 Color)
{
this.angle = Angle;
this.dist = Distance;
this.color = Color;
}
}
static void Main(string[] args)
{
// create an OpenGL window
Glut.glutInit();
Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
Glut.glutInitWindowSize(width, height);
Glut.glutCreateWindow("SCREENSAVER");
// provide the Glut callbacks that are necessary for running this tutorial
Glut.glutIdleFunc(OnRenderFrame);
//Glut.glutIdleFunc(OnRenderFrame_2);
Glut.glutDisplayFunc(OnDisplay);
//<<<<<<<<<<<<< KEYBOARD FUNCTIONS
Glut.glutSpecialFunc(new Glut.SpecialCallback(OnKeyPress));
Glut.glutKeyboardFunc(OnKeyboardDown);
Glut.glutKeyboardUpFunc(OnKeyboardUp);
//<<<<<<<<<<<<< DISPOSE
Glut.glutCloseFunc(OnClose);
// enable depth testing to ensure correct z-ordering of our fragments
Gl.Enable(EnableCap.DepthTest);
Gl.Disable(EnableCap.DepthTest);
Gl.Enable(EnableCap.Blend);
Gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One);
//<<<<<<<<<<< COMPILE SHADER PROGRAMS
program = new ShaderProgram(VertexShader, FragmentShader);
program_2 = new ShaderProgram(VertexShader_2, FragmentShader_2);
//set the view and projection matrix, which are static throughout this tutorial
program.Use();
program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f));
program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 10), Vector3.Zero, Vector3.Up));
program["light_direction"].SetValue(new Vector3(1, 1, 1));
program["enable_lighting"].SetValue(lighting);
program_2.Use();
program_2["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f));
program_2["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 20), Vector3.Zero, Vector3.Up));
//<<<<<<<<< LOAD TEXTURES
crateTexture = new Texture("crate.jpg");
brickTexture = new Texture("bricks.jpg");
cracked_glassTexture = new Texture("crack.jpg");
desert_surfaceTexture = new Texture("desert.jpg");
numbersTexture = new Texture("numbers.jpg");
ziggyTexture = new Texture("ziggy.jpg");
starTexture = new Texture("star.bmp");
// each star is simply a quad
star = new VBO<Vector3>(new Vector3[] { new Vector3(-1, -1, 0), new Vector3(1, -1, 0), new Vector3(1, 1, 0), new Vector3(-1, 1, 0) });
starUV = new VBO<Vector2>(new Vector2[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1) });
starQuads = new VBO<int>(new int[] { 0, 1, 2, 3 }, BufferTarget.ElementArrayBuffer);
// create 50 stars for this tutorial
int numStars = 50;
for (int i = 0; i < numStars; i++)
{
stars.Add(new Star(0, (float)i / numStars * 4f, new Vector3(generator.NextDouble(), generator.NextDouble(), generator.NextDouble())));
}
// create a crate with vertices and UV coordinates
cube = new VBO<Vector3>(new Vector3[]
{
new Vector3(-1.5, 0, -0.5), new Vector3(-0.5, 1, -0.5), new Vector3(-0.5, 1, 0.5), new Vector3(-1.5, 0, 0.5),
new Vector3(-0.5, 1, -0.5), new Vector3(0.5, 1, -0.5), new Vector3(0.5, 1, 0.5), new Vector3(-0.5, 1, 0.5),
new Vector3(0.5, 1, -0.5), new Vector3(1.5, 0, -0.5), new Vector3(1.5, 0, 0.5), new Vector3(0.5, 1, 0.5),
new Vector3(1.5, 0, -0.5), new Vector3(1.5, 0, 0.5), new Vector3(0.5, -1, 0.5), new Vector3(0.5, -1, -0.5),
new Vector3(0.5, -1, 0.5), new Vector3(0.5, -1, -0.5), new Vector3(-0.5, -1, -0.5), new Vector3(-0.5, -1, 0.5),
new Vector3(-0.5, -1, -0.5), new Vector3(-0.5, -1, 0.5), new Vector3(-1.5, 0, 0.5), new Vector3(-1.5, 0, -0.5)
});
cubeUV = new VBO<Vector2>(new Vector2[] {
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1) });
top_pyramid = new VBO<Vector3>(new Vector3[]
{
new Vector3(-1.5, 0, -0.5), new Vector3(-0.5, 1, -0.5), new Vector3(0, 0, -1.5),
new Vector3(-0.5, 1, -0.5), new Vector3(0.5, 1, -0.5), new Vector3(0, 0, -1.5),
new Vector3(0.5, 1, -0.5), new Vector3(1.5, 0, -0.5), new Vector3(0, 0, -1.5),
new Vector3(1.5, 0, -0.5), new Vector3(0.5, -1, -0.5), new Vector3(0, 0, -1.5),
new Vector3(0.5, -1, -0.5), new Vector3(-0.5, -1, -0.5), new Vector3(0, 0, -1.5),
new Vector3(-0.5, -1, -0.5), new Vector3(-1.5, 0, -0.5), new Vector3(0, 0, -1.5)
});
top_pyramidUV = new VBO<Vector2>(new Vector2[] {
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1),
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1),
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1),
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1),
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1),
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1)});
bottom_pyramid = new VBO<Vector3>(new Vector3[]
{
new Vector3(-1.5, 0, 0.5), new Vector3(-0.5, 1, 0.5), new Vector3(0, 0, 1.5),
new Vector3(-0.5, 1, 0.5), new Vector3(0.5, 1, 0.5), new Vector3(0, 0, 1.5),
new Vector3(0.5, 1, 0.5), new Vector3(1.5, 0, 0.5), new Vector3(0, 0, 1.5),
new Vector3(1.5, 0, 0.5), new Vector3(0.5, -1, 0.5), new Vector3(0, 0, 1.5),
new Vector3(0.5, -1, 0.5), new Vector3(-0.5, -1, 0.5), new Vector3(0, 0, 1.5),
new Vector3(-0.5, -1, 0.5), new Vector3(-1.5, 0, 0.5), new Vector3(0, 0, 1.5)
});
bottom_pyramidUV = new VBO<Vector2>(new Vector2[] {
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1),
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1),
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1),
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1),
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1),
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1)});
cubeNormals = new VBO<Vector3>(new Vector3[] {
new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 0),
new Vector3(0, -1, 0), new Vector3(0, -1, 0), new Vector3(0, -1, 0), new Vector3(0, -1, 0),
new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1),
new Vector3(0, 0, -1), new Vector3(0, 0, -1), new Vector3(0, 0, -1), new Vector3(0, 0, -1),
new Vector3(-1, 0, 0), new Vector3(-1, 0, 0), new Vector3(-1, 0, 0), new Vector3(-1, 0, 0),
new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0)
});
top_pyramidNormals = new VBO<Vector3>(new Vector3[]{
new Vector3(0,1,0), new Vector3(0,1,0), new Vector3(0,1,0), new Vector3(0,1,0),
new Vector3(0, -1, 0), new Vector3(0, -1, 0), new Vector3(0, -1, 0),
new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1),
new Vector3(0, 0, -1), new Vector3(0, 0, -1), new Vector3(0, 0, -1),
new Vector3(-1, 0, 0), new Vector3(-1, 0, 0), new Vector3(-1, 0, 0),
new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0)
});
bottom_pyramidNormals = new VBO<Vector3>(new Vector3[]{
new Vector3(0,1,0), new Vector3(0,1,0), new Vector3(0,1,0), new Vector3(0,1,0),
new Vector3(0, -1, 0), new Vector3(0, -1, 0), new Vector3(0, -1, 0),
new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1),
new Vector3(0, 0, -1), new Vector3(0, 0, -1), new Vector3(0, 0, -1),
new Vector3(-1, 0, 0), new Vector3(-1, 0, 0), new Vector3(-1, 0, 0),
new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0)
});
cubeQuads = new VBO<int>(new int[] { 0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15,
16, 17, 18, 19,
20, 21, 22, 23 }, BufferTarget.ElementArrayBuffer);
top_pyramidTrianlges = new VBO<int>(new int[] {
0,1,2,
3,4,5,
6,7,8,
9,10,11,
12,13,14,
15,16,17}, BufferTarget.ElementArrayBuffer);
bottom_pyramidTrianlges = new VBO<int>(new int[] {
0,1,2,
3,4,5,
6,7,8,
9,10,11,
12,13,14,
15,16,17}, BufferTarget.ElementArrayBuffer);
watch = System.Diagnostics.Stopwatch.StartNew();
Gl.BindTexture(desert_surfaceTexture);
Glut.glutMainLoop();
}
private static void OnClose()
{
// dispose of all of the resources that were created
//must still update
cube.Dispose();
cubeUV.Dispose();
top_pyramid.Dispose();
top_pyramidTrianlges.Dispose();
cubeQuads.Dispose();
crateTexture.Dispose();
program.DisposeChildren = true;
program.Dispose();
}
private static void OnDisplay()
{
}
private static void OnRenderFrame()
{
// calculate how much time has elapsed since the last frame
watch.Stop();
float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;
watch.Restart();
// use the deltaTime to adjust the angle of the cube
angle += deltaTime;
// set up the OpenGL viewport and clear both the color and depth bits
Gl.Viewport(0, 0, width, height);
Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
// use our shader program and bind the crate texture
Gl.UseProgram(program);
//<<<<<<<<<<<< TOP PYRAMID
// set the transformation of the top_pyramid
program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle * rotate));
program["enable_lighting"].SetValue(lighting);
// bind the vertex positions, UV coordinates and element array
Gl.BindBufferToShaderAttribute(top_pyramid, program, "vertexPosition");
Gl.BindBufferToShaderAttribute(top_pyramidNormals, program, "vertexNormal");
Gl.BindBufferToShaderAttribute(top_pyramidUV, program, "vertexUV");
Gl.BindBuffer(top_pyramidTrianlges);
// draw the textured top_pyramid
Gl.DrawElements(BeginMode.Triangles, top_pyramidTrianlges.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
//<<<<<<<<<< CUBE
// set the transformation of the cube
program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle *rotate));
program["enable_lighting"].SetValue(lighting);
// bind the vertex positions, UV coordinates and element array
Gl.BindBufferToShaderAttribute(cube, program, "vertexPosition");
Gl.BindBufferToShaderAttribute(cubeNormals, program, "vertexNormal");
Gl.BindBufferToShaderAttribute(cubeUV, program, "vertexUV");
Gl.BindBuffer(cubeQuads);
// draw the textured cube
Gl.DrawElements(BeginMode.Quads, cubeQuads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
//<<<<<<<<<<<< BOTTOM PYRAMID
// set the transformation of the bottom_pyramid
program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle * rotate));
program["enable_lighting"].SetValue(lighting);
// bind the vertex positions, UV coordinates and element array
Gl.BindBufferToShaderAttribute(bottom_pyramid, program, "vertexPosition");
Gl.BindBufferToShaderAttribute(bottom_pyramidNormals, program, "vertexNormal");
Gl.BindBufferToShaderAttribute(bottom_pyramidUV, program, "vertexUV");
Gl.BindBuffer(bottom_pyramidTrianlges);
// draw the textured bottom_pyramid
Gl.DrawElements(BeginMode.Triangles, bottom_pyramidTrianlges.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
Glut.glutSwapBuffers();
}
private static void OnRenderFrame_2()
{
watch.Stop();
float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;
watch.Restart();
// perform rotation of the scene depending on keyboard input
if (right) phi += deltaTime;
if (left) phi -= deltaTime;
if (up) theta += deltaTime;
if (down) theta -= deltaTime;
if (theta < 0) theta += (float)Math.PI * 2;
// set up the OpenGL viewport and clear both the color and depth bits
Gl.Viewport(0, 0, width, height);
Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
//<<<<<<<<< STARS
// make sure the shader program and texture are being used
Gl.UseProgram(program_2);
Gl.BindTexture(starTexture);
// calculate the camera position using some fancy polar co-ordinates
Vector3 position = 20 * new Vector3(Math.Cos(phi) * Math.Sin(theta), Math.Cos(theta), Math.Sin(phi) * Math.Sin(theta));
Vector3 upVector = ((theta % (Math.PI * 2)) > Math.PI) ? Vector3.Up : Vector3.Down;
program_2["view_matrix"].SetValue(Matrix4.LookAt(position, Vector3.Zero, upVector));
// loop through the stars, drawing each one
for (int i = 0; i < stars.Count; i++)
{
// set the position and color of this star
program_2["model_matrix"].SetValue(Matrix4.CreateTranslation(new Vector3(stars[i].dist, 0, 0)) * Matrix4.CreateRotationZ(stars[i].angle));
program_2["color"].SetValue(stars[i].color);
Gl.BindBufferToShaderAttribute(star, program_2, "vertexPosition");
Gl.BindBufferToShaderAttribute(starUV, program_2, "vertexUV");
Gl.BindBuffer(starQuads);
Gl.DrawElements(BeginMode.Quads, starQuads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
// update the position of the star
stars[i].angle += (float)i / stars.Count * deltaTime * 2;
stars[i].dist -= 0.2f * deltaTime;
// if we've reached the center then move this star outwards and give it a new color
if (stars[i].dist < 0f)
{
stars[i].dist += 5f;
stars[i].color = new Vector3(generator.NextDouble(), generator.NextDouble(), generator.NextDouble());
}
}
Glut.glutSwapBuffers();
}
public static void OnKeyPress(int theKey, int x, int y)
{
switch (theKey)
{
//<<<<<<< ROTATE
case Glut.GLUT_KEY_F5:
{
rotate += 1;
Console.WriteLine("Hallo!");
}
break;
case Glut.GLUT_KEY_F6:
{
rotate -= 1;
}
break;
//<<<<<<<<<< TEXTURES
case Glut.GLUT_KEY_F7:
{
Gl.BindTexture(cracked_glassTexture);
}
break;
case Glut.GLUT_KEY_F8:
{
Gl.BindTexture(desert_surfaceTexture);
}
break;
case Glut.GLUT_KEY_F9:
{
Gl.BindTexture(brickTexture);
}
break;
case Glut.GLUT_KEY_F10:
{
Gl.BindTexture(ziggyTexture);
}
break;
case Glut.GLUT_KEY_F11:
{
Gl.BindTexture(numbersTexture);
}
break;
}
Glut.glutPostRedisplay();
}
private static void OnKeyboardDown(byte key, int x, int y)
{
if (key == 'w') up = true;
else if (key == 's') down = true;
else if (key == 'd') right = true;
else if (key == 'a') left = true;
else if (key == 27) Glut.glutLeaveMainLoop();
}
private static void OnKeyboardUp(byte key, int x, int y)
{
if (key == 'w') up = false;
else if (key == 's') down = false;
else if (key == 'd') right = false;
else if (key == 'a') left = false;
else if (key == 'f')
{
fullscreen = !fullscreen;
if (fullscreen) Glut.glutFullScreen();
else
{
Glut.glutPositionWindow(0, 0);
Glut.glutReshapeWindow(1280, 720);
}
}
}
public static string VertexShader = #"
#version 130
in vec3 vertexPosition;
in vec3 vertexNormal;
in vec2 vertexUV;
out vec3 normal;
out vec2 uv;
uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;
void main(void)
{
normal = normalize((model_matrix * vec4(floor(vertexNormal), 0)).xyz);
uv = vertexUV;
gl_Position = projection_matrix * view_matrix * model_matrix * vec4(vertexPosition, 1);
}
";
public static string FragmentShader = #"
#version 130
uniform sampler2D texture;
uniform vec3 light_direction;
uniform bool enable_lighting;
in vec3 normal;
in vec2 uv;
out vec4 fragment;
void main(void)
{
float diffuse = max(dot(normal, light_direction), 0);
float ambient = 0.2;
float lighting = (enable_lighting ? max(diffuse, ambient) : 1);
fragment = lighting * texture2D(texture, uv);
}
";
public static string VertexShader_2 = #"
#version 130
in vec3 vertexPosition;
in vec2 vertexUV;
out vec2 uv;
uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;
void main(void)
{
uv = vertexUV;
gl_Position = projection_matrix * (view_matrix * model_matrix * vec4(0, 0, 0, 1) + vec4(vertexPosition.x, vertexPosition.y, vertexPosition.z, 0));
//gl_Position = projection_matrix * view_matrix * model_matrix * vec4(vertexPosition, 1);
}
";
public static string FragmentShader_2 = #"
#version 130
uniform sampler2D texture;
uniform vec3 color;
in vec2 uv;
out vec4 fragment;
void main(void)
{
fragment = vec4(color * texture2D(texture, uv).xyz, 1);
}
";
}
}
Also I've added some other textures that is not in the original tutorials
From scanning through your code, it looks like you have most of what you need. To render both items at the same time, you basically have to add the rendering code that you currently have in OnRenderFrame_2() to OnRenderFrame(). Make sure that you only have one glClear() at the start, and one glutSwapBuffers() at the end.
If the outline of your code currently looks like this:
OnRenderFrame
prepare rendering of Item 1
glViewport
glClear
glUseProgram(program1)
render Item 1
glutSwapBuffers
OnRenderFrame_2
prepare rendering of Item 2
glViewport
glClear
glUseProgram(program2)
render Item 2
glutSwapBuffers
Rearrange it like this:
OnRenderFrame
prepare rendering of Item 1
prepare rendering of Item 2
glViewport
glClear
glUseProgram(program1)
render Item 1
glUseProgram(program2)
render Item 2
glutSwapBuffers
I've been looking through the other questions but just can't seem to figure this out.. I'm using XNA in a custom tilemap loader. Here is the code.
http://pastebin.com/cuatQHTb
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Pressure
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D player;
Vector2 pos = Vector2.Zero;
Vector2 playersp = new Vector2(50.0f, 50.0f);
Texture2D road1;
Texture2D road2;
Texture2D brickwall;
Texture2D floor1;
Texture2D floor2;
Texture2D floor3;
Texture2D grass;
Texture2D sidewalk1;
Texture2D wood;
Texture2D road3;
private Vector2 origin;
KeyboardState currentState;
Camera camera = new Camera();
Vector2 motion;
List<Texture2D> tiles = new List<Texture2D>();
static int tileWidth = 64;
static int tileHeight = 64;
int tileMapWidth;
int tileMapHeight;
static int screenWidth;
static int screenHeight;
static int mapWidthInPixels;
static int mapHeightInPixels;
int[,] map = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 5, 6, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8, 7, 5, 6, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 8, 1, 1, 1, 2, 2, 1, 8, 7, 5, 6, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 8, 1, 1, 1, 2, 2, 1, 8, 7, 5, 5, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 8, 1, 1, 1, 1, 1, 1, 8, 7, 5, 5, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 8, 1, 3, 3, 3, 1, 1, 8, 7, 5, 6, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 8, 8, 1, 1, 8, 8, 8, 8, 7, 5, 6, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 6, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,},
{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,},
{9, 9, 9, 5, 5, 9, 9, 9, 5, 5, 9, 9, 9, 5, 5, 5, 5, 5, 9, 9, 9, 5, 5, 9, 9, 9, 5, 5, 9,},
{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,},
{7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
};
public static int ScreenWidth
{
get { return screenWidth; }
}
public static int ScreenHeight
{
get { return screenHeight; }
}
public static int MapWidthInPixels
{
get { return mapWidthInPixels; }
}
public static int MapHeightInPixels
{
get { return mapHeightInPixels; }
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
player = Content.Load<Texture2D>("haz");
origin.X = player.Width /2;
origin.Y = player.Height /2;
grass = Content.Load<Texture2D>("grass");
floor1 = Content.Load<Texture2D>("floor1");
floor2 = Content.Load<Texture2D>("floor2");
floor3 = Content.Load<Texture2D>("floor3");
wood = Content.Load<Texture2D>("wood");
road1 = Content.Load<Texture2D>("road1");
road2 = Content.Load<Texture2D>("road2");
sidewalk1 = Content.Load<Texture2D>("sidewalk1");
brickwall = Content.Load<Texture2D>("brickwall");
road3 = Content.Load<Texture2D>("road3");
tiles.Add(grass); //0
tiles.Add(floor1);//1
tiles.Add(floor2);//2
tiles.Add(floor3);//3
tiles.Add(wood);//4
tiles.Add(road1);//5
tiles.Add(road2);//6
tiles.Add(sidewalk1);//7
tiles.Add(brickwall);//8
tiles.Add(road3);//9
tileMapWidth = map.GetLength(1);
tileMapHeight = map.GetLength(0);
mapWidthInPixels = tileMapWidth * tileWidth;
mapHeightInPixels = tileMapHeight * tileHeight;
screenWidth = GraphicsDevice.Viewport.Width;
screenHeight = GraphicsDevice.Viewport.Height;
}
protected override void UnloadContent()
{
}
private float RotationAngle;
private float oldx;
private float oldy;
protected override void Update(GameTime gameTime)
{
oldx = playersp.X;
oldy = playersp.Y;
currentState = Keyboard.GetState();
pos = playersp;
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
IsMouseVisible = true;
MouseState Mouses = Mouse.GetState();
Vector2 mouseLook = new Vector2(Mouses.X, Mouses.Y);
motion = Vector2.Zero;
Vector2 direction = (playersp ) - mouseLook;
float angle = (float)(Math.Atan2(direction.Y, direction.X));
RotationAngle = angle ;
if (currentState.IsKeyDown(Keys.W))
{
if (playersp.Y > screenHeight /4)
{
playersp.Y = playersp.Y - 1;
}
else
{
ScrollUp();
}
}
if (currentState.IsKeyDown(Keys.A))
{
if (playersp.X > screenWidth / 4)
{
playersp.X = playersp.X - 1;
}
else
{
ScrollLeft();
}
}
if (currentState.IsKeyDown(Keys.S))
{
if (playersp.Y > screenHeight / 1.5f)
{
ScrollDown();
}else{
playersp.Y = playersp.Y + 1;
}
}
if (currentState.IsKeyDown( Keys.D))
{
if (playersp.X > screenWidth / 1.5f)
{
ScrollRight();
}
else
{
playersp.X = playersp.X + 1;
}
}
if (motion != Vector2.Zero)
{
motion.Normalize();
camera.Position += motion * camera.Speed;
}
base.Update(gameTime);
}
private void ScrollUp()
{
motion.Y = -0.5f;
}
private void ScrollRight()
{
motion.X = 0.5f;
}
private void ScrollDown()
{
motion.Y = 0.5f;
}
private void ScrollLeft()
{
motion.X = -0.5f;
}
private Point VectorToCell(Vector2 vector)
{
return new Point(
(int)(vector.X / tileWidth),
(int)(vector.Y / tileHeight));
}
private Vector2 ViewPortVector()
{
return new Vector2(
screenWidth + tileWidth,
screenHeight + tileHeight);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
DrawMap();
base.Draw(gameTime);
}
private void DrawMap()
{
Point cameraPoint = VectorToCell(camera.Position);
Point viewPoint = VectorToCell(camera.Position +
ViewPortVector());
Point min = new Point();
Point max = new Point();
min.X = cameraPoint.X;
min.Y = cameraPoint.Y;
max.X = (int)Math.Min(viewPoint.X, map.GetLength(1));
max.Y = (int)Math.Min(viewPoint.Y, map.GetLength(0));
Rectangle tileRectangle = new Rectangle(
0,
0,
tileWidth,
tileHeight);
spriteBatch.Begin();
for (int y = min.Y; y < max.Y; y++)
{
for (int x = min.X; x < max.X; x++)
{
tileRectangle.X = x * tileWidth - (int)camera.Position.X;
tileRectangle.Y = y * tileHeight - (int)camera.Position.Y;
spriteBatch.Draw(tiles[map[y, x]],
tileRectangle,
Color.White);
spriteBatch.Draw(player, pos, null, Color.White, RotationAngle,
origin, 1.0f, SpriteEffects.None, 0f);
}
}
spriteBatch.End();
}
}
}
How would I detect the tiles position and make sure the player doesn't enter that tile? thanks!
You could do something like this:
private static float scalingFactor = 10;
private static float mapSizeX = 19;
private static float mapSizeY = 29;
in Update:
if (playersp.Y > screenHeight /4) {
int mapX = (int) (playersp.X / scalingFactor);
int mapY = (int) (playersp.Y / scalingFactor) - 1;
if (isMovable(mapX, mapY)) {
playersp.Y = playersp.Y - scalingFactor;
}
} else {
ScrollUp();
}
and a new method:
public bool isMovable(int mapX, int mapY)
{
if (mapX < 0 || mapX > 19 || mapY < 0 || mapY > 29) {
return false;
}
int tile = map[mapX, mapY];
if (tile == 4 || tile == 8) {
return false;
}
return true;
}
Similarly for the other directions.
The above code calls the function isMovable to decide whether the player can move to the new location based on the type of tile stored in the map at that position. The decision is false (the player cannot move there) if it is wood or brick wall, otherwise it is true.
The scaling factor is to map between the screen position (captured in playersp) and the tile map. In this case each tile is equivalent to 10 screen "pixels" (You can break it into two separate scales if you want to: one for X dimention, the other for Y).
Note, you need to make sure the values mapSizeX and mapSizeY are correct.
It would be best if you introduced named constants for the type of tile instead of using the numbers -- it will make your code more readable (for you in the future and for others reading it).
EDIT: updated code & explanation
EDIT: Fixed
Hello everybody, I have got the following eventhandler in my Window:
private void buttonView_Click(object sender, RoutedEventArgs e)
{
//Camera
PerspectiveCamera camera = new PerspectiveCamera();
camera.LookDirection = new Vector3D(5, -2, -3);
camera.Position = new Point3D(-5, 2, 3);
camera.UpDirection = new Vector3D(0, 1, 0);
camera.NearPlaneDistance = 1;
camera.FarPlaneDistance = 10;
//Lighting
DirectionalLight light = new DirectionalLight(Colors.White, new Vector3D(-3, -4, -5));
//Cube
Cube cube = new Cube();
GeometryModel3D cubeModel = new GeometryModel3D();
cubeModel.Geometry = cube.Mesh;
cubeModel.Material = new DiffuseMaterial(Brushes.Red);
//ModelGroup
Model3DGroup modelGroup = new Model3DGroup();
modelGroup.Children.Add(light);
modelGroup.Children.Add(cubeModel);
//Model
ModelVisual3D model = new ModelVisual3D();
model.Content = modelGroup;
//Viewport
Viewport3D view = new Viewport3D();
view.Camera = camera;
view.Children.Add(model);
//Show it all
Frame f = new Frame();
f.Content = view;
grid1.Children.Add(f);
}
and this is my cube class:
public class Cube : Primitive
{
Point3D[] positions = new Point3D[] {
new Point3D(0, 1, 1),
new Point3D(0, 0, 1),
new Point3D(1, 1, 1),
new Point3D(1, 0, 1),
new Point3D(1, 1, 0),
new Point3D(0, 0, 0),
new Point3D(0, 1, 1),
new Point3D(0, 0, 1),
new Point3D(0, 1, 0),
new Point3D(0, 1, 0),
new Point3D(1, 1, 1),
new Point3D(0, 1, 1),
new Point3D(1, 1, 1),
new Point3D(1, 0, 1),
new Point3D(1, 1, 0),
new Point3D(1, 0, 0),
new Point3D(1, 0, 1),
new Point3D(0, 0, 1),
new Point3D(1, 0, 0),
new Point3D(0, 0, 0),
new Point3D(1, 1, 0),
new Point3D(1, 0, 0),
new Point3D(0, 1, 0),
new Point3D(0, 0, 0)
};
int[] vertices = new int[] {
0,
1,
2,
1,
3,
2,
4,
5,
6,
5,
7,
6,
8,
9,
10,
9,
11,
10,
12,
13,
14,
13,
15,
14,
16,
17,
18,
17,
19,
18,
20,
21,
22,
21,
23,
22
};
public Cube()
{
this.Mesh.Positions = new Point3DCollection(positions);
this.Mesh.TriangleIndices = new Int32Collection(vertices);
}
}
public abstract class Primitive
{
public Primitive()
{
this.Mesh = new MeshGeometry3D();
}
public MeshGeometry3D Mesh { get; protected set; }
}
However, when I click the button, nothing shows. Is there an error in my event handler?
I am an idiot. Sorry for bothering you. The problem was that I did not set
camera.FarPlaneDistance but only camera.NearPlaneDistance (twice).
Sorry for bothering you.