Jump to content
  • Advertisement
Sign in to follow this  
MatsK

Recursive calls with pass-by-reference indexers!

This topic is 2473 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to render a skeleton to the screen, and making it render properly requires me to use recursion and pass by reference. But I can't do that, because I get the error A property or indexer may not be passed as an out or ref parameter.
Here's what I'm trying to do:

public static void DrawBonesSkeleton(ref Bone Bne)
{
Gl.glPointSize(5.0f);
Gl.glTranslatef(Bne.GlobalTranslation.X, Bne.GlobalTranslation.Y, Bne.GlobalTranslation.Z);
float[] RotationMatrix = FindQuaternionMatrix(Bne.GlobalRotation);
Gl.glMultMatrixf(RotationMatrix);
if (Bne.BoneName == "ROOT")
Gl.glColor3f(1.0f, 0.0f, 0.0f);
else if (Bne.BoneName == "HEAD")
Gl.glColor3f(1.0f, 1.0f, 0.0f);
else
Gl.glColor3f(0.0f, 1.0f, 0.0f);
Gl.glBegin(Gl.GL_POINTS); Gl.glVertex3f(0, 0, 0); Gl.glEnd();
if (Bne.Children.Count == 1)
DrawBonesSkeleton(ref Bne.Children[0]);
else if (Bne.Children.Count > 1)
{
for (int i = 0; i < Bne.Children.Count; i++)
DrawBonesSkeleton(ref Bne.Children);
}
}


The original code is:

void DrawBonesSkeleton(Bone_t& Bone)
341 {
342 glPointSize(5.0);
343 glTranslatef(Bone.Translation.x, Bone.Translation.y, Bone.Translation.z);
344 float RotationMatrix[16];
345 FindQuaternionMatrix(RotationMatrix, &Bone.Rotation);
346 glMultMatrixf(RotationMatrix);
347
348 if(!strcmp(Bone.Name, "ROOT"))
349 glColor3f(1.0, 0.0, 0.0);
350 else if(!strcmp(Bone.Name, "HEAD"))
351 glColor3f(1.0, 1.0, 0.0);
352 else
353 glColor3f(0.0, 1.0, 0.0);
354 glBegin(GL_POINTS); glVertex3f(0, 0, 0); glEnd();
355
356 if(Bone.ChildrenCount == 1){
357 DrawBonesSkeleton(*Bone.Children[0]);
358 }else if(Bone.ChildrenCount > 1){
359 for(unsigned i=0; i<Bone.ChildrenCount; i++){
360 glPushMatrix();
361 DrawBonesSkeleton(*Bone.Children);
362 glPopMatrix();
363 }
364 }
365 }


Please keep in mind that I'm trying to do this *without* pointers. Pointers are my last resort. Is there any way to get around this problem, or am I stuck with having to use pointers?

Note: I already tried looping through the bones using foreach, but it results in the skeleton being rendered incorrectly.

Share this post


Link to post
Share on other sites
Advertisement
I figured out how to do this. Turns out you cannot pass elements of List<> by reference, but you *can* pass elements of a normal array by reference!

Share this post


Link to post
Share on other sites
If you have a List<> of structures then the structures are pass-by-value. Since operator[] is actually a method call into List<> this ends up returning an unnamed temporary value that is a copy of the structure contained in the list (not the structure in the list). You cannot take a reference to an unnamed temporary.

Share this post


Link to post
Share on other sites
You don't need to use the ref keyword at all, seeing as you're not assigning to the bne parameter.

Also, keep in mind that in C# parameters of a class type are automatically passed by reference and using the ref keyword creates a reference to a reference.

Share this post


Link to post
Share on other sites
I do need to use ref, the skeleton isn't drawn correctly otherwise. I don't fully understand the skeleton format though, both the skeleton and mesh format is proprietary and AAA (used by The Sims and TSO). I think it might have something to do with the fact that the bones' rotation is normalized (a normalized quaternion, referred to as a 'vertor').

Share this post


Link to post
Share on other sites

I do need to use ref, the skeleton isn't drawn correctly otherwise.


Then there's an error elsewhere in your code.

Share this post


