colouring each pixel on the surface area one at a time

Started by
5 comments, last by Brother Bob 15 years, 10 months ago
Hi Guys, My OpenGL window is 256x256 pixels. I want to iterate through each pixel in my window and colour it a specific colour. My render function looks like this:


void Render( )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		
glLoadIdentity();						

const int windowWidth = 256 ;
const int windowHeight = 256 ;

// scale everything real small so its about pixel size (probably better way to do this
glScalef(0.005,0.005f,1.0f);
	
for (int x = 0 ; x < windowWidth; x++)
{
	for (int y = 0 ; y < windowHeight; y++)
	{
		float transX = (x-(windowWidth/2));
		float transY = (y+(windowHeight/2));

		glTranslatef(transX,transY,1.0f) ;
		glColor3f(GET_NEXT_COLOUR);				
		glBegin(GL_QUADS);					
		glVertex3f(-1.0f, 1.0f, 0.0f);			
		glVertex3f( 1.0f, 1.0f, 0.0f);			
		glVertex3f( 1.0f,-1.0f, 0.0f);			
		glVertex3f(-1.0f,-1.0f, 0.0f);			
		glEnd();	
			
	}
}

So what im basically doing is scaling everything really small (there must be a more acurate way to draw a pixel than this). Then i iterate through all the pixels and draw and quad and translate it to its correct position. This only draws one pixel on the screen. Any ideas on how I can get this working, or if there is a much better and faster way to do this? Thanks
Advertisement
Dear god, no. Please don't draw thousands of tiny squares.

Write pixel data into a buffer. Use glTexSubImage2D to write that buffer into a texture. Draw that texture to the screen using a textured quad. There's slightly more efficient ways (using PBOs) but this is pretty good... certainly better than all those per-pixel function calls.
Hi,

thanks for the reply. I had a look at glTexSubImage2D but glDrawPixels seemed easier, and this is what i've come up with:

static float pixels[256][256][3];void Render( ){		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glLoadIdentity();	glRasterPos2i(0, 0);	glDrawPixels(1.0, 1.0, GL_RGB, GL_FLOAT, **pixels);	glFlush();			}void InitPixels( ){// all pixels whitefor (int i = 0; i < 256; i++)	{		for (int y = 0; y < 256; y++)		{			pixels[y][0] = 255 ;			pixels[y][1] = 255 ;			pixels[y][2] = 255 ;		}	}}


However this is only drawing the one pixel in the middle of the window. Any ideas?

Thanks
Quote:Original post by juffed
However this is only drawing the one pixel in the middle of the window. Any ideas?

Replace your glDrawPixels() to:

glDrawPixels(windowWidth, windowHeight, GL_RGB, GL_FLOAT, pixels);

And, why do you use float data type for the pixel array? I think GL_UNSIGNED_BYTE (GLubyte) is enough, and even you initialized it with 255.
that worked great. Thanks a lot! The only difficulty im having now is that its drawing pixel[0][0] at position bottom left, when i'd rather it be top left. Any way I can fix this?

Thanks both for your patience as you can probably guess I know very little about OpenGL. But for my current project this render function is the only thing I need OpenGL for, so as you can imagine I dont really want to spend long learning a new API when I have very little time left to finish the rest :)

Thanks again guys.
You mean the image is flipped on the y-axis?
With glDrawPixels you have to do the flip yourself: just swap the first and the last line, the second and the next to last and so on.

With glTexImage2D (i.e. using textures) you could just flip the texture coordinates of the quad.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Flipping can be done with glPixelZoom(1, -1). This will draw the image downwards, instead of upwards, from the raster position. Keep in mind that the raster position must be placed in the upper left corner, as this does not flip the image being drawn, but draws it downwards instead of upwards.

This topic is closed to new replies.

Advertisement