Basic Analysis of OpenGL programming in Ubuntu

Source: Internet
Author: User
This article focuses on how to implement basic OpenGL programming in Ubuntu. The main steps are as follows: 1. First of all, you must install some basic libraries and OpenGL files, mainly install the following two on the OK basic compilation Library: build-essentialsudoapt-getinstallbuild-essentialOpenGL: freeglut3-devsudoapt-getinstallfree

This article focuses on how to implement basic OpenGL programming in Ubuntu. The main steps are as follows:

1: first of all, of course, you need to install some basic compiling library and OpenGL library files. It is okay to install the following two files:

Basic compilation Library: build-essential

Sudo apt-get install build-essential

OpenGL tool Library: freeglut3-dev

Sudo apt-get install freeglut3-dev

The above is my installation prompt on the virtual machine, you can see that in addition to freeglut3, there are other additional software packages installed.

2: The second step, of course, is to write the code. You don't need to talk about it. You can post it yourself and study it if you have time.

/* Rotating cube with color interpolation */

# Include
# Include
 
GLfloat vertices [] [3] = {-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0 },
{1.0, 1.0,-1.0}, {-1.0, 1.0,-1.0}, {-1.0,-1.0, 1.0 },
{1.0,-1.0, 1.0}, {1.0, 1.0, 1.0}, {-1.0, 1.0, 1.0 }};

GLfloat normals [] [3] = {-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0 },
{1.0, 1.0,-1.0}, {-1.0, 1.0,-1.0}, {-1.0,-1.0, 1.0 },
{1.0,-1.0, 1.0}, {1.0, 1.0, 1.0}, {-1.0, 1.0, 1.0 }};

GLfloat colors [] [3] = {0.0, 0.0, 0.0}, {1.0, 0.0, 0.0 },
{1.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0 },
{1.0, 0.0, 1.0 },{ 1.0, 1.0, 1.0 },{ 0.0, 1.0, 1.0 }};


Void polygon (int a, int B, int c, int d)
{
GlBegin (GL_POLYGON );
GlColor3fv (colors [a]);
GlNormal3fv (normals [a]);
GlVertex3fv (vertices [a]);
GlColor3fv (colors [B]);
GlNormal3fv (normals [B]);
GlVertex3fv (vertices [B]);
GlColor3fv (colors [c]);
GlNormal3fv (normals [c]);
GlVertex3fv (vertices [c]);
GlColor3fv (colors [d]);
GlNormal3fv (normals [d]);
GlVertex3fv (vertices [d]);
GlEnd ();
}
 
 
 
Void colorcube (void)
{
/* Map vertices to faces */

Polygon (0, 3, 2, 1 );
Polygon (2, 3, 7, 6 );
Polygon (0, 4, 7, 3 );
Polygon (1, 2, 6, 5 );
Polygon (4,5, 6,7 );
Polygon (0, 1, 5, 4 );
}

Static GLfloat theta [] = {0.0, 0.0, 0.0 };
Static GLint axis = 2;

Void display (void)
{
/* Display callback, clear frame buffer and z buffer,
Rotate cube and draw, swap buffers */

GlClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
GlLoadIdentity ();
GlRotatef (theta [0], 1.0, 0.0, 0.0 );
GlRotatef (theta [1], 0.0, 1.0, 0.0 );
GlRotatef (theta [2], 0.0, 0.0, 1.0 );
Colorcube ();
GlFlush ();
Gluswapbuffers ();
}
 
 

Void spinCube ()
{
/* Idle callback, spin cube 2 degrees about selected axis */
Theta [axis] + = 2.0;
If (theta [axis] & gt; 360.0) theta [axis]-= 360.0;
Display ();
}
 
 
 
Void mouse (int btn, int state, int x, int y)
{
/* Mouse callback, selects an axis about which to rotate */
If (btn = maid & state = maid) axis = 0;
If (btn = maid) axis = 1;
If (btn = maid & state = maid) axis = 2;
}
 
Void myReshape (int w, int h)
{
GlViewport (0, 0, w, h );
GlMatrixMode (GL_PROJECTION );
GlLoadIdentity ();
If (w <= h)
{
GlOrtho (-2.0, 2.0,-2.0 * (GLfloat) h/(GLfloat) w,
2.0 * (GLfloat) h/(GLfloat) w,-10.0, 10.0 );
}
Else
{
GlOrtho (-2.0 * (GLfloat) w/(GLfloat) h,
2.0 * (GLfloat) w/(GLfloat) h,-2.0, 2.0,-10.0, 10.0 );
}
 
GlMatrixMode (GL_MODELVIEW );
}
 

Main (int argc, char ** argv)
{
Gluinit (& argc, argv );
/* Need both double buffering and z buffer */

Fig );
Gluinitwindowsize (500,500 );
Ngcreatewindow ("colorcube ");
Glureshapefunc (myReshape );
Gludisplayfunc (display );
GlutIdleFunc (spinCube );
Glumousefunc (mouse );
Glable (GL_DEPTH_TEST);/* Enable hidden -- surface -- removal */
Glumainloop ();
 
Return 0;
 
}

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.