opengl入门系列5-视景体定义
2014-09-19 13:35
204 查看
----
main.cpp:
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include "jitter.h"
#define PI_ 3.14159265358979323846
void accFrustum(GLdouble left, GLdouble right, GLdouble bottom,
GLdouble top, GLdouble zNear, GLdouble zFar, GLdouble pixdx,
GLdouble pixdy, GLdouble eyedx, GLdouble eyedy, GLdouble focus)
{
GLdouble xwsize, ywsize;
GLdouble dx, dy;
GLint viewport[4];
//x, y, left, bottom
glGetIntegerv (GL_VIEWPORT, viewport);
xwsize = right - left;
ywsize = top - bottom;
dx = -(pixdx*xwsize/(GLdouble) viewport[2] + eyedx*zNear/focus);
dy = -(pixdy*ywsize/(GLdouble) viewport[3] + eyedy*zNear/focus);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum (left + dx, right + dx, bottom + dy, top + dy, zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (-eyedx, -eyedy, 0.0);
}
/* accPerspective()
*
* The first 4 arguments are identical to the gluPerspective() call.
* pixdx and pixdy are anti-alias jitter in pixels.
* Set both equal to 0.0 for no anti-alias jitter.
* eyedx and eyedy are depth-of field jitter in pixels.
* Set both equal to 0.0 for no depth of field effects.
*
* focus is distance from eye to plane in focus.
* focus must be greater than, but not equal to 0.0.
*
* Note that accPerspective() calls accFrustum().
*/
void accPerspective(GLdouble fovy, GLdouble aspect,
GLdouble zNear, GLdouble zFar, GLdouble pixdx, GLdouble pixdy,
GLdouble eyedx, GLdouble eyedy, GLdouble focus)
{
GLdouble fov2,left,right,bottom,top;
fov2 = ((fovy*PI_) / 180.0) / 2.0;
top = zNear / (cos(fov2) / sin(fov2));
bottom = -top;
right = top * aspect;
left = -right;
accFrustum (left, right, bottom, top, zNear, zFar,
pixdx, pixdy, eyedx, eyedy, focus);
}
/* Initialize lighting and other values.
*/
void init(void)
{
GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 0.0, 0.0, 10.0, 1.0 };
GLfloat lm_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, 50.0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lm_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glShadeModel (GL_FLAT);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearAccum(0.0, 0.0, 0.0, 0.0);
}
void displayObjects(void)
{
GLfloat torus_diffuse[] = { 0.7, 0.7, 0.0, 1.0 };
GLfloat cube_diffuse[] = { 0.0, 0.7, 0.7, 1.0 };
GLfloat sphere_diffuse[] = { 0.7, 0.0, 0.7, 1.0 };
GLfloat octa_diffuse[] = { 0.7, 0.4, 0.4, 1.0 };
glPushMatrix ();
glTranslatef (0.0, 0.0, -5.0);
glRotatef (30.0, 1.0, 0.0, 0.0);
glPushMatrix ();
glTranslatef (-0.80, 0.35, 0.0);
glRotatef (100.0, 1.0, 0.0, 0.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, torus_diffuse);
glutSolidTorus (0.275, 0.85, 16, 16);
glPopMatrix ();
glPushMatrix ();
glTranslatef (-0.75, -0.50, 0.0);
glRotatef (45.0, 0.0, 0.0, 1.0);
glRotatef (45.0, 1.0, 0.0, 0.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, cube_diffuse);
glutSolidCube (1.5);
glPopMatrix ();
glPushMatrix ();
glTranslatef (0.75, 0.60, 0.0);
glRotatef (30.0, 1.0, 0.0, 0.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, sphere_diffuse);
glutSolidSphere (1.0, 16, 16);
glPopMatrix ();
glPushMatrix ();
glTranslatef (0.70, -0.90, 0.25);
glMaterialfv(GL_FRONT, GL_DIFFUSE, octa_diffuse);
glutSolidOctahedron ();
glPopMatrix ();
glPopMatrix ();
}
#define ACSIZE 8
void display(void)
{
GLint viewport[4];
int jitter;
glGetIntegerv (GL_VIEWPORT, viewport);
glClear(GL_ACCUM_BUFFER_BIT);
for (jitter = 0; jitter < ACSIZE; jitter++) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
accPerspective (50.0,
(GLdouble) viewport[2]/(GLdouble) viewport[3],
1.0, 15.0, j8[jitter].x, j8[jitter].y, 0.0, 0.0, 1.0);
displayObjects ();
glAccum(GL_ACCUM, 1.0/ACSIZE);
}
glAccum (GL_RETURN, 1.0);
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
/* Main Loop
* Be certain you request an accumulation buffer.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB
| GLUT_ACCUM | GLUT_DEPTH);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
----
jitter.h :
typedef struct
{
GLfloat x, y;
} jitter_point;
#define MAX_SAMPLES 66
/* 2 jitter points */
jitter_point j2[] =
{
{ 0.246490, 0.249999},
{-0.246490, -0.249999}
};
/* 3 jitter points */
jitter_point j3[] =
{
{-0.373411, -0.250550},
{ 0.256263, 0.368119},
{ 0.117148, -0.117570}
};
/* 4 jitter points */
jitter_point j4[] =
{
{-0.208147, 0.353730},
{ 0.203849, -0.353780},
{-0.292626, -0.149945},
{ 0.296924, 0.149994}
};
/* 8 jitter points */
jitter_point j8[] =
{
{-0.334818, 0.435331},
{ 0.286438, -0.393495},
{ 0.459462, 0.141540},
{-0.414498, -0.192829},
{-0.183790, 0.082102},
{-0.079263, -0.317383},
{ 0.102254, 0.299133},
{ 0.164216, -0.054399}
};
/* 15 jitter points */
jitter_point j15[] =
{
{ 0.285561, 0.188437},
{ 0.360176, -0.065688},
{-0.111751, 0.275019},
{-0.055918, -0.215197},
{-0.080231, -0.470965},
{ 0.138721, 0.409168},
{ 0.384120, 0.458500},
{-0.454968, 0.134088},
{ 0.179271, -0.331196},
{-0.307049, -0.364927},
{ 0.105354, -0.010099},
{-0.154180, 0.021794},
{-0.370135, -0.116425},
{ 0.451636, -0.300013},
{-0.370610, 0.387504}
};
/* 24 jitter points */
jitter_point j24[] =
{
{ 0.030245, 0.136384},
{ 0.018865, -0.348867},
{-0.350114, -0.472309},
{ 0.222181, 0.149524},
{-0.393670, -0.266873},
{ 0.404568, 0.230436},
{ 0.098381, 0.465337},
{ 0.462671, 0.442116},
{ 0.400373, -0.212720},
{-0.409988, 0.263345},
{-0.115878, -0.001981},
{ 0.348425, -0.009237},
{-0.464016, 0.066467},
{-0.138674, -0.468006},
{ 0.144932, -0.022780},
{-0.250195, 0.150161},
{-0.181400, -0.264219},
{ 0.196097, -0.234139},
{-0.311082, -0.078815},
{ 0.268379, 0.366778},
{-0.040601, 0.327109},
{-0.234392, 0.354659},
{-0.003102, -0.154402},
{ 0.297997, -0.417965}
};
/* 66 jitter points */
jitter_point j66[] =
{
{ 0.266377, -0.218171},
{-0.170919, -0.429368},
{ 0.047356, -0.387135},
{-0.430063, 0.363413},
{-0.221638, -0.313768},
{ 0.124758, -0.197109},
{-0.400021, 0.482195},
{ 0.247882, 0.152010},
{-0.286709, -0.470214},
{-0.426790, 0.004977},
{-0.361249, -0.104549},
{-0.040643, 0.123453},
{-0.189296, 0.438963},
{-0.453521, -0.299889},
{ 0.408216, -0.457699},
{ 0.328973, -0.101914},
{-0.055540, -0.477952},
{ 0.194421, 0.453510},
{ 0.404051, 0.224974},
{ 0.310136, 0.419700},
{-0.021743, 0.403898},
{-0.466210, 0.248839},
{ 0.341369, 0.081490},
{ 0.124156, -0.016859},
{-0.461321, -0.176661},
{ 0.013210, 0.234401},
{ 0.174258, -0.311854},
{ 0.294061, 0.263364},
{-0.114836, 0.328189},
{ 0.041206, -0.106205},
{ 0.079227, 0.345021},
{-0.109319, -0.242380},
{ 0.425005, -0.332397},
{ 0.009146, 0.015098},
{-0.339084, -0.355707},
{-0.224596, -0.189548},
{ 0.083475, 0.117028},
{ 0.295962, -0.334699},
{ 0.452998, 0.025397},
{ 0.206511, -0.104668},
{ 0.447544, -0.096004},
{-0.108006, -0.002471},
{-0.380810, 0.130036},
{-0.242440, 0.186934},
{-0.200363, 0.070863},
{-0.344844, -0.230814},
{ 0.408660, 0.345826},
{-0.233016, 0.305203},
{ 0.158475, -0.430762},
{ 0.486972, 0.139163},
{-0.301610, 0.009319},
{ 0.282245, -0.458671},
{ 0.482046, 0.443890},
{-0.121527, 0.210223},
{-0.477606, -0.424878},
{-0.083941, -0.121440},
{-0.345773, 0.253779},
{ 0.234646, 0.034549},
{ 0.394102, -0.210901},
{-0.312571, 0.397656},
{ 0.200906, 0.333293},
{ 0.018703, -0.261792},
{-0.209349, -0.065383},
{ 0.076248, 0.478538},
{-0.073036, -0.355064},
{ 0.145087, 0.221726}
};
---
main.cpp:
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include "jitter.h"
#define PI_ 3.14159265358979323846
void accFrustum(GLdouble left, GLdouble right, GLdouble bottom,
GLdouble top, GLdouble zNear, GLdouble zFar, GLdouble pixdx,
GLdouble pixdy, GLdouble eyedx, GLdouble eyedy, GLdouble focus)
{
GLdouble xwsize, ywsize;
GLdouble dx, dy;
GLint viewport[4];
//x, y, left, bottom
glGetIntegerv (GL_VIEWPORT, viewport);
xwsize = right - left;
ywsize = top - bottom;
dx = -(pixdx*xwsize/(GLdouble) viewport[2] + eyedx*zNear/focus);
dy = -(pixdy*ywsize/(GLdouble) viewport[3] + eyedy*zNear/focus);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum (left + dx, right + dx, bottom + dy, top + dy, zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (-eyedx, -eyedy, 0.0);
}
/* accPerspective()
*
* The first 4 arguments are identical to the gluPerspective() call.
* pixdx and pixdy are anti-alias jitter in pixels.
* Set both equal to 0.0 for no anti-alias jitter.
* eyedx and eyedy are depth-of field jitter in pixels.
* Set both equal to 0.0 for no depth of field effects.
*
* focus is distance from eye to plane in focus.
* focus must be greater than, but not equal to 0.0.
*
* Note that accPerspective() calls accFrustum().
*/
void accPerspective(GLdouble fovy, GLdouble aspect,
GLdouble zNear, GLdouble zFar, GLdouble pixdx, GLdouble pixdy,
GLdouble eyedx, GLdouble eyedy, GLdouble focus)
{
GLdouble fov2,left,right,bottom,top;
fov2 = ((fovy*PI_) / 180.0) / 2.0;
top = zNear / (cos(fov2) / sin(fov2));
bottom = -top;
right = top * aspect;
left = -right;
accFrustum (left, right, bottom, top, zNear, zFar,
pixdx, pixdy, eyedx, eyedy, focus);
}
/* Initialize lighting and other values.
*/
void init(void)
{
GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 0.0, 0.0, 10.0, 1.0 };
GLfloat lm_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, 50.0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lm_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glShadeModel (GL_FLAT);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearAccum(0.0, 0.0, 0.0, 0.0);
}
void displayObjects(void)
{
GLfloat torus_diffuse[] = { 0.7, 0.7, 0.0, 1.0 };
GLfloat cube_diffuse[] = { 0.0, 0.7, 0.7, 1.0 };
GLfloat sphere_diffuse[] = { 0.7, 0.0, 0.7, 1.0 };
GLfloat octa_diffuse[] = { 0.7, 0.4, 0.4, 1.0 };
glPushMatrix ();
glTranslatef (0.0, 0.0, -5.0);
glRotatef (30.0, 1.0, 0.0, 0.0);
glPushMatrix ();
glTranslatef (-0.80, 0.35, 0.0);
glRotatef (100.0, 1.0, 0.0, 0.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, torus_diffuse);
glutSolidTorus (0.275, 0.85, 16, 16);
glPopMatrix ();
glPushMatrix ();
glTranslatef (-0.75, -0.50, 0.0);
glRotatef (45.0, 0.0, 0.0, 1.0);
glRotatef (45.0, 1.0, 0.0, 0.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, cube_diffuse);
glutSolidCube (1.5);
glPopMatrix ();
glPushMatrix ();
glTranslatef (0.75, 0.60, 0.0);
glRotatef (30.0, 1.0, 0.0, 0.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, sphere_diffuse);
glutSolidSphere (1.0, 16, 16);
glPopMatrix ();
glPushMatrix ();
glTranslatef (0.70, -0.90, 0.25);
glMaterialfv(GL_FRONT, GL_DIFFUSE, octa_diffuse);
glutSolidOctahedron ();
glPopMatrix ();
glPopMatrix ();
}
#define ACSIZE 8
void display(void)
{
GLint viewport[4];
int jitter;
glGetIntegerv (GL_VIEWPORT, viewport);
glClear(GL_ACCUM_BUFFER_BIT);
for (jitter = 0; jitter < ACSIZE; jitter++) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
accPerspective (50.0,
(GLdouble) viewport[2]/(GLdouble) viewport[3],
1.0, 15.0, j8[jitter].x, j8[jitter].y, 0.0, 0.0, 1.0);
displayObjects ();
glAccum(GL_ACCUM, 1.0/ACSIZE);
}
glAccum (GL_RETURN, 1.0);
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
/* Main Loop
* Be certain you request an accumulation buffer.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB
| GLUT_ACCUM | GLUT_DEPTH);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
----
jitter.h :
typedef struct
{
GLfloat x, y;
} jitter_point;
#define MAX_SAMPLES 66
/* 2 jitter points */
jitter_point j2[] =
{
{ 0.246490, 0.249999},
{-0.246490, -0.249999}
};
/* 3 jitter points */
jitter_point j3[] =
{
{-0.373411, -0.250550},
{ 0.256263, 0.368119},
{ 0.117148, -0.117570}
};
/* 4 jitter points */
jitter_point j4[] =
{
{-0.208147, 0.353730},
{ 0.203849, -0.353780},
{-0.292626, -0.149945},
{ 0.296924, 0.149994}
};
/* 8 jitter points */
jitter_point j8[] =
{
{-0.334818, 0.435331},
{ 0.286438, -0.393495},
{ 0.459462, 0.141540},
{-0.414498, -0.192829},
{-0.183790, 0.082102},
{-0.079263, -0.317383},
{ 0.102254, 0.299133},
{ 0.164216, -0.054399}
};
/* 15 jitter points */
jitter_point j15[] =
{
{ 0.285561, 0.188437},
{ 0.360176, -0.065688},
{-0.111751, 0.275019},
{-0.055918, -0.215197},
{-0.080231, -0.470965},
{ 0.138721, 0.409168},
{ 0.384120, 0.458500},
{-0.454968, 0.134088},
{ 0.179271, -0.331196},
{-0.307049, -0.364927},
{ 0.105354, -0.010099},
{-0.154180, 0.021794},
{-0.370135, -0.116425},
{ 0.451636, -0.300013},
{-0.370610, 0.387504}
};
/* 24 jitter points */
jitter_point j24[] =
{
{ 0.030245, 0.136384},
{ 0.018865, -0.348867},
{-0.350114, -0.472309},
{ 0.222181, 0.149524},
{-0.393670, -0.266873},
{ 0.404568, 0.230436},
{ 0.098381, 0.465337},
{ 0.462671, 0.442116},
{ 0.400373, -0.212720},
{-0.409988, 0.263345},
{-0.115878, -0.001981},
{ 0.348425, -0.009237},
{-0.464016, 0.066467},
{-0.138674, -0.468006},
{ 0.144932, -0.022780},
{-0.250195, 0.150161},
{-0.181400, -0.264219},
{ 0.196097, -0.234139},
{-0.311082, -0.078815},
{ 0.268379, 0.366778},
{-0.040601, 0.327109},
{-0.234392, 0.354659},
{-0.003102, -0.154402},
{ 0.297997, -0.417965}
};
/* 66 jitter points */
jitter_point j66[] =
{
{ 0.266377, -0.218171},
{-0.170919, -0.429368},
{ 0.047356, -0.387135},
{-0.430063, 0.363413},
{-0.221638, -0.313768},
{ 0.124758, -0.197109},
{-0.400021, 0.482195},
{ 0.247882, 0.152010},
{-0.286709, -0.470214},
{-0.426790, 0.004977},
{-0.361249, -0.104549},
{-0.040643, 0.123453},
{-0.189296, 0.438963},
{-0.453521, -0.299889},
{ 0.408216, -0.457699},
{ 0.328973, -0.101914},
{-0.055540, -0.477952},
{ 0.194421, 0.453510},
{ 0.404051, 0.224974},
{ 0.310136, 0.419700},
{-0.021743, 0.403898},
{-0.466210, 0.248839},
{ 0.341369, 0.081490},
{ 0.124156, -0.016859},
{-0.461321, -0.176661},
{ 0.013210, 0.234401},
{ 0.174258, -0.311854},
{ 0.294061, 0.263364},
{-0.114836, 0.328189},
{ 0.041206, -0.106205},
{ 0.079227, 0.345021},
{-0.109319, -0.242380},
{ 0.425005, -0.332397},
{ 0.009146, 0.015098},
{-0.339084, -0.355707},
{-0.224596, -0.189548},
{ 0.083475, 0.117028},
{ 0.295962, -0.334699},
{ 0.452998, 0.025397},
{ 0.206511, -0.104668},
{ 0.447544, -0.096004},
{-0.108006, -0.002471},
{-0.380810, 0.130036},
{-0.242440, 0.186934},
{-0.200363, 0.070863},
{-0.344844, -0.230814},
{ 0.408660, 0.345826},
{-0.233016, 0.305203},
{ 0.158475, -0.430762},
{ 0.486972, 0.139163},
{-0.301610, 0.009319},
{ 0.282245, -0.458671},
{ 0.482046, 0.443890},
{-0.121527, 0.210223},
{-0.477606, -0.424878},
{-0.083941, -0.121440},
{-0.345773, 0.253779},
{ 0.234646, 0.034549},
{ 0.394102, -0.210901},
{-0.312571, 0.397656},
{ 0.200906, 0.333293},
{ 0.018703, -0.261792},
{-0.209349, -0.065383},
{ 0.076248, 0.478538},
{-0.073036, -0.355064},
{ 0.145087, 0.221726}
};
---
相关文章推荐
- javascript入门系列演示·函数的定义以及简单参数使用,调用函数
- javascript入门系列演示·函数的定义以及简单参数使用,调用函数
- MAC Cocoa Opengl入门系列教程二(OpenGl坐标系)
- cocos2dx opengl入门系列二-画一个三角形
- cocos2dx opengl入门系列一-序言
- 入门系列--OpenGL最简单的入门
- 云星数据---Scala实战系列(精品版)】:Scala入门教程014-Scala实战源码-Scala函数定义与调用方式
- Opengl入门系列- FBO的渲染到纹理的用法
- opengl入门系列- 颜色混合
- cocos2dx opengl入门系列四-显示图片
- Joe's OpenGL ES 2.0 系列经典入门教程(第一课:Introduction)
- OpenGL入门系列6 - 累积缓冲区用法例子
- opengl入门系列4-颜色索引模式
- MAC Cocoa Opengl入门系列教程一(基本图元绘制)
- opengl入门系列- openglES 2.0 的 VBO 用法和例子
- javascript入门系列演示·函数的定义以及简单参数使用,调用函数
- opengl入门系列3-裁剪平面
- OpenGL入门系列- OpenGL_ES 在 本地操作系统窗口上绘制的过程过程
- ABP入门系列(6)——定义导航菜单
- cocos2dx opengl入门系列五-显示多纹理