You are here:

C++/C++/OpenGL Help

Advertisement


Question
Hi Ralph,
Im a begginer in c++ and opengl and am using the languages to work on a 3d animated tutorial. I have run into several problems in understanding how to carry out basic functions.

Firstly i have a flat polygon on the screen to which i want to map a bitmap from paint on to but am unable to do so?!

Secondly how do i run another exe file from my c++ file??

All my code so far is listed below:
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>


GLfloat angle = 0.0;       // Current turn angle.
GLfloat angle_step = 0.1;  // How much the angle changes between frames.
GLfloat angle_step2 = 0.1;
GLfloat change = 0.1;      // How much angle_step changes when arrows pressed.
GLuint texture[1];

GLfloat vertices[][3] =
  {  {1.0, 1.0, 0.0},
     {1.5, 1.0, 0.0},
     {1.5, -1.0, 0.0},
     {1.0, -1.0, 0.0},
     {1.5, 0.5, 0.0},
     {-1.5, 0.5, 0.0},
     {-1.5, 1.0, 0.0},
     {-1.0, 1.0, 0.0},
     {-1.0, -1.0, 0.0},
     {-1.5, -1.0, 0.0},
     {-1.5, -0.5, 0.0},
     {1.5, -0.5, 0.0},
     {-1.5, 1.0, -0.25},
     {1.5, 1.0, -0.25},
     {-0.75, 0.25, -2.0},
     {0.75, 0.25, -2.0},
     {-0.75, -1.0, -2.0},
     {0.75, -1.0, -2.0},
     {1.5, 1.0, -0.25},
     {1.5, -1.0, -0.25},
     {-1.5, -1.0, -0.25},
     {-1.5, 1.0, -0.25},
     {-1.0, 0.5, -0.2},
     {-1.0, -0.5, -0.2},
     {1.0, -0.5, -0.2},
     {1.0, 0.5, -0.2},
     {-1.25, -1.0, 0.5},
     {1.25, -1.0, 0.5},
     {1.25, -1.0, 1.5},
     {-1.25, -1.0, 1.5}

     };
     
GLfloat colors[][3] =
  {  {1.0, 1.0, 1.0},
     {0.0, 0.0, 0.0},
  };

/*Stuff i got from a tutorial but cant get to work*/
/*AUX_RGBImageRec *LoadBMP(char *Filename)          // Loads A Bitmap Image
{
  FILE *File=NULL;          // File Handle

  if (!Filename)          // Make Sure A Filename Was Given
  {
     return NULL;          // If Not Return NULL
  }

  File=fopen(Filename,"r");          // Check To See If The File Exists

  if (File)          // Does The File Exist?
  {
     fclose(File);          // Close The Handle
     return auxDIBImageLoad(Filename);          // Load The Bitmap And Return A Pointer
  }

  return NULL;          // If Load Failed Return NULL
}

int LoadGLTextures()          // Load Bitmaps And Convert To Textures
{
  int Status=FALSE;          // Status Indicator

  AUX_RGBImageRec *TextureImage[1];          // Create Storage Space For The Texture

  memset(TextureImage,0,sizeof(void *)*1);          // Set The Pointer To NULL

  // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
  if (TextureImage[0]=LoadBMP("Data/ali.bmp"))
  {
     Status=TRUE;          // Set The Status To TRUE

     glGenTextures(1, &texture[0]);          // Create The Texture

     // Typical Texture Generation Using Data From The Bitmap
     glBindTexture(GL_TEXTURE_2D, texture[0]);
     glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  }

  if (TextureImage[0])          // If Texture Exists
  {
     if (TextureImage[0]->data)          // If Texture Image Exists
     {
///////////////////////////////////////////////////////////   ////////////////////////////////   
     free(TextureImage[0]->data);          // Free The Texture Image Memory
     }

     free(TextureImage[0]);          // Free The Image Structure
  }

  return Status;          // Return The Status
} */


void init(void)
{
  glClearColor(0.0, 0.0, 0.5, 1.0);
  glEnable(GL_DEPTH_TEST);
  /*if (!LoadGLTextures())          // Jump To Texture Loading Routine ( NEW )
  {
     return FALSE;          // If Texture Didn't Load Return FALSE
  }

  glEnable(GL_TEXTURE_2D);*/
}

