参考链接
http://im-possible.info/english/article ... real3.html
https://en.wikipedia.org/wiki/Penrose_triangle
http://psylux.psych.tu-dresden.de/i1/ka ... angle.html
http://psylux.psych.tu-dresden.de/i1/ka ... svard.html
不可能的三角形
Computer Graphics, Animaition, OpenGL, DirectX, OpenCL
- 523066680
- Administrator
- 帖子: 573
- 注册时间: 2016年07月19日 12:14
- 联系:
示例代码
旧风格的OpenGL代码
Code: [show] | [select all]
#include <GL/freeglut.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#define SIZE_X 500
#define SIZE_Y 500
#define PI 3.1415926
#define PI2 PI*2
int winID;
static float rx = 45.0;
static float ry = 35.0;
static float rz = 0.0;
void triangleBox( float half )
{
glBegin(GL_TRIANGLES);
glNormal3f( -1.0, 0.0, 0.0);
glVertex3f( -half, -half, -half);
glVertex3f( -half, half, -half);
glVertex3f( -half, -half, half);
glNormal3f( 1.0, 0.0, 0.0);
glVertex3f( half, -half, -half);
glVertex3f( half, half, -half);
glVertex3f( half, -half, half);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0, -1.0);
glVertex3f( -half, -half, -half);
glVertex3f( -half, half, -half);
glVertex3f( half, half, -half);
glVertex3f( half, -half, -half);
glNormal3f(0.0, -1.0, 0.0);
glVertex3f( -half, -half, -half);
glVertex3f( -half, -half, half);
glVertex3f( half, -half, half);
glVertex3f( half, -half, -half);
glNormal3f(0.0, -0.7, 0.7);
glVertex3f( -half, half, -half);
glVertex3f( -half, -half, half);
glVertex3f( half, -half, half);
glVertex3f( half, half, -half);
glEnd();
}
void display(void)
{
static float move = 1.0;
// 清理颜色缓冲区
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glRotatef(ry, 0.0, 1.0, 0.0);
glRotatef(rx, 1.0, 0.0, 0.0);
glRotatef(rz, 0.0, 0.0, 1.0);
glPushMatrix();
for (int i = 0; i < 5; i++)
{
glutSolidCube(1.0);
glTranslatef(move, 0.0, 0.0);
}
glPopMatrix();
glPushMatrix();
for (int i = 0; i < 5; i++)
{
glutSolidCube(1.0);
glTranslatef(0.0, move, 0.0);
}
glPopMatrix();
glPushMatrix();
glTranslatef(0.0, move*4.0, 0.0);
for (int i = 0; i < 3; i++)
{
glutSolidCube(1.0);
glTranslatef(0.0, 0.0, move);
}
glRotatef(90.0, 1.0, 0.0, 0.0);
triangleBox( 0.5 );
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_BLEND);
glLineWidth( 2.0 );
glPointSize( 10.0 );
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
GLfloat mat_ambient[] = { 0.8, 0.8, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 10.0 };
GLfloat mat_diffuse[] = {0.5, 0.5, 0.5, 1.0};
GLfloat light_position[] = { 10.0, 10.0, 10.0, 0.0 };
GLfloat light_specular[] = { 0.5, 0.5, 0.5, 0.1 };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient );
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse );
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
GLfloat spot_direction[] = { 0.0, 0.0, 0.0 };
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
}
void idle(void)
{
usleep(10000);
glutPostRedisplay();
//printf("%f %f\n", rx, ry);
}
void reshape(int Width,int Height)
{
const float fa = 10.0;
const float half = 10.0;
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-half, half, -half, half, 0.0, fa*2.0);
//gluPerspective(60.0, 1.0, 1.0, fa*2.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,fa, 0.0,0.0,0.0, 0.0,1.0,fa);
}
void keypress(unsigned char key, int mousex, int mousey)
{
switch (key)
{
case 'q':
glutDestroyWindow(winID);
exit(0);
break;
case 'Q':
glutDestroyWindow(winID);
exit(0);
break;
case 'w':
rx += 0.5;
glutPostRedisplay();
break;
case 's':
rx -= 0.5;
glutPostRedisplay();
break;
case 'a':
ry += 0.5;
glutPostRedisplay();
break;
case 'd':
ry -= 0.5;
glutPostRedisplay();
break;
case 'j':
rz -= 0.5;
glutPostRedisplay();
break;
case 'k':
rz += 0.5;
glutPostRedisplay();
break;
}
}
void main(int argc, char *argv[])
{
glutInit(&argc, argv);
//显示模式 双缓冲 RGBA
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE );
glutInitWindowSize(SIZE_X, SIZE_Y); //窗口大小
glutInitWindowPosition(200, 200); //位置
winID = glutCreateWindow("TorusVortex"); //窗口句柄
init();
glutDisplayFunc(display); //显示
glutKeyboardFunc(keypress); //按键事件响应
glutReshapeFunc(reshape); //窗口事件响应
glutIdleFunc(idle); //闲时回调函数
glutMainLoop(); //开始主循环
}
- impossibletri.png
- (2.19 KiB) 已下载 1178 次
前往
- 专区
- ↳ 计算机图形
- ↳ OpenGL
- ↳ Vulkan
- ↳ DirectX
- ↳ 资料、教程
- ↳ WebGL
- ↳ 算法和编码
- ↳ Math
- ↳ 扩展库
- ↳ AI
- 程序设计
- ↳ 编程语言
- ↳ C/C++
- ↳ 编译、链接与标准库
- ↳ 资料、教程
- ↳ 文件与系统
- ↳ TroubleShooting
- ↳ Java
- ↳ Go
- ↳ Rust
- ↳ TroubleShooting
- ↳ CSharp/FSharp
- ↳ Functional Programming
- ↳ 脚本
- ↳ Python
- ↳ 资料、教程
- ↳ 模块
- ↳ Ruby
- ↳ Perl
- ↳ 模块
- ↳ TroubleShooting
- ↳ Lua
- ↳ Web Development
- ↳ JavaScript
- ↳ PHP
- ↳ CSS/SVG
- ↳ 应用开发
- ↳ Qt
- ↳ 系统管理自动化
- ↳ Autoit/AHK
- ↳ VBScript
- ↳ BatchScript
- ↳ 第三方命令行工具
- ↳ Topics
- ↳ 综合
- 论坛
- ↳ 论坛管理
- ↳ phpBB论坛配置笔记
- ↳ 论坛使用和通知
- ↳ 申请、建议、举报
- ↳ 谈天说地
- ↳ 日志
- ↳ 资源分享
- ↳ 软件配置和推荐
- ↳ 招聘专区
- 友情链接
- ↳ phpBB 简体中文支持社区
在线用户
正浏览此版面之用户: 没有注册用户 和 2 访客