?
Solved

Multi dimensions array using pointers

Posted on 2003-03-11
13
Medium Priority
?
245 Views
Last Modified: 2010-04-01
I have several functions that do the following:

//********************************
// SETUP FUNCTION
//********************************

// allocate space. m_ppSortedList is a pointer to a pointer
m_ppSortedList = new CPolygon*[TextureCount];

// init list to null
for (int i = 0; i < TextureCount; i++)

     m_ppSortedList[i] = NULL;
}

// loop thru and sort polygons by textures
for (int i = 0; i < PolygonCount; i++) {

     CPolygon* pPolygon;
     pPolygon = GetPolygon(i);

     // next is a pointer that is used to point to another polygon. used to create linked list
     pPolygon->pNextPolygon = m_ppSortedList[pPolygon->TextureID];

     m_ppSortedList[pPolygon->TextureID] = pPolygon;
}

//********************************
// DRAW FUNCTION
//********************************

// loop through sorted polygons
for (int i = 0; i < TextureCount; i++ ) {

     // point variable to first polygon in array
     pPolygon = m_ppSortedList[i];

     // continue as long as the linked list is not set to NULL.
     if (m_ppSortedList[i] != NULL) {

          while (pPolygon != NULL) {

               // code that draws polygon
               // ....

               // point to next polygon
               pPolygon = pPolygon->pNextPolygon;
          }
     }
}

Note that I just coded this by memory and is not the actual code, so hopefully it's correct and understandable (I'm at work right now).

------------------

This will sort a list of polygons by texture before rendering them. Now what I want to do is sort by lightmaps as well. In otherwords I'm looking for something like this:

// instead of this like I have above
pPolygon->pNextPolygon = m_ppSortedList[pPolygon->TextureID];
m_ppSortedList[pPolygon->TextureID] = pPolygon;

// psuedo code:
// I'd like to do this but am not sure how to go about it in C++.
pPolygon->pNextPolygon = m_ppSortedList[pPolygon->TextureID][pPolygon->LightmapID]
m_ppSortedList[pPolygon->TextureID][pPolygon->LightmapID] = pPolygon;
0
Comment
Question by:Mag2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 4

Expert Comment

by:abancroft
ID: 8113605
So you want to sort on texture id and for those polygons with the same texture id, you want to sort by light map id?

Or something else?
0
 

Author Comment

by:Mag2000
ID: 8114293
Exactly
0
 

Author Comment

by:Mag2000
ID: 8114366
Exactly
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:abancroft
ID: 8114450
I'd use a std::vector<CPolygon *> and std::sort with a functor. I'd also recommend storing the polygons themselves in a std::list or std::vector. e.g.

#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <iostream>

class CPolygon
{
public:
     int LightmapID;
     int TextureID;
};


class CSortPolygon
{
public:
     operator()(const CPolygon *pP1, const CPolygon *pP2)
     {
          return     pP1->TextureID<pP2->TextureID ||
                              (pP1->TextureID==pP2->TextureID && pP1->LightmapID<pP2->LightmapID);
     }
};


int main(int argc, char* argv[])
{
     int TextureCount = 4;
     std::vector<CPolygon> m_PolyList(TextureCount);

     m_PolyList[0].TextureID = 1;
     m_PolyList[0].LightmapID = 1;
     m_PolyList[1].TextureID = 1;
     m_PolyList[1].LightmapID = 0;
     m_PolyList[2].TextureID = 4;
     m_PolyList[2].LightmapID = 1;
     m_PolyList[3].TextureID = 2;
     m_PolyList[3].LightmapID = 1;

     std::vector<CPolygon*> m_SortedList(TextureCount, NULL);
     for (int i = 0; i < TextureCount; i++)
          m_SortedList[i] = &m_PolyList[i];

     for (i = 0; i < TextureCount; i++)
          std::cout << i << ": " << m_SortedList[i]->TextureID << ", " << m_SortedList[i]->LightmapID << '\n';

     std::sort(m_SortedList.begin(), m_SortedList.end(), CSortPolygon());

     for (i = 0; i < TextureCount; i++)
          std::cout << i << ": " << m_SortedList[i]->TextureID << ", " << m_SortedList[i]->LightmapID << '\n';

     return 0;
}

