⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wgl应用.cpp

📁 C++源代码集
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ewew.cpp : Defines the entry point for the application.
//


// WGL应用.cpp : Defines the entry point for the application.
//
#include"如何设置断言.h"
#include<windows.h>
#include<malloc.h>
#include<math.h>
#include<stdio.h>
#include<GL/gl.h>

#pragma comment(lib,"opengl32")
#pragma comment(lib,"glu32")
#pragma comment(lib,"glaux")

#if defined(GL_SGI_cull_vertex)
      PFNGLCULLPARAMETERFVSGIPROC CullParameterfv;
#endif

#if defined(GL_SGI_compiled_vertex_array)
	  PFNGLLOCKARRAYSSGIPROC LockArrays;
	  PFNGLUNLOCKARRAYSSGIPROC UnlockArrays;
#endif

#if !defined(M_PI)
      #define M_PI 3.14159265F
#endif

#define X_OFFSET_STEP 0.025F;
#define Y_OFFSET_STEP 0.025F;

#define NUM_OBJECTS (sizeof(drawObject)/sizeof(drawObject[0]))

//
//char *windowName="WGL 应用";
int winX,winY;
int winWidth,winHeight;
HDC hDC;
HGLRC hGLRC;
HPALETTE hPalette;

void (*idleFunc)(void);

int objectIndex;
int objectNumMajor=24,objectNumMinor=23;
BOOL halfObject=FALSE;
BOOL redrawContinue=TRUE;
BOOL doubleBuffered=TRUE;
BOOL depthBuffered=TRUE;
BOOL drawOutlines=FALSE;
BOOL textureReplace=FALSE;
BOOL textureEnabled=FALSE;
BOOL useLighting=TRUE;
BOOL useVertexCull=TRUE;
BOOL useFaceCull=TRUE;
BOOL useVertexArray=TRUE;
BOOL useVertexLocking=TRUE;
BOOL perspectiveProj=TRUE;
BOOL useFog=FALSE;
enum MoveModes{MoveNone,MoveObject};
enum MoveModes mode=MoveObject;
float angle=10.0F,axis[3]={0.0F,0.0F,1.0F};
GLfloat objectXform[4][4];
GLfloat xOffset,yOffset;

void drawCube(void);
void drawTorus(void);
void drawSphere(void);

void (*drawObject[])(void)=
{
	drawTorus,drawSphere,drawCube,
};

void drawCube(void)
{
	glBegin(GL_QUADS);
	    glNormal3f(-1.0F,0.0F,0.0F);
		glTexCoord2f(0.0F,1.0F);glVertex3f(-0.5F,-0.5F,-0.5F);
		glTexCoord2f(0.0F,0.0F);glVertex3f(-0.5F,-0.5F,0.5F);
		glTexCoord2f(1.0F,0.0F);glVertex3f(-0.5F,0.5F,0.5F);
		glTexCoord2f(1.0F,1.0F);glVertex3f(-0.5F,0.5F,-0.5F);

	    glNormal3f(1.0F,0.0F,0.0F);
		glTexCoord2f(1.0F,1.0F);glVertex3f(0.5F,0.5F,0.5F);
		glTexCoord2f(0.0F,1.0F);glVertex3f(0.5F,-0.5F,0.5F);
		glTexCoord2f(0.0F,0.0F);glVertex3f(0.5F,-0.5F,-0.5F);
		glTexCoord2f(1.0F,0.0F);glVertex3f(0.5F,0.5F,-0.5F);

	    glNormal3f(0.0F,-1.0F,0.0F);
		glTexCoord2f(0.0F,1.0F);glVertex3f(-0.5F,-0.5F,-0.5F);
		glTexCoord2f(0.0F,0.0F);glVertex3f(0.5F,-0.5F,-0.5F);
		glTexCoord2f(1.0F,0.0F);glVertex3f(0.5F,-0.5F,0.5F);
		glTexCoord2f(1.0F,1.0F);glVertex3f(-0.5F,0.5F,0.5F);

	    glNormal3f(0.0F,0.0F,-1.0F);
		glTexCoord2f(0.0F,1.0F);glVertex3f(-0.5F,-0.5F,-0.5F);
		glTexCoord2f(0.0F,0.0F);glVertex3f(-0.5F,0.5F,-0.5F);
		glTexCoord2f(1.0F,0.0F);glVertex3f(0.5F,0.5F,-0.5F);
		glTexCoord2f(1.0F,1.0F);glVertex3f(0.5F,-0.5F,-0.5F);

	    glNormal3f(0.0F,0.0F,1.0F);
		glTexCoord2f(1.0F,1.0F);glVertex3f(0.5F,0.5F,0.5F);
		glTexCoord2f(0.0F,1.0F);glVertex3f(-0.5F,0.5F,0.5F);
		glTexCoord2f(0.0F,0.0F);glVertex3f(-0.5F,-0.5F,0.5F);
		glTexCoord2f(1.0F,0.0F);glVertex3f(0.5F,-0.5F,0.5F);
	glEnd();
}

