ListView items do not appear when added - c++

I want to insert some items to my ListCtrl. This is my Code. I can't seem to get errors in the program. Unfortunately, not All items show in List, I can't know why it is. Please help me.
LVITEM lItem;
lItem.mask=LVIF_TEXT;
lItem.iItem = 1 + (nColNum - 1) * 3;
lItem.iSubItem=0;
lItem.pszText = W2T(L"");
pDlgSportNews->m_List.InsertItem( 1 + (nColNum-1 ) * 3 , lItem.pszText);
WCHAR szzText[MAX_PATH];
lItem.iItem = 1 + (nColNum - 1) * 3;
lItem.iSubItem = 1;
lItem.pszText = W2T(m_arData[nColNum - 1].szGameName);
pDlgSportNews->m_List.SetItemText(1 + (nColNum - 1) * 3 , 1 , lItem.pszText);
lItem.mask=LVIF_TEXT;
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 0;
lItem.pszText = W2T(m_arData[nColNum - 1].szGameTime);
pDlgSportNews->m_List.InsertItem(2 + + (nColNum - 1) * 3 , lItem.pszText);
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 1;
lItem.pszText = W2T(m_arData[nColNum - 1].szA_Team);
pDlgSportNews->m_List.SetItemText( 2 + (nColNum - 1) * 3 , 1 , lItem.pszText);
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 2;
if (m_arData[nColNum - 1].bFT_HDP)
{
swprintf(szzText , L"%s %s" , m_arData[nColNum - 1].szFT_HDP , m_arData[nColNum - 1].szFT_A_HDP);
lItem.pszText = W2T(szzText);
}
else
lItem.pszText = W2T(m_arData[nColNum - 1].szFT_A_HDP);
pDlgSportNews->m_List.SetItemText( 2 + (nColNum - 1) * 3 , 2 , lItem.pszText);
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 3;
swprintf(szzText , L"%s %s" , m_arData[nColNum - 1].szFT_UO , m_arData[nColNum - 1].szFT_A_UO);
lItem.pszText=W2T(szzText);
pDlgSportNews->m_List.SetItemText( 2 + (nColNum - 1) * 3 , 3 , lItem.pszText);
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 4;
lItem.pszText = W2T(m_arData[nColNum - 1].szFT_S_Odd);
pDlgSportNews->m_List.SetItemText( 2 + (nColNum - 1) * 3 , 4 , lItem.pszText);
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 5;
if (m_arData[nColNum - 1].bHT_HDP)
{
swprintf(szzText , L"%s %s" , m_arData[nColNum - 1].szHT_HDP , m_arData[nColNum - 1].szHT_A_HDP);
lItem.pszText=W2T(szzText);
}
else
lItem.pszText = W2T(m_arData[nColNum - 1].szHT_A_HDP);
pDlgSportNews->m_List.SetItemText( 2 + (nColNum - 1) * 3 , 5 , lItem.pszText);
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 6;
swprintf(szzText , L"%s %s" , m_arData[nColNum - 1].szHT_UO , m_arData[nColNum - 1].szHT_A_UO);
lItem.pszText=W2T(szzText);
pDlgSportNews->m_List.SetItemText( 2 + (nColNum - 1) * 3 , 6 , lItem.pszText);
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 7;
lItem.pszText = W2T(m_arData[nColNum - 1].szHT_S_Odd);
pDlgSportNews->m_List.SetItemText( 2 + (nColNum - 1) * 3 , 7 , lItem.pszText);
lItem.pszText = W2T(L" ");
pDlgSportNews->m_List.InsertItem( 3 + (nColNum-1 ) * 3 , lItem.pszText);
lItem.iItem = 3 + (nColNum - 1) * 3;
lItem.iSubItem = 1;
lItem.pszText = W2T(m_arData[nColNum - 1].szB_Team);
pDlgSportNews->m_List.SetItemText( 3 + (nColNum - 1) * 3 , 1 , lItem.pszText);
lItem.iItem = 3 + (nColNum - 1) * 3;
lItem.iSubItem = 2;
if (!m_arData[nColNum - 1].bFT_HDP)
{
swprintf(szzText , L"%s %s" , m_arData[nColNum - 1].szFT_HDP , m_arData[nColNum - 1].szFT_B_HDP);
lItem.pszText=W2T(szzText);
}
else
lItem.pszText = W2T(m_arData[nColNum - 1].szFT_A_HDP);
pDlgSportNews->m_List.SetItemText( 3 + (nColNum - 1) * 3 , 2 , lItem.pszText);
lItem.iItem = 3 + (nColNum - 1) * 3;
lItem.iSubItem = 3;
lItem.pszText = W2T(m_arData[nColNum - 1].szFT_B_UO);
pDlgSportNews->m_List.SetItemText( 3 + (nColNum - 1) * 3 , 3 , lItem.pszText);
lItem.iItem = 3 + (nColNum - 1) * 3;
lItem.iSubItem = 4;
lItem.pszText = W2T(m_arData[nColNum - 1].szFT_D_Odd);
pDlgSportNews->m_List.SetItemText( 3 + (nColNum - 1) * 3 , 4 , lItem.pszText);
lItem.iItem = 3 + (nColNum - 1) * 3;
lItem.iSubItem = 5;
if (!m_arData[nColNum - 1].bHT_HDP)
{
swprintf(szzText , L"%s %s" , m_arData[nColNum - 1].szHT_HDP , m_arData[nColNum - 1].szHT_B_HDP);
lItem.pszText=W2T(szzText);
}
else
lItem.pszText = W2T(m_arData[nColNum - 1].szHT_B_HDP);
pDlgSportNews->m_List.SetItemText( 3 + (nColNum - 1) * 3 , 5 , lItem.pszText);
lItem.iItem = 3 + (nColNum - 1) * 3;
lItem.iSubItem = 6;
lItem.pszText = W2T(m_arData[nColNum - 1].szHT_B_UO);
pDlgSportNews->m_List.SetItemText( 3 + (nColNum - 1) * 3 , 6 , lItem.pszText);
lItem.iItem = 3 + (nColNum - 1) * 3;
lItem.iSubItem = 7;
lItem.pszText = W2T(m_arData[nColNum - 1].szHT_D_Odd);
pDlgSportNews->m_List.SetItemText( 3 + (nColNum - 1) * 3 , 7 , lItem.pszText);
pDlgSportNews->m_List.UpdateData();
UpdateData(FALSE);

