PDA

View Full Version : need help with list in c# (opengl)



clipsymaze
11-28-2011, 01:55 PM
Hi, I'm having a problem moving bugs. I have a draw method which has the code for the texture to be drawn. I have a init method which adds 4 bugs to a list. I have an idle method which is not working (supposed to move the bugs). I load the bugs in a list and in the idle method I create a foreach loop which loops though all bugs in the list, each time supposedly (which doesn't work) translating it using: public void Idle() { foreach (Fly fly in Fly_List) { GL.glPushMatrix(); GL.glTranslatef(fly.SpeedX, fly.SpeedY, 0.0f); GL.glRotatef(CalcAngle(fly.PositionX, fly.PositionY, 0.0f, 0.0f), 0.0f, 0.0f, 1.0f); GL.glPopMatrix(); } }

will share my fileserve account with anyone who can solve this problem;

the code is below:

using System;
using OpenGL;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Drawing.Imaging;
using System.Numerics;
using System.Text;



namespace Project3
{
class World
{

#region Member Variables

List<Fly> Fly_List = new List<Fly>();


uint[] m_TextureIDs = new uint[10];


// Mouse
Vector2d m_Point1 = new Vector2d(0.0f, 0.0f);


bool DrawGridBool = false;

float Pie_Size = 0.8f;

float m_TextureXOffset = 0.005f;

Vector2d FlyPos = new Vector2d(1.5f, 1.5f);

int MaxFlyNum = 2;
int CurrentFlyNum = 0;

#endregion

enum GameState
{
Playing,
Paused,
Stopped
}

public void Init()
{
GL.glGenTextures(6, m_TextureIDs);
GL.glEnable(GL.GL_TEXTURE_2D);

LoadTexture("C:\\background.png", 0);

LoadTexture(@"Artwork\Pie\PieMask.bmp", 1);
LoadTexture(@"Artwork\Pie\Pie.bmp", 2);

LoadTexture(@"Artwork\Fly2\FlyMask.bmp", 3);
LoadTexture(@"Artwork\Fly2\Fly.bmp", 4);

LoadTexture(@"Artwork\Finger\FingerMask.bmp", 5);
LoadTexture(@"Artwork\Finger\Finger.bmp", 6);

Fly_List.Add(new Fly(1.5f, 1.5f, 0.005f, 0.005f));
Fly_List.Add(new Fly(-1.5f, -1.5f, 0.005f, 0.005f));
Fly_List.Add(new Fly(-1.5f, 1.5f, 0.005f, 0.005f));
Fly_List.Add(new Fly(1.5f, -1.5f, 0.005f, 0.005f));
}

public void Draw()
{

GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

GL.glEnable(GL.GL_TEXTURE_2D);

DrawBackground();
DrawPie();

foreach (Fly fly in Fly_List)
{
DrawFly(fly.PositionX, fly.PositionY, fly.SpeedX, fly.SpeedY);
DrawLine(fly.PositionX, fly.PositionY);
}

DrawFinger();
DrawGraph();
}

public void Idle()
{


foreach (Fly fly in Fly_List)
{
GL.glPushMatrix();
GL.glTranslatef(fly.SpeedX, fly.SpeedY, 0.0f);
GL.glRotatef(CalcAngle(fly.PositionX, fly.PositionY, 0.0f, 0.0f), 0.0f, 0.0f, 1.0f);
GL.glPopMatrix();

//fly.PositionX = fly.PositionX - fly.SpeedX;
//fly.PositionY = fly.PositionY - fly.SpeedY;
}
}

public void Exit()
{
GL.glDeleteTextures(6, m_TextureIDs);
}

#region Textures
public void LoadTexture(string FilePath, int TextureID)
{
Bitmap image = new Bitmap(FilePath);
image.RotateFlip(RotateFlipType.RotateNoneFlipX);
BitmapData bitmapdata;
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
bitmapdata = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb) ;

GL.glBindTexture(GL.GL_TEXTURE_2D, m_TextureIDs[TextureID]);
GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, (int)GL.GL_RGB8, image.Width, image.Height, 0, GL.GL_BGR_EXT, GL.GL_UNSIGNED_byte, bitmapdata.Scan0);
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, (int)GL.GL_LINEAR);
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, (int)GL.GL_LINEAR);

image.UnlockBits(bitmapdata);
image.Dispose();
}

private void DrawBackground()
{
// Background
GL.glBindTexture(GL.GL_TEXTURE_2D, m_TextureIDs[0]);
GL.glBegin(GL.GL_QUADS);
GL.glTexCoord2f(0.0f, 0.0f);
GL.glVertex2f(-2.0f, -2.0f);
GL.glTexCoord2f(1.0f, 0.0f);
GL.glVertex2f(-2.0f, 2.0f);
GL.glTexCoord2f(1.0f, 1.0f);
GL.glVertex2f(2.0f, 2.0f);
GL.glTexCoord2f(0.0f, 1.0f);
GL.glVertex2f(2.0f, -2.0f);
GL.glEnd();
}

