|
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/ sudo apt-get install libglut3-dev #include <stdlib.h> Source: http://www.wakayama-u.ac.jp/~tokoi/opengl/libglut.html billh@Billaspire:~/Desktop/temp$ gcc opengl.c -lGL -lglut #include <GL/glut.h> |
見れば分かる通り, プログラムは, 初期化して, |
#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;
}