0
 
LVL 4

Expert Comment

by:abancroft
ID: 8114459
If you define CPolygon::operator<() to compare using both the texture & light map id's, it becomes even simpler since you don't need CSortPolygon: std::sort(m_SortedList.begin(), m_SortedList.end());
0
 

Author Comment

by:Mag2000
ID: 8114555

I did think about using the std::vector and sort way back when I first was sorting just the textureID, but I realized since I'm doing this in realtime this would be to costly performance wise since this has to be done every frame on alot of polygons. You see the polygons change depending on what leaf(in a bsp tree) I'm in.

That's were I came up with the pointer to a pointer array because no actual sorting was neccessary.

Any other suggestions? Can't I do this using some sort of multi dimensional array?
0
 

Author Comment

by:Mag2000
ID: 8114598

In other words, is there a way I can get something like this to work:

CPolygon** m_ppSortedList = new CPolygon[20][30];

I get this error: cannot convert from 'CPolygon (*)[30]' to 'CPolygon ** '

Then access itlike this:
m_ppSortedList[pPolygon->TextureID][pPolygon->LightmapID] = pPolygon;
0
 
LVL 4

Expert Comment

by:abancroft
ID: 8114665
In that case, don't you want a 2-d array of pointers-to-polygons? (you have declared a 2d array of polygons).

e.g.
   CPolygon *(*m_ppSortedList)[30] = new CPolygon*[20][30];

BTW, is the number of texture & light maps fixed at compile time?
0
 

Author Comment

by:Mag2000
ID: 8114776

The [20][30] was just any arbitrary number I put in there when infact this needs to be created and destroyed on the fly depending on what map I load.

So to answer your question, neither the textures or lightmaps are fixed at compile time.
0
 
LVL 4

Accepted Solution

by:
abancroft earned 225 total points
ID: 8114971
In that case you can't declare it as a 2-d array at compile time since only the left most dimension can be unknown.

How about this:

     CPolygon ***m_ppSortedList = new CPolygon**[N_TEXTURES];
     std::fill(m_ppSortedList, m_ppSortedList+N_TEXTURES, (CPolygon **)NULL);

     for (int nLoop=0; nLoop<N_TEXTURES; ++nLoop)
     {
          m_ppSortedList[nLoop] = new CPolygon*[N_LIGHTMAPS];
          std::fill(&m_ppSortedList[nLoop][0],
                                   &m_ppSortedList[nLoop][0]+N_LIGHTMAPS, (CPolygon *)NULL);
     }

        // Use m_ppSortedList

     for (int nLoop=0; nLoop<N_TEXTURES; ++nLoop)
          delete []m_ppSortedList[nLoop];
     delete []m_ppSortedList;
0
 

Author Comment

by:Mag2000
ID: 8118949

I ended up going about it a different way but thanks anyways for your help. I'll still credit you for your time.
0
 
LVL 4

Expert Comment

by:abancroft
ID: 8119367
Thx.

As a matter of interest, how did you solve this?
0
 

Author Comment

by:Mag2000
ID: 8120109

It was really simple actually. Here is what I did:

// class data
CPolygonFace** m_ppSortedList;

// allocate space.
m_ppSortedList = new CPolygonFace*[TextureCount * LightmapCount];

// new function for placing polygons in list
void AddToList(CPolygonFace* pPolygon)
{
  DWORD Index = pPolygon->TextureID * LightmapCount;
  Index += pPolygon->LightmapID;

  pPolygon->pNextFace = m_ppSortedList[Index];
  m_ppSortedList[Index] = pPolygon;
}

This will give me a sorted list of polygons by texture id and lightmap id without actual sorting.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question