You are not logged in.
Pages: 1
Topic closed
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
you could use freeglut
Give what you have. To someone, it may be better than you dare to think.
Offline
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
try this: http://nehe.gamedev.net/
To know recursion, you must first know recursion.
Offline
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
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
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
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
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
To know recursion, you must first know recursion.
Offline
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
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
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
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
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
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
Yeah; using just "-lglut" worked for me too. Thanks for all the help, everyone!
Offline
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
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
Thanks jac - I made a new posting at:
http://bbs.archlinux.org/viewtopic.php?id=91281
David
Offline
As Jac says...
Offline
Pages: 1
Topic closed