void face4(int v0, int v1, int v2, int v3, int c)
  /* Render face with 4 vertices v1,v2,v3,v4 and colour c. */
{
  glColor3fv(colors[c]);
  glBegin(GL_POLYGON);
     glVertex3fv(vertices[v0]);
     glVertex3fv(vertices[v1]);
     glVertex3fv(vertices[v2]);
     glVertex3fv(vertices[v3]);
  glEnd();
}

void cable(int x1, int y1, int z1, int x2, int y2, int z2)
{
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f((x1),(y1),(z1));
glVertex3f((x2),(y2),(z2));
glEnd();
}



void comp(int v0, int v1, int v2)
/*render a computer at location v0,v1,v2 */
{
  glTranslatef(v0, v1, v2);

  face4(0,1,2,3,0);
  face4(1,4,5,6,0);
  face4(6,7,8,9,0);
  face4(9,10,11,2,0);
  face4(6,12,13,1,0);
  face4(13,12,14,15,0);
  face4(15,14,16,17,0);
  face4(1,18,19,2,0);
  face4(6,9,20,21,0);
  face4(21,14,16,20,0);
  face4(18,15,17,19,0);
  face4(20,19,17,16,0);
  face4(22,23,24,25,1);
  face4(26,27,28,29,0);

   }


void network(int x, int y, int z)
{
    glTranslatef(x, y , z);

    glPushMatrix();
  glScalef(0.25, 0.25, 0.25);

  glPushMatrix();
  glTranslatef(-7.0, 0.0, -7.0);
  glRotatef(45.0, 0.0, 1.0, 0.0);
  comp(0,0,0);
  glPopMatrix();

  glPushMatrix();
  glTranslatef(-7.0, 0.0, 7.0);
  glRotatef(135.0, 0.0, 1.0, 0.0);
  comp(0,0,0);
  glPopMatrix();

 glPushMatrix();
  glTranslatef(7.0, 0.0, 7.0);
  glRotatef(-135.0, 0.0, 1.0, 0.0);
  comp(0,0,0);
  glPopMatrix();

  glPushMatrix();
  glTranslatef(7.0, 0.0, -7.0);
  glRotatef(-45.0, 0.0, 1.0, 0.0);
  comp(0,0,0);
  glPopMatrix();

  glPushMatrix();
  glLineWidth(5.0);
  cable(8,-0.1,-8,8,-0.1,8);
  cable(-8,-0.1,-8,-8,-0.1,8);
  cable(8,-0.1,-8,-8,-0.1,-8);
  cable(8,-0.1,8,-8,-0.1,8);
  glPopMatrix();

  glPopMatrix();

}

void lan(void)
{  glPushMatrix();

  glTranslatef(-10.0, 0.0, 0.0);
  glRotatef(angle, 0.0, 1.0, 0.0);
  glScalef(1.5, 1.5, 1.5);
  network(0,0,0);
  glPopMatrix();
}

void wan(void)
{
  glTranslatef(-12.5, 0.0, 0.0);
  glPushMatrix();
  glScalef(0.5, 0.5, 0.5);

  glPushMatrix();
  network(10,0,-5);
  glPopMatrix();

  glPushMatrix();
  network(10,0,-10);
  glPopMatrix();

  glPushMatrix();
  network(15,0,-5);
  glPopMatrix();

  glPushMatrix();
  network(15,0,-10);
  glPopMatrix();

  glPushMatrix();
  network(15,0,10);
  glPopMatrix();

  glPushMatrix();
  network(15,0,15);
  glPopMatrix();

  glPushMatrix();
  network(10,0,10);
  glPopMatrix();

  glPushMatrix();
  network(10,0,15);
  glPopMatrix();

  glPushMatrix();
  network(30,0,10);
  glPopMatrix();

  glPushMatrix();
  network(30,0,15);
  glPopMatrix();

  glPushMatrix();
  network(35,0,10);
  glPopMatrix();

  glPushMatrix();
  network(35,0,15);
  glPopMatrix();

   glPushMatrix();
  network(30,0,-5);
  glPopMatrix();

  glPushMatrix();
  network(30,0,-10);
  glPopMatrix();

  glPushMatrix();
  network(35,0,-5);
  glPopMatrix();

  glPushMatrix();
  network(35,0,-10);
  glPopMatrix();

  glPushMatrix();
  glLineWidth(5.0);
  cable(10,-0.1,-3,10,-0.1,8);
  cable(17,-0.1,-10,28,-0.1,-10);
  cable(17,-0.1,15,28,-0.1,15);
  glPopMatrix();

  glPopMatrix();
}


void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(100.0, 1.4, 5.0, 50.0);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  glPushMatrix();
  glTranslatef(0.0, 0.0, -5.0);
