PDA

View Full Version : Glut mouse Draw in C or C++



Gl_glue
03-08-2001, 06:08 PM
How do I set this up completely to draw on backbuffer. I know I need mouseMove and glutMouseFunc( mouseButton ) but I don't see how to do it with less than 75 lines of code
and have it work well.(click mouse once to choose x1,y1 position click again to produce x2,y2

void mouse(int button, int state, int Mx, int My) {
int ii;
switch(button){
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN){
//

frogger
03-08-2001, 08:06 PM
I have written a program that will do that I think, but you will have to describe more in detail what you want the behavior of the mouse to be....

do you want it to draw while dragging....
do you want it to be a two click drawing
do you wnat to preview the line as it is drawn...

etc....

it is actually very easy, but I just need more info

thank you

Gl_glue
03-09-2001, 04:58 AM
Yes wile it's drawing would be nice(see lines stretch out as you go). (Need working src to get on with my studies)

Thank you Frogger http://www.opengl.org/discussion_boards/ubb/smile.gif

I wish they had a routine that would trace through source and Identify all used parts of a routine.

*********************************************
Heres the routine I am developing now kick the crap out of it in detail I would love to here why it does not work Thanks In advance to anyone who fixes it up because I am trying to have fun making a game and without fundementals it's not easy. don't look at it here high-lite it right click and copy and put it in word pad or Visual C++ and compile as glut0.c compiles without any errors but does nothing thats visable
*********************************************
#include<gl/glut.h>
#include<gl/glu.h>
#include<gl/gl.h>
int vrx1 = .2914;
int vrx2 = .2870;
int vry1 = 0;
int vry2 = 0;
//#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )

//void mouse (void);

void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_LINES);
glColor3f(0.0,0.0,1.0);
glVertex3f(vrx1,vry1,0.0); //vrx1 at x position and vry1 at y's initial value
glVertex3f(vrx2,vry2,0.0); // vrx2 in x2 position vry2 at y2's postion
glVertex3f(0.0,0.0,0.0); //set to zero
glEnd();
glFlush();
}

void main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("3D Tech- GLUT Tutorial");
glutDisplayFunc(renderScene);
glutMainLoop();
}


//Storage area use this for routines you might need
//glutMouseFunc(mouse);
//glutMotionFunc(MouseMove);


/************************************************** ***************\
Mouse functions! Transforms Mouse Points to relative GL
coordinates...
\************************************************* ****************/

void mouse(int button, int state, int Mx, int My) {
switch(button){
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN){
vrx1 == Mx;
vry1 == My;

}
}
if (vrx1 != .2914) {
vrx2 = Mx;
vry2 = My;
glutDisplayFunc(renderScene); //thought this would re-render(update screen)
vrx1 = .2914;
vrx2 = 0;
}
}


http://www.opengl.org/discussion_boards/ubb/smile.gif

[This message has been edited by Gl_glue (edited 03-11-2001).]

Nocturnal
03-09-2001, 01:22 PM
[snip]

/************************************************** ***************\
Mouse functions! Transforms Mouse Points to relative GL
coordinates...
\************************************************* ****************/

void mouse(int button, int state, int Mx, int My) {
switch(button){
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN){
vrx1 == Mx;
vry1 == My;

}
}
if (vrx1 != .2914) {
vrx2 = Mx;
vry2 = My;
// NO - use glutPostRedisplay();
//glutDisplayFunc(renderScene); //thought this would re-render(update screen)
vrx1 = .2914;
vrx2 = 0;
}
}

Gl_glue
03-09-2001, 03:58 PM
Nop that( glutPostRedisplay(); ) did not work any better.....................
I think the mouse routine is not working at all is there some way to check it print out my varx1 on dos screen...

Nocturnal
03-09-2001, 05:48 PM
You forgot to call glutMouseFunc() ?

Gl_glue
03-09-2001, 06:26 PM
Originally posted by Nocturnal:
You forgot to call glutMouseFunc() ?

Used that to I remembered to add it in and even that did not fix it I also changed
void main to int main and added return 0;
to see if it was something flukey but no go
even got it to compile with no errors warnings 0 0 still wont function dam........
will I ever fix this S__@! thanks anyway http://www.opengl.org/discussion_boards/ubb/smile.gif

