/*****************************************************************************
Copyright: 2012, ustc All rights reserved.
contact:k283228391@126.com
File name: main3D.c
Description:Sierpinski 3D.
Author:Silang Quan
Version: 1.0
Date: 2012.12.02
*****************************************************************************/
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
SDL_Surface *screen;
GLfloat colors[4][3]={{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{0.0,1.0,1.0}};
typedef struct Point3f
{
GLfloat x;
GLfloat y;
GLfloat z;
}point;
typedef struct squa2d
{
int isBlack;
point pos[4];
}squa;
void quit( int code )
{
SDL_Quit( );
/* Exit program. */
exit( code );
}
void handleKeyEvent( SDL_keysym* keysym )
{
switch( keysym->sym )
{
case SDLK_ESCAPE:
quit( 0 );
break;
case SDLK_SPACE:
break;
default:
break;
}
}
void resizeGL(int width,int height)
{
if ( height == 0 )
{
height = 1;
}
//Reset View
glViewport( 0, 0, (GLint)width, (GLint)height );
//Choose the Matrix mode
glMatrixMode( GL_PROJECTION );
//reset projection
glLoadIdentity();
//set perspection
gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 );
//choose Matrix mode
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
void handleEvents()
{
// Our SDL event placeholder.
SDL_Event event;
//Grab all the events off the queue.
while( SDL_PollEvent( &event ) ) {
switch( event.type ) {
case SDL_KEYDOWN:
// Handle key Event
handleKeyEvent( &event.key.keysym );
break;
case SDL_QUIT:
// Handle quit requests (like Ctrl-c).
quit( 0 );
break;
case SDL_VIDEORESIZE:
//Handle resize event
screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 16,
SDL_OPENGL|SDL_RESIZABLE);
if ( screen )
{
resizeGL(screen->w, screen->h);
}
break;
}
}
}
void initSDL(int width,int height,int bpp,int flags)
{
// First, initialize SDL's video subsystem.
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
fprintf( stderr, "Video initialization failed: %s\n",
SDL_GetError( ) );
quit( 1 );
}
atexit(SDL_Quit);
//Set some Attribute of OpenGL in SDL
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
//Set the video mode
screen= SDL_SetVideoMode( width, height, bpp,flags);
if(!screen )
{
fprintf( stderr, "Video mode set failed: %s\n",SDL_GetError( ) );
quit( 1 );
}
resizeGL(screen->w, screen->h);
//Set caption
SDL_WM_SetCaption( "Cantuo", NULL );
}
void drawSquar(squa *s)
{
int i;
if(s->isBlack)
{
for(i=0;i<4;i++)
glVertex3f(s->pos[i].x,s->pos[i].y,s->pos[i].z);
}
//glVertex3fv();
//glVertex3fv(vc);
}
void cantuo2(GLfloat *a, GLfloat *b,GLfloat *c,GLfloat *d )
{
static int i=0;
glBegin(GL_QUADS);
glColor3fv(colors[i++%4]);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glVertex3fv(d);
glEnd();
}
void divide_cantuo2(GLfloat *a, GLfloat *b,GLfloat *c,GLfloat *d,int m)
{
GLfloat mid[12][3];
if(m>0)
{
// compute 12 points
mid[0][0]=a[0]+(b[0]-a[0])/3.0;
mid[0][1]=a[1]+(b[1]-a[1])/3.0;
mid[0][2]=0.0;
mid[1][0]=a[0]+2.0*(b[0]-a[0])/3.0;
mid[1][1]=a[1]+2.0*(b[1]-a[1])/3.0;
mid[1][2]=0.0;
mid[2][0]=d[0]+1.0*(a[0]-d[0])/3.0;
mid[2][1]=d[1]+2.0*(a[1]-d[1])/3.0;
mid[2][2]=0.0;
mid[3][0]=a[0]+(b[0]-a[0])/3.0;
mid[3][1]=d[1]+2.0*(a[1]-d[1])/3.0;
mid[3][2]=0.0;
mid[4][0]=a[0]+2.0*(b[0]-a[0])/3.0;
mid[4][1]=d[1]+2.0*(a[1]-d[1])/3.0;
mid[4][2]=0.0;
mid[5][0]=c[0]+2.0*(b[0]-c[0])/3.0;
mid[5][1]=c[1]+2.0*(a[1]-d[1])/3.0;
mid[5][2]=0.0;
mid[6][0]=d[0]+(a[0]-d[0])/3.0;
mid[6][1]=d[1]+(a[1]-d[1])/3.0;
mid[6][2]=0.0;
mid[7][0]=a[0]+(b[0]-a[0])/3.0;
mid[7][1]=d[1]+(a[1]-d[1])/3.0;
mid[7][2]=0.0;
mid[8][0]=a[0]+2.0*(b[0]-a[0])/3.0;
mid[8][1]=d[1]+(a[1]-d[1])/3.0;
mid[8][2]=0.0;
mid[9][0]=c[0]+2.0*(b[0]-c[0])/3.0;
mid[9][1]=d[1]+(a[1]-d[1])/3.0;
mid[9][2]=0.0;
mid[10][0]=a[0]+(b[0]-a[0])/3.0;
mid[10][1]=d[1]+(c[1]-d[1])/3.0;
mid[10][2]=0.0;
mid[11][0]=a[0]+2.0*(b[0]-a[0])/3.0;
mid[11][1]=d[1]+2.0*(c[1]-d[1])/3.0;
mid[11][2]=0.0;
/* create 9 part by subdivision */
divide_cantuo2(a,mid[0],mid[3],mid[2], m-1);
divide_cantuo2(mid[0],mid[1],mid[4],mid[3],m-1);
divide_cantuo2(mid[1],b,mid[5],mid[4],m-1);
divide_cantuo2(mid[2],mid[3],mid[7],mid[6],m-1);
//divide_cantuo2(mid[3],mid[4],mid[8],mid[7],m-1);
divide_cantuo2(mid[4],mid[5],mid[9],mid[8],m-1);
divide_cantuo2(mid[6],mid[7],mid[10],d,m-1);
divide_cantuo2(mid[7],mid[8],mid[11],mid[10],m-1);
divide_cantuo2(mid[8],mid[9],c,mid[11],m-1);
printf("m:%d\n",m);
}
else
cantuo2(a,b,c,d);
}
void renderGL()
{
//Define a triangle in space
//GLfloat v[4][3]={{0.0, 0.0, 1.0},{0.0, 0.942809, -0.33333},{-0.816497, -0.471405, -0.333333},{0.816497, -0.471405, -0.333333}};
GLfloat a[4][3]={{-3.0, 3.0, 0.0},{3.0, 3.0, 0},{3.0, -3.0, 0.0},{-3.0, -3.0, 0}};
// Clear the color and depth buffers.
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// We don't want to modify the projection matrix. */
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
// Move down the z-axis.
glTranslatef( 0.0, 0.0, -6.0 );
glClear(GL_COLOR_BUFFER_BIT); /* clear the window */
divide_cantuo2(a[0],a[1],a[2],a[3],2);
SDL_GL_SwapBuffers( );
}
void initGL( int width, int height )
{
float ratio = (float) width / (float) height;
// Our shading model--Gouraud (smooth).
glShadeModel( GL_SMOOTH );
// Set the clear color.
glClearColor( 0, 0, 0, 0 );
// Setup our viewport.
glViewport( 0, 0, width, height );
//Change to the projection matrix and set our viewing volume.
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 60.0, ratio, 1.0, 100.0 );
}
int main( int argc, char* argv[] )
{
// Dimensions of our window.
int width = 640;
int height = 480;
// Color depth in bits of our window.
int bpp = 32;
int flags= SDL_OPENGL|SDL_RESIZABLE;
//Set the SDL
initSDL(width, height, bpp,flags);
//Set the OpenGL
initGL( width, height );
renderGL( );
//main loop
while(1)
{
/* Process incoming events. */
handleEvents( );
/* Draw the screen. */
}
return 0;
}
OpenGL 分形入门
5星 · 超过95%的资源 86 浏览量
2012-12-29
11:43:59
上传
评论 3
收藏 66KB RAR 举报
拳四郎
- 粉丝: 2600
- 资源: 51
最新资源
- pta题库答案c语言之树结构2ListLeaves.zip
- pta题库答案c语言之树结构1树的同构.zip
- 基于C++实现民航飞行与地图简易管理系统可执行程序+说明+详细注释.zip
- pta题库答案c语言之复杂度1最大子列和问题.zip
- 三维装箱问题(Three-Dimensional Bin Packing Problem,3D-BPP)是一个经典的组合优化问题
- 以下是一些关于Linux线程同步的基本概念和方法.txt
- 以下是一个简化的示例,它使用pygame库来模拟烟花动画的框架.txt
- Linux线程同步机制深度解析与实用指南.zip
- PTA题库C语言解题策略与实战.rar
- SVPWM控制技术的simulink建模与仿真【包括simulink模型,参考文献,操作步骤】
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