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

📄 main.c

📁 opengl对于灯光的运用。不错的灯光学习实例
💻 C
字号:
#include <stdio.h>
#include <windows.h>
#include <GL/gl.h>
#include <GL/glaux.h>

void myinit(void);
void CALLBACK myReshape(GLsizei w,GLsizei h);
void CALLBACK On0KeyDown(void);
void CALLBACK On1KeyDown(void);
void CALLBACK On2KeyDown(void);
void CALLBACK OnLButtonDown(AUX_EVENTREC *);
void CALLBACK OnLButtonUp(AUX_EVENTREC *);
void CALLBACK OnLButtonMove(AUX_EVENTREC *);
void CALLBACK OnRButtonDown(AUX_EVENTREC *);
void CALLBACK OnRButtonUp(AUX_EVENTREC *);
void CALLBACK OnRButtonMove(AUX_EVENTREC *);
void CALLBACK display(void);

HWND  hWnd; //handle of the window
GLint mx,my; //position of mouse;
GLuint nID = 0; //the number of objects, no object numbered 0
GLdouble azim0, inc0; //rotate angle of eye
GLdouble azim1, inc1; //rotate angle of object1
GLdouble azim2, inc2; //rotate angle of object2
GLdouble dist = 25.0; //distance from the center of 2 objects to eye

const GLdouble fovy = 30.0; //field of view angle, in degrees, in the y-direction
const GLdouble nearclip = 5.0; //near clip distance
const GLdouble farclip = 50.0; //far clip distance
const GLdouble steps = 0.1; //step of distance
const GLdouble stepa = 0.2; //step of angle

void myinit(void)
{
	hWnd = auxGetHWND();
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_NORMALIZE);
}

