BillHung.Net


powered by FreeFind     sms

OpenGL 3D Samples

Jan 2009


Source Codes in a Directory
Source Codes in a Zip File
Source:swiftless.com

Oct 16 2008

Source: http://soledadpenades.com/2008/01/12/my-first-opengl-program-in-linux/

openGL sample triangle

sudo apt-get install libglut3-dev
sudo apt-get install libsdl1.2-dev libsdl1.2debian
billh@Billaspire:~/Desktop/temp$ gcc main.cpp -o main.o -lGL -lGLU -lSDL
billh@Billaspire:~/Desktop/temp$ ./main.o
#include <stdlib.h>
#if defined(_MSC_VER)
#include "SDL.h"
#include "SDL_opengl.h"
#else
#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"
#endif
#include
SDL_Surface *screen;
Uint32 flags;
void render()
{ GLubyte data[32*4];
for(int i=0;i<32*4;++i)
{ data[i]=rand(); }
// Lock surface if needed
if (SDL_MUSTLOCK(screen))
if (SDL_LockSurface(screen) < 0) return;
// Ask SDL for the time in milliseconds
int tick = SDL_GetTicks();
glClearColor(1,0,1,0);
glClear(GL_COLOR_BUFFER_BIT);
// and the mighty triangle....
glPushMatrix();
glRotatef(tick*0.01, 0,0,1);
glEnable(GL_POLYGON_STIPPLE);
glPolygonStipple(data);
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd(); glPopMatrix();
glDisable(GL_POLYGON_STIPPLE);
// Unlock if needed
if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
// Tell SDL to update the whole screen
SDL_UpdateRect(screen, 0, 0, 640, 480); }
// Entry point
int main(int argc, char *argv[])
{
// Initialize SDL's subsystems
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
// Register SDL_Quit to be called at exit; makes sure things are
// cleaned up when we quit.
atexit(SDL_Quit);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
flags = SDL_OPENGL;
//flags |= SDL_FULLSCREEN;
//SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, true);
//SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 16);
// Attempt to create a 640x480 window with 32bit pixels.
screen = SDL_SetVideoMode(640, 480, 32, flags);
// If we fail, return error.
if ( screen == NULL ) {
fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}
// Main loop: loop forever.
while (1)
{
// Render stuff
render();
SDL_GL_SwapBuffers();
// Poll for events, and handle the ones we care about.
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
break;
case SDL_KEYUP:
// If escape is pressed, return (and thus, quit)
if (event.key.keysym.sym == SDLK_ESCAPE)
return 0;
break;
case SDL_QUIT:
return(0);
}
}
}
return 0;
}

Source: http://www.wakayama-u.ac.jp/~tokoi/opengl/libglut.html
opengl from japanese website

billh@Billaspire:~/Desktop/temp$ gcc opengl.c -lGL -lglut
billh@Billaspire:~/Desktop/temp$ ./a.out
#include <GL/glut.h>
void display(void)
{
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
 
void glutInit(int *argcp, char **argv)
GLUT および OpenGL 環境を初期化します. 引数には main の引数をそのまま渡します. X Window で使われるオプション -display などはここで処理されます. この関数によって引数の内容が変更されます. プログラム自身で処理すべき引数があるときは, この後で処理します.
int glutCreateWindow(char *name)
ウィンドウを開きます. 引数 name はそのウィンドウの名前の文字列で, タイトルバーなどに表示されます. 以降の OpenGL による図形の描画等は, 開いたウィンドウに対して行われます. なお, 戻り値は開いたウィンドウの識別子です.
void glutDisplayFunc(void (*func)(void))
引数 func は開いたウィンドウ内に描画する関数へのポインタです. ウィンドウが開かれたり, 他のウィンドウによって隠されたウィンドウが再び現れたりして, ウィンドウを再描画する必要があるときに, この関数が実行されます. したがって, この関数内で図形表示を行います.
void glutMainLoop(void)
これは無限ループです. この関数を呼び出すことで, プログラムはイベントの待ち受け状態になります.

見れば分かる通り, プログラムは,

初期化して,
ウィンドウを開いて,
そのウィンドウ内に絵を描く関数を決めて,
何かことが起こるのを待つ.


vertex 2d
#include <GL/glut.h>
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
// glBegin(GL_LINE_LOOP);
glBegin(GL_QUAD_STRIP);
glVertex2d(-0.9, -0.9);
glVertex2d(0.9, -0.9);
glVertex2d(0.9, 0.9);
glVertex2d(-0.9, 0.9);
glEnd();
glFlush();
}
void init(void)
{
glClearColor(0.0, 0.0, 1.0, 1.0);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}


