OpenGL Template Mac Cmake OpenGL (Glut) Template, cmakeglut

Source: Internet
Author: User

OpenGL Template Mac Cmake OpenGL (Glut) Template, cmakeglut

I often use some of the features to make a template, with a light effect, you can use the mouse to zoom, rotate the coordinate system around the origin


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})

In the directory where Main. cpp and CMakeLists.txt are located, open the terminal and execute

Mkdir build

Cd build

Cmake-G "Unix Makefiles "..

Make

./OpenGL


Opengl Construction

No OpenGL. h,
Gl/gl. h is actually OpenGL. h,
In fact, VS2008 already comes with it and does not need to be downloaded.
You 'd better download GLUT,

Downloading and installing the glut library glut of the GLUT library opengl is not required by OpenGL, but it will bring some convenience to our learning and is recommended for installation. GLUT in Windows: (about 150 k) www.opengl.org/...ta.zip
To install GLUT in Windows:
1. Unpack the downloaded package and obtain 5 files.
2. Search for "gl. h" in "My Computer" and find its folder (Program Files \ Microsoft Visual Studio \ VC98 \ Include \ GL folder "). Put the extracted glut. h in this folder.
3. Put the extracted glut. lib and glu32.lib in the folder where the static function library is located (Program Files \ Microsoft Visual Studio \ VC98 \ lib ).
4. Put the extracted glut. dll and glu32.dll In the system32 folder under the operating system directory. (Typical location: C: \ Windows \ System32)

How does glut opengl obtain the size of the current window?

Please refer to this tutorial: blog.csdn.net/..227143.
Hoxily said: I am a Hoxily.
------------------------------------------------
GLUT defines which function should be called when the window size changes. In addition, this function will be called when the window is created for the first time to ensure that the rendering will not be deformed when the initialization window is not a square.

This function is glureshapefunc ().

Void glureshapefunc (void (* func) (int width, int height ));

Parameters:

Func: the name of the function responsible for setting the correct projection.
----------------------------------
Therefore, the first thing we must do is return to the main () function. In the code in the previous chapter, add the call to the glureshapefunc (). Let's call the function responsible for the window size changeSize. The current Code is as follows.

Void main (int argc, char ** argv ){
Gluinit (& argc, argv );
Fig );
Gluinitwindowposition (100,100 );
Gluinitwindowsize (320,320 );
Glucreatewindow ("3D Tech-GLUT Tutorial ");
Gludisplayfunc (renderScene );

// Here is our new entry in the main function
Glureshapefunc (changeSize );

Glumainloop ();
}
What we need to do below is to define the function changeSize (). The Declaration of the glureshapefunc () function shows that the changSize () function has two parameters .. These two parameters represent the new window height and width.

Void changeSize (int w, int h ){

// Prevent the divisor from being 0 in height
// (You can set the window width to 0 ).
If (h = 0)
H = 1;

Float ratio = 1.0 * w/h;

// Unitized projection matrix.
GlMatrixMode (GL_PROJECTION );
GlLoadIdentity ();

// Set the window size to increase the window size
GlViewport (0, 0, w, h );

// Set the correct projection matrix
GluPerspe ...... remaining full text>

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.