- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <Windows.h>
- #include <gl/glew.h>
- #include <gl/GL.h>
- #include <gl/GLU.h>
- #include <gl/glut.h>
- static void init(void);
- static void display(void);
- static void init();
- static void mousedo(int buttton,int state,int x,int y);
- static void motiondo(int x,int y);
- static void keyboarddo(unsigned char key,int x,int y);
- static float _x,_y;
- static GLfloat linear[3] = { 0.0, 0.12, 0.0 };
- GLfloat spot_direction[4]={0.0,0.0,1.0};
- GLfloat position[4]={0,0,-1,1};
- static int angle=0;
- int main(int argc,char **argv)
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
- glutInitWindowSize(400,400);
- glutInitWindowPosition(100,100);
- glutCreateWindow("window");
- init();
- glutDisplayFunc(display);
- glutKeyboardFunc(keyboarddo);
- glutMainLoop();
- return 0;
- }
- void init()
- {
- GLfloat ambient[4]={0.4,0.4,0.4,1};
- GLfloat specular[4]={1,1,1,1};
- GLfloat diffuse[4]={1,1,0,1};
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glLightfv(GL_LIGHT0,GL_POSITION,position);
- glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);
- glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
- glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);
- //一定要注意调整聚光灯的方向,默认是指向屏幕外面,所以开聚光灯会出现黑屏
- glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spot_direction);
- glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,20);
- glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,1);
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambient);
- glewInit();
- //启用混合,不启用混合不使用反走样,可以使用多重采样,但是我的显卡不支持
- glEnable(GL_BLEND);
- //启用点反走样
- glEnable(GL_POINT_SMOOTH);
- //设置反走样提示
- glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
- //设置混合参数
- glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
- //启动规范化法线
- glEnable(GL_NORMALIZE);
- }
- void display(void)
- {
- float i=-1;
- float j=-1;
- glPointSize(10);
- glClear(GL_COLOR_BUFFER_BIT);
- glEnableClientState(GL_NORMAL_ARRAY);
- glEnableClientState(GL_VERTEX_ARRAY);
- glColor3f(1,1,1);
- glBegin(GL_POINTS);
- for(;i<1;i+=0.1)
- {
- for(j=-1;j<1;j+=0.1)
- {
- glNormal3f(i,j,-1);
- glVertex3f(i,j,0);
- }
- }
- glEnd();
- glutSwapBuffers();
- }
- void keyboarddo(unsigned char key,int x,int y)
- {
- switch(key)
- {
- case 'a':
- position[0]-=0.1;
- break;
- case 'd':
- position[0]+=0.1;
- break;
- case 'w':
- position[1]+=0.1;
- break;
- case 's':
- position[1]-=0.1;
- break;
- }
- glLightfv(GL_LIGHT0,GL_POSITION,position);
- glutPostRedisplay();
- }
- //该片段来自于http://www.codesnippet.cn/detail/1609201410424.html
来源: http://www.codesnippet.cn/detail/1609201410424.html