Autor: Jiří Hnídek / jiri.hnidek@tul.cz
glVertex3f(1.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glEnd();
Ve funkci glBegin()
mohou být následující parametry: GL_POINTS
, GL_LINES
, GL_LINE_STRIP
, GL_POINTS
, GL_LINE_LOOP
, GL_TRIANLES
, GL_TRIANGLE_FAN
, GL_TRIANGLE_STRIP
, GL_QUADS
, GL_QUAD_STRIP
, GL_POLYGON
Pokud píšete program v programovacím jazyku C, tak se program přeloží pomocí:
$ gcc -o app ./main.c -lglut -lGL -lGLU -lm -lX11 -lXmu
Vlastnost pro primitiva lze povolit například takto:
glEnable(GL_LINE_SMOOTH);
Nejprve je potřeba říci jakou matici budeme měnit:
glMatrixMode(GLenum mode);
GL_MODELVIEW
- transformace objektů a kameryGL_PROJECTION
- projekce objektů do kameryGL_TEXTURE
- mapování texturBitmapu můžeme vykreslit takto:
const GLubyte bitmap[24] = {
0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00,
0xc0, 0x00, 0xff, 0x00, 0xff, 0x00, 0xc0, 0x00,
0xc0, 0x00, 0xc0, 0x00, 0xff, 0xc0, 0xff, 0xc0
};
glColor3f(1.0, 0.0, 0.0);
glRasterPos2i(100, 100);
glBitmap(10, 12, 0.0f, 0.0f, 0.0f, 0.0f, bitmap);
Pixmapu je možné vykreslovat v několika barevných módech:
#define PIXMAP_WIDTH 256
#define PIXMAP_HEIGHT 256
unsigned char pixmap[PIXMAP_HEIGHT][PIXMAP_WIDTH][3];
glRasterPos2i(0, 0);
glDrawPixels(
PIXMAP_WIDTH, PIXMAP_HEIGHT,
GL_RGB,
GL_UNSIGNED_BYTE,
pixmap);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
// Draw scene ...
glFlush();
glutSwapBuffers();
Nastavení ortogonální kamery je možné provést takto:
glViewport(0, 0, window_width, window_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-20, 20, -20, 20, -30, 30);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(5.0, 5.0, 20.0, // Position of camera
0.0, 0.0, 0.0, // LookAt point
0.0, 0.0, 1.0); // Direction UP
Nastavení perspektivní kamery:
float fov = 70.0; // Field of View
float near_plane = 0.1;
float far_plane = 100.0;
glViewport(0, 0, window_width, window_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov,
(double)window_width/(double)window_height,
near_plane, far_plane);
static GLint vertices[]={
0, 0, 0,
1, 0, 0,
1, 1, 0
0, 1, 0
}
int i;
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_INT, 0, vertices);
glBegin(GL_QUADS);
for (i=0; i<4; i++)
glArrayElement(i);
glEnd();
GLfloat fog_color[4] = {0.5, 0.5, 0.5, 1.0};
glEnable(GL_FOG);
glFogi(GL_FOG_MODE, GL_EXP);
glFogfv(GL_FOG_COLOR, fog_color);
glFogf(GL_FOG_DENSITY, 0.1);
glHint(GL_FOG_HINT, GL_DONT_CARE);
glFogf(GL_FOG_START, 1.0);
glFogf(GL_FOG_END, 10.0);
GLfloat light_position[] = {100.0, 100.0, 100.0, 1.0};
GLfloat light_direction[] = {-1, -1, -1};
GLfloat light_color[] = {1.0, 1.f, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light_direction);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 7); // Spot angle
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 0.5f);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color);
glEnable(GL_LIGHTING); // Enable ligthing in the scen
glEnable(GL_LIGHT0); // Enable light
GLfloat material_ambient[] = {0.6, 0.6, 0.6, 1.0};
GLfloat material_diffuse[] = {0.8, 0.4, 0.4, 1.0};
GLfloat material_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat material_shininess[] = {50.0};
glMaterialfv(GL_FRONT, GL_AMBIENT, material_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, material_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, material_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, material_shininess);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
TEXTURE_WIDTH, TEXTURE_HEIGHT,
0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2i(100, 100);
glTexCoord2f(1.0f, 0.0f); glVertex2i(300, 100);
glTexCoord2f(1.0f, 1.0f); glVertex2i(300, 300);
glTexCoord2f(0.0f, 1.0f); glVertex2i(100, 300);
glEnd();