• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
MatsK

Recursive calls with pass-by-reference indexers!

6 posts in this topic

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 [i]A property or indexer may not be passed as an out or ref parameter.[/i]
Here's what I'm trying to do:

[code] 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[i]);
}
}[/code]

The original code is:

[code]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[i]);
362 glPopMatrix();
363 }
364 }
365 }[/code]

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.
0

Share this post


Link to post
Share on other sites
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!
0

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.
1

Share this post


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

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

Share this post


Link to post
Share on other sites
I do need to use [i]ref, [/i]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').
0

Share this post


Link to post
Share on other sites
[quote name='MatsK' timestamp='1331205110' post='4920388']
I do need to use ref, the skeleton isn't drawn correctly otherwise.
[/quote]

Then there's an error elsewhere in your code.
0

Share this post


Link to post
Share on other sites
Ok?

[code]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[i]);
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;
}
}
}
[/code]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0