void drawSphere(void)
{
	struct vertex
	{
		GLfloat t[2];
		GLfloat n[3];
		GLfloat v[3];
	};

	int numVerts=(objectNumMajor+1)*(objectNumMinor+1);
	int numStrips=halfObject?objectNumMajor/2:objectNumMajor;
	int numPerStrip=2*(objectNumMinor+1);
	int numElements=(objectNumMajor+1)*numPerStrip;

	static struct vertex *vertexArray,*v;
	static GLuint *elementArray,*e;
	static int numMajor;
	static int numMinor;
	int i,j;

	if(!vertexArray||numMajor!=objectNumMajor||
		numMinor!=objectNumMinor)
	{
		float radius=0.6F;
		double majorStep=2.0F*M_PI/objectNumMajor;
		double minorStep=M_PI/objectNumMinor;

		if(vertexArray)free(vertexArray);
		vertexArray=(struct vertex*)calloc(numVerts,sizeof(struct vertex));

		if(elementArray)free(elementArray);
		elementArray=(GLuint*)calloc(numElements,sizeof(GLuint));

		numMajor=objectNumMajor;
		numMinor=objectNumMinor;

		v=vertexArray;
		e=elementArray;
		for(i=0;i<=numMajor;++i)
		{
			double a=i*majorStep;
			GLfloat x=(GLfloat)cos(a);
			GLfloat y=(GLfloat)sin(a);

			for(j=0;j<=numMinor;++j)
			{
				double b=j*minorStep;
				GLfloat c=(GLfloat)sin(b);
				GLfloat r=c*radius;
				GLfloat z=(GLfloat)cos(b);

				v->t[0]=i/(GLfloat)numMajor;
				v->t[1]=j/(GLfloat)numMinor;

				v->n[0]=x*c;
				v->n[1]=y*c;
				v->n[2]=z;

				v->v[0]=x*r;
				v->v[1]=y*r;
				v->v[2]=z*radius;

				v++;

				*e++=i*(numMinor+1)+j;
				*e++=(i+1)*(numMinor+1)+j;
			}
		}
	}

	if(useVertexArray)
	{
		glInterleavedArrays(GL_T2F_N3F_V3F,0,vertexArray);

        #if defined(GL_SGI_compiled_vertex_array)
		if(useVertexLocking && LockArray)
			LockArray(0,numVerts);
		#endif

		for(i=0,e=elementArray;i<numStrips;++i,e+=numPerStrip)
			glDrawElements(GL_TRIANGLE_STRIP,numPerStrip,
			GL_UNSIGNED_INT,e);

		#if defined(GL_SGI_compiled_vertex_array)
		if(useVertexLocking&& UnlockArray)UnlockArrays();
		#endif
	}
	else
	{
		for(i=0,e=elementArray;i<numStrips;++i,e+=numPerStrip)
		{
			glBegin(GL_TRIANGLE_STRIP);
			    for(j=0;j<numPerStrip;++j)
				{
					v=&vertexArray[e[j]];

					glTexCoord2fv(v->t);
					glNormal3fv(v->n);
					glVertex3fv(v->v);
				}
			glEnd();
		}
	}
}

