You are not logged in.

#1 2008-05-09 13:16:05

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

[SOLVED] How to Issue OpenGL Statements in a C Program

I know there must be a simple answer for this. I would like to draw a 3-D OpenGL cube with a programme written in C. How do I go about doing this and do I need to install any software other than gcc? I am not looking for a big OpenGL engine with physics processing and what-not. All I need is a way to make my C programme draw a 3-D cube on the screen. Can anyone help me?

Last edited by tony5429 (2008-05-17 06:24:55)

Offline

#2 2008-05-09 13:48:52

wonder
Developer
From: Bucharest, Romania
Registered: 2006-07-05
Posts: 5,941
Website

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

you could use freeglut


Give what you have. To someone, it may be better than you dare to think.

Offline

#3 2008-05-09 13:53:05

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

If you want to write "straight" openGL, you'll need mesa (which includes gl.h) and one of the libgl providers (libgl, fglrx-utils, nvidia-utils)

Offline

#4 2008-05-09 14:00:42

lloeki
Member
From: France
Registered: 2007-02-20
Posts: 456
Website

Re: [SOLVED] How to Issue OpenGL Statements in a C Program


To know recursion, you must first know recursion.

Offline

#5 2008-05-09 15:08:31

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

Thanks for all the support. I am away from my computer right now (actually stuck on windows at the moment) but I'll definitely install mesa and libgl (which appears to be a dependency anyway) when I get home. At that point, I suppose the first step in my C programme should be to clear the screen? And I am guessing here....

system("glClear( GL_COLOR_BUFFER_BIT );");

Is that the way to do it in C?

Offline

#6 2008-05-09 16:31:29

lloeki
Member
From: France
Registered: 2007-02-20
Posts: 456
Website

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

no. system() makes a call using a subshell or whatever. that's for when you want to call shell commands or start a program or so on.

seriously, start here: http://nehe.gamedev.net/data/lessons/le … ?lesson=01
those tutorials are awesome.
down the page there's a number of downloadable source files for various languages and OS matching the  lesson. there are notably three for linux.

Last edited by lloeki (2008-05-09 16:33:57)


To know recursion, you must first know recursion.

Offline

#7 2008-05-09 16:51:23

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

Thank you very much. I see now that if I include gl.h, I can use the openGL functions with no problem. I will certainly look through that tutorial. Thanks again.

Offline

#8 2008-05-10 03:07:11

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

Can anyone explain this? I have an NVIDIA GeForce 8600 GT...

bash-3.2# pacman -Q freeglut
freeglut 2.4.0-3
bash-3.2# pacman -Q mesa
mesa 7.0.3rc2-1
bash-3.2# pacman -Q nvidia-utils
nvidia-utils 169.12-1
bash-3.2# gcc -o /usr/bin/cube /home/karam/cube.c
/tmp/ccKd3D2l.o: In function `cube':
cube.c:(.text+0x13): undefined reference to `glColor3f'
cube.c:(.text+0x20): undefined reference to `glutWireCube'
/tmp/ccKd3D2l.o: In function `display':
cube.c:(.text+0x40): undefined reference to `glClearColor'
cube.c:(.text+0x4a): undefined reference to `glClear'
cube.c:(.text+0x4f): undefined reference to `glLoadIdentity'
cube.c:(.text+0x89): undefined reference to `gluLookAt'
cube.c:(.text+0x93): undefined reference to `glFlush'
/tmp/ccKd3D2l.o: In function `reshape':
cube.c:(.text+0xb8): undefined reference to `glViewport'
cube.c:(.text+0xc2): undefined reference to `glMatrixMode'
cube.c:(.text+0xc7): undefined reference to `glLoadIdentity'
cube.c:(.text+0x10d): undefined reference to `gluPerspective'
cube.c:(.text+0x117): undefined reference to `glMatrixMode'
/tmp/ccKd3D2l.o: In function `main':
cube.c:(.text+0x135): undefined reference to `glutInit'
cube.c:(.text+0x13f): undefined reference to `glutInitDisplayMode'
cube.c:(.text+0x14e): undefined reference to `glutInitWindowSize'
cube.c:(.text+0x15d): undefined reference to `glutInitWindowPosition'
cube.c:(.text+0x167): undefined reference to `glutCreateWindow'
cube.c:(.text+0x171): undefined reference to `glutDisplayFunc'
cube.c:(.text+0x17b): undefined reference to `glutReshapeFunc'
cube.c:(.text+0x180): undefined reference to `glutMainLoop'
collect2: ld returned 1 exit status
bash-3.2#

cube.c has this...

#include <GL/gl.h>
#include <GL/glut.h>

void cube(void) {
    glColor3f(1.0, 0.0, 0.0);
    glutWireCube(2);
}

