PDA

View Full Version : Help



ayushman4
04-26-2012, 12:31 AM
Hi I am new to OpenGl and just got started.I was trying to make a program that draws a square where ever i click the mouse button.
It is not working.If anyone has any ideas please help.


**********CODE*****************************

#include<GL/glut.h>
#include<iostream>
#include<math.h>

void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glTranslatef(9,5,0);
glutSwapBuffers();
}
void drawSquare()
{
glBegin(GL_QUADS);
glVertex2f(10.0, 10.0);
glVertex2f(-10.0, 10.0);
glVertex2f(-10.0, -10.0);
glVertex2f(10.0, -10.0);
glEnd();
}
void drawSquare1()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
glVertex2f(10.0, 10.0);
glVertex2f(-10.0, 10.0);
glVertex2f(-10.0, -10.0);
glVertex2f(10.0, -10.0);
glEnd();
}
void myMouse(int button,int state,int x,int y)
{
if((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
{
drawSquare();
myDisplay();
}
else
drawSquare1();
}



void myinit()
{

glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,50.0,0.0,50.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(0,0);
glutCreateWindow("SQUARE PROGRAM");
glutMouseFunc(myMouse);
glutDisplayFunc(myDisplay);
myinit();
glutMainLoop();
return 1;
}

BionicBytes
04-26-2012, 01:01 AM
Firstly, you don't seem to have read the posting guide. If you had, you would know about certain protocols for asking questions. Mainly things like 'Help me' type questions. You need to be more specific with you line of questions so people can be specific with their answers.

Anyway, the problem you have is that you are not understanding the concept of events in Windows. The Mouse event handler is not the place to be putting drawing code - that's the job of your onDraw event handler.


void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glTranslatef(9,5,0);
glutSwapBuffers();
}

This onDraw event is not going to do much is it? It does not even contain the code required to draw anything. You need to insert drawSquare();
after the glTranslatef command to position the square before drawing it.

ayushman4
04-26-2012, 04:22 AM
I will go through the posting guide.Anyway even after putting the drawSquare function the mouse click events are not getting called.Can you go through the code properly and tell me if the code is ok or where am i going wrong.and should the myDisplay function be in the mouse event handler function.
And what i want the program to do is--- it should display a white screen,when i click on a particular area it should display a square.

Thank You

BionicBytes
04-26-2012, 05:52 AM
I'm not going to do the code for you - that's where you get benefit from - by spend the time fixing things yourself. It's the only way to learn (by putting in the hours).
You should obly be setting flags in the mouse events - no draw code - as I said earlier.
Your draw code should be something like this:


void drawSquare1()
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
glVertex2f(10.0, 10.0);
glVertex2f(-10.0, 10.0);
glVertex2f(-10.0, -10.0);
glVertex2f(10.0, -10.0);
glEnd();

Since you are not using textures, you should also disable texturing - glDisable (GL_TEXTURE2D) during init()

myasovik
04-26-2012, 09:14 AM
There few thing you need to be more clear about:
1. To use glutSwapBuffers() you need to init two buffers > glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);. if you want to use GLUT_SINGLE you need to use glFlash(). But I would suggest to continue use SwapBuffers.
2. You need to use glutPostRedisplay function in the end of myMouse(...). That function set the flag that you would like to redraw the screen;
3. As was mentioned before you should put the draw function in myDisplay(...) and put glTranslate in myMouse(). If you think about, you want to change coordinates of a quad when a mouse clicked, not when you draw it(if you will draw 100 times per second you will not see how it moved).

Carmine
04-26-2012, 02:21 PM
It is not working. That's way to vague to inspire anyone to help you. Does this code compile and link? What happens when you run it? Forget about the mouse stuff for a while. Are you able to simply draw a square somewhere in your window? Get that working first. Then move on to the mouse stuff. Good luck.

ayushman4
05-01-2012, 01:48 AM
thank you .. got it working like i wanted to..