Link to post
Share on other sites
Ok?

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using SFML;
using SFML.Graphics;
using SFML.Window;
using Tao.Glfw;
using Tao.OpenGl;
namespace SFML_test
{
class Program
{
private static Character[] m_Characters;
private static float m_Zoom = -20.0f;
private static int[] m_VertexBuffer = new int[1];
private static int[] m_TextureBuffer = new int[1];
private static int[] m_IndexBuffer = new int[1];
private static float m_TimeDelta = 0.0f;
static void Main(string[] args)
{
// Create main window
RenderWindow App = new RenderWindow(new VideoMode(800, 600), "Project Dollhouse SFML.Net Test");
App.PreserveOpenGLStates(true);
App.Resized += new EventHandler<SizeEventArgs>(App_Resized);
App.KeyPressed += new EventHandler<KeyEventArgs>(App_KeyPressed);
m_Characters = new Character[1];

Character Char = new Character();
Char.Translation = new Vector3(0.0f, -1.5f, 0.0f);
Char.Rotation = new Quaternion(0.0f, 0.0f, 0.0f, 0.0f);
Char.BodyMesh = new Mesh("data\\pelvis-body.mesh", true);
Char.BodyTexture = new SFML.Graphics.Image("data\\body.jpg");
Char.Skel = new Skeleton("data\\adult.skel");
Char.Skel.AssignChildren(ref Char.Skel);
Char.CurrentAnimation = new Anim("data\\a2a-dancee-loop-whip.anim");
m_Characters[0] = Char;
/*=========================================================================================*/
/* Requirement: Being able to load compressed BMPs.
* ========================================================================================*/
System.Drawing.Image Img = System.Drawing.Image.FromFile("data\\person_select_background.bmp");
MemoryStream ImgStream = new MemoryStream();
Img.Save(ImgStream, ImageFormat.Jpeg);
// Create a sprite for the background
SFML.Graphics.Image BackgroundImage = new SFML.Graphics.Image(ImgStream);
Sprite Background = new Sprite(BackgroundImage);
/*==========================================================================================*/
/* Requirement: Being able to seamlessly combine OpenGL and SFML-calls
* =========================================================================================*/
Gl.glShadeModel(Gl.GL_SMOOTH);
Gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Gl.glClearDepth(1.0f);
Gl.glEnable(Gl.GL_DEPTH_TEST);
Gl.glEnable(Gl.GL_CULL_FACE);
Gl.glEnable(Gl.GL_RESCALE_NORMAL);
Gl.glDisable(Gl.GL_BLEND);
Gl.glDepthFunc(Gl.GL_LEQUAL);
Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST);
Gl.glViewport(0, 0, 800, 600);
// Setup a perspective projection
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Glu.gluPerspective(45.0F, (float)800 / 600, 0.1F, 100.0F);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();
while (App.IsOpened())
{
// Set the active window before using OpenGL commands
// It's useless here because the active window is always the same,
// but don't forget it if you use multiple windows
App.SetActive();
// Process events
App.DispatchEvents();
// Clear the window
App.Clear();
//Clear the screen and the depth buffer
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
// Draw background
App.Draw(Background);
m_TimeDelta = App.GetFrameTime();
foreach (Character Sim in m_Characters)
{
Gl.glLoadIdentity();
Gl.glTranslatef(Sim.Translation.X, Sim.Translation.Y, m_Zoom + Sim.Translation.Z);
Gl.glRotatef(Sim.Rotation.X, 1.0f, 0.0f, 0.0f);
Gl.glRotatef(Sim.Rotation.Y, 0.0f, 1.0f, 0.0f);
Gl.glRotatef(Sim.Rotation.Z, 0.0f, 0.0f, 1.0f);
Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_FILL);
Gl.glColor3f(1.0f, 1.0f, 1.0f);
DrawMeshes(Sim);
Gl.glClear(Gl.GL_DEPTH_BUFFER_BIT);
DrawSkeleton(Sim.Skel);
}
App.Display();
}
}
private static void App_KeyPressed(object sender, KeyEventArgs e)
{
if (e.Code == KeyCode.N)
{
m_Characters[0].AdvanceFrame(m_TimeDelta);
}
}
private static void App_Resized(object sender, SizeEventArgs e)
{
if (e.Height == 0)
e.Height++;
Gl.glShadeModel(Gl.GL_SMOOTH);
Gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Gl.glClearDepth(1.0f);
Gl.glEnable(Gl.GL_DEPTH_TEST);
Gl.glEnable(Gl.GL_CULL_FACE);
Gl.glEnable(Gl.GL_RESCALE_NORMAL);
Gl.glDisable(Gl.GL_BLEND);
Gl.glDepthFunc(Gl.GL_LEQUAL);
Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST);
Gl.glViewport(0, 0, 800, 600);
// Setup a perspective projection
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Glu.gluPerspective(45.0F, (float)800 / (float)600, 0.1F, 100.0F);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();
}
private static void DrawMeshes(Character Sim)
{
Gl.glPointSize(2.0f);
Gl.glColor3f(1.0f, 1.0f, 1.0f);
Gl.glPushMatrix();
Gl.glLoadIdentity();
Sim.BodyMesh.TransformVertices2(ref Sim.Skel.Bones[0]);
Gl.glPopMatrix();
Sim.BodyMesh.BlendVertices2();
Gl.glEnable(Gl.GL_TEXTURE_2D);
Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY);
Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
Sim.BodyTexture.Bind();
unsafe
{
fixed (Vector3* Ptr = &Sim.BodyMesh.TransformedVertices[0])
Gl.glVertexPointer(3, Gl.GL_FLOAT, (int)Marshal.SizeOf(typeof(Vector3)), (IntPtr)Ptr);
fixed (Vector2* Ptr = &Sim.BodyMesh.TransformedTexVertices[0])
Gl.glTexCoordPointer(2, Gl.GL_FLOAT, Marshal.SizeOf(typeof(Vector2)), (IntPtr)Ptr);
Gl.glDrawElements(Gl.GL_TRIANGLES, Sim.BodyMesh.FaceCount * 3, Gl.GL_UNSIGNED_INT,
Sim.BodyMesh.FaceIndexes);
}
Gl.glDisableClientState(Gl.GL_VERTEX_ARRAY);
Gl.glDisableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
Gl.glDisable(Gl.GL_TEXTURE_2D);
}
private static void DrawSkeleton(Skeleton Skel)
{
Gl.glPushMatrix();
DrawBonesSkeleton(ref Skel.Bones[0]);
Gl.glPopMatrix();
}
private static void DrawBonesSkeleton(ref Bone Bne)
{
Gl.glPointSize(5.0f);
Gl.glTranslatef(Bne.GlobalTranslation.X, Bne.GlobalTranslation.Y, Bne.GlobalTranslation.Z);
float[] RotationMatrix = FindQuaternionMatrix(Bne.GlobalRotation);
Gl.glMultMatrixf(RotationMatrix);
if (Bne.BoneName == "ROOT")
Gl.glColor3f(1.0f, 0.0f, 0.0f);
else if (Bne.BoneName == "HEAD")
Gl.glColor3f(1.0f, 1.0f, 0.0f);
else
Gl.glColor3f(0.0f, 1.0f, 0.0f);
Gl.glBegin(Gl.GL_POINTS); Gl.glVertex3f(0, 0, 0); Gl.glEnd();
if (Bne.NumChildren == 1)
DrawBonesSkeleton(ref Bne.Children[0]);
else if (Bne.NumChildren > 1)
{
for (int i = 0; i < Bne.NumChildren; i++)
{
Gl.glPushMatrix();
DrawBonesSkeleton(ref Bne.Children);
Gl.glPopMatrix();
}
}
}
private static float[] FindQuaternionMatrix(Quaternion Rotation)
{
float[] Matrix = new float[16];
float X2 = Rotation.X * Rotation.X;
float Y2 = Rotation.Y * Rotation.Y;
float Z2 = Rotation.Z * Rotation.Z;
float XY = Rotation.X * Rotation.Y;
float XZ = Rotation.X * Rotation.Z;
float YZ = Rotation.Y * Rotation.Z;
float WX = Rotation.W * Rotation.X;
float WY = Rotation.W * Rotation.Y;
float WZ = Rotation.W * Rotation.Z;
Matrix[0] = 1.0f - 2.0f * (Y2 + Z2);
Matrix[1] = 2.0f * (XY - WZ);
Matrix[2] = 2.0f * (XZ + WY);
Matrix[3] = 0.0f;
Matrix[4] = 2.0f * (XY + WZ);
Matrix[5] = 1.0f - 2.0f * (X2 + Z2);
Matrix[6] = 2.0f * (YZ - WX);
Matrix[7] = 0.0f;
Matrix[8] = 2.0f * (XZ - WY);
Matrix[9] = 2.0f * (YZ + WX);
Matrix[10] = 1.0f - 2.0f * (X2 + Y2);
Matrix[11] = 0.0f;
Matrix[12] = 0.0f;
Matrix[13] = 0.0f;
Matrix[14] = 0.0f;
Matrix[15] = 1.0f;
return Matrix;
}
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!