void drawTorus(void)
{
	struct vertex
	{
		GLfloat t[2];
		GLfloat n[3];
		GLfloat v[3];
	};

	int numVerts=(objectNumMajor+1)*(objectNumMinor+1);
	int numStrips=halfObject?objectNumMajor/2:objectNumMajor;
	int numPerStrip=2*(objectNumMinor+1);
	int numElements=(objectNumMajor+1)*numPerStrip;

	static struct vertex *vertexArray,*v;
	static GLuint *elementArray,*e;
	static int numMajor;
	static int numMinor;
	int i,j;

	if(!vertexArray||numMajor!=objectNumMajor||
		numMinor!=objectNumMinor)
	{
		float majorRadius=0.6F;
		float minorRadius=0.2F;
		double majorStep=2.0F*M_PI/objectNumMajor;
		double minorStep=2.0F*M_PI/objectNumMinor;

		if(vertexArray)free(vertexArray);
		vertexArray=(struct vertex*)calloc(numVerts,sizeof(struct vertex));

		if(elementArray)free(elementArray);
		elementArray=(GLuint*)calloc(numElements,sizeof(GLuint));

		numMajor=objectNumMajor;
		numMinor=objectNumMinor;

		v=vertexArray;
		e=elementArray;
		for(i=0;i<=numMajor;++i)
		{
			double a=i*majorStep;
			GLfloat x=(GLfloat)cos(a);
			GLfloat y=(GLfloat)sin(a);

			for(j=0;j<=numMinor;++j)
			{
				double b=j*minorStep;
				GLfloat c=(GLfloat)cos(b);
				GLfloat r=minorRadius*c+majorRadius;
				GLfloat z=minorRadius*(GLfloat)sin(b);

				v->t[0]=i/(GLfloat)numMajor;
				v->t[1]=j/(GLfloat)numMinor;

				v->n[0]=x*c;
				v->n[1]=y*c;
				v->n[2]=z/minorRadius;

				v->v[0]=x*r;
				v->v[1]=y*r;
				v->v[2]=z;

				v++;

				*e++=i*(numMinor+1)+j;
				*e++=(i+1)*(numMinor+1)+j;
			}
		}
	}

	if(useVertexArray)
	{
		glInterleavedArrays(GL_T2F_N3F_V3F,0,vertexArray);

        #if defined(GL_SGI_compiled_vertex_array)
		if(useVertexLocking && LockArray)
			LockArray(0,numVerts);
		#endif

		for(i=0,e=elementArray;i<numStrips;++i,e+=numPerStrip)
			glDrawElements(GL_TRIANGLE_STRIP,numPerStrip,
			GL_UNSIGNED_INT,e);

		#if defined(GL_SGI_compiled_vertex_array)
		if(useVertexLocking&& UnlockArray)UnlockArrays();
		#endif
	}
	else
	{
		for(i=0,e=elementArray;i<numStrips;++i,e+=numPerStrip)
		{
			glBegin(GL_TRIANGLE_STRIP);
			    for(j=0;j<numPerStrip;++j)
				{
					v=&vertexArray[e[j]];

					glTexCoord2fv(v->t);
					glNormal3fv(v->n);
					glVertex3fv(v->v);
				}
			glEnd();
		}
	}
}

void setCheckTexture(void)
{
	int texWidth=256;
	int texHeight=256;
	GLubyte* texPixels,*p;
	int texSize;
	int i,j;

	texSize=texWidth*texHeight*4*sizeof(GLubyte);
	if(textureReplace)
	{
		glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
	}
	else
	{
		glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
	}

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);

	texPixels=(GLubyte *)malloc(texSize);
	if(texPixels==NULL)return;

	p=texPixels;
	for(i=0;i<texHeight;++i)
	{
		for(j=0;j<texWidth;++j)
		{
			if((i^j)&32)
			{
				p[0]=0xff;p[1]=0xff;p[2]=0xff;p[3]=0xff;
			}
			else
			{
				p[0]=0x10;p[1]=0x10;p[2]=0x10;p[3]=0xff;
			}
			p+=4;
		}
	}
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,texWidth,texHeight,0
		,GL_RGBA,GL_UNSIGNED_BYTE,texPixels);
	free(texPixels);
}

