grunge fightr
grunge fightr

Reputation: 1370

per pixel drawing in OpenGL

I used to write small games with 'per pixel' drawing, I mean with some SetPixel(x,y,color) function or such.

I am also interested in OpenGL but do not know it much.

Is it a good (fast) way to do a per pixel drawing in OpenGL ?

It would be good for example to use textured quads as a sprites, or whole application background screen, with possibility to set distinct pixel with some kind of my own SetPixel routine i would write... or any other way - but it should be efficient as much as it cans

(especialy im interested in basic fundamenta 1.0 version of OGL)

Upvotes: 2

Views: 3174

Answers (2)

datenwolf
datenwolf

Reputation: 162164

You can set a projection that will map vertex coordinates 1:1 to pixel coordinates:

glViewport(0, 0, window_width, window_height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, window_width, 0, window_height, -1, 1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

From here on, vertex X,Y coordinates are in pixels with the origin in the lower left corner. In theory you could use the immediate mode with GL_POINT primitives. But it's a much better idea to batch things up. Instead of sending each point indivisually create an array of all the points you want to draw:

struct Vertex
{
    GLfloat x,y,red,green,blue;
};

std::vector<Vertex> vertices;
/* fill the vertices vector */

This you can OpenGL point to…

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

/* Those next two calls don't copy the data, they set a pointer, so vertices must not be deallocated, as long OpenGL points to it! */
glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].x);
glColorPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].red);

…and have it access and draw it all with a single call:

glDrawArrays(GL_POINTS, 0, vertices.size();

Upvotes: 6

ssube
ssube

Reputation: 48247

You really don't want to do this.

Is it a good (fast) way to do a per pixel drawing in OpenGL ?

There is no good or fast way to do this. It is highly discouraged due to the speed.

The proper way, although not easy (or in some cases possible) in OGL 1, is to use pixel shaders or blend modes. That is the only correct way, anything else is hacking around the entire system.

Depending on how the data needs modified, vertex colors and blend modes may be able to solve the some uses. It won't tint each pixel individually, but you can modify the texture must faster.

To do it, you can draw single-pixel quads (although care must be taken to offset them and handle filtering to prevent blurring) or you can get texture data and manipulate it later. Both will be unbelievably slow, but could function.

Working with the texture data is probably simpler and may be slightly faster.

Upvotes: 1

Related Questions