Jump to content
  • Advertisement
Sign in to follow this  
bergen14

OpenGL Basic OpenGL

This topic is 4413 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I'm currently learning OpenGL and am having problems with a program that I'm writing as an exercise. I'm trying to draw dots in the window at the point the mouse clicks. For some reason that eludes me, the dots aren't appearing. This is my code and I'm very sorry for being a noob. We all start somewhere :) Thanks #include <glut.h> #include <iostream> using namespace std; void drawDot(int x, int y) { glPointSize(4.0); glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); } void myMouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { drawDot(x, 640 - y); cout << "Click at (" << x << ", " << y << ")" << endl; } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { glClear(GL_COLOR_BUFFER_BIT); glFlush(); } } void myInit(void) { glClearColor(1.0,1.0,1.0,0.0); glColor3f(0.0,0.0,0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,640.0,0.0,480.0); } void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); //drawDot(100,100); glFlush(); } void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640, 480); glutInitWindowPosition(100, 150); glutCreateWindow("My Window"); glutMouseFunc(myMouse); glutDisplayFunc(myDisplay); myInit(); glutMainLoop(); }

Share this post


Link to post
Share on other sites
Advertisement
I'm not familiar with glut, but IIRC the glut main loop will swap the buffers at the end. You're probably drawing the dots, but since you never store them they're being wiped before you actually see them.

You probably want a vector of points, and add to this on a left click (and clear on a right click). Then every time around your main loop you'll want to iterate over the vector and call drawDot for each.

Share this post


Link to post
Share on other sites
Quote:
Original post by OrangyTang
I'm not familiar with glut, but IIRC the glut main loop will swap the buffers at the end. You're probably drawing the dots, but since you never store them they're being wiped before you actually see them.

You probably want a vector of points, and add to this on a left click (and clear on a right click). Then every time around your main loop you'll want to iterate over the vector and call drawDot for each.
That would be true for a double buffered display mode (you still have to manually swap the buffers though) but the OP is specifying a single buffered display. I would recommend going double buffered and using OrangyTang's suggestion however, because it won't mess up if you move another window over it.

I think your problem is that you aren't flushing the GL command buffer after you call drawDot. Try adding a glFlush there. I think you have another problem converting to OpenGL's coordinates as well...
Quote:
drawDot(x, 640 - y);
I assume you want to use the window's height there instead of its width, so replace 640 with 480 (it'd be better if it were a variable however - not only would it make changing the window size easier but it will also make errors like this very obvious).

Share this post


Link to post
Share on other sites
In my small experience I've always seen a different approach for GLUT applications.
All the rendering stuff is contained in the Display Function.
So in my opinion the correct way to implement your application is to collect the input using the GLUT callbacks for the mouse or the keyboard. Save input in some variables. Then perform the rendering in the Display Function using those variables.

So the MyMouse function should be:

bool gb_LeftMouseButtonPressed = false; // Global

void myMouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
gb_LeftMouseButtonPressed = true;
cout << "Click at (" << x << ", " << y << ")" << endl;
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
gb_LeftMouseButtonPressed = false;
}:

And the My Display function should be

void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
if ( gb_LeftMouseButtonPressed )
drawDot(100,100);

glutSwapBuffers () ;
glutPostRedisplay();
};

Or something like this.

Kind regards

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!