PDA

View Full Version : Drawing mere pixel



Dogcow
09-19-2003, 10:37 PM
I'm trying to just draw a single pixel in an opengl view... I've tried the following code but not seen anything but the black background...what else is needed?



glClearColor(0.0, 0.0, 0.0, 0.0);

glClear(GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT+GL _STENCIL_BUFFER_BIT);

glBegin(GL_POINTS);

glColor3f(1.0,1.0,1.0);
glVertex2f(250,250);

glEnd();

Thanks,
-Dogcow "moof!"

mikael_aronsson
09-19-2003, 10:42 PM
Hi !

You need to setup the modelview transformation, the default "camera" in OpenGL is located at 0,0,0 looking along Z axis and you would probably need to change that to make your point visible, take a look at a tutorial on OpenGL, you can just do a glTranslate( 0, 0, distance_from_origin) or use gluLookAt() to change the "camera"

Mikael

zeckensack
09-19-2003, 11:18 PM
On the default modelview and projection (identity matrices), a glVertex3f(0.0f,0.0f,0.0f); will give you a point in the center of the screen.

Obli
09-20-2003, 07:48 AM
Not sure, but maybe DrawPixels would be closer to what you are trying to do.

Dogcow
09-20-2003, 08:09 AM
Yeah, the problem is I have be able to draw a pixel in the right spot in a view...

I'm required for a class to write the code to draw a line and to convert from world coordinates to screen coordinates. Once in screen coordinates, I'm supposedly able to use this code:




void Pixel::drawPixel(int xPixel, int yPixel)
{
//convert from pixel to world projected coordinates and plot one pixel
double xCanonical;
double yCanonical;

xCanonical=(xPixel*2.0)/xWindow + (-1.0*(xWindow-1))/xWindow;
yCanonical=(-1.0*yPixel*2.0)/yWindow + (1.0*(yWindow-1))/yWindow;

glVertex2f((float)xCanonical,(float)yCanonical);
}

to actually draw the pixel, but I haven't had any luck as of yet...

Ideas?

-Dogcow "moof!"

09-20-2003, 09:57 AM
How far can you stretch the assignment? You can make the conversion trivial, but I expect that will break the rules...

Use glOrtho or gluOrtho2D to create a scene with the xmax and ymax set to the window's width and height, and xmin and ymin set to 0. This should give a one-to-one correspondence between your drawing space and the pixels on the screen.

Hint for the future: instead of adding together values in the glClear call, bitwise OR them (use | instead of +). If you ever typo and include the same value twice, this will save a lot of head-scratching.

Dogcow
09-20-2003, 10:29 AM
Thanks for the tip with the or operand YA Anonymous User...

Turns out what I was forgetting was the fact that I have to use values between -1 and 1... *oops*

Thanks all...
-Dogcow "moof!"