PDA

View Full Version : pixel coordinates please help!



Kate
01-29-2003, 05:18 AM
hey

I want to draw to particular pixels, colour ones white or black next to each other, but the (0,0) and (0,1) coordinates dont seem to be next to each other - theres a gap inbetween - what are the coordinates for the actual pixels?

please please help!!

cheers
Kate

nexusone
01-29-2003, 05:25 AM
What command are you using to draw your pixels?

Post a bit of the drawing code?

I am guessing you are using the wrong command for pixel access.


Originally posted by Kate:
hey

I want to draw to particular pixels, colour ones white or black next to each other, but the (0,0) and (0,1) coordinates dont seem to be next to each other - theres a gap inbetween - what are the coordinates for the actual pixels?

please please help!!

cheers
Kate

Kate
01-29-2003, 05:36 AM
hey,

I've got an array, say array[10][10] and I want to say if element array[x][y]==1 then draw pixel as white in position (x,y) so that I can specify the colour directly of every pixel on the screen by storing them in the array.

I'm using the following bit of code, but the positions x,y are not next to each other:

glBegin(GL_POINTS);
for(i=0;i<ROWS;i++)
for(j=0;j<COLUMNS;j++)
if(array[i][j][0]==1){
glVertex2f(i,j);
}
glEnd();

any ideas??

cheers!
Kate

Dave.D
01-29-2003, 05:52 AM
Is your perspective matrix orthogonal? As there can be an infinite number of points between (0, 0) and (1, 0), you may have to "trick" OpenGL to rasterizing on a pixel-aligned grid. If you're just concerned about 2d, you might try something like this...

// initialization
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, 400.0f, 0.0f, 400.0f, 0.01f, 100.0f); // note - this assumes a rendering window of 400x400
glMatrixMode(GL_MODELVIEW);

// render loop
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -1.0f); // hop off the near-z a tad
glBegin(GL_POINTS);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(200.0f, 200.0f); // (0.0f, 400.0f) is the top left corner
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(201.0f, 200.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(202.0f, 200.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(203.0f, 200.0f);
// etc...

This should render nearly pixel-sized points in a line near the center of the window.

Hope this is helpful,
Dave

[This message has been edited by Dave.D (edited 01-29-2003).]

Kate
01-29-2003, 06:03 AM
cheers for that although i'm afraid i'm very new to this and don't quite get it- do I put all that code into the display function or does the top bit go in the main function or what?
i'm using this reshape function which might be causing the problem and i've put the other functions i'm using below? do I just alter this? I'm doing 3d but as 2d slices if you see what I mean so the array goes array[i][j][slice]

which bits of my code do I need to change??
cheers!
Kate

void reshape(int w,int h)
{
glViewport(0,0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0,50.0,-50.0, 50.0,-50.0,50.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}


void init(void)

{

glClearColor(0.0, 0.0, 0.0, 0.0) ;
glShadeModel(GL_FLAT);

}
void display(void)
{

glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();
glRotatef(spin, 0.0, 1.0, 1.0);
glColor3f(6,1,1);


//draw valve

growValve();

glBegin(GL_POINTS);
for(i=0;i<xMAX*2;i++)
for(j=0;j<yMAX*2;j++)
if(valve[i][j][0]==2){
glVertex2f(i-xMAX,j-yMAX);
}
glEnd();
glPopMatrix();
glutSwapBuffers();
glFlush();

}
int main(int argc, char** argv)

{

int x;

GrowthCounter = 0;

glutInit (&argc, argv) ;

glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB) ;

glutInitWindowSize (250, 250) ;

glutInitWindowPosition (400, 400) ;

glutCreateWindow (argv[0]) ;

init() ;

initialiseTemplateValve();

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutMouseFunc(mouse);

x= pointLineTest(2.06,5.07);

printf("\nResult: %d", x);

glutMainLoop();

return 0 ;

}

nexusone
01-29-2003, 06:21 AM
Just as I suppected, glBegin(GL_POINTS) are vector point not pixel points.

Depending on how you have setup your ortho view 0 to 1 in vector leave a infinite number of points between them 0.1, 0.2,...,1

Maybe by adjusting the ortho2d view and location of the points in relation to the camera view could do the trick.

Also you can use glDrawPixel to draw directly to the screen a pixel. But not always the best way to do things, but would give you what you want a pixel to pixel drawing of a point.


Originally posted by Kate:
hey,

I've got an array, say array[10][10] and I want to say if element array[x][y]==1 then draw pixel as white in position (x,y) so that I can specify the colour directly of every pixel on the screen by storing them in the array.

I'm using the following bit of code, but the positions x,y are not next to each other:

glBegin(GL_POINTS);
for(i=0;i<ROWS;i++)
for(j=0;j<COLUMNS;j++)
if(array[i][j][0]==1){
glVertex2f(i,j);
}
glEnd();

any ideas??

cheers!
Kate



[This message has been edited by nexusone (edited 01-29-2003).]

Kate
01-29-2003, 06:25 AM
ah! that sounds good - how does the glDrawPixel command work exactly, I take it its not within a glBegin(); glEnd(); thingy then? is it just glDrawPixel(x,y,z);?

cheers Kate

nexusone
01-29-2003, 06:34 AM
It is glDrawPixels which is a 2D operation, so there is no Z axis, since it is drawn directly to the screen. Can not be used between glBegin/end.


Here is the link to the reference page for it. You draw a group of pixels at a time with it, maybe could do a single pixel also.
http://www.opengl.org/developers/documen...write#first_hit (http://www.opengl.org/developers/documentation/man_pages/hardcopy/GL/html/gl/drawpixels.html?gl+pixel+write#first_hit)


Originally posted by Kate:
ah! that sounds good - how does the glDrawPixel command work exactly, I take it its not within a glBegin(); glEnd(); thingy then? is it just glDrawPixel(x,y,z);?

cheers Kate

[This message has been edited by nexusone (edited 01-29-2003).]

[This message has been edited by nexusone (edited 01-29-2003).]

Dave.D
01-29-2003, 06:35 AM
Ok, now I'm definately no expert in this area, but I think the problem you're experiencing has to do with the size of your window vs the projection matrix.

Based on the code you presented, it appears as if your window has a dimension of 250x250 pixels. Your projection matrix is set at 100 units wide and 100 units in height, with 0,0 being the center of the viewport. In this scenario, (0.0f, 0.0f) is relatively close to the pixel at [124, 124]. (1.0f, 0.0f), however, is rasterized close to pixel [127, 124] - this creates the "gaps" between points. In this scenario, pixel_x = x * 250 / 100. To render accurate pixels, you need an algorithm closer to pixel_x = x * 250 / 250.

I think if you modify your reshape function, you can get more accurate results:

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-((float)w / 2.0f), (float)w / 2.0f, -((float)h / 2.0f), (float)h / 2.0f, -50.0f, 50.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

Hope this helps,
Dave

Kate
01-29-2003, 06:43 AM
I love you Dave and I want to have your babies!!! it works!!!

Cheers for the draw pixel command nexusone, may well come in handy!!

woo hoo yipee!!