#include <GL/glut.h>
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
// glBegin(GL_LINE_LOOP);
//glBegin(GL_QUADS);
glBegin(GL_POLYGON);
glColor3d(0.0, 0.0, 1.0);
glVertex2d(-0.9, -0.9);
glColor3d(0.0, 1.0, 1.0);
glVertex2d(0.9, -0.9);
glColor3d(1.0, 1.0, 1.0);
glVertex2d(0.9, 0.9);
glColor3d(0.0, 0.0, 0.0);
glVertex2d(-0.9, 0.9);
glEnd();
glFlush();
}
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.5);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}


#include <stdio.h>
#include <GL/glut.h>
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
// glBegin(GL_LINE_LOOP);
//glBegin(GL_QUADS);
glBegin(GL_POLYGON);
glColor3d(0.0, 0.0, 1.0);
glVertex2d(-0.9, -0.9);
glColor3d(0.0, 1.0, 1.0);
glVertex2d(0.9, -0.9);
glColor3d(1.0, 1.0, 1.0);
glVertex2d(0.9, 0.9);
glColor3d(0.0, 0.0, 0.0);
glVertex2d(-0.9, 0.9);
glEnd();
glFlush();
}
void resize(int w, int h)
{
glViewport(0, 0, w, h);
glLoadIdentity();
//glOrtho(-w / 200.0, w / 200.0, -h / 200.0, h / 200.0, -1.0, 1.0);
glOrtho(-0.5, (GLdouble)w - 0.5, (GLdouble)h - 0.5, -0.5, -1.0, 1.0);
}
void mouse(int button, int state, int x, int y)
{
static int x0, y0;
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_UP) {
glColor3d(0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2i(x0, y0);
glVertex2i(x, y);
glEnd();
glFlush();
}
else {
x0 = x;
y0 = y;
}
printf("left");
break;
case GLUT_MIDDLE_BUTTON:
printf("middle");
break;
case GLUT_RIGHT_BUTTON:
printf("right");
break;
default:
break;
}
printf(" button is ");
switch (state) {
case GLUT_UP:
printf("up");
break;
case GLUT_DOWN:
printf("down");
break;
default:
break;
}
printf(" at (%d, %d)\n", x, y);
}
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.5);
}
int main(int argc, char *argv[])
{
glutInitWindowPosition(100, 100);
glutInitWindowSize(320, 240);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutMouseFunc(mouse);
init();
glutMainLoop();
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>

#define MAXPOINTS 100
GLint point[MAXPOINTS][2];
int pointnum=0;
int rubberband =0;
void display(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
if (pointnum > 1) {
glColor3d(0.0, 0.0, 0.0);
glBegin(GL_LINES);
for (i = 0; i < pointnum; ++i) {
glVertex2iv(point[i]);
}
glEnd();
}
// glBegin(GL_LINE_LOOP);
//glBegin(GL_QUADS);
// glBegin(GL_POLYGON);
// glColor3d(0.0, 0.0, 1.0);
// glVertex2d(-0.9, -0.9);
// glColor3d(0.0, 1.0, 1.0);
// glVertex2d(0.9, -0.9);
// glColor3d(1.0, 1.0, 1.0);
// glVertex2d(0.9, 0.9);
// glColor3d(0.0, 0.0, 0.0);
// glVertex2d(-0.9, 0.9);
// glEnd();
if (pointnum > 1) {
glColor3d(0.0, 0.0, 0.0);
glBegin(GL_LINES);
for (i = 0; i < pointnum; ++i) {
glVertex2iv(point[i]);
}
glEnd();
}
glFlush();
}
void resize(int w, int h)
{
glViewport(0, 0, w, h);
glLoadIdentity();
glOrtho(-w / 200.0, w / 200.0, -h / 200.0, h / 200.0, -1.0, 1.0);
glOrtho(-0.5, (GLdouble)w - 0.5, (GLdouble)h - 0.5, -0.5, -1.0, 1.0);
}
void mouse(int button, int state, int x, int y)
{
static int x0, y0;
switch (button) {
case GLUT_LEFT_BUTTON:
point[pointnum][0] = x;
point[pointnum][1] = y;
if (state == GLUT_UP) {
glColor3d(0.0, 0.0, 0.0);
glBegin(GL_LINES);
// glVertex2i(x0, y0);
// glVertex2i(x, y);
glVertex2iv(point[pointnum - 1]);
glVertex2iv(point[pointnum]); glEnd();
glFlush();
rubberband = 0;
}
else {
//x0 = x;
//y0 = y;
}
if (pointnum < MAXPOINTS - 1) ++pointnum;
printf("left");
break;
case GLUT_MIDDLE_BUTTON:
printf("middle");
break;
case GLUT_RIGHT_BUTTON:
printf("right");
break;
default:
break;
}
printf(" button is ");
switch (state) {
case GLUT_UP:
printf("up");
break;
case GLUT_DOWN:
printf("down");
break;
default:
break;
}
printf(" at (%d, %d)\n", x, y);
}
void motion(int x, int y)
{
static GLint savepoint[2];
glEnable(GL_COLOR_LOGIC_OP);
glLogicOp(GL_INVERT);
glBegin(GL_LINES);
if (rubberband) {
glVertex2iv(point[pointnum - 1]);
glVertex2iv(savepoint);
}
glVertex2iv(point[pointnum - 1]);
glVertex2i(x, y);
glEnd();
glFlush();
glLogicOp(GL_COPY);
glDisable(GL_COLOR_LOGIC_OP);
savepoint[0] = x;
savepoint[1] = y;
rubberband = 1;
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'q':
case 'Q':
case '\033': printf("Quit\n");
exit(0);
break;
default:
break;
}
}
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.5);
}
int main(int argc, char *argv[])
{
glutInitWindowPosition(100, 100);
glutInitWindowSize(320, 240);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
init();
glutMainLoop();
return 0;
}