Possibly going out on a limb here, but:
lItem.iItem = 1 + (nColNum - 1) * 3;
Why are you calculating the item number this way? This could likely be a reason not all of your items are being added.
Boilerplate for adding an item to a CListCtrl:
// First item (first row)
VITEM item;
item.mask = LVIF_TEXT;
item.pszText = "Column Text";
item.iItem = 0; // Item number
item.iSubItem = 0; // Sub item number (column number)
m_CListCtrl.InsertItem(&item);
// Second item (second row)
//...
item.iItem = 1;
//...
As somebody suggested, you should use a loop.

Related

nested for loop optimisation

I have this expression
for (size_t i = 0; i < expression.size(); i++){
for (size_t j = i + 1; j < expression.size(); j++){
result += (expression.at(j) - expression.at(i));
}
result += (g - expression.at(i));
}
return result;
in the vector expression we have for example [1,2,3]. I am trying to get something like:
f1=[(2-1)+(3-1)]
r1 = g-1
h1 = r1+f1
f2=[3-2]
r2 = g-2
h2 = r2+f2
f3 = 0
r3 = g-3
h3 = r3+f3
then h1+h2+h3
What i am doing right now is in Θ(n^2). Is there a way to make it faster even without for loops?
Addition is commutative and associative so the operations can be reordered and grouped without changing the final result. (Note: not taking into account possible overflows in the intermediate calculations, which may be affected by the order and grouping of operations.)
In pseudo-code with n = expression.size() and x[k] = expression.at(k) the original code can be broken down as follows, with the intermediate results indicated in comments.
a = b = c = d = 0
for i = 0 to (n-1)
for j = (i+1) to (n-1)
a += x[j]
// == x[i+1] + x[i+2] + ... x[n-1]
// a == 0 * x[0] + 1 * x[1] + 2 * x[2] + 3 * x[3] + ... + (n-1) * x[n-1]
for i = 0 to (n-1)
for j = (i+1) to (n-1)
b += x[i];
// == (n-i-1) * x[i]
// b == (n-1) * x[0] + (n-2) * x[1] + ... + 2 * x[n-3] + 1 * x[n-2]
for i = 0 to (n-1)
c += g
// c == n * g
for i = 0 to (n-1)
d += expression.at(i))
// d == x[0] + x[1] + ... + x[n-1]
result = c + a - b - d
= n * g
+ (0 - (n-1) - 1) * x[0]
+ (1 - (n-2) - 1) * x[1]
+ ...
+ ((n-2) - 1 - 1) * x[n-2]
+ ((n-1) - 0 - 1) * x[n-1]
The latter result can be calculated directly from that formula, with one single O(n) loop.