frogger
03-09-2001, 07:25 PM
I can either post or mail you an example with full source in about 1 hour..... how about that

Gl_glue
03-09-2001, 07:52 PM
That would be outstanding frogger Thank you!
Gl_glue Out....... http://www.opengl.org/discussion_boards/ubb/smile.gif

frogger
03-09-2001, 08:06 PM
well here it is ..... nothing special ....
just the basics

#include <gl/glut.h>

#include <stdio.h>
int numLines;
typedef enum state
{
waitingforclick,
clickedonce,
};

typedef struct point
{
int x;
int y;
}point;


point lines[256][2] ;


int gState=waitingforclick;
bool lineisvalid=false;
int gHeight;
float gColor[3]={0,1,0};

void drawlines()
{
glColor3fv(gColor);
glBegin(GL_LINES);
for(int i=0; i<=numLines; i++)
{
glVertex2i(lines[i][0].x, gHeight-lines[i][0].y);
glVertex2i(lines[i][1].x, gHeight-lines[i][1].y);
}
glEnd();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawlines();
glutSwapBuffers();
}

void menufunc(int val)
{
switch (val)
{
case 0:
gColor[0]=1;
gColor[1]=0;
gColor[2]=0;
break;
case 1:
gColor[0]=0;
gColor[1]=1;
gColor[2]=0;
break;
case 2:
gColor[0]=0;
gColor[1]=0;
gColor[2]=1;
break;
}
}

void createMenu()
{
int menu=glutCreateMenu(menufunc);
glutAddMenuEntry("Red", 0);
glutAddMenuEntry("Green", 1);
glutAddMenuEntry("Blue", 2);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void init()
{
glClearColor(0,0,0,1);
glMatrixMode(GL_PROJECTION);
glOrtho(-1, 1.0, -1, 1.0, -1.0, 1.0);
numLines=-1;
glMatrixMode(GL_MODELVIEW);
createMenu();
}

void reshape(int width, int height)
{
gHeight=height;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0,height);

glMatrixMode(GL_MODELVIEW);
}


void mouseclick(int button, int state,int x, int y)
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
{
switch(gState)
{

case waitingforclick:
printf("one clidk");
++numLines;
lines[numLines][0].x=x;
lines[numLines][0].y=y;
lines[numLines][1].x=x;
lines[numLines][1].y=y;
gState++;
break;
case clickedonce:
printf("2 clidk");
lines[numLines][1].x=x;
lines[numLines][1].y=y;
gState=waitingforclick;
break;
}
}
glutPostRedisplay();
}

void mousedrag(int x, int y)
{
if(gState==clickedonce)
{lines[numLines][1].x=x;
lines[numLines][1].y=y;
}
glutPostRedisplay();
}

int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("My Drawing App");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouseclick);
glutPassiveMotionFunc(mousedrag);

init();
glutMainLoop();
return 0;
}

Gl_glue
03-09-2001, 09:08 PM
Excellent thanks tons I really appreciate that, thats awfull nice of you. hay can you tell me How does draw lines get activated? I don't see it getting called in Main. Ok I see it gets called from glutDisplayFunc(display); I traced it through. You know what else is cool the back window makes a good debug read out for variables I really like that it gives you that nice little lab console readout type of thing.

int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("My Drawing App");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouseclick);
glutPassiveMotionFunc(mousedrag);

init();
glutMainLoop();
return 0;
}


PS I only need 2 more routines and I am going to try and make my own game. I know how to do shading and some other things all I need now to be off to a great start is
LOAD & SAVE screen and I will be able to get something serious done. if you know of some good material that will lend it self to c++ as I can see c++ was a good lanuage because of the defintion commands there better that ansi C. I see now that your way of coding uses the bare minimum and does not interupt the nice Open_gl flow. Do you think I will find load and save here on the net or will I have to buy the big bad RedBook.

Thank you So Much Again Frogger wicked nice of you. Im happy
Gl_glue Out



[This message has been edited by Gl_glue (edited 03-11-2001).]