// projjy.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "glut.h"
#include <math.h>
#include <string.h>
#define Radius 10
#define inc 0.1
#define pi 3.14
const int Width = 650;
const int Height = 600;
static GLfloat spin = 0.0;
static GLfloat deltaz = .001;
static GLfloat deltax = 0.0;
static GLfloat deltay = 0.0;
void init();
void Display();
void drawPath();
void drawButton();
void drawPerson();
void handleButton(int button, int state, int x, int y);
void translate();
void showsometext();
void spinny();
void *font = GLUT_BITMAP_TIMES_ROMAN_24;
void *fonts[] =
{
GLUT_BITMAP_9_BY_15,
GLUT_BITMAP_TIMES_ROMAN_10,
GLUT_BITMAP_TIMES_ROMAN_24
};
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_MULTISAMPLE | GLUT_DEPTH);
glutInitWindowSize(Width,Height);
glutInitWindowPosition(150,150);
glutCreateWindow("projecty");
glutDisplayFunc(Display);
glutMouseFunc(handleButton);
init();
glutMainLoop();
return 0;
}
void output(int x, int y, char *string)
{
/*an output for the charater strings at the beginning of the program*/
int len, i;
glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(font, string);
}
}
void showsometext()
{
glColor3f(.75,.25,.25);
output(120,50,"A THING THAT MOVES AND STUFF");
glColor3f(.222,.855,.353);
output(20,525,"START");
glColor3f(.5,.7,.9);
output(565,525,"END");
}
void drawPath()
{
glColor3f(0,0,1);
glBegin(GL_LINES);
glVertex2f(25,140);
glVertex2f(25,480);
glEnd();
glBegin(GL_LINES);
glVertex2f(25,140);
glVertex2f(625,140);
glEnd();
glBegin(GL_LINES);
glVertex2f(625,140);
glVertex2f(625,480);
glEnd();
glBegin(GL_LINES);
glVertex2f(100,215);
glVertex2f(100,480);
glEnd();
glBegin(GL_LINES);
glVertex2f(550,215);
glVertex2f(100,215);
glEnd();
glBegin(GL_LINES);
glVertex2f(550,480);
glVertex2f(550,215);
glEnd();
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
showsometext();
drawPath();
drawButton();
drawPerson();
glutSwapBuffers();
}
void drawButton()
{
glBegin(GL_POLYGON);
glVertex2f(280,390);
glVertex2f(280,310);
glVertex2f(370,310);
glVertex2f(370,390);
glEnd();
glColor3f(1,0,0);
output(290,370,"GO");
glFlush();
}
void drawPerson()
{
// glClear(GL_COLOR_BUFFER_BIT);
float theta;
glColor3f(1,1,1);
glPointSize(4.0);
glBegin(GL_POINTS);
glVertex2f(60,415);
glEnd();
glFlush();
glColor3f(.79,.45,.27);
glBegin(GL_POLYGON);
for(theta = 0.0; theta <= 2*pi; theta += inc)
{
glVertex2f(60 + Radius*cos(theta), 420 + Radius*sin(theta));
}
glEnd();
glFlush();
glLineWidth(4);
glBegin(GL_LINES);
glVertex2f(60,430);
glVertex2f(60,450);
glEnd();
glFlush();
glBegin(GL_LINES);
glVertex2f(54,460);
glVertex2f(60,450);
glEnd();
glBegin(GL_LINES);
glVertex2f(66,460);
glVertex2f(60,450);
glEnd();
glBegin(GL_LINES);
glVertex2f(50,435);
glVertex2f(60,440);
glEnd();
glBegin(GL_LINES);
glVertex2f(70,435);
glVertex2f(60,440);
glEnd();
}
void handleButton(int button, int state, int x, int y)
{
// if button is left button
if(button == GLUT_LEFT_BUTTON)
{
if(x >=280 && x <= 370 && y >= 310 && y <=390)
{
if (state == GLUT_DOWN) //state of the button
{
glutIdleFunc(translate);
}
if (state == GLUT_UP)
{
glutIdleFunc(NULL);
}
}
if( x >= 0 && x<= 50 && y >= 0 && y <= 50)
{
if (state == GLUT_DOWN) //state of the button
{
glutIdleFunc(spinny);
}
if (state == GLUT_UP)
{
glutIdleFunc(NULL);
}
}
glutSwapBuffers(); //used for double buffering
}
}
void spinny()
{
spin -= 5;
if(spin < 360){
spin += 360;
}
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin,0,0,1);
drawPerson();
glPopMatrix();
glutSwapBuffers();
}
void translate()
{
static int goingup = 1;
static int goingdown = 0;
static int goingright = 0;
glClear(GL_COLOR_BUFFER_BIT);
if(goingup == 1){
deltay -= 1;
output(170,240,"Going");}
if(deltay == -265){
goingup = 0;
goingright = 1;}
if(goingright == 1){
deltax +=1;
output(220,240, "Still Going...");}
if (deltax == 525) {
goingdown = 1;
goingright = 0;}
if(goingdown == 1){
deltay += 1;
output(280,240,"almost done!");
}
if(deltay == -10){
output(340,450,"DDONEEE");}
//the translations
glPushMatrix();
glTranslatef(550+deltax, 265+deltay, 0.0);
glTranslatef(-550,-265, 0.0);
drawPerson();
glPopMatrix();
drawPath();
drawButton();
showsometext();
glutSwapBuffers();
}
void init()
{
// Clear to black background
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,Width,Height, 0);
return;
}
Need help with rotating an object! Please help...
Hello! I'm having trouble rotating an object. It seems to be rotating the object around the origin. That's obviously not what I want it to do. What I want to do is, wherever the object is on the screen at the time I want to rotate it, it should rotate! .. So maybe rotating around a point that's the center of the object or something would be good! Spinny() is the function I'm focusing on to rotate the object.. and the drawPerson() -- is what I want to rotate!
I hope that makes sense and someone can tell me how to do it! Here's all the code of the program and I really hope someone can respond!...Thanks..
Just before you rotate the object, call glTranslatef() to move the origin to the center of the object (I assume the object has x/y/z values; use those). Then when you rotate, you'll still be rotating around the origin (as always), but now the object is at the center of that origin, so you should get the result you're after.
Just to elaborate somewhat on what Omega147 said.
For rotations about an arbitrary axis through a point (which in your case is within your body), first translate that point to the origin of your coordinate system (and appropriately every other point in the object to its position relative to that point). Second, perform the rotation about the appropriately translated axis which now passes through the origin (and so has a simple representation). Third, invert the translation of the body so that it is returned to its correct position in space, but now at its new orientation.
Cheers,
Timkin
For rotations about an arbitrary axis through a point (which in your case is within your body), first translate that point to the origin of your coordinate system (and appropriately every other point in the object to its position relative to that point). Second, perform the rotation about the appropriately translated axis which now passes through the origin (and so has a simple representation). Third, invert the translation of the body so that it is returned to its correct position in space, but now at its new orientation.
Cheers,
Timkin
hmmm...
Omega ... Well, here's the part of how im getting it to rotate..i added the translate code before rotate .. but it's still rotating around the origin.. :\ ...
And Timkin, I'm not sure how to do what you said..
void spinny() { spin -= 5; if(spin < 360){ spin += 360; } glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glRotatef(spin,0,0,1); glTranslatef(-550,-265, 0.0); drawPerson(); glPopMatrix(); glutSwapBuffers(); }
Omega ... Well, here's the part of how im getting it to rotate..i added the translate code before rotate .. but it's still rotating around the origin.. :\ ...
And Timkin, I'm not sure how to do what you said..
Can someone please help. I don't understand why it's not just rotating the person object .. I don't know what code to add for it to do what I want! ..Thanks
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement