PDA

View Full Version : How can I add in my XY axis?



calvarado777
11-26-2012, 03:10 PM
I have been at this project for weeks....and it finally works!! yay!!! But now I have a new problem..I want to be able to just show the X and Y axis throughout my entire animation. I have tried plugging in this code snippet but I can't figure out where the correct placement would be. I have tried pushing and popping, and putting it in different locations within my display function, but I have yet to see any lines drawn...any suggestions?

Code Snippet:

glColor3f(1.0, 1.0, 1.0);

glBegin(GL_LINE);
glVertex2f(400, 0);
glVertex2f(400, 800);
glEnd();

glBegin(GL_LINE);
glVertex2f(0, 400);
glVertex2f(800, 400);
glEnd();

And here is my amazing program in all of it's glory! It' s a very simple animation showing how rotation is done in openGL.


#include <stdlib.h>
#include <stdio.h>
#include <GLUT/glut.h>




float yPosition = 0;
float zPosition = 0;
float topLeftX = 0.5;
float topLeftY = 0.75;
float topRightX = 0.75;
float topRightY = 0.75;
float bottomRightX = 0.75;
float bottomRightY = 0.5;
float bottomLeftX = 0.5;
float bottomLeftY = 0.5;
float xPos = 0.75;
float yPos = 0.75;
float zPos = 0;
float startXPos = 0.75;
float startYPos = 0.75;
int yDirection = 0; //1-up, 0-down
int xDirection = 0; //1-stopped, 0-left, 2- right
int yRotate = 0; //0- rotate, 1- stop rotating
int zRotate = 0; //0- rotate, 1- stop rotating
int count;

//the init function is used to initialize things that only need to be execute once
void init()
{
glClearColor(1.0,1.0,1.0,0.0);
glEnable(GL_DEPTH_TEST);
}

void setWindow(double left, double right, double bottom, double top)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}

void setViewport(int left, int right, int bottom, int top)
{
glViewport(left, bottom, right-left, top-bottom);
}

//function for drawing, called whenever there needs to be a redraw
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glColor3f(1,0,0);

glPushMatrix();

glTranslated(xPos, yPos, 0);

glRotatef(yPosition, 0, 1, 0);
glRotatef(zPosition, 0, 0, 1);

if (xDirection==0){ //if moving left
xPos-=0.0005; //move in a negative direction along the xaxis
yPos = (startYPos*xPos/startXPos);

glutPostRedisplay();
}

if (xDirection==1) {
xPos==0.0;
yPos==0.0;

yPosition+=0.05;
zPosition+=0.05;

glutPostRedisplay();
}

if (xDirection==2){
xPos+=0.0005; //move in a negative direction along the xaxis
yPos = (startYPos*xPos/startXPos);
glutPostRedisplay();
}

if (xDirection==3) {
xPos==0.0;
yPos==0.0;
glutPostRedisplay();
}


if (xDirection==0 && xPos > 0.0) {
xDirection=0;
}
if (xDirection==0 && xPos < 0.0) {
xDirection=1; //stop at origin
}
if (xDirection==1 && yPosition > 45.0) {
xDirection=2;
}
if (xDirection==2 && xPos==startXPos) {
xDirection=3;
}

//glutWireCube(.25);

glBegin(GL_POLYGON);
glVertex2f(-0.1, -0.1); //bottom left
glVertex2f(-0.1,0.1); //top left
glVertex2f(0.1, 0.1); //top right
glVertex2f(0.1, -0.1); //bottom right
glEnd();

glPopMatrix();

glutSwapBuffers();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/ (GLfloat) h, 1.0, 20.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {

case 27:
exit(0);
break;
case 'y':
yPosition=yPosition+5;
if (yPosition>360)
yPosition=0;
glutPostRedisplay();
break;
case 'z':
zPosition=zPosition+5;
if (zPosition>360)
zPosition=0;
glutPostRedisplay();
break;
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); //double pixel buffer that holds RGB
glutInitWindowSize(800, 800);
glutInitWindowPosition(0,0);

glutCreateWindow(argv[0]);

init();

glutDisplayFunc(display);
glutReshapeFunc(reshape);
//glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

tonyo_au
11-26-2012, 04:47 PM
I would expect to do it after the popMatrix in the display function. But you vertex values look wrong if you are using that glOrtho function since it says
your x values will range from -1 -> 1 and y from -1 -> 1 but your axis draw code is in screen coordinates.

calvarado777
11-27-2012, 10:29 AM
Thank you for your reply, I actually never call the setWindow function where the glOrtho function is so I thought that using the screen coordinates would work?

Found the problem! I needed insert the code snippet where tonyo_au suggested, but I needed a push and a pop surrounding the snippet.

Thank you!!!