Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In OpenGL, can I draw a pixel that exactly at the coordinates (5, 5)?

Tags:

c

opengl

glut

By (5, 5) I mean exactly the fifth row and fifth column.

I found it very hard to draw things using screen coordinates, all the coordinates in OpenGL is relative, and usually ranging from -1.0 to 1.0. Why it is so serious to prevent programmers from using screen coordinates / window coordinates?

like image 993
xzhu Avatar asked May 27 '11 10:05

xzhu


People also ask

How do you find the pixel position?

Determine the Location of the Pixel Region Rectangle To determine the current location of the pixel region in the target image, you can use the pixel information given at the bottom of the tool. This information includes the x- and y-coordinates of pixels in the target image coordinate system.

What is a pixel coordinate?

The pixel coordinate is a number that identifies the location of a pixel in the image array.

How are pixels laid out?

Pictures are made up of little pixels, laid out on an (x,y) grid. Each pixel contains a color. Each color has a red part, a green part, and a blue part. Each color part is actually a number between 0 and 255.


1 Answers

The simplest way is probably to set the projection to match the pixel dimensions of the rendering space via glOrtho. Then vertices can be in pixel coordinates. The downside is that resizing the window could cause problems and you're mostly wasting the accelerated transforms.

Assuming a window that is 640x480:

// You can reverse the 0,480 arguments depending on you Y-axis 
// direction preference
glOrtho(0, 640, 0, 480, -1, 1);

Frame buffer objects and textures are another avenue but you'll have to create your own rasterization routines (draw line, circle, bitmap, etc). There are problaby libs for this.

like image 59
basszero Avatar answered Sep 20 '22 18:09

basszero