This example demonstrates how to get started using OpenGL with biicode. Freeglut is an open source alternative to the OpenGL Utility Toolkit (GLUT) library. It allows you to create and manage windows containing OpenGL contexts on a wide range of platforms, and dealing with user input from mouse, keyboard and joystick devices. You can visit the following pages to learn more about OpenGL and GLUT (and hence freeglut):
First, create a new project as described in the hello world example, using the following options:
~$ cd cpp_freeglut_project
~/cpp_freeglut_project$ bii new anonymous/cpp_freeglut --hello=cpp
This is some example code that makes use of GLUT functions. Note that you must include a reference to a biicode GLUT wrapper library. This is the only information biicode needs to fetch the required files when needed, taking into account your actual development platform. We’ll see how this happens in the next step.
Now, simply put the following code into your cpp_freeglut
block folder (you can also download these files here: sphere-glut.zip
, unzip and copy them into your block folder):
sphere.h
#pragma once
#include "glui/glutwrapper/glut.h"
class Sphere{
public:
Sphere(float _radio=1.0, int _slices=20, int _stacks=20, bool _solid=true);
virtual ~Sphere(); //default virtual destructor
void draw();
void setPosition(float _x=0.0, float _y=0.0, float _z=0.0){
x = _x; y = _y; z = _z;
}
void setColor(unsigned char r=255, unsigned char g=255, unsigned char b=255){
red = r; green = g; blue = b;
}
private:
float radius;
int slices;
int stacks;
unsigned char red, green, blue;
float x,y,z;
bool solid;
};
sphere.cpp
#include "sphere.h"
Sphere::Sphere(float _radius, int _slices, int _stacks, bool _solid):
radius(_radius),slices(_slices),stacks(_stacks),solid(_solid){
setPosition();
setColor();
}
Sphere::~Sphere(){}
void Sphere::draw(){
glColor3ub(red,green,blue);
glTranslatef(x,y,z);
if (solid)
glutSolidSphere(radius, slices, stacks);
else
glutWireSphere(radius, slices, stacks);
glTranslatef(-x,-y,-z);
}
main.cpp
#include "sphere.h"
//Functions declarations
void Init();
void OnDraw();
void Init(){
//Initialize GLUT windows manager
//Create the window
int argc=1;
char* argv[1]={"Glut Application"};
glutInit(&argc, argv);
glutInitWindowSize(800,600);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("My World");
//enable lights and define perspective
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glMatrixMode(GL_PROJECTION);
gluPerspective( 40.0, 800/600.0f, 0.1, 150);
}
void OnDraw(void){
//cleaning the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Define view
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// eye position -> (0.0, 10, 20)
// target -> (0,0,0)
// define positive Y axis -> (0.0, 1.0, 0.0)
gluLookAt(0.0, 10, 20,
0.0, 0, 0.0,
0.0, 1.0, 0.0);
//Put your code here to draw objects
Sphere sphere1;
sphere1.draw();
//no delete this line
glutSwapBuffers();
}
int main(int argc, char** argv) {
Init();
//Enter the callbacks
glutDisplayFunc(OnDraw);
glutMainLoop(); // begin the loop
return 0;
}
Now is when the biicode magic takes place. The previous code needs to link with some library implementing the drawing functions being used. You can resolve all missing dependencies for your platform using the bii find
command. From your project’s folder location execute:
$ bii find
This will find and retrieve all missing files from the biicode servers to your local filesystem, under the deps
folder of your project. You should see a success message confirming that all dependencies have been resolved.
Now it is time to see the result of the previous steps. You can build your main file with the bii cpp:run
command and and run the executable inside the bin folder.
$ bii build
$ cd bin
$ #run your executable
That’s it! You should see a new window named “My World”, containing a single white sphere in its center. Just like this!:
If you experience any difficulties during the coding process, or get any errors during the program execution, visit our forum and feel free to ask any questions.