void display(void) {
    glClearColor(0.0,0.0,0.0,1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    cube();
    glFlush();
}

void reshape(int w, int h) {
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Cube");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

Offline

#9 2008-05-10 06:14:56

lloeki
Member
From: France
Registered: 2007-02-20
Posts: 456
Website

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

I guess you have to link against the libs. IIRC that's done with the -l switch. also, gcc is twitchy about the order of the libs and the args, because the order means something to him (well, to ld, but he passes the args)

gcc -o /usr/bin/cube /home/karam/cube.c -l gl -l glut

wow my C times are far away wink


To know recursion, you must first know recursion.

Offline

#10 2008-05-10 06:17:42

lloeki
Member
From: France
Registered: 2007-02-20
Posts: 456
Website

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

also you may want to use '-Wall' switch which enables all warnings, so as to track any possible discreptancies in your code.


To know recursion, you must first know recursion.

Offline

#11 2008-05-10 06:24:19

lloeki
Member
From: France
Registered: 2007-02-20
Posts: 456
Website

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

in fact, teh way you invoke gcc, it acts as a frontend of a two part process:

gcc -o /usr/bin/cube.o /home/karam/cube.c
ld /usr/bin/cube.o -o /usr/bin/cube -l gl -l glut

the first one reads your code and transforms it into a binary object with symbols (transforms function and variable names into some funky thing, plus machine language generation)
the second one creates a table of symbols<->@ in libs so that when the code is executed, the symbols actually mean something.


To know recursion, you must first know recursion.

Offline

#12 2008-05-10 06:51:45

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

Hrm... I am getting this now....

bash-3.2# gcc -o /usr/bin/cube /home/karam/cube.c -l gl -l glut
/usr/bin/ld: cannot find -lgl
collect2: ld returned 1 exit status
bash-3.2#

Any ideas?

Offline

#13 2008-05-10 07:13:10

lloeki
Member
From: France
Registered: 2007-02-20
Posts: 456
Website

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

in fact it's -lGL. see output of 'ls /usr/lib/lib* |grep -i libgl'

Last edited by lloeki (2008-05-10 07:14:14)


To know recursion, you must first know recursion.

Offline

#14 2008-05-10 07:15:56

lloeki
Member
From: France
Registered: 2007-02-20
Posts: 456
Website

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

also I see you're running this as root. don't. just don't. there's no need to.

just use this in the directory where the file reside:

gcc -o cube cube.c -lGL -lglut
./cube

BTW, it works.

Last edited by lloeki (2008-05-10 07:16:42)


To know recursion, you must first know recursion.

Offline

#15 2008-05-10 21:09:44

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

I think you can get away only linking with "glut", if you're using it. It worked when I was trying to get up and running a few days ago.

Offline

#16 2008-05-17 06:24:43

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

Yeah; using just "-lglut" worked for me too. Thanks for all the help, everyone!

Offline

#17 2010-02-15 18:44:52

dwende
Member
From: Israel
Registered: 2008-12-04
Posts: 27

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

I am having a similar compile problem with gluPerspective.
Any ideas anyone?

[dwende@AMD cube]$ make
g++ -Wall -o cube main.cpp imageloader.cpp -lglut -lGL
imageloader.cpp: In function 'Image* loadBMP(const char*)':
imageloader.cpp:141: warning: suggest parentheses around '&&' within '||'
/tmp/ccTcVEcJ.o: In function `handleResize(int, int)':
main.cpp:(.text+0x183): undefined reference to `gluPerspective'
collect2: ld returned 1 exit status
make: *** [cube] Error 1
[dwende@AMD cube]$

I have freeglut installed as seen here:

[dwende@AMD cube]$ pacman -Qi freeglut
Name           : freeglut
Version        : 2.6.0-1
URL            : http://freeglut.sourceforge.net/
Licenses       : MIT
Groups         : None
Provides       : glut
Depends On     : libxxf86vm  mesa  libxi
Optional Deps  : None
Required By    : gtkglarea  jasper  swftools
Conflicts With : glut
Replaces       : glut
Installed Size : 720.00 K
Packager       : Eric Belanger <eric@archlinux.org>
Architecture   : i686
Build Date     : Thu 31 Dec 2009 01:08:51 AM IST
Install Date   : Tue 16 Feb 2010 05:05:31 AM IST
Install Reason : Installed as a dependency for another package
Install Script : No
Description    : Provides functionality for small OpenGL programs

Offline

#18 2010-02-15 18:58:43

jac
Member
From: /home/jac
Registered: 2009-05-19
Posts: 431
Website

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

This thread is really old, it's against policy here to revive threads like these. Start a new thread and link to this if it's important. Also, the actual source code might be relevant, as glu stuff is in other headers, if I remember correctly.

Offline

#19 2010-02-16 03:26:26

dwende
Member
From: Israel
Registered: 2008-12-04
Posts: 27

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

Thanks jac - I made  a new posting at:
http://bbs.archlinux.org/viewtopic.php?id=91281
David

Offline

#20 2010-02-16 12:25:42

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [SOLVED] How to Issue OpenGL Statements in a C Program

As Jac says... smile

Offline

Board footer

Powered by FluxBB