PDA

View Full Version : Easy Pixel Format Question



typewriter
09-23-2004, 02:05 PM
greetings,

I am a long time programmer who is finally taking the plunge ito open GL.. I can't get this simple program to behave the way I would expect it to. I'm simply trying to draw a 100x100 square full of randomly colored pixels. Instead, I get a square with randomly colored pixels, and a severe vertical striping type effect. I have tried everything I could think of with glPixelStorei, but It doesn't change...


//Graphical "Hello World" routine : random square

glPixelStorei(GL_UNPACK_ROW_LENGTH, 100);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

GLubyte *Square[100*100*3];
int i;

for(i=0;i<30000;i++){
Square[i]=rand()%255;
}

glRasterPos2i(0,0);

glDrawPixels(100, 100, GL_RGB, GL_UNSIGNED_BYTE,Square);
glEnd() ;

THANKS FOR YOUR HELP GUYS!

mikael_aronsson
09-23-2004, 11:01 PM
Hi !


GLubyte *Square[100*100*3]; How about using:


GLubyte Square[100*100*3]; Instead ?

Your code allocates 30000 byte pointers, the second allocates 30000 bytes.

This means that each random value you put in there will use up 4 bytes NOT one as I assume you want.

The result would be 1 random colord pixel and 3 black pixels and then same thing over and over again, is this the problem you have ?

Mikael

Jan
09-24-2004, 01:28 AM
Sounds like disabled v-sync. Set V-Sync to be enabled on default under windows. (Desktop->Right Click -> Properties -> Display Settings -> Advanced Settings).

Jan.

typewriter
09-24-2004, 09:46 AM
Thanks mikael_aronsson...

I knew it was something stupid like that!

typewriter
09-24-2004, 10:08 AM
I am learning Cocoa and oGL at the same time after many many years of C++ and Intel 13h graphics expereince. I know algoritms and theory but I'm a newbie once again in the actual implimentation and syntax. I'm trying to figure out the best way to hold large arrays for graphics. (Which can then be manipulated with my algorithms)

So in that note, what's wrong with:

GLubyte Square[720*486*3];

The same program from above, when enlarged to 720x486 instead of 100x100 crashes.

thanks for your help guys

09-24-2004, 10:18 AM
MAybe its not the best idea to allocate huge arrays on stack. :D

typewriter
09-24-2004, 10:24 AM
(Psst I'm a mathematician not a programmer.. some crossover, but not with stuff like dealing with the memory.. still learning)

typewriter
09-24-2004, 10:47 AM
thoughts;

I have noticed in my educational life in general, that people are often severely lacking in fundamentals. Instructors so often glaze over the very basic "moves" in math and related subjects. The instructors categorize them as "automatic", in their own minds, and then the importance is lost on the students.

This is why I will ask questions, even stupid ones, because I am trying to build up the fundamentals of programming.

So thanks <me>.. now I know that not only is it "bad" to declare large array variables inside "the stack" but it actually crashes the program. Thats good, (now obvious) important information for a beginner that none of the literature I read mentioned.

And this is after I have already written things like realtime fourier transformers and other "advanced" programs.

Teaching yourself something is really hard because these fundamentals are rarely adressed directly. So the web boards like this become EXTREMELY valuable!

mikael_aronsson
09-24-2004, 10:52 PM
GLUbyte* vector = (GLUbyte*) malloc( 100*100*3);

... do something with it...

free( vector);

Or you can of course use new/delete instead in C++.

The space available on the stack is usually not that big by default even though many times you can set it with an argument to the linker.

So use malloc/free or new/delete for larger objects.

Mikael

09-25-2004, 12:52 AM
Originally posted by typewriter:
I have noticed in my educational life in general, that people are often severely lacking in fundamentals. Instructors so often glaze over the very basic "moves" in math and related subjects. The instructors categorize them as "automatic", in their own minds, and then the importance is lost on the students.I already noticed that myself, thats why I answered you. Still I couldnt resist placing the smilie. ;)
Maybe you saw a new thread with another basic question opened be me ( I should have used a more unique name not already registered by someone :rolleyes: ).