"OpenGL Basics"--encapsulating OpenGL functions using an object-oriented approach (i)

Source: Internet
Author: User
Tags dashed line

OpenGL is an open source graphics library that can develop two-dimensional graphics software or three-dimensional graphics software. Many well-known applications have been developed based on OpenGL, such as the famous Artoolkit and Unity3d.

Glut is the OpenGL Application Toolkit, known in English as OpenGL Utility Toolkit, a software-agnostic package that was written by Mark Kilgard at SGI. A more powerful alternative to the AUX library for hiding the complexities of different Windows system APIs. (Baidu Encyclopedia)

Since OpenGL's API is the underlying graphics library API, it's still a little more complicated to use, so I'm going to use an object-oriented approach to encapsulate the functions of OpenGL and glut libraries into a graphics class library, and learn computer graphics (this semester's Class) by the way. and object-oriented programming methods. Knowing these underlying things is also good for understanding the game engine such as Unity3d.

The use of glut, so only implemented a few simple functions, later slowly expand, this is just (a).

Although using C + + to write a class library, it is still a bit of Java, for example, all classes in a class library are subclasses of object (to implement polymorphism).

Below is Object class:

/******************************************************************** file Name: object.h* function: Declares the Object class, This class is the parent class of all classes in the GL Class library (that is, the object in Java) ********************************************************************/#ifndef _ Object_h_#define _object_h_#include <string>using std::string; #include <iostream>using std::cout;using Std::cin;using std::endl;/* class declaration */class object{public://default Constructor object () {className = "object";} Returns the class name virtual string toString () {return this->classname;} Display interface virtual void Show () {//virtual function, left to inherit to implement Polymorphic}//create object virtual void Create () {}//Delete object virtual void Destory () {}protected: String classname;//class name}; #endif


Then there is aWindowClass, is the window (before learning MFC and other languages, the most feared is the API and the choice of solutions more, so I wrote only a few functions here, after all, just to learn)