Recursion with Double Call (C++)

Can someone please explain to me how exactly this adds up to 26?
I get confused by the 'double call'. Maybe I just don't understand recursion as well as I think I do.
#include <iostream>
using namespace std;
int rec(int * N, int max) {
if (max < 0)
return 0;
return N[max] + rec(N, max - 1) + rec(N, max - 2);
}
int main() {
const int max = 5;
int N[] = { 1, 2, 3, 4, 5 };
int f = rec(N, max - 1);
cout << f << endl;
return 0;
}
int f = rec(N, 4)
= N[4] + rec(N, 3) + rec(N, 2)
= 5 + (N[3] + rec(N, 2) + rec(N, 1)) + (N[2] + rec(N, 1) + rec(N, 0))
= 5 + (4 + (N[2] + rec(N, 1) + rec(N, 0)) + (N[1] + rec(N, 0) + rec(N, -1)) + (3 + (N[1] + rec(N, 0) + rec(N, -1)) + (N[0] + rec(N, -1) + rec(N, -2)))
= 5 + (4 + (3 + rec(N, 1) + rec(N, 0)) + (2 + rec(N, 0) + 0) + (3 + (2 + rec(N, 0) + 0) + (1 + 0 + 0))
= 5 + (4 + (3 + (N[1] + rec(N, 0) + rec(N, -1)) + (N[0] + rec(N, -1) + rec(N, -2))) + (2 + (N[0] + rec(N, -1) + rec(N, -2)) + 0) + (3 + (2 + (N[0] + rec(N, -1) + rec(N, -2)) + 0) + 1)
= 5 + (4 + (3 + (2 + (N[0] + rec(N, -1) + rec(N, -2)) + 0) + (1 + 0 + 0)) + (2 + (1 + 0 + 0) + 0) + (3 + (2 + (1 + 0 + 0) + 0) + 1))
= 5 + (4 + (3 + (2 + (1 + 0 + 0) + 0) + 1) + (2 + 1 + 0) + (3 + (2 + 1 + 0) + 1))
= 5 + (4 + (3 + (2 + 1 + 0) + 1) + 3 + (3 + 3 + 1))
= 5 + (4 + (3 + 3 + 1) + 3 + 7)
= 5 + (4 + 7 + 10)
= 5 + 21
= 26
And as suggested above, the following code generates the entire expression that, when evaluated, equals 26. It can also be played with to generate step-by-step work.
#include <iostream>
#include <string>
using namespace std;
string rec(string * N, int max) {
if (max < 0)
return "0";
auto a = rec(N, max - 1), b = rec(N, max - 2);
return "(" + N[max] + " + " + a + " + " + b + ")";
}
int main() {
const int max = 5;
string N[] = { "1", "2", "3", "4", "5" };
auto f = rec(N, max - 1);
cout << f << endl;
return 0;
}

C++/3D Terrain: std::vector pushback() crashes with c0000374

When attempted to push back a vector of UINT, the progrma crashes with Critical error detected c0000374. Below is the initial code:
void Terrain::CreateIndexList(UINT Width, UINT Height){
UINT sz_iList = (Width - 1)*(Height - 1) * 6;
UINT *iList = new UINT[sz_iList];
for (int i = 0; i < Width; i++){
for (int j = 0; j < Height; j++){
iList[(i + j * (Width - 1)) * 6] = ((UINT)(2 * i));
iList[(i + j * (Width - 1)) * 6 + 1] = (UINT)(2 * i + 1);
iList[(i + j * (Width - 1)) * 6 + 2] = (UINT)(2 * i + 2);
iList[(i + j * (Width - 1)) * 6 + 3] = (UINT)(2 * i + 2);
iList[(i + j * (Width - 1)) * 6 + 4] = (UINT)(2 * i + 1);
iList[(i + j * (Width - 1)) * 6 + 5] = (UINT)(2 * i + 3);
}
}
for (int i = 0; i < sz_iList; i++){
Geometry.IndexVertexData.push_back(iList[i]);
}
delete[] iList;
}
The goal is to take the generated indices from the iList array and fill the Geometry.IndexVertexData vector array. While debugging this, I've created several other implementations of this:
//After creating the iList array:
Geometry.IndexVertexData.resize(sz_iList); //Fails with "Vector subscript out of range?"
UINT in = 0;
for (int i = 0; i < Width; i++){
for (int j = 0; j < Height; j++){
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 1] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 2] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 3] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 4] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 5] = iList[in];
in++;
}
}
And a final, direct to vector implementation:
Geometry.IndexVertexData.reserve(sz_iList);
for (int index = 0; index < sz_iList; index+=6) {
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6] = ((UINT)(2 * i));
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 1] = (UINT)(2 * i + 1);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 2] = (UINT)(2 * i + 2);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 3] = (UINT)(2 * i + 2);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 4] = (UINT)(2 * i + 1);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 5] = (UINT)(2 * i + 3);
}
sz_iList has a final value of 2166, resultant from a grid of 20x20 (400 total points) and is used to initialize sizes. In all cases, the vector array would not fully fill, crashing with Critical error detected c0000374. Am I doing something wrong?
Your sz_iList doesn't appear to be big enough. Let's use a simple example of Width = Height = 2;, then sz_iList = (2 - 1) * (2 - 1) * 6 = 6, right? But in your nested loops, the last iteration occurs when i = j = 1 (i is one less than Width and j is one less than Height), where (in the last line of your loop), you try to access element (i + j * (Width - 1)) * 6 + 5 = (1 + 1 * (2 - 1)) * 6 + 5 = (1 + 1 * 1) * 6 + 5 = 2 * 6 + 5 = 17, which is bigger than the size of your array. This results in undefined behavior.

