Recently, I was reading the fifth version of OpenGL super baodian. The system is Ubuntu and I want to use codeblocks to run the examples in the book. Many problems have been solved on the way. Now I want to share the steps below:
1. Create a basic compilation environment
Sudo apt-getinstall build-essential
2. Install OpenGL library
Sudo apt-getinstall libgl1-mesa-dev
3. Install OpenGL utilities
Sudo apt-getinstall libglu1-mesa-dev
4. Install OpenGL utility Toolkit
Sudo apt-Get installfreeglut3-dev
5. Install codeblocks
Sudo apt-Get install codeblocks
6. Create a codeblocks Project
Create a console application project --> select C ++ language. For example, the project name is helloworld.
7. Contains link libraries related to OpenGL/glut
Project à build options à linkersettings
Add file: libgl. So libglut. So libglu. sO
[1 ~ 7 steps can refer to the blog http://blog.csdn.net/jarvischu/article/details/8226938, 6th Step 1 must establish the console application and C ++ language]
8. Download freeglutandgltools
Address: http://download.csdn.net/detail/xhz1234/7707213,
. Unzip the downloaded gltoolsandfreeglut.tar. xz to the freeglut-2.6.0 and gltools, and copy these two directories and their files to the new project helloworld directory.
9. Add instance code
Take sphereworld4 of OpenGL (version 5th) as an example to copy the content of sphereworld4.cpp to the main of the new project. CPP. delete CPP, directly copy sphereworld4.cpp to the helloworld project), and then add these files to codeblocks.
10. Modify sphereworld4.cpp
The code is in red as follows:
// SphereWorld.cpp// OpenGL SuperBible// New and improved (performance) sphere world// Program by Richard S. Wright Jr.// SphereWorld.cpp// OpenGL SuperBible// New and improved (performance) sphere world// Program by Richard S. Wright Jr.<span style="color:#FF0000;">#include "./GLTools/include/GL/glew.h"#include "./GLTools/include/GLTools.h"#include "./GLTools/include/GLShaderManager.h"#include "./GLTools/include/GLFrustum.h"#include "./GLTools/include/GLBatch.h"#include "./GLTools/include/GLMatrixStack.h"#include "./GLTools/include/GLGeometryTransform.h"#include "./GLTools/include/GLTools.h"#include "./GLTools/include/StopWatch.h"</span>#include <math.h>#include <stdio.h>#ifdef __APPLE__#include <glut/glut.h>#else#define FREEGLUT_STATIC#include <GL/glut.h>#endif#define NUM_SPHERES 50GLFrame spheres[NUM_SPHERES];GLShaderManagershaderManager;// Shader ManagerGLMatrixStackmodelViewMatrix;// Modelview MatrixGLMatrixStackprojectionMatrix;// Projection MatrixGLFrustumviewFrustum;// View FrustumGLGeometryTransformtransformPipeline;// Geometry Transform PipelineGLTriangleBatchtorusBatch;GLBatchfloorBatch;GLTriangleBatch sphereBatch;GLFrame cameraFrame;//////////////////////////////////////////////////////////////////// This function does any needed initialization on the rendering// context.void SetupRC() {// Initialze Shader ManagershaderManager.InitializeStockShaders();glEnable(GL_DEPTH_TEST);glClearColor(0.0f, 0.0f, 0.0f, 1.0f);// This makes a torusgltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30); // This make a sphere gltMakeSphere(sphereBatch, 0.1f, 26, 13);floorBatch.Begin(GL_LINES, 324); for(GLfloat x = -20.0; x <= 20.0f; x+= 0.5) { floorBatch.Vertex3f(x, -0.55f, 20.0f); floorBatch.Vertex3f(x, -0.55f, -20.0f); floorBatch.Vertex3f(20.0f, -0.55f, x); floorBatch.Vertex3f(-20.0f, -0.55f, x); } floorBatch.End(); // Randomly place the spheres for(int i = 0; i < NUM_SPHERES; i++) { GLfloat x = ((GLfloat)((rand() % 400) - 200) * 0.1f); GLfloat z = ((GLfloat)((rand() % 400) - 200) * 0.1f); spheres[i].SetOrigin(x, 0.0f, z); } }///////////////////////////////////////////////////// Screen changes size or is initializedvoid ChangeSize(int nWidth, int nHeight) {glViewport(0, 0, nWidth, nHeight); // Create the projection matrix, and load it on the projection matrix stackviewFrustum.SetPerspective(35.0f, float(nWidth)/float(nHeight), 1.0f, 100.0f);projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix()); // Set the transformation pipeline to use the two matrix stackstransformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix); }// Called to draw scenevoid RenderScene(void){ // Color values static GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f}; static GLfloat vTorusColor[] = { 1.0f, 0.0f, 0.0f, 1.0f }; static GLfloat vSphereColor[] = { 0.0f, 0.0f, 1.0f, 1.0f }; // Time Based animationstatic CStopWatchrotTimer;float yRot = rotTimer.GetElapsedSeconds() * 60.0f;// Clear the color and depth buffersglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Save the current modelview matrix (the identity matrix)modelViewMatrix.PushMatrix(); M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.PushMatrix(mCamera); // Transform the light position into eye coordinates M3DVector4f vLightPos = { 0.0f, 10.0f, 5.0f, 1.0f }; M3DVector4f vLightEyePos; m3dTransformVector4(vLightEyePos, vLightPos, mCamera);// Draw the groundshaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vFloorColor);floorBatch.Draw(); for(int i = 0; i < NUM_SPHERES; i++) { modelViewMatrix.PushMatrix(); modelViewMatrix.MultMatrix(spheres[i]); shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vLightEyePos, vSphereColor); sphereBatch.Draw(); modelViewMatrix.PopMatrix(); } // Draw the spinning Torus modelViewMatrix.Translate(0.0f, 0.0f, -2.5f); // Save the Translation modelViewMatrix.PushMatrix(); // Apply a rotation and draw the torus modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vLightEyePos, vTorusColor); torusBatch.Draw(); modelViewMatrix.PopMatrix(); // "Erase" the Rotation from before // Apply another rotation, followed by a translation, then draw the sphere modelViewMatrix.Rotate(yRot * -2.0f, 0.0f, 1.0f, 0.0f); modelViewMatrix.Translate(0.8f, 0.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vLightEyePos, vSphereColor); sphereBatch.Draw();// Restore the previous modleview matrix (the identity matrix)modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); // Do the buffer Swap glutSwapBuffers(); // Tell GLUT to do it again glutPostRedisplay(); }// Respond to arrow keys by moving the camera frame of referencevoid SpecialKeys(int key, int x, int y) {float linear = 0.1f;float angular = float(m3dDegToRad(5.0f));if(key == GLUT_KEY_UP)cameraFrame.MoveForward(linear);if(key == GLUT_KEY_DOWN)cameraFrame.MoveForward(-linear);if(key == GLUT_KEY_LEFT)cameraFrame.RotateWorld(angular, 0.0f, 1.0f, 0.0f);if(key == GLUT_KEY_RIGHT)cameraFrame.RotateWorld(-angular, 0.0f, 1.0f, 0.0f); }int main(int argc, char* argv[]) {gltSetWorkingDirectory(argv[0]); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(800,600); glutCreateWindow("OpenGL SphereWorld"); glutSpecialFunc(SpecialKeys); glutReshapeFunc(ChangeSize); glutDisplayFunc(RenderScene); GLenum err = glewInit(); if (GLEW_OK != err) { fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); return 1; } SetupRC(); glutMainLoop(); return 0; }
11. Compile and run the program as follows::
The body is over!
Appendix:
1) OpenGL super collection code: http://www.starstonesoftware.com/files/
You can directly use the code of Apsara stack. The Code cannot be compiled in codeblocks and can run normally through the above steps.
2) there will be many warnings during compilation, which are divided into two types:
2-1)
||=== Build: Debug in TestForLove2 (compiler: GNU GCC Compiler) ===|/home/xhz/Program/TestForLove2/GLTools/src/GLTools.cpp||In function ‘GLbyte* gltReadBMPBits(const char*, int*, int*)’:|/home/xhz/Program/TestForLove2/GLTools/src/GLTools.cpp|1062|warning: converting ‘false’ to pointer type ‘GLbyte* {aka signed char*}’ [-Wconversion-null]|/home/xhz/Program/TestForLove2/GLTools/src/GLTools.cpp|1074|warning: converting ‘false’ to pointer type ‘GLbyte* {aka signed char*}’ [-Wconversion-null]|||=== Build finished: 0 error(s), 2 warning(s) (0 minute(s), 3 second(s)) ===|
Solution: change the value of false in the corresponding position of gltools. cpp to null.
2-2)
||=== Build: Debug in TestForLove2 (compiler: GNU GCC Compiler) ===|/home/xhz/Program/TestForLove2/GLTools/src/../include/GLBatch.h||In constructor ‘GLBatch::GLBatch()’:|/home/xhz/Program/TestForLove2/GLTools/src/../include/GLBatch.h|127|warning: ‘GLBatch::nNumTextureUnits’ will be initialized after [-Wreorder]|/home/xhz/Program/TestForLove2/GLTools/src/../include/GLBatch.h|126|warning: ‘GLuint GLBatch::nNumVerts’ [-Wreorder]|/home/xhz/Program/TestForLove2/GLTools/src/GLBatch.cpp|57|warning: when initialized here [-Wreorder]|/home/xhz/Program/TestForLove2/GLTools/src/../include/GLBatch.h|135|warning: ‘GLBatch::pTexCoords’ will be initialized after [-Wreorder]|/home/xhz/Program/TestForLove2/GLTools/src/../include/GLBatch.h|119|warning: ‘GLuint GLBatch::uiVertexArray’ [-Wreorder]|/home/xhz/Program/TestForLove2/GLTools/src/GLBatch.cpp|57|warning: when initialized here [-Wreorder]|/home/xhz/Program/TestForLove2/GLTools/src/../include/GLBatch.h|129|warning: ‘GLBatch::bBatchDone’ will be initialized after [-Wreorder]|/home/xhz/Program/TestForLove2/GLTools/src/../include/GLBatch.h|125|warning: ‘GLuint GLBatch::nVertsBuilding’ [-Wreorder]|/home/xhz/Program/TestForLove2/GLTools/src/GLBatch.cpp|57|warning: when initialized here [-Wreorder]|/home/xhz/Program/TestForLove2/GLTools/src/../include/GLBatch.h|125|warning: ‘GLBatch::nVertsBuilding’ will be initialized after [-Wreorder]|/home/xhz/Program/TestForLove2/GLTools/src/../include/GLBatch.h|122|warning: ‘GLuint* GLBatch::uiTextureCoordArray’ [-Wreorder]|/home/xhz/Program/TestForLove2/GLTools/src/GLBatch.cpp|57|warning: when initialized here [-Wreorder]|||=== Build finished: 0 error(s), 12 warning(s) (0 minute(s), 4 second(s)) ===|
The reason for the above alarm is:
The initialization sequence of the variable in the constructor is inconsistent with that defined in the class glbatch.
The order of the member variables in the glbatch definition in the Code is as follows:
class GLBatch : public GLBatchBase{ ... protected:GLenumprimitiveType;// What am I drawing.... GLuintuiVertexArray;GLuint uiNormalArray;GLuintuiColorArray;GLuint*uiTextureCoordArray;GLuintvertexArrayObject; GLuint nVertsBuilding;// Building up vertexes counter (immediate mode emulator) GLuint nNumVerts;// Number of verticies in this batch GLuint nNumTextureUnits;// Number of texture coordinate sets boolbBatchDone;// Batch has been built M3DVector3f *pVerts;M3DVector3f *pNormals;M3DVector4f *pColors;M3DVector2f **pTexCoords;};
The class initialization list is defined as follows:
GLBatch::GLBatch(void): nNumTextureUnits(0), nNumVerts(0), pVerts(NULL), pNormals(NULL), pColors(NULL), pTexCoords(NULL), uiVertexArray(0),uiNormalArray(0), uiColorArray(0), vertexArrayObject(0), bBatchDone(false), nVertsBuilding(0), uiTextureCoordArray(NULL)
To eliminate the above alarms, you only need to unify the order of the two. I changed the order of the member variables in glbatch.
The above changes are synchronized to freeglutandgltools, address: the corresponding file in the compressed package of the http://download.csdn.net/detail/xhz1234/7707213.