PDA

View Full Version : Making actions on Lines



nick2price
10-13-2008, 08:11 AM
I have decided to start of my letter creating using basic lines, and just having them like 'stick' letters. I have managed to write my name like this, but for this demonstration i am just showing you the letter N.
The next step i was trying to do was put an action on the letter N. I was trying to get my left mouse button on click to rotate the letter. What hapens though is my program compiles, but just shows up with a blue screen and no letter on it. Can someone advise me on my mistake please.
thanks


#include "stdafx.h"

#include "glut.h"
#include <stdlib.h>

static GLfloat spin = 0.0;


void init(void)
{
glClearColor (0.0, 0.0, 0.85, 0.0);
glShadeModel (GL_FLAT);


}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


void display(void)
{

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective


glColor3f (0.0, 0.0, 0.0);
glLineWidth(3.0);

glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0);

glBegin(GL_LINES); //LETTER N

glVertex3f(50.0, 50.0, 0.0);
glVertex3f(50.0, 250.0, 0.0);

glVertex3f(50.0, 250.0, 0.0);
glVertex3f(80.0, 50.0, 0.0);

glVertex3f(80.0, 50.0, 0.0);
glVertex3f(80.0, 250.0, 0.0);

glEnd();
glPopMatrix();

glutSwapBuffers();
}

void spinDisplay(void)
{
spin = spin + 2.0;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}

void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
case GLUT_MIDDLE_BUTTON:
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}


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
}
}

int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (700, 400);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutKeyboardFunc(handleKeypress);

glutMainLoop();
return 0;
}

trinitrotoluene
10-13-2008, 12:08 PM
You call glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); which seem
to be 1) too small to view your letter entirely , 2) the min coords of your letter are 50 in each x and y coord and your viewport mapping range are [-50,50] for both x and y, so you are viewing below the letter. try glOrtho(-500.0, 500.0, -500.0, 500.0, -1.0, 1.0);

nick2price
10-13-2008, 01:06 PM
Kool, thats working great now. Just a couple of quick things if you dont mind.
Firstly,in my mouse action, i have

case GLUT_MIDDLE_BUTTON:
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);

Instead of the spin stopping dead on the click of the right button, can i get it to go back to the original state instead?

My second question is whether it would be hard to produce a set of fireworks where when they explode, it will turn into my name.
cheers

trinitrotoluene
10-13-2008, 01:24 PM
1) This is very easy.
2) Seem a little harder but I will try it.


void stopDisplay(void)
{
float EPSILON = 0.001;
if(fabs(spin) > 2.0 + EPSILON)
{
spinDisplay();
glutPostRedisplay();
}
else
{
glutIdleFunc(NULL);
}
}
void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
case GLUT_MIDDLE_BUTTON:
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(stopDisplay);
break;
default:
break;
}
}