Sign in to follow this  

OpenGL under X

This topic is 3732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know how to make an OpenGL window under windows and learning how to do this in Linux. I found this article which should work. http://msdn2.microsoft.com/en-us/library/ms537577.aspx My problem right now is compiling. I get a whole bunch of undefined reference when I simply 'gcc main.c'. My guess is I need to include paths to where the OpenGL libs are but I'm not really sure how to do that or where these library's and include files are. So any help would be appreciated. I tried looking this up last night but didn't find anything.

Share this post


Link to post
Share on other sites
This is the command I tryed - got the same erros.

>gcc -IGL -IGLU -IX11 main.c

Erros:
Quote:

dustin@sulaco:~/dev/tests/gl> gcc -IGL -IGLU -IX11 main.c
main.c: In function ‘main’:
main.c:62: warning: incompatible implicit declaration of built-in function ‘exit’
main.c:67: warning: incompatible implicit declaration of built-in function ‘exit’
main.c:74: warning: incompatible implicit declaration of built-in function ‘exit’
main.c:81: warning: incompatible implicit declaration of built-in function ‘exit’
main.c:248:3: warning: no newline at end of file
/tmp/ccirRVFR.o: In function `main':
main.c:(.text+0x67): undefined reference to `XOpenDisplay'
main.c:(.text+0xb9): undefined reference to `glXQueryExtension'
main.c:(.text+0x100): undefined reference to `glXChooseVisual'
main.c:(.text+0x14a): undefined reference to `glXCreateContext'
main.c:(.text+0x1b8): undefined reference to `XCreateColormap'
main.c:(.text+0x25a): undefined reference to `XCreateWindow'
main.c:(.text+0x2a0): undefined reference to `XSetStandardProperties'
main.c:(.text+0x2b7): undefined reference to `glXMakeCurrent'
main.c:(.text+0x2ca): undefined reference to `XMapWindow'
main.c:(.text+0x2ec): undefined reference to `XIfEvent'
main.c:(.text+0x322): undefined reference to `XNextEvent'
main.c:(.text+0x36c): undefined reference to `XLookupString'
main.c:(.text+0x45a): undefined reference to `XPending'
/tmp/ccirRVFR.o: In function `resize':
main.c:(.text+0x48c): undefined reference to `glViewport'
main.c:(.text+0x4af): undefined reference to `glMatrixMode'
main.c:(.text+0x4b4): undefined reference to `glLoadIdentity'
main.c:(.text+0x4fb): undefined reference to `gluPerspective'
main.c:(.text+0x505): undefined reference to `glMatrixMode'
/tmp/ccirRVFR.o: In function `createObjects':
main.c:(.text+0x51e): undefined reference to `glNewList'
main.c:(.text+0x523): undefined reference to `gluNewQuadric'
main.c:(.text+0x535): undefined reference to `gluQuadricDrawstyle'
main.c:(.text+0x55b): undefined reference to `gluSphere'
main.c:(.text+0x560): undefined reference to `glEndList'
main.c:(.text+0x56f): undefined reference to `glNewList'
main.c:(.text+0x574): undefined reference to `gluNewQuadric'
main.c:(.text+0x586): undefined reference to `gluQuadricDrawstyle'
main.c:(.text+0x594): undefined reference to `gluQuadricNormals'
main.c:(.text+0x5d1): undefined reference to `gluCylinder'
main.c:(.text+0x5d6): undefined reference to `glEndList'
main.c:(.text+0x5e5): undefined reference to `glNewList'
main.c:(.text+0x5ea): undefined reference to `glPushMatrix'
main.c:(.text+0x605): undefined reference to `glRotatef'
main.c:(.text+0x618): undefined reference to `glTranslatef'
main.c:(.text+0x61d): undefined reference to `gluNewQuadric'
main.c:(.text+0x62f): undefined reference to `gluQuadricDrawstyle'
main.c:(.text+0x63d): undefined reference to `gluQuadricNormals'
main.c:(.text+0x68c): undefined reference to `gluCylinder'
main.c:(.text+0x691): undefined reference to `glPopMatrix'
main.c:(.text+0x696): undefined reference to `glEndList'
/tmp/ccirRVFR.o: In function `initializeGL':
main.c:(.text+0x6ae): undefined reference to `glClearIndex'
main.c:(.text+0x6bb): undefined reference to `glClearDepth'
main.c:(.text+0x6c5): undefined reference to `glEnable'
main.c:(.text+0x6cf): undefined reference to `glMatrixMode'
main.c:(.text+0x72f): undefined reference to `gluPerspective'
main.c:(.text+0x739): undefined reference to `glMatrixMode'
/tmp/ccirRVFR.o: In function `polarView':
main.c:(.text+0x7f9): undefined reference to `glTranslated'
main.c:(.text+0x81f): undefined reference to `glRotated'
main.c:(.text+0x845): undefined reference to `glRotated'
main.c:(.text+0x867): undefined reference to `glRotated'
/tmp/ccirRVFR.o: In function `drawScene':
main.c:(.text+0x87b): undefined reference to `glClear'
main.c:(.text+0x880): undefined reference to `glPushMatrix'
main.c:(.text+0x8fb): undefined reference to `glIndexi'
main.c:(.text+0x905): undefined reference to `glCallList'
main.c:(.text+0x90f): undefined reference to `glIndexi'
main.c:(.text+0x919): undefined reference to `glCallList'
main.c:(.text+0x923): undefined reference to `glIndexi'
main.c:(.text+0x928): undefined reference to `glPushMatrix'
main.c:(.text+0x940): undefined reference to `glTranslatef'
main.c:(.text+0x965): undefined reference to `glRotatef'
main.c:(.text+0x96f): undefined reference to `glCallList'
main.c:(.text+0x974): undefined reference to `glPopMatrix'
main.c:(.text+0x979): undefined reference to `glPopMatrix'
main.c:(.text+0x98c): undefined reference to `glXSwapBuffers'
collect2: ld returned 1 exit status

Share this post


Link to post
Share on other sites
gcc -lGL -lGLU -lX11 -I/usr/include -I/usr/include/GL main.c -o main

yay this works.

now I just need to figure out why it says 'could not get visual'

Added:
I'll try to find the source for GLX gears and see if I can build and run that.

Share this post


Link to post
Share on other sites
Some month ago I've written a little demo for OpenGL on X, but somehow I lost the code. Nevertheless I remember this tutorial about setting up OpenGL on X11 - http://gpwiki.org/index.php/OpenGL:Codes:Setting_up_X11 - and the source code of Quake (only the GFX part, but the code is awesome) was a big help.

Have a question, why don't you use OpenGL on SDL? It's much easier to setup. Btw, the SDL code has interesting X11 stuff, too.

Share this post


Link to post
Share on other sites
Hey,

If you want to do some OpenGL coding under linux, I would recommend glfw (glfw.sourceforge.net). It is awesome, and it compiles under windows, linux and mac. So you don't have to change the source when doing linux or windows. This was a great help for me when I was beginning OpenGL. God speed

Share this post


Link to post
Share on other sites
Nice to see some people actually looking to do things for themselves instead of depending on libraries to cook their meals.

Here is a really simple code example which works on Linux, and also source for MacOSX for full screen rendering (its a cut from a common codebase I use for OpenGL programming, should have enough info for you). Feel free to use as a reference to get your source working. First time getting visuals working right can be confusing, also you will want to make sure you correctly set and allocate your colormap also as some X servers crap out when the colormap is wrong...

Enjoy.

BTW, the XF86VidMode stuff is broken on NVidia drivers right now I think... it has been a while since I have used that code path.



typedef unsigned char iu1; typedef signed char is1;
typedef unsigned short iu2; typedef signed short is2;
typedef unsigned int iu4; typedef signed int is4;
typedef unsigned long iup; typedef signed long isp;
typedef float fp4; typedef double fp8;
#if V_BIT==64
typedef unsigned long iu8; typedef signed long is8;
#else
typedef unsigned long long iu8; typedef signed long long is8;
#endif
/*============================================================================

OPEN GL DISPLAY INTERFACE

------------------------------------------------------------------------------
Set vGlX and vGlY before opening display.
Setting them to zero will initialize screen default resolution.
After display is open they will be corrected to size displayable.
------------------------------------------------------------------------------
vGlX - screen width
vGlY - screen height
VGlOpen() - open connection to display (set vGlX and vGlY first)
VGlClose() - close connection to display
VGlBefore() - call before working next frame
VGlAfter() - call after doing next frame (swaps buffers)
============================================================================*/

static isp vGlX,vGlY;
static void (*VGlExit)(void)=VNop;

/*============================================================================
X WINDOW
============================================================================*/

#if V_UNIX_X
Cursor vGlCur1; /* default */
Cursor vGlCur2; /* blank */
Display* vGlDpy;
XVisualInfo* vGlVis;
Colormap vGlMap;
isp vGlScr;
isp vGlSX; /* screen size */
isp vGlSY;
isp vGlWX; /* window position */
isp vGlWY;
Window vGlRoot;
Window vGlWin;
iu1 vGlBlank[32*32]; /* hidden cursor */
GLXContext vGlCon;
/*--------------------------------------------------------------------------*/
#if V_UNIX_XF86==1
/*============================================================================
Allow for XF86 VidMode Extension, changing resolution when lock mouse.
Finds largest resolution that does not exceed vGlX and vGlY.
============================================================================*/

static void (*VXf86Leave)(void)=VNop;
static void (*VXf86Enter)(void)=VNop;
static isp vXf86Best;
static XF86VidModeModeInfo** vXf86Modes;
/*--------------------------------------------------------------------------*/
static void VXf86Begin(void); /* prototype required */
static void VXf86Done(void) {
XF86VidModeSwitchToMode(vGlDpy,vGlScr,vXf86Modes[0]);
XMoveResizeWindow(vGlDpy,vGlWin,vGlWX,vGlWY,vGlX,vGlY);
VXf86Leave=VNop; VXf86Enter=VXf86Begin;}
/*--------------------------------------------------------------------------*/
static void VXf86Begin(void) {
XF86VidModeSwitchToMode(vGlDpy,vGlScr,vXf86Modes[vXf86Best]);
XMoveResizeWindow(vGlDpy,vGlWin,0,0,vGlX,vGlY);
XF86VidModeSetViewPort(vGlDpy,vGlScr,0,0);
VXf86Leave=VXf86Done; VXf86Enter=VNop; }
/*--------------------------------------------------------------------------*/
static void VXf86Check(void) { int n=0,b=0,bx=0,by=0,i=0;
VXf86Leave=VNop; VXf86Enter=VNop;
if(!XF86VidModeQueryVersion(vGlDpy,&n,&b)) return;
XF86VidModeGetAllModeLines(vGlDpy,vGlScr,&n,&vXf86Modes); b=0;
for(;i<n;i++) {
isp x=vXf86Modes[i]->hdisplay,y=vXf86Modes[i]->vdisplay;
if((x<=vGlX)&&(y<=vGlY)&&(x>=bx)&&((x!=bx)||(y>by))) {
b=i; bx=x; by=y; } }
vGlX=vXf86Modes[b]->hdisplay; vGlY=vXf86Modes[b]->vdisplay;
vXf86Best=b; VXf86Enter=VXf86Begin; }
#endif /* V_UNIX_XF86 */
/*--------------------------------------------------------------------------*/
static isp VGlVBPP(XVisualInfo* v) {
XImage* i; isp b; iu1 t[8];
i=XCreateImage(vGlDpy,v->visual,v->depth,ZPixmap,0,(iu1*)t,1,1,32,0);
b=i->bits_per_pixel; i->data=0; XDestroyImage(i); return b; }
/*--------------------------------------------------------------------------*/
static XVisualInfo* VGlVisGet(isp d) {
XVisualInfo t; XVisualInfo* v; XVisualInfo* c; int n=0;
t.screen=vGlScr; t.class=TrueColor; t.depth=24;
c=v=XGetVisualInfo(vGlDpy,
VisualScreenMask|VisualClassMask|VisualDepthMask,&t,&n);
while(n>0) {
if(VGlVBPP(c)==d) { t.visualid=c->visualid; XFree(v);
return XGetVisualInfo(vGlDpy,VisualIDMask,&t,&n); }
n--; c++; }
XFree(v); return 0; }
/*--------------------------------------------------------------------------*/
static void VGlBefore(void) { glXMakeCurrent(vGlDpy,vGlWin,vGlCon); }
/*--------------------------------------------------------------------------*/
static void VGlAfter(void) { glXSwapBuffers(vGlDpy,vGlWin); }
/*--------------------------------------------------------------------------*/
static void VGlClose(void) {
#if V_UNIX_XF86==1
VXf86Leave();
#endif /* V_UNIX_XF86 */
VGlBefore();
glXDestroyContext(vGlDpy,vGlCon);
XCloseDisplay(vGlDpy); }
/*--------------------------------------------------------------------------*/
static int VGlError(Display* d, XErrorEvent* e) { return 0; }
/*--------------------------------------------------------------------------*/
static void VGlOpen(void) {
VGlExit(); VGlExit=VGlClose;
vGlDpy=XOpenDisplay(0);
XSetErrorHandler(&VGlError);
vGlScr=DefaultScreen(vGlDpy);
vGlSX=XDisplayWidth(vGlDpy,vGlScr);
vGlSY=XDisplayHeight(vGlDpy,vGlScr);
if(vGlX==0)vGlX=vGlSX;
if(vGlY==0)vGlY=vGlSY;
#if V_UNIX_XF86==1
VXf86Check();
#endif /* V_UNIX_XF86 */
vGlRoot=RootWindow(vGlDpy,vGlScr);
vGlVis=VGlVisGet(32);
if(vGlVis==0) { vGlVis=VGlVisGet(24); }
vGlMap=XCreateColormap(vGlDpy,vGlRoot,vGlVis->visual, AllocNone);
vGlCur1=XCreateFontCursor(vGlDpy,XC_top_left_arrow);
{ Pixmap s1,s2; XColor c={0,0,0,0,0,0};
s1=XCreateBitmapFromData(vGlDpy,vGlRoot,vGlBlank,32,32);
s2=XCreateBitmapFromData(vGlDpy,vGlRoot,vGlBlank,32,32);
vGlCur2=XCreatePixmapCursor(vGlDpy,s1,s2,&c,&c,15,15);
XFreePixmap(vGlDpy,s1); XFreePixmap(vGlDpy,s2); }
{ XSetWindowAttributes a; XSizeHints h; XEvent e;
a.save_under=0; a.override_redirect=1;
a.border_pixel=0; a.colormap=vGlMap; a.cursor=vGlCur1;
vGlWin=XCreateWindow(vGlDpy,vGlRoot,0,0,1,1,0,24,
InputOutput,vGlVis->visual,CWColormap|CWCursor|CWBorderPixel,&a);
h.flags=PPosition | PMinSize | PMaxSize;
h.x=0; h.y=0;
h.width=1; h.height=1;
h.min_width=h.max_width=vGlX;
h.min_height=h.max_height=vGlY;
XSetWMNormalHints(vGlDpy,vGlWin,&h);
XSelectInput(vGlDpy,vGlWin,
ExposureMask|ButtonPressMask|StructureNotifyMask|
KeyPressMask|KeyReleaseMask|ButtonReleaseMask|
EnterWindowMask|LeaveWindowMask|PointerMotionMask|
VisibilityChangeMask);
XStoreName(vGlDpy,vGlWin,V_NAME);
XMapWindow(vGlDpy,vGlWin);
XMoveResizeWindow(vGlDpy,vGlWin,0,0,vGlX,vGlY);
vGlWX=vGlWY=0;
XWindowEvent(vGlDpy,vGlWin,StructureNotifyMask,&e);
while(XCheckWindowEvent(vGlDpy,vGlWin,StructureNotifyMask,&e)) {} }
XFlush(vGlDpy);
XSync(vGlDpy,0);
{ XVisualInfo* v;
int a[]={GLX_RGBA,GLX_RED_SIZE,8,GLX_GREEN_SIZE,8,GLX_BLUE_SIZE,8,
GLX_DOUBLEBUFFER,None};
v=glXChooseVisual(vGlDpy,vGlScr,a);
vGlCon=glXCreateContext(vGlDpy,v,0,1); }
XSync(vGlDpy,0);
VGlBefore(); }
#endif /* V_UNIX_X */

/*============================================================================
MAC
============================================================================*/

#if V_UNIX_MAC
static CGLContextObj vGlContext;
static CFDictionaryRef vGlOld;
static CFDictionaryRef vGlNew;
/*--------------------------------------------------------------------------*/
static void VGlClose(void) {
CGLSetCurrentContext(0);
CGLClearDrawable(vGlContext);
CGLDestroyContext(vGlContext);
CGDisplayShowCursor(kCGDirectMainDisplay);
CGDisplaySwitchToMode(CGMainDisplayID(),vGlOld);
CGReleaseAllDisplays();
VGlExit=VNop; }
/*--------------------------------------------------------------------------*/
static void VGlOpen(void) {
CGLPixelFormatObj f; long n; long p[1];
CGLPixelFormatAttribute a[6]={kCGLPFAFullScreen,kCGLPFASingleRenderer,
kCGLPFADisplayMask,0,kCGLPFADoubleBuffer,0 };
VGlExit(); VGlExit=VGlClose;
vGlOld=CGDisplayCurrentMode(CGMainDisplayID());
vGlX=(vGlX==0)?CGDisplayPixelsWide(CGMainDisplayID()):vGlX;
vGlY=(vGlY==0)?CGDisplayPixelsHigh(CGMainDisplayID()):vGlY;
vGlNew=CGDisplayBestModeForParameters(CGMainDisplayID(),32,vGlX,vGlY,0);
CGCaptureAllDisplays();
CGDisplaySwitchToMode(CGMainDisplayID(),vGlNew);
vGlX=CGDisplayPixelsWide(CGMainDisplayID());
vGlY=CGDisplayPixelsHigh(CGMainDisplayID());
a[3]=CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay);
CGLChoosePixelFormat(a,&f,&n);
CGLCreateContext(f,0,&vGlContext);
CGLDestroyPixelFormat(f);
CGDisplayHideCursor(kCGDirectMainDisplay);
CGLSetFullScreen(vGlContext);
CGLSetCurrentContext(vGlContext);
CGLEnable(vGlContext,kCGLCESwapRectangle);
p[0]=1; CGLSetParameter(vGlContext,kCGLCPSwapInterval,p); }
/*--------------------------------------------------------------------------*/
static void VGlBefore(void) { CGLSetCurrentContext(vGlContext); }
/*--------------------------------------------------------------------------*/
static void VGlAfter(void) { CGLFlushDrawable(vGlContext); }
#endif /* V_UNIX_MAC */


Share this post


Link to post
Share on other sites
Sign in to follow this