Shoutbox

C++ Help - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: C++ Help (/showthread.php?tid=79448)

C++ Help by Reaper on 11-28-2007 at 05:28 PM

I'm doing a bit of coding with C++ and OpenGL.
I'm using cubic bezier curves. I having a some trouble. Instead of using lines to draw the curves, I want to use cubes. I've managed to changed it but when I draw 1 curve, it shows many cubes spaced out.
How can I get the cubes to stay together?

code:
/* Draw x , y and z axis */


#include <stdlib.h>
#include <glut.h>        //OpenGL library

GLfloat Ax,Bx,Cx,Dx,Ay,By,Cy,Dy,Az,Bz,Cz,Dz,x1,k,l,x2,y2,z2,t,p,constx1,consty1,constz1;
int i;

/* define a draw routine to draw the x,y and z axis */
void drawaxis(void) /*x,y,z*/
{
  // x coordinates to draw the x axis
  GLdouble x1[3] = {-100.0, 0.0, 0.0}; // define coordinates as an array
  GLdouble x2[3] = {50.0, 0.0, 0.0};  //GLdouble opengl varaible for storing doubles

  // y coordinates to draw the y axis
  GLdouble y1[3] = {0.0, -100.0, 0.0};
  GLdouble y2[3] = {0.0, 50.0, 0.0};

  // z coordinates to draw the z axis
  GLdouble z1[3] = {0.0, 0.0, -100.0};
  GLdouble z2[3] = {0.0, 0.0, 50.0};


  glBegin(GL_LINES);
  glColor3f(1.0, 0.0, 0.0);  //give different color to the three axis, red for x axis
  glVertex3dv(x1);
  glVertex3dv(x2);
  glColor3f(0.0, 1.0, 0.0); //give green color to y axis
  glVertex3dv(y1);
  glVertex3dv(y2);
  glColor3f(1.0, 1.0, 1.0); //give white color to z axis
  glVertex3dv(z1);
  glVertex3dv(z2);
  glEnd();

}
// define the Bezier routine here
void DrawBezier(float firstx1,float firsty1,float firstz1,float lastx2,float lasty2,float lastz2,float conx1, float cony1,float conz1,float conx2,float cony2,float conz2)
{
Ax =lastx2 -firstx1-3*conx2+3*conx1;
Bx = 3*firstx1 -6*conx1 +3*conx2;
Cx=-3*firstx1+3*conx1;
Dx=firstx1;
Ay =lasty2 -firsty1-3*cony2+3*cony1;
By = 3*firsty1 -6*cony1 +3*cony2;
Cy=-3*firsty1+3*cony1;
Dy=firsty1;
Az =lastz2 -firstz1-3*conz2+3*conz1;
Bz = 3*firstz1 -6*conz1 +3*conz2;
Cz=-3*firstz1+3*conz1;
Dz=firstz1;
// now draw the curve
for (t=0.0;t<=1.0 ; t=t+0.05)
{
    x1=Ax*t*t*t +Bx*t*t + Cx*t + Dx;
    k =Ay*t*t*t +By*t*t + Cy*t + Dy;
    l =Az*t*t*t +Bz*t*t + Cz*t + Dz;
    p=t+0.05;
    x2=Ax*p*p*p +Bx*p*p + Cx*p + Dx;
    y2=Ay*p*p*p +By*p*p + Cy*p + Dy;
    z2=Az*p*p*p +Bz*p*p + Cz*p + Dz;
    glLineWidth(5);
    glTranslatef(x1,k,l);
    glutSolidCube(1);
}

}








/* reshape callback function  executed when window is moved or resized */
void reshape(int width, int height)
{
   glViewport(0, 0, width, height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(50.0,1.0,15.0,350.0);  //Perspective
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}


/* display routine this where the drawing takes place  */
void display(void)
{
   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   /* clear window */
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

  gluLookAt(50.0, 20.0, 50.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);  //position of the eye
  drawaxis();  // draw the coordinate axis
  //draw Bezier curve here notice use of z coordinates in the control points

  //DrawBezier(-5,10,10,5,10,10,1.5,10,10,3.5,10,10); //first arc
  //DrawBezier(-10,13,10,0,10,10,-2.5,10.5,10,-7.5,12.5,10); //second arc

  //DrawBezier(0,0,0,1,0,0,0.25,0,0,0.75,0,0);
  DrawBezier(5,0,0,10,2,0,6.25,0.5,0,8.25,1.5,0);
  //DrawBezier(10,2,0,15,4,0,11.25,2.5,0,13.75,3.5,0);
  //DrawBezier(0,0,0,0,0,0,0,0,0,0,0,0);

  glutSwapBuffers();
glFlush();
}

/* graphics initialisation */
void init(void)
{
   glClearColor (0.0, 0.0, 0.0, 0.0);   /* window will be cleared to black */
      glEnable(GL_DEPTH_TEST);
   glEnable(GL_CULL_FACE);  // Enable back  culling
   glCullFace(GL_BACK);     // Cull back faces

}


int main(int argc, char** argv)
{
   /* window management code ... */
   /* initialises GLUT and processes any command line arguments */ 
   glutInit(&argc, argv);
   /* use single-buffered window and RGBA colour model */
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
   /* enable the depth test */
   glEnable(GL_DEPTH_TEST);
   /* window width = 600 pixels, height = 600 pixels */
   glutInitWindowSize (600, 600);
   /* window upper left corner at (0, 0) */
   glutInitWindowPosition (0, 0);
   /* creates an OpenGL window with command argument in its title bar */
   glutCreateWindow ("Opening Window"); 
   init();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutMainLoop();
   return 0;
}


(You need OpenGL installed to be able to run this)
RE: C++ Help by RaceProUK on 11-29-2007 at 09:39 PM

Shot in the dark:
You'll need more iterations when drawing the curve, so it'll draw them closer together (and more of them too).


RE: C++ Help by Reaper on 12-02-2007 at 02:34 PM

I actually fixed it myself. I had to put a glPushMatrix(); and a glPopMatrix(); where I set the dinosaur to be cubes