自己经常使用的一些功能做一个模板,有灯光效果,你可以用鼠标放大,围绕所述旋转坐标系的原点
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHlhbmcxOTg5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" >
Main.cpp
#include <GLUT/glut.h> #include <cstdlib> /* for exit */ #include <vector> using namespace std; float zoom = 2000.f; float rotx = 20; float roty = 0; float tx = 0; float ty = 0; int lastx=0; int lasty=0; unsigned char Buttons[3] = {0}; float lightPosition[] = { -200, 300, 300, 1.0f}; float ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f }; float diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f }; float specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f }; void DrawCoordinate(float _flengthX, float _flengthY, float _flengthZ) { glLineWidth(5); glBegin(GL_LINES); glColor3f(1,0,0); glVertex3f(0,0,0); glVertex3f(_flengthX,0,0); glEnd(); glBegin(GL_LINES); glColor3f(0,1,0); glVertex3f(0,0,0); glVertex3f(0,_flengthY,0); glEnd(); glBegin(GL_LINES); glColor3f(0,0,1); glVertex3f(0,0,0); glVertex3f(0,0,_flengthZ); glEnd(); } void Init() { glShadeModel(GL_SMOOTH); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glEnable(GL_NORMALIZE); } void Reshape(int w, int h) { // prevent divide by 0 error when minimised if(w==0) h = 1; glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45,(float)w/h,0.1,5000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } //------------------------------------------------------------------------------- // void Motion(int x,int y) { int diffx=x-lastx; int diffy=y-lasty; lastx=x; lasty=y; if( Buttons[2] ) { zoom -= (float) 1* diffx*2; } else if( Buttons[0] ) { rotx += (float) 1 * diffy; roty += (float) 1 * diffx; } else if( Buttons[1] ) { tx += (float) 1 * diffx; ty -= (float) 1 * diffy; } glutPostRedisplay(); } //------------------------------------------------------------------------------- // void Mouse(int b,int s,int x,int y) { lastx=x; lasty=y; switch(b) { case GLUT_LEFT_BUTTON: Buttons[0] = ((GLUT_DOWN==s)?1:0); break; case GLUT_MIDDLE_BUTTON: Buttons[1] = ((GLUT_DOWN==s)?1:0); break; case GLUT_RIGHT_BUTTON: Buttons[2] = ((GLUT_DOWN==s)?1:0); break; default: break; } glutPostRedisplay(); } //------------------------------------------------------------------------------- /// void Display() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0,0,-zoom); glTranslatef(tx,ty,0); glRotatef(rotx,1,0,0); glRotatef(roty,0,1,0); glColor3f(1.0f,1.0f,1.0f); // draw grid glLineWidth(2); glBegin(GL_LINES); for(int i=-1000;i<=1000;i+=100) { glVertex3f(i,0,-1000); glVertex3f(i,0,1000); glVertex3f(1000,0,i); glVertex3f(-1000,0,i); } glEnd(); glLightfv(GL_LIGHT1, GL_AMBIENT, ambientLight); glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuseLight); glLightfv(GL_LIGHT1, GL_SPECULAR, specularLight); glLightfv(GL_LIGHT1, GL_POSITION, lightPosition); glEnable(GL_LIGHT1); glEnable(GL_LIGHTING); glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE); glEnable(GL_COLOR_MATERIAL); glColor3f(0, 1, 0); glPushMatrix(); glTranslatef(0, 200, 0); glutSolidSphere(200, 50, 50); glPopMatrix(); DrawCoordinate(1000,1000,1000); glDisable( GL_LIGHTING ); glDisable(GL_COLOR_MATERIAL); glutPostRedisplay(); glutSwapBuffers(); } void Keyboard(unsigned char key, int x, int y) { switch(key) { case 'q': case 'Q': case 27: // ESC key exit(0); break; } } int main(int argc,char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH); glutInitWindowSize(640,480); glutInitWindowPosition(100,100); glutCreateWindow("GLUT Framework"); glutDisplayFunc(Display); glutReshapeFunc(Reshape); glutMouseFunc(Mouse); glutMotionFunc(Motion); glutKeyboardFunc(Keyboard); Init(); glutMainLoop(); return 0; }
CMakeLists.txt
cmake_minimum_required (VERSION 2.6) project(OpenGL) find_package(OpenGL REQUIRED) include_directories(${OPENGL_INCLUDE_DIR}) find_package(GLUT REQUIRED) include_directories(${GLUT_INCLUDE_DIR}) set(CMAKE_CXX_FLAGS "-g -Wall") add_executable(OpenGL Main.cpp) target_link_libraries(OpenGL ${GLUT_LIBRARY} ${OPENGL_LIBRARY})
在Main.cpp 和 CMakeLists.txt所在文件夹 打开终端 依次运行
mkdir build
cd build
cmake -G "Unix Makefiles" ..
make
./OpenGL
版权声明:本文博主原创文章,博客,未经同意不得转载。
时间: 2024-10-26 22:57:18