private void DrawPie()
{
GL.glDisable(GL.GL_DEPTH_TEST);
GL.glEnable(GL.GL_BLEND);

GL.glBlendFunc(GL.GL_DST_COLOR, GL.GL_ZERO); // blend screen colour with black

GL.glBindTexture(GL.GL_TEXTURE_2D, m_TextureIDs[1]);
GL.glBegin(GL.GL_QUADS);
GL.glTexCoord2f(0.0f, 0.0f);
GL.glVertex2f(-1.0f, -1.0f);
GL.glTexCoord2f(1.0f, 0.0f);
GL.glVertex2f(-1.0f, 1.0f);
GL.glTexCoord2f(1.0f, 1.0f);
GL.glVertex2f(1.0f, 1.0f);
GL.glTexCoord2f(0.0f, 1.0f);
GL.glVertex2f(1.0f, -1.0f);
GL.glEnd();

GL.glBlendFunc(GL.GL_ONE, GL.GL_ONE);

GL.glBindTexture(GL.GL_TEXTURE_2D, m_TextureIDs[2]);
GL.glBegin(GL.GL_QUADS);
GL.glTexCoord2f(0.0f, 0.0f);
GL.glVertex2f(-1.0f, -1.0f);
GL.glTexCoord2f(1.0f, 0.0f);
GL.glVertex2f(-1.0f, 1.0f);
GL.glTexCoord2f(1.0f, 1.0f);
GL.glVertex2f(1.0f, 1.0f);
GL.glTexCoord2f(0.0f, 1.0f);
GL.glVertex2f(1.0f, -1.0f);
GL.glEnd();

GL.glDisable(GL.GL_BLEND);
GL.glEnable(GL.GL_DEPTH_TEST);
}

private void DrawFly(float pX, float pY, float sX, float sY)
{
GL.glDisable(GL.GL_DEPTH_TEST);
GL.glEnable(GL.GL_BLEND);

GL.glBlendFunc(GL.GL_DST_COLOR, GL.GL_ZERO); // blend screen colour with black

GL.glBindTexture(GL.GL_TEXTURE_2D, m_TextureIDs[3]);
GL.glBegin(GL.GL_QUADS);
GL.glTexCoord2f(0.0f, 0.0f);
GL.glVertex2f(-0.10f + pX, -0.10f + pY);
GL.glTexCoord2f(1.0f, 0.0f);
GL.glVertex2f(-0.10f + pX, 0.10f + pY);
GL.glTexCoord2f(1.0f, 1.0f);
GL.glVertex2f(0.10f + pX, 0.10f + pY);
GL.glTexCoord2f(0.0f, 1.0f);
GL.glVertex2f(0.10f + pX, -0.10f + pY);
GL.glEnd();

GL.glBlendFunc(GL.GL_ONE, GL.GL_ONE);

GL.glBindTexture(GL.GL_TEXTURE_2D, m_TextureIDs[4]);
GL.glBegin(GL.GL_QUADS);
GL.glTexCoord2f(0.0f, 0.0f);
GL.glVertex2f(-0.10f + pX, -0.10f + pY);
GL.glTexCoord2f(1.0f, 0.0f);
GL.glVertex2f(-0.10f + pX, 0.10f + pY);
GL.glTexCoord2f(1.0f, 1.0f);
GL.glVertex2f(0.10f + pX, 0.10f + pY);
GL.glTexCoord2f(0.0f, 1.0f);
GL.glVertex2f(0.10f + pX, -0.10f + pY);
GL.glEnd();

GL.glDisable(GL.GL_BLEND);
GL.glEnable(GL.GL_DEPTH_TEST);
}