How Can I Remove Pixel Noise from ofxKinect Video?

I'm looking for some help figuring out how to remove some low quality pixel noise from a video, that I'm obtaining from an xbox kinect via open frameworks. I'm running logic against "moving" parts of an image, to determine what color is moving the most, and use those regions to also detect the depth of which those pixels are moving. I'm attaching a photo to try to better explain my issue.
http://imago.bryanmoyles.com/xxw80
Of course I know code will be asked for, so I'll post what I have so far, but what I'm looking for more than anything else, is a good algorithm for smoothing out pixelated regions in a photo using C++
for(int y = 0; y < kinect.height; y += grid_size) {
for(int x = 0; x < kinect.width * 3; x += 3 * grid_size) {
unsigned int total_r = 0, total_b = 0, total_g = 0;
for(int r = 0; r < grid_size; r++) {
for(int c = 0; c < grid_size; c++) {
total_r += color_pixels[(y * kinect.width * 3 + r * kinect.width * 3) + (c * 3 + x + 0)];
total_b += color_pixels[(y * kinect.width * 3 + r * kinect.width * 3) + (c * 3 + x + 1)];
total_g += color_pixels[(y * kinect.width * 3 + r * kinect.width * 3) + (c * 3 + x + 2)];
}
}
unsigned char average_r = total_r / (grid_size * grid_size),
average_b = total_b / (grid_size * grid_size),
average_g = total_g / (grid_size * grid_size);
for(int r = 0; r < grid_size; r++) {
for(int c = 0; c < grid_size; c++) {
color_pixels[(y * kinect.width * 3 + r * kinect.width * 3) + (c * 3 + x + 0)] = average_r;
color_pixels[(y * kinect.width * 3 + r * kinect.width * 3) + (c * 3 + x + 1)] = average_b;
color_pixels[(y * kinect.width * 3 + r * kinect.width * 3) + (c * 3 + x + 2)] = average_g;
}
}
}
}
for(int y = 0; y < kinect.height; y++) {
for (int x = 0; x < kinect.width * 3; x += 3) {
int total_difference = abs(color_pixels[y * kinect.width * 3 + x + 0] - rgb[0])
+ abs(color_pixels[y * kinect.width * 3 + x + 1] - rgb[1])
+ abs(color_pixels[y * kinect.width * 3 + x + 2] - rgb[2]);
unsigned char defined_color;
if(total_difference < 40) {
defined_color = (unsigned char) 255;
} else {
defined_color = (unsigned char) 0;
}
color_pixels[y * kinect.width * 3 + x + 0] = defined_color;
color_pixels[y * kinect.width * 3 + x + 1] = defined_color;
color_pixels[y * kinect.width * 3 + x + 2] = defined_color;
}
}
Again, I'd like to reiterate that my code is not the problem, I'm simply posting it here so that you understand I'm not just asking blindly. What I really need, is some direction on how to smooth out pixelated images, so that my averages don't get messed up frame by frame by poor quality.
You can process your image from the camera with some methods from the ofxOpenCV addon. There you will have methods like blur, undistort, erode, etc. Its easy to setup, because its already an addon. Have a look at the openCvExample which should be packed with your openFrameworks. For more information on the mentioned methods, take a look here. If I understand your problem correctly, then a little blur on the image could fix your problem already.