void CALLBACK myReshape(GLsizei w,GLsizei h)
{
	glViewport(0,0,w,h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	if(h < 1) h=1;
	gluPerspective(fovy, 1.0*w/h, nearclip, farclip);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void CALLBACK On0KeyDown(void) {nID = 0;}
void CALLBACK On1KeyDown(void) {nID = 1;}
void CALLBACK On2KeyDown(void) {nID = 2;}

void CALLBACK OnLButtonDown(AUX_EVENTREC *event)
{
	mx=event->data[AUX_MOUSEX];
	my=event->data[AUX_MOUSEY];
	SetCapture(hWnd);
}
void CALLBACK OnLButtonUp(AUX_EVENTREC *event){ReleaseCapture();}
void CALLBACK OnLButtonMove(AUX_EVENTREC *event)
{
	GLint dx,dy; //offset of mouse;

	dx = event->data[AUX_MOUSEX]-mx;
	dy = event->data[AUX_MOUSEY]-my;
	mx=event->data[AUX_MOUSEX];
	my=event->data[AUX_MOUSEY];

	if(nID == 0)
	{
		azim0 += dx*stepa;
		inc0 += dy*stepa;
	}
	else if(nID == 1)
	{
		azim1 += dx*stepa;
		inc1 += dy*stepa;
	}
	else if(nID == 2)
	{
		azim2 += dx*stepa;
		inc2 += dy*stepa;
	}
	display();
}
void CALLBACK OnRButtonDown(AUX_EVENTREC *event)
{
	mx=event->data[AUX_MOUSEX];
	my=event->data[AUX_MOUSEY];
	SetCapture(hWnd);
}
void CALLBACK OnRButtonUp(AUX_EVENTREC *event){ReleaseCapture();}
void CALLBACK OnRButtonMove(AUX_EVENTREC *event)
{
	GLint dx,dy; //offset of mouse;

	dx = event->data[AUX_MOUSEX]-mx;
	dy = event->data[AUX_MOUSEY]-my;
	mx=event->data[AUX_MOUSEX];
	my=event->data[AUX_MOUSEY];

	dist += (dx+dy)*steps;
	if(dist < nearclip) dist = nearclip;
	if(dist > farclip) dist = farclip;
	display();
}

void CALLBACK display(void)
{
	glClearColor(0.0,0.0,0.0,0.0);
	glClearDepth(1.0);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	glPushMatrix();
		glTranslated(0.0, 0.0, -dist);
		glRotated(inc0, 1.0, 0.0, 0.0);
		glRotated(-90.0, 1.0, 0.0, 0.0);
		glRotated(azim0, 0.0, 0.0, 1.0);

		glDisable(GL_LIGHTING);
		glDisable(GL_LIGHT0);
		glDisable(GL_COLOR_MATERIAL);
		glColor3f(1.0,1.0,1.0);
		glBegin(GL_LINES);
			glVertex2d(0.0,0.0);
			glVertex2d(0.0,6.0);
			glVertex2d(0.0,0.0);
			glVertex2d(6.0,0.0);
		glEnd();
		glColor3f(1.0,1.0,0.0);
		glPushMatrix();
			glTranslated(-2.0, 0.0, 0.0);
			glRotated(inc1, 1.0, 0.0, 0.0);
			glRotated(azim1, 0.0, 0.0, 1.0);
			glRotated(90.0, 1.0, 0.0, 0.0);
			auxWireTeapot(1.0);
		glPopMatrix();

		glEnable(GL_LIGHTING);
		glEnable(GL_LIGHT0);
		glEnable(GL_COLOR_MATERIAL);
		glLightModeli(GL_LIGHT_MODEL_TWO_SIDE ,GL_TRUE);
		glColor3f(0.0,1.0,1.0);
		glPushMatrix();
			glTranslated(2.0, 0.0, 0.0);
			glRotated(inc2, 1.0, 0.0, 0.0);
			glRotated(azim2, 0.0, 0.0, 1.0);
			//glRotated(90.0, 1.0, 0.0, 0.0);
			auxSolidCone(1.0,2.0);
		glPopMatrix();

		glColor3f(1.0,0.0,0.0);
		glPushMatrix();
			glTranslated(0.0, -2.0, 0.0);
			glBegin(GL_TRIANGLES);
				glNormal3d(0.0,0.0,1.0);
				glVertex2d(0.0,0.0);
				glVertex2d(1.0,0.0);
				glVertex2d(0.0,1.0);
			glEnd();
		glPopMatrix();

		glColor3f(0.0,0.0,1.0);
		glPushMatrix();
			glTranslated(0.0, 2.0, 0.0);
			glBegin(GL_TRIANGLES);
				glNormal3d(-1.0,-1.0,1.0);
				glVertex3d(0.0,0.0,0.0);
				glNormal3d(1.0,-1.0,1.0);
				glVertex3d(2.0,0.0,0.0);
				glNormal3d(-1.0,1.0,1.0);
				glVertex3d(0.0,2.0,0.0);
			glEnd();
		glPopMatrix();
	glPopMatrix();
	glFlush();
	auxSwapBuffers();
}

void main(void)
{
	auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA|AUX_DEPTH24);
	auxInitPosition(0,0,500,500);
	auxInitWindow("AUX_SAMPLE");
	myinit();

	printf("0 keydown means control the eye\n");
	printf("1 keydown means control the No.1 object\n");
	printf("2 keydown means control the No.2 object\n");

	auxReshapeFunc(myReshape);
	auxKeyFunc(AUX_0,On0KeyDown);
	auxKeyFunc(AUX_1,On1KeyDown);
	auxKeyFunc(AUX_2,On2KeyDown);
	auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSEDOWN, OnLButtonDown);
	auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSEUP, OnLButtonUp);
	auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSELOC, OnLButtonMove);
	auxMouseFunc(AUX_RIGHTBUTTON, AUX_MOUSEDOWN, OnRButtonDown);
	auxMouseFunc(AUX_RIGHTBUTTON, AUX_MOUSEUP, OnRButtonUp);
	auxMouseFunc(AUX_RIGHTBUTTON, AUX_MOUSELOC, OnRButtonMove);

	auxMainLoop(display);
}

⌨️ 快捷键说明

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