private void DrawFinger()
{
GL.glDisable(GL.GL_DEPTH_TEST);
GL.glEnable(GL.GL_BLEND);

GL.glBlendFunc(GL.GL_DST_COLOR, GL.GL_ZERO); // blend screen colour with black

GL.glBindTexture(GL.GL_TEXTURE_2D, m_TextureIDs[5]);
GL.glBegin(GL.GL_QUADS);
GL.glTexCoord2f(0.0f, 0.0f);
GL.glVertex2f(-0.3f + m_Point1.X, -0.3f + m_Point1.Y);
GL.glTexCoord2f(1.0f, 0.0f);
GL.glVertex2f(-0.3f + m_Point1.X, 0.3f + m_Point1.Y);
GL.glTexCoord2f(1.0f, 1.0f);
GL.glVertex2f(0.3f + m_Point1.X, 0.3f + m_Point1.Y);
GL.glTexCoord2f(0.0f, 1.0f);
GL.glVertex2f(0.3f + m_Point1.X, -0.3f + m_Point1.Y);
GL.glEnd();

GL.glBlendFunc(GL.GL_ONE, GL.GL_ONE);

GL.glBindTexture(GL.GL_TEXTURE_2D, m_TextureIDs[6]);
GL.glBegin(GL.GL_QUADS);
GL.glTexCoord2f(0.0f, 0.0f);
GL.glVertex2f(-0.3f + m_Point1.X, -0.3f + m_Point1.Y);
GL.glTexCoord2f(1.0f, 0.0f);
GL.glVertex2f(-0.3f + m_Point1.X, 0.3f + m_Point1.Y);
GL.glTexCoord2f(1.0f, 1.0f);
GL.glVertex2f(0.3f + m_Point1.X, 0.3f + m_Point1.Y);
GL.glTexCoord2f(0.0f, 1.0f);
GL.glVertex2f(0.3f + m_Point1.X, -0.3f + m_Point1.Y);
GL.glEnd();

GL.glDisable(GL.GL_BLEND);
GL.glEnable(GL.GL_DEPTH_TEST);
}

private void DrawGrid()
{
if (DrawGridBool == true)
{
for (float i = -2f; i < 2; i = i + 0.2f)
{
GL.glBegin(GL.GL_LINE_STRIP);
GL.glVertex2f(i, 2.0f);
GL.glVertex2f(i, -2.0f);
GL.glEnd();
}

for (float i = -2f; i < 2; i = i + 0.2f)
{
GL.glBegin(GL.GL_LINE_STRIP);
GL.glVertex2f(-2.0f, i);
GL.glVertex2f(2.0f, i);
GL.glEnd();
}
}
}

private void DrawGraph()
{
// X Axis
GL.glBegin(GL.GL_LINE_STRIP);
GL.glVertex2f(-2.0f, 0.0f);
GL.glVertex2f(2.0f, 0.0f);
GL.glEnd();

// Y Axis
GL.glBegin(GL.GL_LINE_STRIP);
GL.glVertex2f(0.0f, -2.0f);
GL.glVertex2f(0.0f, 2.0f);
GL.glEnd();
}
#endregion

#region Mouse/Keyboard Handling
public void MousePassiveMove(int pX, int pY)
{
m_Point1.X = -2 + (4 * (float)pX / 800);
m_Point1.Y = m_Point1.Y = 2 - (4 * (float)pY / 800);
}

public void MouseClick(int pX, int pY, MouseButtons pButtons)
{
if (pButtons == MouseButtons.Left)
{
if (Pie_Size == 0.4f)
{
// Delete specific texture
// Draw crumbs
MessageBox.Show("");
}
else
{
Pie_Size = Pie_Size - 0.03f;
}
}
}

public void KeyDown(char pKey)
{
if (pKey == '1')
{
DrawGridBool = true;
}
if (pKey == '2')
{
DrawGridBool = false;
}
}
#endregion

#region Other Methods
public float CalcAngle(float x1, float y1, float x2, float y2)
{
float opp;
float adj;
float ang1;

// Calculate vector differences
opp = y1 - y2;
adj = x1 - x2;

if (x1 == x2 &amp;&amp; y1 == y2) return (-1);

//trig function to calculate angle
if (adj == 0) // to catch vertical co-ord to prevent division by 0
{
if (opp >= 0)
return (0);
else
return (180);
}
else
{
ang1 = (((float)Math.Atan2(y2, x2) * 180.0f / (float)Math.PI) * -1) - ((float)Math.Atan2(y1, x1) * 180.0f / (float)Math.PI);
}
return (ang1);
}

private void DrawLine(float x, float y)
{
GL.glBegin(GL.GL_LINE_STRIP);
GL.glVertex2f(0.0f, 0.0f); // Origin
GL.glVertex2f(x, y);
GL.glEnd();
}
#endregion
}
}

BionicBytes
11-28-2011, 03:37 PM
Firstly please read the posting guidelines. This shows you what tags you should use when posting code. Secondly pls just post relevant code rather than everything.
Your problem stems from the fact that the idle method does not actually do anything. You push the modelview, rotate and translate then pop matrix. You have not saved the model matrix which you have just made gl compute. Perhaps use glGetFloatv to get the modelview matrix contents.

clipsymaze
11-28-2011, 03:48 PM
I apologise, this is my first time on here, will make sure i address my problems properly next time.

according to you response, I revised the code and i realised the idle does in fact does nothing, but when ever i implement something new and remove the idle method, my program freaks out.

if its not too much to ask, can you show me exactly what you mean?.

You will be really saving my a** on this.

Thanks in advance.