PDA

View Full Version : why is this not appearing right?

Dogcow
09-30-2002, 06:20 PM
In the process of recoding my transformation method, I've somehow messed up my cube. The points should define a cube (I think) but all that appears is a line. I've gone over the numbers a dozen times but have no idea why it's not working...

here: code (http://theunderground.eon.com.au/code/)

Help would be greatly appreciated. Hopefully someone else looking over it will catch the stupid mistake I'm missing.

-Dogcow "moof!"
Visit The Underground (http://theunderground.eon.com.au)

Bob
10-01-2002, 12:37 AM
If that's supposed to be C od C++ code, you have the index to the matrix array wrong. Array indices in C and C++ starts counting with 0, not 1.

float matrix[3][8];

This will allocate a matrix with 8 vectors, and three components each, which I believe is what you want. But the index into the arrai is ranging from 0 to 2 and 0 to 7, not 1 to 3 and 1 to 8.

So to pick some examples from the code.

matrix[3][8] = -1.0;

This will set the fourth component of the ninth vector to -1. But since you haven't allocated that many vectors and components, you have a buffer overrun, and that is BAD.

for (int j=1;j<=3;j++)
{
matrix[j][i]=result[j-1];
}

Your index i will loop from 1 to 3, addressing the second, third and fourth element in the vector. Again, the fourth element is outside the allocated space.

[This message has been edited by Bob (edited 10-01-2002).]

Dogcow
10-01-2002, 07:55 PM
Okay, thanks a lot. A few languages I've used (don't even remember which ones) start at 1 and not 0. Confusing. I believe I've righted the errors. I am still assigning the value of the third column, 8th row to [3][8], but my matrix is [4][9] now (I ignore the 0 row.)

The "cube" is still only a line though... I'm confused. The code I linked to before is now updated. Can anyone see any other mistakes?

-Dogcow "moof!"
Visit The Underground (http://theunderground.eon.com.au)

tinak
10-01-2002, 10:54 PM
Perhaps if you visualize the points of each line on paper and see if there's something seemingly wrong there?

For some reason my VC++ compiler didn't like the code. Had to make several changes just to get it to compile. What compiler are you using?

The main problem for was with the changed=result assignments. I had to adjust these to this :
for (int i=0;i<4;i++) changed[i] = result[i];

Ah, located the problem.. and it showed me a problem with one of the points.

change Ortho to
gluPerspective(45,1,-1000,1);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
And Clear to this
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

With those commands a 3D cube can be seen that takes up the whole screen.

Tina

Dogcow
10-02-2002, 09:22 AM
Originally posted by tinak:
Perhaps if you visualize the points of each line on paper and see if there's something seemingly wrong there?

For some reason my VC++ compiler didn't like the code. Had to make several changes just to get it to compile. What compiler are you using?

The main problem for was with the changed=result assignments. I had to adjust these to this :
for (int i=0;i<4;i++) changed[i] = result[i];

Ah, located the problem.. and it showed me a problem with one of the points.

change Ortho to
gluPerspective(45,1,-1000,1);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
And Clear to this
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

With those commands a 3D cube can be seen that takes up the whole screen.

Tina

Hehe, I jsut caught those too...

I made the changes you suggested (to the gl commands) but still don't get a cube. Instead I get this (http://theunderground.eon.com.au/code/window.pdf)

It's still just a solid line across the screen. Any ideas? I could have sworn I haven't made any changes to the matrix since you helped me to get it to show up right the first time...

-Dogcow "moof!"
Visit The Underground (http://theunderground.eon.com.au)

nexusone
10-02-2002, 11:10 AM
Realy not confusing, very logical.

When addressing a variable space, the first data bytes start at zero offset from the space assigned to it.

example:

int x
or
int x[1]; both have taken one space, but have a zero offset.

How we go to int x[2], two spaces are reserved for storage of our variable.
In memory terms it looks like this:

x[0] = our first storage space is at zero offset.
x[1] = is +1 offset higher then the first space.

I am not sure what language starts with 1 as the first address.

I think that if you start using 1 as the start of your memory space and not zero, when other people look at your code and try to help you it will only comfuse them.

Originally posted by Dogcow:
Okay, thanks a lot. A few languages I've used (don't even remember which ones) start at 1 and not 0. Confusing. I believe I've righted the errors. I am still assigning the value of the third column, 8th row to [3][8], but my matrix is [4][9] now (I ignore the 0 row.)

The "cube" is still only a line though... I'm confused. The code I linked to before is now updated. Can anyone see any other mistakes?

-Dogcow "moof!"
Visit The Underground (http://theunderground.eon.com.au)

tinak
10-02-2002, 11:22 AM
really weird...

I've posted the adjusted program I am using on my site...
http://programming.swangen.co.uk/opengl/cubewithlines.asp

What I did notice that was weird is that I had to keep Ortho and Perspective both up and running for it to work... Perhaps something with the near and far values.

Hope that helps you figure out how it works.

Tina

Dogcow
10-02-2002, 01:35 PM
Sweet!

Found out my compiler doesn't like my commented lines towards the beginning (where I placed points into the matrix.) Apparently the single forward slash was throwing it off. Deleted those and I got it showing up right (though I now see that I drew a line to the wrong point...

Thanks a lot for the help, hopefully my learning experience will enable me to help others on here in the future...

-Dogcow "moof!"
Visit The Underground (http://theunderground.eon.com.au)

tinak
10-02-2002, 02:43 PM
I'm sure it will, I didn't know an ounce of OpenGL before April this year... and I'm happy to say that I have been able to help at least some of the more recent visitors to OpenGL.

Glad you managed to get the program working correctly.. well almost.

Tina