//Need to combine bmp texture to this polygon!
  /*glBindTexture(GL_TEXTURE_2D, texture[0]);*/
  glBegin(GL_POLYGON);
     glVertex2f(8.0, -3.0);
     glVertex2f(-8.0, -3.0);
     glVertex2f(-8.0, -6.0);
     glVertex2f(8.0, -6.0);
  glEnd();
  glPopMatrix();

  gluLookAt(0.0, 5.0, 15.0, 3.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  /*glRotatef(angle, 0.0, 1.0, 0.0);*/

  glPushMatrix();
  lan();
  glPopMatrix();

  glPushMatrix();
  glTranslatef(12.5, 0.0, 0.0);
  glRotatef(angle, 0.0, 1.0, 0.0);
  wan();
  glPopMatrix();


  glutSwapBuffers();



}

void specialKeys(int key, int x, int y)
{
  if (key == GLUT_KEY_LEFT)
     angle_step = angle_step + change;
  else if (key == GLUT_KEY_RIGHT)
     angle_step = angle_step - change;
}

void keyboard(unsigned char code, int x, int y)
{
  if (code == ' ')
     angle_step = 0.0;
  else if (code == 27)
     exit(0);
}

void update(void)
{
  angle = angle + angle_step2;
  if (angle >= 360.0)
     angle = angle - 360.0;
  else if (angle < 0.0)
     angle = angle + 360.0;
  glutPostRedisplay();
}

void reshape(int w, int h)
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(40.0, (GLdouble)w/(GLdouble)h, 0.5, 20.0);
  glViewport(0, 0, (GLsizei)w, (GLsizei)h);
}

int main(int argc, char** argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
  glutInitWindowSize(1000,700);
  glutInitWindowPosition(12.5,10);
  glutCreateWindow("Square");
  init();
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutIdleFunc(update);
  glutSpecialFunc(specialKeys);
  glutKeyboardFunc(keyboard);
  glutMainLoop();
  return 0;
}
Any help you can offer would be much appreciated!
Thank you
ALI  

Answer
My first thought is that you should be including the correct headers - which for MS Windows OpenGL applications appear to be as follows (from MSDN Library OpenGL documentation:

<START QUOTED MATERIAL>
"Applications that use:

- The core OpenGL functions must include the header file <GL\gl.h>.

- The OpenGL Utility library must include the header file <GL\glu.h>.

- The OpenGL Programming Guide auxiliary library must include the header file <GL\glaux.h>.

- The WGL functions must include the header file windows.h.

The new Win32 functions that support Microsoft's implementation of OpenGL in Windows NT/2000 and Windows 95/98 must include the header file windows.h."
<END QUOTED MATERIAL>

The only gl header files I have installed on my MS Visual C++ .NET 2003 installation are those listed above.

Next ensure you are linking using all the required libraries.

Note that I could not locate any information in my local MSDN library on the glaux functions such as auxDIBImageLoad - but it does appear to be defined for both ANSI and UNICODE character type builds in the gl/glaux.h header.

Finally, OpenGL started life on UNIX systems - which have different rules for working with their GUI than does MS Windows. Hence, if you are learning OpenGL with an eye to running under MS Windows (as indicated by your reference to Paint, attempt to load DIB image files using auxDIBImageLoad and your query about running exe files) I suggest you to locate some examples and tutorials specifically for this platform. Try here for starters: http://www.codesampler.com/oglsrc.htm.

As to running one executable from another - the only standard way in C and C++ is the library function system:

       int system( const char *command );

If you wish to use anything more complex then you can look at the MS VC++ specific runtime library spawn family of functions or the Win32 functions such as ShellExecute and CreateProcess (and their variants). Documentation on these can be found in the MSDN library - see http://www.msdn.microsoft.com/ if you do not have a local copy on CD or DVD.

Finally I would learn to walk before trying to run - 3D applications are complex, Windows programming is complex and C++ is complex. You might like to get a grounding in one complex thing at a time before moving onto another. I would start with C++ (if this is your language of choice) then move onto MS Windows programming basics then attempt the 3D stuff. I do not mean you should be an expert in one before moving onto another but you should have then basics of one thing sorted out before moving onto another - specifically you should know where you can get good reference material for each subject and recognise when the information applies to your case - for example where OpenGL applications under Windows differ from UN*X implementations or where C++ ends and other libraries or systems begin, or where standard C++ says one thing but your compiler requires another...  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.