For the sake of learning, I'm accessing individual pixel data using GLUT and manually setting pixel color by going through all pixels in the window, like this (some non-related code omitted):
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, WIDTH, 0.0, HEIGHT);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
glPointSize(1.0f);
glColor3f(255, 0, 0);
glBegin(GL_POINTS);
glVertex2i(i, j);
glEnd();
}
}
glutSwapBuffers();
}
void timer(int obsolete) {
glutPostRedisplay();
glutTimerFunc(16, timer, 0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("GLUT Test");
init();
glutDisplayFunc(display);
timer(0);
glutMainLoop();
return 0;
}
I'm expecting to get a fully red pixels window, but I'm getting something different - a window with black vertical stripes, as if horizontal for loop suddenly skipped some lines.
Any ideas what am I doing wrong here? I have a suspicion it might be related to float to int conversion somewhere "inside", but I'm not sure what to search for.
Edit: I've found out that if I resize the window in runtime to be one pixel less in width, then these black stripe tears disappear.
You set up the projection such that the left edge is at 0, and the right one at WIDTH. Note that your pixels are small squares with an area, and this means that 0.0 maps to the left edge ot the left-most pixel, and WIDTH maps to the right edge of the right-most pixel. Integer coordinates will lie exactly in the middle between two pixels. And with some numerical precision loss during transformation, you might end up with two neighboring points beeing rounded to the same pixel.
You can either add 0.5 to x and y when drawing your points, or just shift your orth projection by half a pixel so that integers are mapped to pixel centers:
Ortho(-0.5f, WIDTH-0.5f, -0.5f, HEIGHT-0.5f, ...);
Related
I have to make a 2d Chess Board of 8 * 8 blocks in OpenGL with VisualC++. I have the following code that i tried .
But I have a problem in this code .
I can't reduce the board size. e.g. 5*5.
When i click on the window it redraws the board.
I want to make this code to just work with loops. Except If , Else.
#include<GL\glut.h>
int black=0,white=1,color=0;
GLint a1=10,b1=10,a2=30,b2=30;
void init (void)
{
glClearColor (0.0, 0.0,1.0,0.0);
glMatrixMode (GL_PROJECTION);
glClear (GL_COLOR_BUFFER_BIT);
gluOrtho2D (0.0,120.0,0.0,140.0);
}
void lineSegment ()
{
for(int i=0;i<3;i++)
{
if(b2<120)
{
a1=10;b1=b2;
a2=30;b2=b2+20;
}
for(int j=0;j<5;j++)
{
if(a2<120)
{
if(color==black)
{
glColor3f(0,0,0);
glRecti(a1,b1,a2,b2);
color=1;
a1=a2;
a2=a2+20;
}
else
{
glColor3f(1,1,1);
glRecti(a1,b1,a2,b2);
color=0;
a1=a2;
a2=a2+20;
}
}
}
//GLint a1=10,b1=10,a2=30,b2=30;
}
glFlush();
}
void main (int argc, char** argv)
{
glutInit(&argc,argv); //Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition (50,100); //Set top-left display-window position.
glutInitWindowSize (400,300); //Set display-window width and height.
glutCreateWindow ("An Example OpenGL Program"); //Create display window.
init(); // Execute initialization procedure.
glutDisplayFunc(lineSegment); //send graphics to display window.
glutMainLoop(); //display everything and wait.
}
Im going to suggest you significantly reduce the code you are working with. Define a width and height for the board and the number of divisions per side of the board.
Lets define width and height as w and h respectively and the number of divisions n and m respectively. w.l.o.g. assume n and m divide w and h evenly.
void DrawBoard(int w, int h, int n, int m) {
bool color = true;
int sw = w/n, sh = h/m; //square width and height respectively
//for each width and height draw a rectangle with a specific color
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
//oscillate the color per square of the board
if(color)
glColor3f(1, 1, 1);
else
glColor3f(0, 0, 0);
color = !color;
//draw a rectangle in the ith row and jth column
glRecti(i*sw, j*sh, (i+1)*sw, (j+1)*sh);
}
if(m % 2 == 0) color = !color; //switch color order at end of row if necessary
}
}
This should give the basic idea, though I might have hessed up an index or two. But essentially, iterate over the grid cells and draw a rectangle per board square.
This code will also draw the board starting at coordinate (0, 0) and finishing at (w, h). However if you would like it in an arbitrary position you can either add an (x, y) to eaxh corresponding coordinate in the glRecti call, or learn about transforms in openGL and use glTranslatef.
I am fairly new to using GLUT, and I have been attempting to compile a program (which I found here, first response) that uses the mouse to draw a rectangle by recording the starting and ending points of a click-and-drag.
As a clean copy/paste, it will compile but not draw anything. It just displays a white screen, even after changing the background color to black (in the setup() function). I've read several sources to verify that this program doesn't miss anything in its draw and reshape functions, and it's all there.
I create a window, set the viewport to the window dimensions, and then use the gluOrtho2D function to set the mapping (since the window and viewport are the same dimensions, I set the mapping to the window dimensions). The mouse callback records where I left-click, and where I release left-click, then calls the glutPostRedisplay() function to redraw the window with the new coordinates. After a bit of debugging, I discovered the coordinates are recorded and saved appropriately, and are measured in pixels (x and y are integers between 0 and window dimension), so I should be able to draw a rectangle from one vertex to the other vertex using the coordinates. But, like I said, it only displays a white screen.
So, is there something wrong with the way I am drawing the rectangle? Am I mapping the window incorrectly? I am seriously lost, and any feedback would be greatly appreciated.
EDIT2: I changed the glutInitDisplayMode from GLUT_SINGLE to GLUT_DOUBLE, and that fixed the whole non-interactive white screen thing. Now it will draw a rectangle with the mouse with a flipped y-coordinate (which I fixed), and it works great now. Thank you very much for the suggestion.
Here is my program (EDIT1: added comments):
#include <cstdlib>
#include <GL/glut.h>
using namespace std;
GLsizei width, height;
struct Position
{
Position() : x(0), y(0) {}
float x;
float y;
};
Position start; // Records left-click location
Position finish; // Records left-click release location
void display()
{
glClear(GL_COLOR_BUFFER_BIT); // clear window
glColor3ub(rand()%256, rand()%256, rand()%256); // generates random color
glBegin(GL_QUADS);
glVertex2f(start.x,start.y);
glVertex2f(finish.x,start.y);
glVertex2f(finish.x,finish.y);
glVertex2f(start.x,finish.y);
glEnd();
glutSwapBuffers(); // display newly drawn image in window
}
void reshape( int w, int h )
{
glViewport( 0, 0, (GLsizei)w, (GLsizei)h ); // set to size of window
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0.0, (float)w, 0.0, (float)h );
width = w; // records width globally
height = h; // records height globally
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouse(int button, int state, int x, int y)
{
switch(button)
{
case GLUT_LEFT_BUTTON:
if(state==GLUT_DOWN)
{
start.x = x; //x1
start.y = y; //y1
}
if(state==GLUT_UP)
{
finish.x = x; //x2
finish.y = y; //y2
}
break;
glutPostRedisplay();
}
}
void motion( int x, int y )
{
finish.x = x;
finish.y = y;
glutPostRedisplay();
}
void setup()
{
glClearColor(0.0, 0.0, 0.0, 1.0); // *should* display black background
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow("");
setup();
// initializing callbacks
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
return 0;
}
As my comment suggested:
change:
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
to:
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
I am making a simple GLUT program which tracks the mouse and put points on the path.
Well This is my code:
void init()
{
glClearColor( 0.0, 0.0, 1.0, 1.0);
glMatrixMode( GL_PROJECTION);
gluOrtho2D( 0.0, 400.0, 0.0, 400.0);
for(int i=0;i<5000;i++)
{
arr[i][0]=0;
arr[i][1]=0;
}
glPointSize(10.0);
}
void drawPoints()
{
glBegin( GL_POINTS );
glColor3f( 0.0,0.0,0.0 );
for ( int i = 0; i < z; i++ )
{
glVertex2f( arr[i][0], arr[i][1]);
}
glEnd();
}
void myDisplay()
{
glClear( GL_COLOR_BUFFER_BIT);
drawPoints();
glutSwapBuffers();
glutPostRedisplay();
}
void myMouseMove( int x, int y)
{
arr[z][0]=x;
arr[z++][1]=y;
}
int main( int argc, char ** argv)
{
glutInit( &argc, argv);
glutInitDisplayMode( GLUT_DOUBLE| GLUT_RGB);
glutInitWindowPosition( 100, 100);
glutInitWindowSize(600,600);
glutCreateWindow( "Testing");
init();
glutDisplayFunc( myDisplay);
glutPassiveMotionFunc( myMouseMove);
glutMainLoop();
return 0;
}
However I am having few problems:
Y coordinate runs in opposite direction.
Draws point ahead of cursor position(while moving in a direction).
Is there any better way to do this?
The Y coordinate being flipped is actually expected behavior. Simply correct for it in your code and you should be fine.
If you want to make sure that your rendered image and mouse cursor are completely synchronized, simply have glut hide the mouse cursor, and then render it yourself using OpenGL.
You should be aware that when using a traditional projection matrix in OpenGL: (0,0) is the lower-left corner. Most window systems will map (0,0) to the top-left corner.
In some circumstances, you can make them match up simply by swapping the bottom/top fields in your call to glOrtho (...) or gluOrtho2D (...) - this has other consequences like reversing polygon winding, so it is not always the best approach.
As for "drawing points ahead of the cursor," I think you may be describing input latency (particularly if you are using VSYNC).
When you use a software cursor vs. hardware cursor the position of the mouse may be off by one or more frames. There is a somewhat technical discussion here (see: Idiosyncrasies) on the effect of buffer swap intervals (OpenGL's mechanism for VSYNC) on input latency.
To fix the inversion do 600 - y for setting arr element. Also your gluOrtho2D is on a scale of 400 by 400 while your windows is 600 by 600 so change your gluOrtho2D scale to the same as the window size and you'll be fine.
I am trying to draw a triangle using GL_POLYGON but for some reason it is taking the whole window..
...
typedef struct myTriangle {
float tx;
float ty;
} myTriangle;
std::vector<myTriangle> container;
void display() {
glClear(GL_COLOR_BUFFER_BIT);
for(int i = 0; i < (int)container.size(); ++i) {
glBegin(GL_POLYGON);
glColor3f(0.35, 0.0, 1.0);
glVertex2f(container.at(i).tx, container.at(i).ty + 20);
glVertex2f(container.at(i).tx - 20, container.at(i).ty - 20);
glVertex2f(container.at(i).tx + 20, container.at(i).ty - 20);
glEnd();
}
glutSwapBuffers();
}
...
int main(int argc, char** argv) {
myTriangle t1;
container.push_back(t1);
container.back().tx = (float)0.;
container.back().ty = (float)0.;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
// initializations
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow( "Transformer" );
glClearColor(1.0, 1.0, 1.0, 1.0);
// global variable initialization
GW = GH = 200;
// callback functions
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(mouseMove);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
It should be drawing an equilateral 40x40 triangle at the origin (0,0) in a 400x400 window. Is there something I did wrong?
You seem to be confusing 3D (world) coordinates with 2D (screen) coordinates. The coordinates you pass to glVertex2f are 3D coordinates that need to be transformed appropriately before they are displayed in your window. The size of your window is immaterial: you can always set up your projection matrix to show as much of the 3D space as you want in any window.
You haven't set up any transformations after initializing OpenGL, so you're using the default matrix, which sits at the origin (0,0,0) in 3D space and the triangle is drawn right over you.
Here's a quick tutorial that shows you how to set up the transformation matrices so that you appear to view the triangle from a distance.
In OpenGL, your screen is defined from [-1, -1] to [1, 1]. Its how rendering systems work.
Try doing
glScalef(2.0f/400, 2.0f/400, 1);
glTranslatef(-1f, -1f, 0);
What the graphics card now does is take your vertices which are defined in pixels, and transforms them so that they correctly sit inside the [-1, -1] to [1, 1] boundry.
You'll see that it first scales it from the [0,0]-[400, 400] boundry to a [0, 0]-[2, 2]. The it translates it to the final [-1, -1]-[1, 1]
Maybe I set up GLUT wrong. I want verticies to be relative to their size in pixels. Right now if I create a hexagon, it takes up the whole screen even though the units are 6.
#include <iostream>
#include <stdlib.h> //Needed for "exit" function
#include <cmath>
//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
using namespace std;
//Called when a key is pressed
void handleKeypress(unsigned char key, //The key that was pressed
int x, int y) { //The current mouse coordinates
switch (key) {
case 27: //Escape key
exit(0); //Exit the program
}
}
//Initializes 3D rendering
void initRendering() {
//Makes 3D drawing work when something is in front of something else
glEnable(GL_DEPTH_TEST);
}
//Called when the window is resized
void handleResize(int w, int h) {
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0, //The camera angle
(double)w / (double)h, //The width-to-height ratio
1.0, //The near z clipping coordinate
200.0); //The far z clipping coordinate
}
//Draws the 3D scene
void drawScene() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); //Reset the drawing perspective
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBegin(GL_POLYGON); //Begin quadrilateral coordinates
//Trapezoid
glColor3f(255,0,0);
for(int i = 0; i < 6; ++i) {
glVertex2d(sin(i/6.0*2* 3.1415),
cos(i/6.0*2* 3.1415));
}
glEnd(); //End quadrilateral coordinates
glutSwapBuffers(); //Send the 3D scene to the screen
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(400, 400); //Set the window size
//Create the window
glutCreateWindow("Basic Shapes - videotutorialsrock.com");
initRendering(); //Initialize rendering
//Set handler functions for drawing, keypresses, and window resizes
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
return 0; //This line is never reached
}
How can I make it so that the coordinates:
(0,0),
(10,0),
(10,10),
and (0,10) define a polygon starting at the top left of the screen and is a width and height of 10 pixels?
If you want the objects to be scaled that sort of way, you should use an orthographic projection.
Right now, with perspective, things are scaled not only by their size, but by their Z-axis position. So use this function instead of gluPerspective:
gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top);
That function basically defines the space you can see, which is like a big rectangular prism. That makes far things appear the same size as near things.
As for the exact scaling, it will also change relative to the viewport size. To get the pixels exactly right, you would have to constantly change the projection, or keep the viewport size fixed.
For it to work out as 1:1, if your viewport is x pixels wide, the orthographic projection should be x pixels wide as well.
If you're drawing in 2D, you don't want to use perspective projection. If you set up your camera with gluOrtho2D(0, window_width, window_height, 0); then you should get what you're looking for.