Help with Diamond Square algorithm implementation

I'm trying to implement the Diamond-square algorithm, but the problem is only part of the bitmap is being filled and I'm not sure what's wrong. I'm doing it recursively:
GLuint CreateDsquare()
{
std::vector<GLubyte> pdata(256 * 256 * 4);
vector2i loc;
vector2i sz;
GLubyte val;
sz.x = 256;
sz.y = 256;
val = rand() % 255;
loc = vector2i(0,0);
pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;
loc.x = sz.x - 1;
loc.y = 0;
val = rand() % 255;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;
loc.x = sz.x - 1;
loc.y = sz.y - 1;
val = rand() % 255;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;
loc.x = 0;
loc.y = sz.y - 1;
val = rand() % 255;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;
RescursiveDiamond(pdata,sz,vector2i(0,0));
return CreateTexture(pdata,256,256);
}
void RescursiveDiamond(std::vector<GLubyte> &pdata,vector2i psz, vector2i offset)
{
int val;
int newnum;
if(psz.x < 2 && psz.y < 2)
{
return;
}
vector2i loc;
vector2i sz = psz;
std::vector<int> pvertz(4,0);
loc = offset;
pvertz[0] = pdata[loc.y * 4 * sz.x + loc.x * 4 + 0];
loc.x = offset.x + (psz.x - 1);
loc.y = offset.y;
pvertz[1] = pdata[loc.y * 4 * sz.x + loc.x * 4 + 0];
loc.x = offset.x + (psz.x - 1);
loc.y = offset.y + (psz.y - 1);
pvertz[2] = pdata[loc.y * 4 * sz.x + loc.x * 4 + 0];
loc.x = offset.x;
loc.y = offset.y + (psz.y - 1);
pvertz[3] = pdata[loc.y * 4 * sz.x + loc.x * 4 + 0];
val = (pvertz[0] + pvertz[1]) / 2;
val += 255;
loc.x = (offset.x + (sz.x - 1)) / 2;
loc.y = offset.y;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;
val = (pvertz[1] + pvertz[2]) / 2;
val += 255;
loc.x = (offset.x + (sz.x)) - 1;
loc.y = ((offset.y + (sz.y)) / 2) - 1;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;
val = (pvertz[3] + pvertz[2]) / 2;
val += 255;
loc.x = ((offset.x + (sz.x)) / 2) - 1;
loc.y = (offset.y + (sz.y)) - 1 ;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;
val = (pvertz[0] + pvertz[3]) / 2;
val += 255;
loc.x = offset.x;
loc.y = (offset.y + (sz.y)) - 1 ;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;
//center
val = (pdata[(offset.y) * 4 * sz.x + ((offset.x + (sz.x - 1)) / 2) * 4 + 0] +
pdata[(offset.y + (sz.y - 1)) * 4 * sz.x + ((offset.x + (sz.x - 1)) / 2) * 4 + 0]) / 2;
int ad = (rand() % 12) - 6;
if(val + ad < 0)
{
val = 0;
}
else
{
val += ad;
}
val += 255;
loc.x = ((offset.x + (sz.x) ) / 2) - 1;
loc.y = ((offset.y + (sz.y)) / 2) - 1;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;
vector2i newoffset;
vector2i newparentsz;
newoffset = offset;
newparentsz = (psz / 2);
RescursiveDiamond(pdata,newparentsz,newoffset);
newoffset.x = offset.x + (newparentsz.x);
newoffset.y = offset.y;
RescursiveDiamond(pdata,newparentsz,newoffset);
newoffset.x = offset.x;
newoffset.y = offset.y + (newparentsz.y);
RescursiveDiamond(pdata,newparentsz,newoffset);
newoffset.x = offset.x + (newparentsz.x);
newoffset.y = offset.y + (newparentsz.y);
RescursiveDiamond(pdata,newparentsz,newoffset);
}
I suspect that I might be recalling the function with the wrong offset or something.
offset is like the top left and then there is the size, together these nake the square.
what could be wrong here?
Thanks
Ok, first, Let's start with cleaning up the violations of D-R-Y, your code should read more along the lines of this:
int position( _y, _x, _offset ){
return _y * _x * 4 + _x * 4 + _offset;
}
void adjust(vector<GLubyte> &pdata, _x, _y){
GLubyte val = rand() % 255;
for(int j=0; j < 3; ++j){
pdata[ position( _y, _x, j ) ] = val;
}
pdata[ position( _y, _x, 3 ) ] = 255;
}
GLuint CreateDsquare(){
vector2i sz;
sz.x = 256;
sz.y = 256;
adjust( pdata, 0, 0 );
adjust( pdata, sz.x - 1, 0 );
adjust( pdata, sz.x -1, sz.y - 1 );
adjust( pdata, 0, sz.y - 1 );
RescursiveDiamond(pdata,sz,vector2i(0,0));
return CreateTexture(pdata,256,256);
}
Can you format the rest of it down so it's more readable/understandable? Then I'll update so that I can better answer your question (if someone hasn't beaten me to it or the woman decides I've had enough computer time.)
When you are calculating the offset into your height map for each row (y * pitch) you are using the current size of the square you are calculating instead of the actual pitch which is 256. The deeper you go into the recursion you are writing into your height map as if it was smaller and smaller until the last step of your recursion is writing into pixel (0, 0).