Fedora Linux Support Community & Resources Center
  #1  
Old 13th June 2008, 03:48 AM
Ichimaru_Gin Offline
Registered User
 
Join Date: Apr 2008
Posts: 9
How to compile OpenGL programs

Visual Studio has spoiled me . I need some assistance on how to compile programs using the OpenGL API. I already know that I have hardware accelarated support for OpenGL. Does anyone know the command line functions to use so that I can compile programs? I will start off using the glut libraries until I learn how to create windows using the XWindows system.

Thanks

Specs

Laptop
Fedora Core 8
Nvidia 6400 128 MB ram

Desktop
Fedora Core 9
Nvidia 8800 GT 512 MB
Reply With Quote
  #2  
Old 13th June 2008, 09:27 PM
Negative_3 Offline
Registered User
 
Join Date: Jun 2008
Posts: 2
I too would like to know.
Reply With Quote
  #3  
Old 14th June 2008, 07:37 PM
dr death Offline
Registered User
 
Join Date: Dec 2006
Posts: 82
Example (save as test.c)
Code:
#include <GL/freeglut.h>
#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#define LIST_EXAMPLE 1

void reshape(int w, int h);
void processNormalKeys(unsigned char key, int x, int y);
void display(void);

int WIDTH = 800;
int HEIGHT = 500;
int window;

int main(int argc, char *argv[])
{
  char *text="Hello World";

  /* Initialise glut */
  glutInit(&argc, argv);
  /* Open the window in the top-left of the screen */
  glutInitWindowPosition(0, 0);
  /* The size of the window */
  glutInitWindowSize(WIDTH, HEIGHT);
  /* Set the display mode */
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  window = glutCreateWindow("My Window");

  /* The function to call when the windowing system says the screen
   * needs to be redrawn */
  glutDisplayFunc(display);

  /* The function to call when the window is resized */
  glutReshapeFunc(reshape);

  /* Process keyboard events */
  glutKeyboardFunc(processNormalKeys); 
  glutSpecialFunc(NULL); 

  /* Process mouse events */
  glutMouseFunc(NULL);

  /* Non-passive (only when a button is down) routine */
  glutPassiveMotionFunc(NULL);
  glutMotionFunc(NULL);

  /* Idle function */
  glutIdleFunc(display);

  /* Create something to draw on the screen */
  glNewList(LIST_EXAMPLE, GL_COMPILE);
  glPushMatrix();
  /* Set up normalised 2D coordinates*/
  glLoadIdentity();
  gluOrtho2D(0.0, 1.0, 0.0, 1.0);
  /* Text at the center of the screen */
  glRasterPos3d(0.5 - glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (unsigned char*)text)/(2.0*WIDTH),
		0.5 -  glutBitmapHeight(GLUT_BITMAP_HELVETICA_12)/ (2.0*HEIGHT),
		0.0);
  glutBitmapString(GLUT_BITMAP_HELVETICA_12, (unsigned char*)text);
  glPopMatrix();
  glEndList();

  glutMainLoop();

  return 0;
}

void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  /* Run the list */
  glCallList(LIST_EXAMPLE);

  glutSwapBuffers();
}

void reshape(int w, int h)
{
  /* Set the viewport to the new width and height */
  glViewport(0, 0, w, h);
  /* update variables */
  WIDTH = w;
  HEIGHT = h;
}

void processNormalKeys(unsigned char key,
		       __attribute__((unused)) int x,
		       __attribute__((unused)) int y)
{
  char *text="Goodbye World";

  switch (key)
    {
    case 0x1B: /* Esc */
      glutDestroyWindow(window);
      break;
    case 'a' :
    case 'A' :
      glDeleteLists(LIST_EXAMPLE, 1);
      glNewList(LIST_EXAMPLE, GL_COMPILE);
      glPushMatrix();
      /* Set up normalised 2D coordinates*/
      glLoadIdentity();
      gluOrtho2D(0.0, 1.0, 0.0, 1.0);
      /* Text at the center of the screen */
      glRasterPos3d(0.5 - glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (unsigned char*)text)/(2.0*WIDTH),
		    0.5 -  glutBitmapHeight(GLUT_BITMAP_HELVETICA_12)/ (2.0*HEIGHT),
		    0.0);
      glutBitmapString(GLUT_BITMAP_HELVETICA_12, (unsigned char*)text);
      glPopMatrix();
      glEndList();
      break;
    }
}
Compile with
Code:
gcc -ansi -Wall -O3 -o test test.c -lglut
And run
Code:
./test