#include <GL/glut.h>
GLdouble vertex[][3] = {
{ 0.0, 0.0, 0.0 },
{ 1.0, 0.0, 0.0 },
{ 1.0, 1.0, 0.0 },
{ 0.0, 1.0, 0.0 },
{ 0.0, 0.0, 1.0 },
{ 1.0, 0.0, 1.0 },
{ 1.0, 1.0, 1.0 },
{ 0.0, 1.0, 1.0 }
};
int edge[][2] = {
{ 0, 1 },
{ 1, 2 },
{ 2, 3 },
{ 3, 0 },
{ 4, 5 },
{ 5, 6 },
{ 6, 7 },
{ 7, 4 },
{ 0, 4 },
{ 1, 5 },
{ 2, 6 },
{ 3, 7 }
};
void display(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
/* ¿Þ·Á€ÎÉÁ²è */
glColor3d(0.0, 0.0, 0.0);
glBegin(GL_LINES);
for (i = 0; i < 12; ++i) {
glVertex3dv(vertex[edge[i][0]]);
glVertex3dv(vertex[edge[i][1]]);
}
glEnd();
// glRotated(180.0, 0.0, 1.0, 0.0);
// glBegin(GL_POLYGON);
// glColor3d(1.0, 0.0, 0.0); /* ÀÖ */
// glVertex2d(-0.9, -0.9);
// glColor3d(0.0, 1.0, 0.0); /* ÎÐ */
// glVertex2d(0.9, -0.9);
// glColor3d(0.0, 0.0, 1.0); /* ÀÄ */
// glVertex2d(0.9, 0.9);
// glColor3d(1.0, 1.0, 0.0); /* ²« */
// glVertex2d(-0.9, 0.9);
// glEnd();
glFlush();
}
void resize(int w, int h)
{
glViewport(0, 0, w, h);
glLoadIdentity();
// glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
// glTranslated(0.0, 0.0, -5.0);
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutReshapeFunc(resize);
init();
glutMainLoop();
return 0;
}



#include <GL/glut.h>
#include <stdlib.h>
GLdouble vertex[][3] = {
{ 0.0, 0.0, 0.0 },
{ 1.0, 0.0, 0.0 },
{ 1.0, 1.0, 0.0 },
{ 0.0, 1.0, 0.0 },
{ 0.0, 0.0, 1.0 },
{ 1.0, 0.0, 1.0 },
{ 1.0, 1.0, 1.0 },
{ 0.0, 1.0, 1.0 }
};
int edge[][2] = {
{ 0, 1 },
{ 1, 2 },
{ 2, 3 },
{ 3, 0 },
{ 4, 5 },
{ 5, 6 },
{ 6, 7 },
{ 7, 4 },
{ 0, 4 },
{ 1, 5 },
{ 2, 6 },
{ 3, 7 }
};
void idle(void){
glutPostRedisplay();
}
void display(void)
{
int i;
static int r = 0;
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotated((double)r, 0.0, 1.0, 0.0);
glColor3d(0.0, 0.0, 0.0);
glBegin(GL_LINES);
for (i = 0; i < 12; ++i) {
glVertex3dv(vertex[edge[i][0]]);
glVertex3dv(vertex[edge[i][1]]);
}
glEnd();
// glRotated(180.0, 0.0, 1.0, 0.0);
// glBegin(GL_POLYGON);
// glColor3d(1.0, 0.0, 0.0); // glVertex2d(-0.9, -0.9);
// glColor3d(0.0, 1.0, 0.0); // glVertex2d(0.9, -0.9);
// glColor3d(0.0, 0.0, 1.0); // glVertex2d(0.9, 0.9);
// glColor3d(1.0, 1.0, 0.0); // glVertex2d(-0.9, 0.9);
// glEnd();
// glFlush();
glutSwapBuffers();
if (++r >= 360) r = 0;
}
void resize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
// glTranslated(0.0, 0.0, -5.0);
//gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
}
void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN) {
glutIdleFunc(idle);
}
else {
glutIdleFunc(0);
}
break;
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) {
glutPostRedisplay();
}
break;
default:
break;
}
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'q':
case 'Q':
case '\033': /* '\033' $B$O(B ESC $B$N(B ASCII $B%3!<%I(B */
exit(0);
default:
break;
}
}
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
init();
glutMainLoop();
return 0;
}


