下面代码是网上叫如何用鼠标旋转画面的。
我只修改了圆柱那句话,和加入了一小段addlight子程序显示光线效果。
#define PI_OVER_360 3.1415926535897932384626433832795028841971694/360.
#define PI_OVER_180 3.1415926535897932384626433832795028841971694/180.
#include <iostream>
#include <stdlib.h> //Needed for "exit" function
#include <math.h>
#include <stdio.h>
//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <GL/glext.h>
const int MaxNumberOfVertex = 5000;
float vert[MaxNumberOfVertex][3];
int ndata=0;
static int g_xClick, g_yClick,g_sClick;
bool g_Button1Down;
static float ViewMatrx[16]={1.,0.,0.,0., 0.,1.,0.,0., 0.,0.,1.,0., 0.,0.,0.,1};
static float ViewMatrx0[16]={1.,0.,0.,0., 0.,1.,0.,0., 0.,0.,1.,0., 0.,0.,0.,1};
static float dViewMatrx[16]={1.,0.,0.,0., 0.,1.,0.,0., 0.,0.,1.,0., 0.,0.,0.,1};
static float Emtx [16]= {1.,0.,0.,0., 0.,1.,0.,0., 0.,0.,1.,0., 0.,0.,0.,1};
static int mouseX0=0, mouseY0=0;
using namespace std;
//Called when a key is pressed
void handleKeypress(unsigned char key, //The key that was pressed
int x, int y) { //The current mouse coordinates
switch (key) {
case 27: //Escape key
exit(0); //Exit the program
}
}
//Initializes 3D rendering
void initRendering() {
//Makes 3D drawing work when something is in front of something else
glEnable(GL_DEPTH_TEST);
}
void BuildPerspProjMat(float *m, float fov, float aspect,
float znear, float zfar)
{
float xymax = znear * tan(fov * PI_OVER_360);
float ymin = -xymax;
float xmin = -xymax;
float width = xymax - xmin;
float height = xymax - ymin;
float depth = zfar - znear;
float q = -(zfar + znear) / depth;
float qn = -2 * (zfar * znear) / depth;
float w = 2 * znear / width;
w = w / aspect;
float h = 2 * znear / height;
m[0] = w;
m[1] = 0;
m[2] = 0;
m[3] = 0;
m[4] = 0;
m[5] = h;
m[6] = 0;
m[7] = 0;
m[8] = 0;
m[9] = 0;
m[10] = q;
m[11] = -1;
m[12] = 0;
m[13] = 0;
m[14] = qn;
m[15] = 0;
}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float ratio=float(w)/h;
glOrtho(-1.0*ratio, 1.0*ratio, -1.0, 1.0, -10, 10.0);
// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
}
//Called when the window is resized
void handleResize(int w, int h) {
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity(); //Reset the camera
float m[16] = {0};
float fov = 45.f;
// aspect s x/y ratio
float aspect = 1.f;
aspect = float(w)/h;
printf("%i %i,aspect=%f\n",h,w,aspect);
float znear=1.0f, zfar=200.f;
BuildPerspProjMat(m, fov, aspect, znear, zfar);
glLoadMatrixf(m);
}
void addLight() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0); //Enable light #0
glEnable(GL_LIGHT1); //Enable light #1
glEnable(GL_LIGHT2); //Enable light #1
glEnable(GL_NORMALIZE); //Automatically normalize normals
GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
//glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 30.0f);
glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, ambientColor);
//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//Add positioned light
GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 0.9f}; //Color (0.5, 0.5, 0.5)
GLfloat lightPos0[] = {4.0f, 3.0f, 5.0f, 1.0f}; //Positioned at (4, 0, 8)
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
//Add directed light
GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)
//Coming from the direction (-1, 0.5, 0.5)
GLfloat lightPos1[] = {1.0f, -0.8f, 0.5f, 0.0f};
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
return ;
}
float angleX = 0.0f;
float angleY = 0.0f;