Last edited by dr death; 14th June 2008 at 07:39 PM.
Reply With Quote
  #4  
Old 18th June 2008, 06:05 PM
Ichimaru_Gin Offline
Registered User
 
Join Date: Apr 2008
Posts: 9
Thanks, but I will be using the g++ compiler - I'd be using the same parameters correct?
Reply With Quote
  #5  
Old 18th June 2008, 06:58 PM
dr death Offline
Registered User
 
Join Date: Dec 2006
Posts: 82
Should be the same. I see that the freeglut headers have a
Code:
#ifdef __cplusplus
    extern "C" {
#endif
so they should handle name mangling just fine.

Libraries are in /usr/lib, include files in /usr/include, so there should be no problems there. Just make sure that you have the freeglut-devel and mesa-libGL-devel packages installed. If you want to link against 3rd party libraries, then specify their location with the -L parameter (e.g. -L /usr/lib/nvidia/ -lGL)
Reply With Quote
  #6  
Old 19th June 2008, 11:25 AM
dejai Offline
Registered User
 
Join Date: Apr 2008
Posts: 41
I wrote my first open gl application with emacs :P I love that program.
Reply With Quote
  #7  
Old 20th June 2008, 06:58 AM
Ichimaru_Gin Offline
Registered User
 
Join Date: Apr 2008
Posts: 9
Thanks, I'm going to give that a try. I'll let you know how it works out.
Reply With Quote
  #8  
Old 7th March 2009, 03:22 PM
nilabhnikunj Offline
Registered User
 
Join Date: Oct 2008
Posts: 7
hi, can any one help me which headers to use for compiling open gl codes ... am new to open gl and trying to learn.
I wrote a simple code


#include "/usr/include/GL/gl.h"
void display()
{
int p[2][2]={{0,0},{0,1}};
glBegin(GL_POINTS);
glVertex2iv(p);
glEnd();
glFlush();
}

void myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,10.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho2D(0.0,50.0,0.0,50.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
gutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Something New");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0;
}

on compilation it gave following errors

gcc point.c
point.c: In function ‘display’:
point.c:8: warning: passing argument 1 of ‘glVertex2iv’ from incompatible pointer type
point.c: In function ‘main’:
point.c:25: error: ‘GLUT_SINGLE’ undeclared (first use in this function)
point.c:25: error: (Each undeclared identifier is reported only once
point.c:25: error: for each function it appears in.)
point.c:25: error: ‘GLUT_RGB’ undeclared (first use in this function)

Last edited by nilabhnikunj; 7th March 2009 at 03:28 PM.
Reply With Quote
  #9  
Old 7th March 2009, 05:16 PM
Reisswolf Offline
Registered User
 
Join Date: May 2006
Posts: 337
Quick question: Is O3 stable yet? In the past I found that programmes compiled with such aggressive optimisation sometimes behaved weirdly.

As a general policy I stick to O2.
__________________
Registered user # 441814
Reply With Quote
  #10  
Old 7th March 2009, 07:37 PM
dr death Offline
Registered User
 
Join Date: Dec 2006
Posts: 82
You need to install and link to GLUT. OpenGL is just the gl... statments. It doesn't include any windowing stuff.
To install:
Code:
yum install freeglut-devel
Use the following include in your source
Code:
#include <GL/freeglut.h>
and use "-lglut" when linking.
Reply With Quote
  #11  
Old 8th March 2009, 04:20 PM
nilabhnikunj Offline
Registered User
 
Join Date: Oct 2008
Posts: 7
Thanks for replying sir , but after installing the windowing stuff it gave another set of errors and warnings
Code:
gcc -ansi -Wall -O3 -o point point.c -lglut
point.c: In function ‘display’:
point.c:8: warning: passing argument 1 of ‘glVertex2iv’ from incompatible pointer type
point.c: In function ‘myinit’:
point.c:19: warning: implicit declaration of function ‘glOrtho2D’
point.c: In function ‘main’:
point.c:25: warning: implicit declaration of function ‘gutInitDisplayMode’
/tmp/cc6HrGFb.o: In function `myinit':
point.c:(.text+0x63): undefined reference to `glOrtho2D'
/tmp/cc6HrGFb.o: In function `main':
point.c:(.text+0xfe): undefined reference to `gutInitDisplayMode'
point.c:(.text+0x199): undefined reference to `glOrtho2D'
collect2: ld returned 1 exit status
[regular@localhost ~]$ 

kindly guide once again
thank you ...........

Last edited by nilabhnikunj; 8th March 2009 at 04:23 PM.
Reply With Quote
  #12  
Old 8th March 2009, 06:17 PM
pankajp Offline
Registered User
 
Join Date: Jul 2007
Location: Mumbai, India
Posts: 226
I'm not actually an opengl programmer but based on a few sample files which i have i could modify your sample to make it run. There was a few typo in your file and also the usage of glVertex2iv was different and also glOrtho2D did not exist in the gl libraries so i replaced it with an equivalent form of glOrtho. Also i added the reshape function to display properly and glClear(GL_COLOR_BUFFER_BIT);
The modified code is as follows
Code:
#include "GL/glut.h"
void reshape(int w, int h)
{
  glViewport(0, 0, w, h);       /* Establish viewing area to cover entire window. */
  glMatrixMode(GL_PROJECTION);  /* Start modifying the projection matrix. */
  glLoadIdentity();             /* Reset project matrix. */
  glOrtho(0, w, 0, h, -1, 1);   /* Map abstract coords directly to window coords. */
  glScalef(1, -1, 1);           /* Invert Y axis so increasing Y goes down. */
  glTranslatef(0, -h, 0);       /* Shift origin up to upper-left corner. */
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    int p[2][2]={{10,100},{10,11}};
    glBegin(GL_POINTS);
    glColor3f(0.0, 0.0, 1.0);  /* green */
    glVertex2iv(p[0]);
    glVertex2iv(p[1]);
    glEnd();
    glFlush();
}

void myinit()
{
    glClearColor(1.0,1.0,1.0,1.0);
    glColor3f(0.0,1.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //glOrtho2D(0.0,50.0,0.0,50.0);
    glOrtho(0.0,50.0,0.0,50.0, 0.0,0.0);
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Something New");
    glutDisplayFunc(display);
    myinit();
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}
Also if you want to know the sample from where i got the idea it is
Code:
#include <GL/glut.h>

void reshape(int w, int h)
{
  /* This short bit of code sets up the coordinate
     system to correspond to actual window coodrinates.  This code
     wouldn't be required if you chose a (more typical in 3D) abstract
     coordinate system. */

  glViewport(0, 0, w, h);       /* Establish viewing area to cover entire window. */
  glMatrixMode(GL_PROJECTION);  /* Start modifying the projection matrix. */
  glLoadIdentity();             /* Reset project matrix. */
  glOrtho(0, w, 0, h, -1, 1);   /* Map abstract coords directly to window coords. */
  glScalef(1, -1, 1);           /* Invert Y axis so increasing Y goes down. */
  glTranslatef(0, -h, 0);       /* Shift origin up to upper-left corner. */
}

void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glBegin(GL_TRIANGLES);
    glColor3f(0.0, 0.0, 1.0);  /* blue */
    glVertex2i(0, 0);
    glColor3f(0.0, 1.0, 0.0);  /* green */
    glVertex2i(200, 200);
    glColor3f(1.0, 0.0, 0.0);  /* red */
    glVertex2i(20, 200);
  glEnd();
  glFlush();  /* Single buffered, so needs a flush. */
}

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutCreateWindow("single triangle");
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}
Hope you can find the relevant things by a bit searching around (man pages help too)
__________________
Fedora 14 Laughlin user on Dell Inspiron 1525
http://www.smolts.org/client/show/pu...0-71f71be44f7d :)
Reply With Quote
  #13  
Old 8th March 2009, 08:50 PM
dr death Offline
Registered User
 
Join Date: Dec 2006
Posts: 82
You have some typos there.

1) You want gluOrtho2D, not glOrtho2D
2) glutInitDisplayMode, not gutInitDisplayMode

Note that glOrtho multiples the current matrix whereas gluOrtho2D sets up a new projection matrix

Last edited by dr death; 8th March 2009 at 08:56 PM. Reason: Didn't see pankajp's reply
Reply With Quote
  #14  
Old 9th March 2009, 03:30 PM
nilabhnikunj Offline
Registered User
 
Join Date: Oct 2008
Posts: 7
am sorry to disturb you two again but thing is i don know what do tried to mean by typos ???
and modified code also gave almost similar errors ??
Code:
gcc -ansi -Wall -O3 -o po po.c -lglut
po.c: In function ‘myinit’:
po.c:29: warning: implicit declaration of function ‘glOrtho2D’
po.c: In function ‘main’:
po.c:44: error: ‘reshspe’ undeclared (first use in this function)
po.c:44: error: (Each undeclared identifier is reported only once
po.c:44: error: for each function it appears in.)
and sample code given by pankajp was giving some more errors

Code:
 vim an.c
[regular@localhost ~]$ gcc -ansi -Wall -O3 -o an an.c -lglut
an.c:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
an.c: In function ‘display’:
an.c:20: warning: implicit declaration of function ‘glClear’
an.c:20: error: ‘GL_COLOR_BUFFER_BIT’ undeclared (first use in this function)
an.c:20: error: (Each undeclared identifier is reported only once
an.c:20: error: for each function it appears in.)
an.c:21: warning: implicit declaration of function ‘glBegin’
an.c:21: error: ‘GL_TRIANGLES’ undeclared (first use in this function)
an.c:22: warning: implicit declaration of function ‘glColor3f’
an.c:23: warning: implicit declaration of function ‘glVertex2i’
an.c:28: warning: implicit declaration of function ‘glEnd’
an.c:29: warning: implicit declaration of function ‘glFlush’
an.c: In function ‘main’:
an.c:34: warning: implicit declaration of function ‘glutInit’
an.c:35: warning: implicit declaration of function ‘glutCreateWindow’
an.c:36: warning: implicit declaration of function ‘glutDisplayFunc’
an.c:37: warning: implicit declaration of function ‘glutReshapeFunc’
an.c:37: error: ‘reshape’ undeclared (first use in this function)
an.c:38: warning: implicit declaration of function ‘glutMainLoop’

kindly help me out
Reply With Quote
  #15  
Old 9th March 2009, 06:51 PM
dr death Offline
Registered User
 
Join Date: Dec 2006
Posts: 82
Typos means that you typed the commands in wrong by mistake.

On line 29 you need to change to
Code:
gluOrtho2D
  ^
On line 44 you need to change to
Code:
reshape
    ^
Reply With Quote
Reply

Tags
compile, opengl, programs

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
can't compile programs thebossant Using Fedora 11 30th December 2007 01:48 PM
How to compile OpenGL code on Fedora Core 4 ??? akshaygautam Using Fedora 2 27th January 2007 03:08 PM
OpenGL and SDL example won't compile cypher543 Programming & Packaging 7 22nd November 2006 08:43 AM
Can't compile OpenGL programs. Conquistador Programming & Packaging 3 5th January 2006 06:37 AM
Can't open OpenGL programs (nvidia driver) uphu Using Fedora 4 17th April 2005 10:01 PM


Current GMT-time: 06:35 (Saturday, 20-12-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
...Creek Side - Stockholm-Arlanda Airport (ARN) Travel Photos on Instagram - Marina of Koper Travel Photos - Te Whiti Park Instagram Photos - Texas State Fair Photos on Instagram