OpenGL templates Mac Cmake OpenGL (Glut) template

Source: Internet
Author: User

Some of the features that you often use to make a template, have a light effect, you can zoom in with the mouse, around the origin point of the rotating coordinate system

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};f Loat lightposition[] = { -200, +, 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 _fle    Ngthy, 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 (w/h,0.1,5000, (float));    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://ESC            Key exit (0);    Break    }}int Main (int argc,char** argv) {glutinit (&AMP;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})

Open the terminal in the folder where Main.cpp and CMakeLists.txt are running

mkdir Build

CD Build

Cmake-g "Unix makefiles".

Make

./opengl

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

OpenGL templates Mac Cmake OpenGL (Glut) template

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.