#include <GL/glut.h>
#include <stdlib.h>
GLdouble vertex[][3] = {
{ 0.0, 0.0, 0.0 },
{ 1.0, 0.0, 0.0 },
{ 1.0, 1.0, 0.0 },
{ 0.0, 1.0, 0.0 },
{ 0.0, 0.0, 1.0 },
{ 1.0, 0.0, 1.0 },
{ 1.0, 1.0, 1.0 },
{ 0.0, 1.0, 1.0 }
};
int edge[][2] = {
{ 0, 1 },
{ 1, 2 },
{ 2, 3 },
{ 3, 0 },
{ 4, 5 },
{ 5, 6 },
{ 6, 7 },
{ 7, 4 },
{ 0, 4 },
{ 1, 5 },
{ 2, 6 },
{ 3, 7 }
};
int face[][4] = {
{ 0, 1, 2, 3 },
{ 1, 5, 6, 2 },
{ 5, 4, 7, 6 },
{ 4, 0, 3, 7 },
{ 4, 5, 1, 0 },
{ 3, 2, 6, 7 }
};
GLdouble color[][3] = {
{ 1.0, 0.0, 0.0 },
{ 0.0, 1.0, 0.0 },
{ 0.0, 0.0, 1.0 },
{ 1.0, 1.0, 0.0 },
{ 1.0, 0.0, 1.0 },
{ 0.0, 1.0, 1.0 }
};
void idle(void){
glutPostRedisplay();
}
void display(void)
{
int i;
int j;
static int r = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotated((double)r, 0.0, 1.0, 0.0);
glColor3d(0.0, 0.0, 0.0);
glBegin(GL_QUADS);
for (j = 0; j < 6; ++j) {
glColor3dv(color[j]);
for (i = 0; i < 4; ++i) {
glVertex3dv(vertex[face[j][i]]);
}
}
glEnd();
// glBegin(GL_QUADS);
// for (j = 0; j < 6; ++j) {
// glColor3dv(color[j]);
// for (i = 0; i < 4; ++i) {
// glVertex3dv(vertex[face[j][i]]);
// }
// }
// glEnd();
// glBegin(GL_LINES);
// for (i = 0; i < 12; ++i) {
// glVertex3dv(vertex[edge[i][0]]);
// glVertex3dv(vertex[edge[i][1]]);
// }
// glEnd();
// glRotated(180.0, 0.0, 1.0, 0.0);
// glBegin(GL_POLYGON);
// glColor3d(1.0, 0.0, 0.0); // glVertex2d(-0.9, -0.9);
// glColor3d(0.0, 1.0, 0.0); // glVertex2d(0.9, -0.9);
// glColor3d(0.0, 0.0, 1.0); // glVertex2d(0.9, 0.9);
// glColor3d(1.0, 1.0, 0.0); // glVertex2d(-0.9, 0.9);
// glEnd();
// glFlush();
glutSwapBuffers();
if (++r >= 360) r = 0;
}
void resize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
// glTranslated(0.0, 0.0, -5.0);
//gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
}
void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN) {
glutIdleFunc(idle);
}
else {
glutIdleFunc(0);
}
break;
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) {
glutPostRedisplay();
}
break;
default:
break;
}
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'd':
case 'D':
glutPostRedisplay();
break;
case 'q':
case 'Q':
case '\033': exit(0);
default:
break;
}
}
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
//glCullFace(GL_BACK);
glCullFace(GL_FRONT);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
init();
glutMainLoop();
return 0;
}