Jump to content
  • Advertisement
Sign in to follow this  
juffed

OpenGL colouring each pixel on the surface area one at a time

This topic is 3831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 white
for (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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!