/*********************************************************** file Name: window.h* function: Window class that encapsulates Windows operations ******************* /#ifndef _window_h_#define _window_h_#include "Object.h" #include "opengl\ Glut.h "#include <vector>using std::vector;class window:protected Object {public://default constructor Window ();// Enter the window name's constructor window (string title);//Set the window name, position, and size of the constructor windows (string title, int x, int y, int width, int height);//display interface void Show ();//Create a window void create ();//Add components, which are displayed in the window in turn to void Add (Object* object);//delete object void Destory ();p rivate:string title;//Window caption int xposition, yposition;//window coordinates int width, height;//window size vector<object*> objectvector;public:// Sets the window caption void Settitle (String t);//Gets the window caption string getTitle (); int getwidth () {return this->width;} int GetHeight () {return this->height;} Sets the window position void setpostion (int x, int y);//sets the window size void setwidthandheight (int width, int height);//Determines whether the window is created bool iscreated;/ /Object name string toString () {return this->classname;} private://Drawing interface void PaiNT (void);}; #endif
The following is the implementation of the CPP file:

#include "Window.h" Window::window () {This->settitle (String ("Unnamed Window")); int x = n, y = 100,width = N, height = 600;thi S->setpostion (x, y); this->setwidthandheight (width, height); this->classname = "Window"; this->iscreated = false;} Window::window (String t) {Window (); This->settitle (t);} Window::window (String t, int x, int y, int width, int height) {Window (); This->settitle (t); this->setpostion (x, y); th Is->setwidthandheight (width, height);} void Window::settitle (String t) {this->title = t;} void window::setpostion (int x, int y) {this->xposition = X;this->yposition = y;} void Window::setwidthandheight (int w, int h) {this->width = W;this->height = h;} void Window::create () {glutinitwindowposition (this->xposition, this->yposition);//Set window position glutinitwindowsize ( This->width, this->height);//Set Window size Glutcreatewindow (THIS-&GT;TITLE.C_STR ());//Create a window named title this-> iscreated = true;gluortho2d (0.0, (double) this->getwidth (), 0.0, (double) this->getheight()); Orthogonal projection matrix}void Window::P aint (void) {int size = This->objectvector.size (); for (int i = 0; i < size; i++) {objectvector. at (i)->show ();}} void Window::show () {this->paint ();} Add components, which are displayed in the window, in turn, in Windows void Window::add (Object* object) {Objectvector.push_back (object);}  void Window::d estory () {int size = This->objectvector.size (); for (int i = 0; i < size; i++) {cout << "destory:" + objectvector.at (i)->tostring () << endl;delete objectvector.at (i); cout << "destory:" + this->tostring () << Endl;}

Then there is a colorColorclass, temporarily only supports RGB mode, from now on, I will try to write the declaration and definition in a file, just like Java.

/************************************************** file Name: Color.h function: Color class, for specifying color ************************************ /#ifndef _color_h_#define _color_h_#include "Object.h" class color:protected Object {Public:color () { Color (0, 0, 0); this->classname = "Color";} Color (int r, int g, int b) {this->r = (double) r/255;this->g = (double) g/255;this->b = (double) b/255;} Public:double R, G, B;}; #endif

And then there's the most important thing in the drawing. Point, with a point, to have everything

/********************************************************** file Name: Point.h function: Implement the coordinate point on the screen ***************************** /#ifndef _point_h#define _point_h#include "Object.h" #include "Color.h" #include " Opengl\glut.h "class point:protected Object {Public:point () {point (0, 0, Color (0, 0, 0)); this->classname = ' point ';} Point (int x, int. y) {point (x, Y, Color (0, 0, 0));} Point (int x, int y, color color) {this->x = X;this->y = Y;this->color = Color;} void Show () {Glcolor3d (color. R, color. G, color. B); Glbegin (gl_points); Glvertex2i (this->x, this->y); Glend ();} Public:int X;int Y; color color;}; #endif

The last is the most important applicationApplicationClass, this class is used to add windows and image redraw refreshes

/********************************************************** file name: application.h* feature: Used to create an application ********************** /#ifndef _application_h_#define _application_h_#include "Object.h" #include " Opengl\glut.h "#include" Window.h "#include <vector>using std::vector;class application:protected Object {public : Application () {className = "application";} ~application () {destory ();} Static vector<window> windowvector;//windows//Initialize glutstatic void init (int argc, char *argv[]) {Glutinit (& ARGC, argv);//Set display mode Glutinitdisplaymode (Glut_rgb | Glut_single);} Add component static bool Add (Window object) {if (object.iscreated) {Windowvector.push_back (object); return true;} return false;} void Show () {if (windowvector.size () > 0) {glutdisplayfunc (&paint);//Enter the display loop (without this sentence The program execution ends) Glutmainloop ();} Paint ();} void Destory () {for (int i = 0; i < windowvector.size (); i++) {windowvector[i].destory ();} cout << "destory:" + this->tostring () <<endl;} private:static void PaInt () {glclear (gl_color_buffer_bit);//Clear window screen for (int i = 0; i < windowvector.size (); i++) {windowvector[i].show ();} Glflush ();//Refresh Drawing command}};vector<window> application::windowvector; #endif

Here is a test file that only draws a purple dashed line:

#include "Window.h" #include "Color.h" #include "Point.h" #include "Application.h"//Hide Console window #pragma comment (linker, "/ Subsystem:\ "Windows\"/entry:\ "Maincrtstartup\" ") int main (int argc, char* argv[]) {int w = N, h = 300; Window window (String ("Hello"), Window.create, W, h); point* p;for (int i = 0; i <; i++) {p = new Point (200,10+4*i,color (255,0,255)); Window.add ((object*) p);} application* app = new Application (), App->init (argc, argv), App->add (window), app->show ();d elete App;return 0;}

Out of the effect such as:



There is a purple dotted line in the middle of the black screen.

Today's code leaks a lot of important things, such as window background color settings, and later with the study of the slowly deepening, will be added.

"OpenGL Basics"--encapsulating OpenGL functions using an object-oriented approach (i)

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.