there is a program that simulates a small satellite and requires that a rotation animation of the satellite along the three axes. But when you try to write an animation problem during compilation: the program simply closes (shutdown occurs when swapbuffers, mainloop, redisplay), when you write the easiest programs have the same problem arose. Trying to catch exception by try-catch but here is not exception. How to solve this? I suffer with this a few days. Work in c# visual studio 2008 framework
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
try
{
InitializeComponent();
AnT1.InitializeContexts();
}
catch(Exception)
{
Glut.glutDisplayFunc(Draw);
Glut.glutTimerFunc(50, Timer, 0);
Glut.glutMainLoop();
}
}
void Timer(int Unused)
{
Glut.glutPostRedisplay();
Glut.glutTimerFunc(50, Timer, 0);
}
private void AnT1_Load(object sender, EventArgs e)
{
Glut.glutInit();
Glut.glutInitDisplayMode(Glut.GLUT_RGB | Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
Gl.glClearColor(255, 255, 255, 1);
Gl.glViewport(0, 0, AnT1.Width, AnT1.Height);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Glu.gluPerspective(45, (float)AnT1.Width / (float)AnT1.Height, 0.1, 200);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();
Gl.glEnable(Gl.GL_DEPTH_TEST);
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
Gl.glPushMatrix();
double xy = 0.2;
Gl.glTranslated(xy, 0, 0);
xy += 0.2;
Draw();
Glut.glutSwapBuffers();
Glut.glutPostRedisplay();
Gl.glPushMatrix();
Draw();
Gl.glPopMatrix();
}
void Draw()
{
Gl.glLoadIdentity();
Gl.glColor3f(0.502f, 0.502f, 0.502f);
Gl.glTranslated(-1, 0, -6);
Gl.glRotated(95, 1, 0, 0);
Glut.glutSolidCylinder(0.7, 2, 60, 60);
Gl.glLoadIdentity();
Gl.glColor3f(0, 0, 0);
Gl.glTranslated(-1, 0, -6);
Gl.glRotated(95, 1, 0, 0);
Glut.glutWireCylinder(0.7, 2, 20, 20);
}
}
}
You should move the statements in the catch block into try block.
Related
I have modified my rendering engine to use multisampled textures, except now depth testing is being ignored.
Here's how I'm creating the multisampled FBO,
public MSAA_FBO(int WindowWidth, int WindowHeight)
{
this.width = WindowWidth;
this.height = WindowHeight;
GL.GenFramebuffers(1, out ID);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, ID);
// Colour texture
GL.GenTextures(1, out textureColorBufferMultiSampled);
GL.BindTexture(TextureTarget.Texture2DMultisample, textureColorBufferMultiSampled);
GL.TexImage2DMultisample(TextureTargetMultisample.Texture2DMultisample, 4, PixelInternalFormat.Rgb8, WindowWidth, WindowHeight, true);
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2DMultisample, textureColorBufferMultiSampled, 0);
// Depth render buffer
GL.GenRenderbuffers(1, out RBO);
GL.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, RBO);
GL.RenderbufferStorageMultisample(RenderbufferTarget.Renderbuffer, 4, RenderbufferStorage.DepthComponent, WindowWidth, WindowHeight);
GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0);
var status = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
Console.WriteLine("MSAA: " + status);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
}
performing the resolve,
public void resolveToFBO(FBO outputFBO)
{
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, outputFBO.ID);
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, this.ID);
GL.BlitFramebuffer(0, 0, this.width, this.height, 0, 0, outputFBO.width, outputFBO.height, ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit, BlitFramebufferFilter.Nearest);
}
and rendering the image,
public void MSAAPass(Shader shader)
{
GL.UseProgram(shader.ID);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, MSAAbuffer.ID);
GL.Viewport(0, 0, Width, Height);
GL.Enable(EnableCap.Multisample);
GL.ClearColor(System.Drawing.Color.Black);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.Enable(EnableCap.DepthTest);
GL.Disable(EnableCap.Blend);
// Uniforms
Matrix4 viewMatrix = player.GetViewMatrix();
GL.UniformMatrix4(shader.getUniformID("viewMatrix"), false, ref viewMatrix);
// Draw all geometry
DrawScene(shader);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
MSAAbuffer.resolveToFBO(testBuffer);
}
Your multisampled FBO does not have a depth buffer, hence the depth test will not work. Although you actually created a multisampled renderbuffer with GL_DEPTH_COMPONENT format, you forgot to attach this one as the GL_DEPTH_ATTACHMENT of your FBO. You need to add a glFramebufferRenderbuffer() call in your MSAA_FBO() function.
I looked at the code in How to draw line in OpenGl? but glBegin and glEnd are marked as deprecated. How are you supposed to draw a line in the current OpenGL version? I found articles that talk about using VBO, and articles that talk about glVertexPointer and glColorPointer, but there's so much framework around building these methods with the shader program compiling and linking and the vertex attribute bit twiddling. Isn't there a simple way to draw a line without using deprecated functions?
I tried this without success:
OpenTK.Vector2[] linepts = { new Vector2(0, 0), new Vector2(1, 1), new Vector2(50, 0), new Vector2(0, 50) };
int h = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, h);
GL.BufferData<Vector2>(BufferTarget.ArrayBuffer, 16, linepts, BufferUsageHint.StreamDraw);
GL.DrawArrays(PrimitiveType.LineStrip, 0, 4);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.DeleteBuffer(h);
I don't know if I'm even in the right ballpark using DrawArrays or if that method requires a whole mess of framework to go along with it, so I don't necessarily want to make this code work. I just want to find the simplest code for drawing a line that's advisable to use in modern OpenGL.
This is the simplest program (using OpenTK) that I can create to draw a line.
using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
namespace OpenGLLine
{
static class Program
{
static OpenTK.GameWindow gw;
static int shaderProgram;
static int vertexInfo;
static int lineVertexBuffer;
static int vertexCount;
[STAThread]
static void Main()
{
gw = new OpenTK.GameWindow(800, 600, OpenTK.Graphics.GraphicsMode.Default, "Game", OpenTK.GameWindowFlags.FixedWindow);
Initialize();
gw.RenderFrame += Gw_RenderFrame;
gw.Closed += Gw_Closed;
gw.Run(60);
}
private static void Gw_Closed(object sender, EventArgs e)
{
CleanUp();
}
private static void Initialize()
{
int vshader = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(vshader, #"#version 130
in vec2 vPosition;
void main()
{
gl_Position = vec4(vPosition, -1.0, 1.0);
}");
GL.CompileShader(vshader);
int fshader = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(fshader, #"#version 130
out vec4 fragColor;
void main()
{
fragColor = vec4(1.0, 1.0, 1.0, 1.0);
}");
GL.CompileShader(fshader);
shaderProgram = GL.CreateProgram();
GL.AttachShader(shaderProgram, vshader);
GL.AttachShader(shaderProgram, fshader);
GL.LinkProgram(shaderProgram);
GL.DetachShader(shaderProgram, vshader);
GL.DetachShader(shaderProgram, fshader);
GL.UseProgram(shaderProgram);
lineVertexBuffer = GL.GenBuffer();
Vector2[] lineVertices = { new Vector2(0, 0), new Vector2(.5f, .5f) };
vertexCount = lineVertices.Length;
GL.BindBuffer(BufferTarget.ArrayBuffer, lineVertexBuffer);
GL.BufferData(BufferTarget.ArrayBuffer, System.Runtime.InteropServices.Marshal.SizeOf(lineVertices[0]) * vertexCount,
lineVertices, BufferUsageHint.StreamDraw);
vertexInfo = GL.GenVertexArray();
GL.BindVertexArray(vertexInfo);
int locVPosition = GL.GetAttribLocation(shaderProgram, "vPosition");
GL.EnableVertexAttribArray(locVPosition);
GL.VertexAttribPointer(locVPosition, 2, VertexAttribPointerType.Float, false,
System.Runtime.InteropServices.Marshal.SizeOf(lineVertices[0]), 0);
GL.BindVertexArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.UseProgram(0);
}
static void CleanUp()
{
GL.DeleteProgram(shaderProgram);
GL.DeleteVertexArray(vertexInfo);
GL.DeleteBuffer(lineVertexBuffer);
}
private static void Gw_RenderFrame(object sender, OpenTK.FrameEventArgs e)
{
GL.ClearColor(Color4.Black);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.BindBuffer(BufferTarget.ArrayBuffer, lineVertexBuffer);
GL.UseProgram(shaderProgram);
GL.BindVertexArray(vertexInfo);
GL.DrawArrays(PrimitiveType.LineStrip, 0, vertexCount);
GL.BindVertexArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.UseProgram(0);
gw.SwapBuffers();
}
}
}
Im getting an Access Violation when ever I run this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using System.IO;
namespace OpenTKTutorial1
{
class Game : GameWindow
{
int pgmID;
int vsID;
int fsID;
int attribute_vcol;
int attribute_vpos;
int uniform_mview;
int vbo_position;
int vbo_color;
int vbo_mview;
Vector3[] vertdata;
Vector3[] coldata;
Matrix4[] mviewdata;
void initProgram()
{
pgmID = GL.CreateProgram();
loadShader("vs.glsl", ShaderType.VertexShader, pgmID, out vsID);
loadShader("fs.glsl", ShaderType.FragmentShader, pgmID, out fsID);
GL.LinkProgram(pgmID);
Console.WriteLine(GL.GetProgramInfoLog(pgmID));
attribute_vpos = GL.GetAttribLocation(pgmID, "vPosition");
attribute_vcol = GL.GetAttribLocation(pgmID, "vColor");
uniform_mview = GL.GetUniformLocation(pgmID, "modelview");
if (attribute_vpos == -1 || attribute_vcol == -1 || uniform_mview == -1)
{
Console.WriteLine("Error binding attirbutes");
}
GL.GenBuffers(1, out vbo_position);
GL.GenBuffers(1, out vbo_color);
GL.GenBuffers(1, out vbo_mview);
}
void loadShader(String filename, ShaderType type, int program, out int address)
{
address = GL.CreateShader(type);
using (StreamReader sr = new StreamReader(filename))
{
GL.ShaderSource(address, sr.ReadToEnd());
}
GL.CompileShader(address);
GL.AttachShader(program, address);
Console.WriteLine(GL.GetShaderInfoLog(address));
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
initProgram();
vertdata = new Vector3[]{
new Vector3(-0.8f, -0.8f, 0f),
new Vector3(0.8f, -0.8f, 0f),
new Vector3(0f, 0.8f, 0f)};
coldata = new Vector3[]{
new Vector3(1f, 0f, 0f),
new Vector3(0f, 0f, 1f),
new Vector3(0f, 1f, 0f)};
mviewdata = new Matrix4[]{
Matrix4.Identity};
Title = "Title";
GL.ClearColor(Color.CornflowerBlue);
GL.PointSize(5f);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Viewport(0, 0, Width, Height);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.Enable(EnableCap.DepthTest);
GL.EnableVertexAttribArray(attribute_vpos);
GL.EnableVertexAttribArray(attribute_vcol);
GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
GL.DisableVertexAttribArray(attribute_vpos);
GL.DisableVertexAttribArray(attribute_vcol);
GL.Flush();
//Everything before this
SwapBuffers();
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_position);
GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, (IntPtr)(vertdata.Length * Vector3.SizeInBytes), vertdata, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(attribute_vpos, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_color);
GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, (IntPtr)(coldata.Length * Vector3.SizeInBytes), coldata, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(attribute_vcol, 3, VertexAttribPointerType.Float, true, 0, 0);
GL.UniformMatrix4(uniform_mview, false, ref mviewdata[0]);
GL.UseProgram(pgmID);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
}
}
}
It makes it to rendering the background then crashes.
I'm quite new to OpenTK and OpenGL so I'm not 100% sure what the problem is.
The error is thrown at
GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
There are a few minor issues with your code, but it works for me without changes.
Regardless, I have made a few changes and added commentary to explain why, maybe something fixes it for you. If not please post your shaders and more detail on the exact exception, if there is any.
using System;
using System.Drawing;
using System.IO;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace OpenTKTutorial1
{
public class Game
: GameWindow
{
int pgmID;
int vsID;
int fsID;
int attribute_vcol;
int attribute_vpos;
int uniform_mview;
int vbo_position;
int vbo_color;
int vbo_mview;
Vector3[] vertdata;
Vector3[] coldata;
Matrix4[] mviewdata;
public Game()
{
// better use the events instead of overriding the inherited methods
// at least thats what the documentation on Update- and RenderFrame says
Load += OnLoad;
UpdateFrame += OnUpdateFrame;
RenderFrame += OnRenderFrame;
}
void InitProgram()
{
pgmID = GL.CreateProgram();
LoadShader("vs.glsl", ShaderType.VertexShader, pgmID, out vsID);
LoadShader("fs.glsl", ShaderType.FragmentShader, pgmID, out fsID);
GL.LinkProgram(pgmID);
Console.WriteLine(GL.GetProgramInfoLog(pgmID));
attribute_vpos = GL.GetAttribLocation(pgmID, "vPosition");
attribute_vcol = GL.GetAttribLocation(pgmID, "vColor");
uniform_mview = GL.GetUniformLocation(pgmID, "modelview");
if (attribute_vpos == -1 || attribute_vcol == -1 || uniform_mview == -1)
{
Console.WriteLine("Error binding attributes");
}
GL.GenBuffers(1, out vbo_position);
GL.GenBuffers(1, out vbo_color);
// what is this buffer for?
//GL.GenBuffers(1, out vbo_mview);
}
void LoadShader(String filename, ShaderType type, int program, out int address)
{
address = GL.CreateShader(type);
using (StreamReader sr = new StreamReader(filename))
{
GL.ShaderSource(address, sr.ReadToEnd());
}
GL.CompileShader(address);
GL.AttachShader(program, address);
Console.WriteLine(GL.GetShaderInfoLog(address));
}
protected void OnLoad(object sender, EventArgs eventArgs)
{
InitProgram();
vertdata = new Vector3[]{
new Vector3(-0.8f, -0.8f, 0f),
new Vector3(0.8f, -0.8f, 0f),
new Vector3(0f, 0.8f, 0f)};
coldata = new Vector3[]{
new Vector3(1f, 0f, 0f),
new Vector3(0f, 0f, 1f),
new Vector3(0f, 1f, 0f)};
mviewdata = new Matrix4[]{
Matrix4.Identity};
Title = "Title";
GL.ClearColor(Color.CornflowerBlue);
GL.PointSize(5f);
}
protected void OnRenderFrame(object sender, FrameEventArgs frameEventArgs)
{
// if you only have one viewport you can safely move this to the OnResize event
GL.Viewport(0, 0, Width, Height);
// if the state never changes move it to OnLoad
GL.Enable(EnableCap.DepthTest);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.EnableVertexAttribArray(attribute_vpos);
GL.EnableVertexAttribArray(attribute_vcol);
// always make sure the program is enabled ..
GL.UseProgram(pgmID);
// .. before you set any uniforms
GL.UniformMatrix4(uniform_mview, false, ref mviewdata[0]);
// .. or draw anything
GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
GL.DisableVertexAttribArray(attribute_vpos);
GL.DisableVertexAttribArray(attribute_vcol);
// do not call glFlush unless you have a very good reason to
// it can result in significant slow downs
//GL.Flush();
SwapBuffers();
}
protected void OnUpdateFrame(object sender, FrameEventArgs frameEventArgs)
{
// your vertex and color data never changes, thus everything you do here
// could be moved to OnLoad instead of having it repeated all the time
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_position);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertdata.Length * Vector3.SizeInBytes), vertdata, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(attribute_vpos, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_color);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(coldata.Length * Vector3.SizeInBytes), coldata, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(attribute_vcol, 3, VertexAttribPointerType.Float, true, 0, 0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
}
}
}
I'm doing a Piano Keyboard which also has a Music Staff with it so that when you press a Key on the Keyboard, according to the duration a music note (crotchet, semi-breve etc) pops up on the Music Staff.
I have a Form (called Form1) which in it it has a Panel (Panel1) which contains the Keyboard Keys made of Buttons. I also have a Music Staff made of a PictureBox which holds the staff (or stave don't know what it's called) and a Test Text Box which holds the pitches (basically the note played as they are mapped)
The problem is this. I need to make the music notes show on the staff (pictureBox1) so in Form1 which is acting as the main class but whenever I write
mn = new MusicNote (nPos,nPitch, duration,nShape);
pictureBox1.Controls.Add(this.mn);
pictureBox1.Controls[pictureBox1.Controls.Count - 1].BringToFront(); //Bring the notes OVER the Stave
It basically doesn't work BUT when I replace every pictureBox1 with just this OR panel1 (e.g.)
this.Controls.Add(this.mn);
it shows the Music Note either on the FORM1 (the grey space) or on the Keyboard itself (see further down to see the Keyboard). The problem is that with this, it doesn't actually adds to the PictureBox but to the Form1/Panel
Do you have any idea how to fix this and make the music Notes actually PART of the PictureBox1? because I also need some methods to work on that pictureBox like when I click one of the Notes they actually play that sound with that duration (in which I still need to figure out how to pass the Controls From Form1.cs to MusicNote.cs)
Part of the coding for MusicNote.cs which has to do with the "adding" of the image is this below:
public class MusicNote : PictureBox
{
public int pitch;
public int duration;
public string noteShape;
string nShape;
bool isDragging = false;
public string rootFolder = #"..\..\\bin\\Debug\\images\\";
ArrayList al = new ArrayList();
SoundPlayer sp = new SoundPlayer();
Timer t1 = new Timer();
public MusicNote(int x, int mPitch, int mDuration,string nShape)
: base()
{
pitch = mPitch;
duration = mDuration;
noteShape = nShape;
Location = new Point(x, 100);
this.Size = new Size(35, 40);
//--------- Get the Image of Note ----------
Bitmap bmp = new Bitmap(#rootFolder + nShape + ".bmp");
this.Image = bmp;
this.BackColor = Color.Transparent;
//-------Mouse Events-------
this.MouseDown += new MouseEventHandler(StartDrag);
this.MouseUp += new MouseEventHandler(StopDrag);
this.MouseMove += new MouseEventHandler(NoteDrag);
}
etc
...
...
...
}
Form1.cs Coding (posting all as all are connected from one method to another)
The Problem lies in private void onMouseUp (object sender, MouseEventArgs e) which is the last method
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
using System.Collections;
namespace Piano
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Objects of Music Note, Piano Keys, and all variables surronding the Keyboard and Notes
WhiteKey wk;
BlackKey bk;
public int pitch;
public int duration = 0;
string nShape = "";
public const int xOff = 35;
int count = 0;
int nPos = 0;
public string rootFolder = #"..\..\\bin\\Debug\\images\\";
MusicNote mn;
MusicStaff ms;
SoundPlayer sp = new SoundPlayer();
Stopwatch sw = new Stopwatch();
//--------------- White and Black Keys Creation both Buttons and Position------------------
public int[] wKeys = { 1, 3, 5, 6, 8, 10, 12, 13, 15, 17, 18, 20, 22, 24, 25 }; //White Keys notes numbers (pitch)
public int[] bKeys = { 0, 2, 0, 4, 0, 0, 7, 0, 9, 0, 11, 0, 0, 14, 0, 16, 0, 0, 19, 0, 21, 0, 23, 0, 0 }; //Black Keys notes numbers (pitch)
int[] wPos = { 35, 70, 105, 140, 175, 210, 245, 280, 315, 350, 385, 420, 455, 490, 525 }; // Position of White Keys on the Panel
int[] bPos = { 0, 57, 0, 92, 0, 0, 162, 0, 197, 0, 232, 0, 0, 302, 0, 337, 0, 0, 407, 0, 442, 0, 477, 0, 1 }; //Position of the Black Keys in the Panel
private void Form1_Load(object sender, System.EventArgs e)
{
for (int i = 0; i < 15; i++)
{
WhiteKey wk = new WhiteKey(wKeys[i], wPos[i]-35,0); //create a new white Key with [i] Pitch, at that x position and at y =0 position
wk.MouseDown += onMouseDown; //Plays the Key and starts Timer
wk.MouseUp += onMouseUp; // Holds the data like Time and shape and so
this.panel1.Controls.Add(wk); //Give it control (to play and edit)
}
for (int i = 0; i < 25; i++) //same for the Black Keys but instead we use 25 keys and those denoted at 0 are where the WHITE KEYS should be placed
{
if (bKeys[i] != 0)
{
//Same as above but for Black Key which is inherits from WhiteKey
bk = new BlackKey(bKeys[i], bPos[i]-35, 0);
bk.MouseDown += onMouseDown;
bk.MouseUp += onMouseUp;
this.panel1.Controls.Add(bk);
this.panel1.Controls[this.panel1.Controls.Count - 1].BringToFront(); //Make the Black Keys show OVER the white
}
}
}
//Method showing what happens when you do a MouseDown Event
private void onMouseDown(object sender, MouseEventArgs e)
{
wk = sender as WhiteKey; //gets the WhiteKey Controls
pitch = wk.pitch; //assign pitch
sp.SoundLocation = #"..\\..\\bin\\Debug\\sound\\mapped\\" + pitch + ".wav"; //find that pressed note
sp.Play(); //play it
sw.Reset(); //Reset Stop Watch
sw.Start(); //Start Time
}
private void timeTick(object sender, EventArgs e)
{
duration++;
}
private void onMouseUp (object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
sw.Stop(); //Stop time
duration = (int)sw.ElapsedMilliseconds / 100;
textBox.Text += (string)(pitch + " , "); //add the Pitch used to the textbox
// Will determine the Music Note Image
if (duration >= 0 && duration < 2)
{
nShape = "Quaver";
}
else if (duration >= 2 && duration < 5)
{
nShape = "Crotchet";
}
else if (duration >= 5 && duration < 8)
{
nShape = "minim";
}
else if (duration >= 8 && duration < 10)
{
nShape = "DotMin";
}
else if (duration >= 10)
{
nShape = "SemiBreve";
}
count++; //will help Determine the 'x' coordiante of the Music Note
nPos = xOff * count; //moves the x-coordinate by 35 pixels each note
mn = new MusicNote(nPos,pitch,duration,nShape); //Creation of a new MusicNote
pictureBox1.Controls.Add(this.mn); //PROBLEM --- Doesn't add to the PictureBox (Does Nothing)
pictureBox1.Controls[pictureBox1.Controls.Count - 1].BringToFront(); //Brought to front of stave to make sure it doesn't get hidden in background
}
}
}
}
Any Idea how I can add CONTROL to the PictureBox1 and make the Music Note Show? Because I managed to make it Show on the Form1 and the Panel1 but failed on a pictureBox
How the Piano Looks with music notes on FORM1
According to the Visual Studio DEBUGGER [Piano.Form1]
http://postimg.org/image/vdu0x1gv1/
N.B Sorry for the long post but I don't know exactly how to explain the problem.
I think the Program was just messing with me.. I tried changing something to png and back to bmp and Now it's working perfectly.. I do not know why but It's the same Code there is up there
Thanks to those who have seen the question.
To play sound in picture box do this
[DllImport("winmm.dll")]
private static extern bool PlaySound(string lpszName, int hModule, int dwFlags);
and PlaySound("your sound loc ", 1, 0x0011);
We are trying to make an app using Xamarin which will have a small animated face in a GLKView on a particular screen. We have looked for solutions for rendering sprites, and the best solution we came up with stems from this solution here. We are having trouble even drawing a simple image in the GLKView, and the error in the output does not really make sense. We are converting this from iOS to Xamarin C# so there are differences between certain calls, but we have tried to keep most pieces in tact.
Here are the parts of the code this is related to:
public class Sprite : NSObject
{
public void Render()
{
Effect.Texture2d0.GLName = TextureInfo.Name;
Effect.Texture2d0.Enabled = true;
Effect.PrepareToDraw();
GL.EnableVertexAttribArray((int)GLKVertexAttrib.Position);
GL.EnableVertexAttribArray((int)GLKVertexAttrib.TexCoord0);
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(Quad));
Marshal.StructureToPtr(Quad, ptr, false);
int offset = (int)ptr;
GL.VertexAttribPointer((uint)GLKVertexAttrib.Position, 2, VertexAttribPointerType.Float, false, Marshal.SizeOf(typeof(TexturedVertex)), offset + (int)Marshal.OffsetOf(typeof(TexturedVertex), "geomertryVertex"));
GL.VertexAttribPointer((uint)GLKVertexAttrib.Position, 2, VertexAttribPointerType.Float, false, Marshal.SizeOf(typeof(TexturedVertex)), offset + (int)Marshal.OffsetOf(typeof(TexturedVertex), "textureVertex"));
GL.DrawArrays(BeginMode.TriangleStrip, 0, 4);
Marshal.FreeHGlobal(ptr);
}
}
Sprite.Render() is called in this GLKViewController here:
public class AnimationViewController : GLKViewController
{
GLKView animationView;
EAGLContext context;
Sprite player;
GLKBaseEffect effect;
public override void ViewDidLoad()
{
base.ViewDidLoad();
context = new EAGLContext(EAGLRenderingAPI.OpenGLES2);
if (context == null)
Console.WriteLine("Failed to create ES context...");
animationView = new GLKView(new RectangleF(UIScreen.MainScreen.Bounds.Width * 0.05f,
UIScreen.MainScreen.Bounds.Height * 0.05f,
UIScreen.MainScreen.Bounds.Width * 0.9f,
UIScreen.MainScreen.Bounds.Height * 0.75f), context);
EAGLContext.SetCurrentContext(context);
animationView.DrawInRect += new EventHandler<GLKViewDrawEventArgs>(animationView_DrawInRect);
View.AddSubview(animationView);
effect = new GLKBaseEffect();
Matrix4 projectionMatrix = Matrix4.CreateOrthographicOffCenter(0, animationView.Frame.Width, 0, animationView.Frame.Height, -1024, 1024);
effect.Transform.ProjectionMatrix = projectionMatrix;
player = new Sprite(#"Player.png", effect);
}
void animationView_DrawInRect(object sender, GLKViewDrawEventArgs e)
{
GL.ClearColor(0.98f, 0.98f, 0.98f, 1.0f);
//GL.Clear((uint)(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit));
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
GL.Enable(EnableCap.Blend);
player.Render();
}
}
Links to whole code files:
Sprite Class and related Structs
AnimationViewController Class
Looks like the problem is just a typo in the second call to VertexAttribPointer. The second GLKVertexAttrib.Position should instead be GLKVertexAttrib.TexCoord0:
GL.VertexAttribPointer((uint)GLKVertexAttrib.Position, 2, VertexAttribPointerType.Float, false, Marshal.SizeOf(typeof(TexturedVertex)), offset + (int)Marshal.OffsetOf(typeof(TexturedVertex), "geomertryVertex"));
GL.VertexAttribPointer((uint)GLKVertexAttrib.TexCoord0, 2, VertexAttribPointerType.Float, false, Marshal.SizeOf(typeof(TexturedVertex)), offset + (int)Marshal.OffsetOf(typeof(TexturedVertex), "textureVertex"));