void matrixIdentity(GLfloat m[4][4])
{
	m[0][0]=1.0f;m[0][1]=0.0f;m[0][2]=0.0f;m[0][3]=0.0f;
	m[1][0]=0.0f;m[1][1]=1.0f;m[1][2]=0.0f;m[1][3]=0.0f;
	m[2][0]=0.0f;m[2][1]=0.0f;m[2][2]=1.0f;m[2][3]=0.0f;
	m[3][0]=0.0f;m[3][1]=0.0f;m[3][2]=0.0f;m[3][3]=1.0f;
}

void setProjection(void)
{
	GLfloat aspect=(GLfloat)winWidth/(GLfloat)winHeight;

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	if(perspectiveProj)
	{
		glFrustum(-0.5F*aspect,0.5F*aspect,-0.5F,0.5F,1.0F,3.0F);
		#if defined(GL_SGI_cull_vertex)
		if(CullParameterfv)
		{
			GLfloat eye[4]={0.0F,0.0F,0.0F,1.0F};
			CullParameterfv(GL_CULL_VERTEX_EYE_POSITION_SGI,eye);
		}
		#endif
	}
	else
	{
		glOrtho(-1.0F*aspect,1.0F*aspect,-1.0F,1.0F,1.0F,3.0F);
		#if defined(GL_SGI_cull_vertex)
		if(CullParameterfv)
		{
			GLfloat eye[4]={0.0F,0.0F,1.0F,0.0F};
			CullParameterfv(GL_CULL_VERTEX_EYE_POSITION_SGI,eye);
		}
		#endif
	}
	glMatrixMode(GL_MODELVIEW);
}

void setMaterial(void)
{
	GLfloat matAmb[4]={0.01F,0.01F,0.01F,1.00F};
	GLfloat matDiff[4]={0.45F,0.05F,0.65F,0.60F};
	GLfloat matSpec[4]={0.50F,0.50F,0.50F,1.00F};
	GLfloat matShine=20.00F;

	glMaterialfv(GL_FRONT,GL_AMBIENT,matAmb);
	glMaterialfv(GL_FRONT,GL_DIFFUSE,matDiff);
	glMaterialfv(GL_FRONT,GL_SPECULAR,matSpec);
	glMaterialf(GL_FRONT,GL_SHININESS,matShine);
}

void init(void)
{
	GLfloat lightOPos[4]={0.70F,0.70F,1.25F,0.00F};
	GLfloat fogDensity=2.35F*0.180F;
	GLfloat fogColor[4]={0.4F,0.4F,0.5F,0.0F};

	#if defined(GL_SGI_cull_vertex)
	  CullParameterfv=(PFNGLCULLPARAMETERFVSGIPROC)
		 wglGetProcAddress("glCullParameterfvSGI");
	#endif

	#if defined(GL_SGI_compiled_vertex_array)
	  LockArrays=(PFNGLLOCKARRAYSSGIPROC)
		  wglGetProcAddress("glLockArraysSGI");
	  UnlockArrays=(PFNGLUNLOCKARRAYSSGIPROC)
		  wglGetProcAddress("glUnlockArraysSGI");
	#endif

	glFogi(GL_FOG_MODE,GL_EXP2);
	glFogf(GL_FOG_DENSITY,fogDensity);
	glFogfv(GL_FOG_COLOR,fogColor);

	setProjection();
	glTranslatef(0.0F,0.0F,-2.0F);
	setMaterial();
	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
	glLightfv(GL_LIGHT0,GL_POSITION,lightOPos);
	glEnable(GL_LIGHT0);
	setCheckTexture();
	matrixIdentity(objectXform);
}

void resize(void)
{
	setProjection();
	glViewport(0,0,winWidth,winHeight);
}

void doRedraw(void)
{
	if(useFog)
	{
		glClearColor(0.4F,0.4F,0.5F,1.0F);
		glEnable(GL_FOG);
	}
	else
	{
		glClearColor(0.2F,0.2F,0.1F,1.0F);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -