1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#include <QtGui/QtGui>
#include <QtOpenGL/QtOpenGL>
#include <math.h>
#include "globj.h"
GLobj::GLobj(QWidget *parent)
: QGLWidget(parent)
{
}
GLobj::~GLobj()
{
}
//Initialize the GL settings
void GLobj::initializeGL()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(5.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glPointSize(2.0);
glLineWidth(4.0);
}
//Set up the viewport based on the screen dimentions
//Function is called implicitly by initializeGL and when screen is resized
void GLobj::resizeGL( int w, int h )
{
//algorithm to keep scene "square" (preserve aspect ratio)
//even if screen is streached
if(w>h)
glViewport((w-h)/2, 0, h, h);
else
glViewport(0, (h-w)/2, w, w);
//setup the projection and switch to model view for transformations
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//implicit call to paintGL after resized
}
//Paints the GL scene
void GLobj::paintGL()
{
glClear (GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glBegin(GL_POINTS);
glColor3f (1.0, 1.0, 1.0);
glVertex2f(-0.95,0.85);
glEnd();
glBegin(GL_LINES);
glColor3f (1.0, 0.0, 0.0);
glVertex2f(-0.75,0.65);
glVertex2f(-0.75,0.25);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f (1.0, 0.0,1.0);
glVertex2f(-0.65,0.0);
glVertex2f(-0.45,0.0);
glVertex2f(-0.55,0.5);
glEnd();
glBegin(GL_QUADS);
glColor3f (0.0, 1.0,1.0);
glVertex2f(0.1, 0.1);
glVertex2f(0.3,0.1);
glVertex2f(0.3,0.3);
glVertex2f(0.1,0.3);
glEnd();
glBegin(GL_QUADS);
glColor3f (0.0, 0.0,1.0);
glVertex2f(0.4, 0.1);
glVertex2f(0.9, 0.1);
glVertex2f(0.9, 0.3);
glVertex2f(0.4, 0.3);
glEnd();
glBegin(GL_POLYGON);
glColor3f (1.0, 1.0,0.0);
glVertex2f(0.1, 0.6);
glVertex2f(0.2, 0.4);
glVertex2f(0.4, 0.4);
glVertex2f(0.5, 0.6);
glVertex2f(0.3, 0.8);
glEnd();
glBegin(GL_POLYGON);
glColor3f (0.5, 1.0,1.0);
glVertex2f(0.6, 0.6);
glVertex2f(0.7, 0.5);
glVertex2f(0.8, 0.5);
glVertex2f(0.9, 0.6);
glVertex2f(0.8, 0.7);
glVertex2f(0.7, 0.7);
glEnd();
glBegin(GL_POLYGON);
glColor3f (0.5, 0.5,1.0);
glVertex2f(-0.1, -0.2);
glVertex2f(-0.2, -0.1);
glVertex2f(-0.3, -0.1);
glVertex2f(-0.4, -0.2);
glVertex2f(-0.4, -0.3);
glVertex2f(-0.25, -0.45);
glVertex2f(-0.1, -0.3);
glEnd();
glBegin(GL_QUADS);
glColor3f (0.5, 0.5,0.5);
glVertex2f(0.1, -0.2);
glVertex2f(0.3, -0.2);
glVertex2f(0.3, -0.4);
glVertex2f(0.1, -0.4);
glEnd();
glBegin(GL_QUADS);
glColor3f (1.0, 0.5,1.0);
glVertex2f(0.3, -0.2);
glVertex2f(0.35, -0.3);
glVertex2f(0.35, -0.5);
glVertex2f(0.3, -0.4);
glEnd();
glBegin(GL_QUADS);
glColor3f (1.0, 1.0,0.5);
glVertex2f(0.1, -0.4);
glVertex2f(0.3, -0.4);
glVertex2f(0.35, -0.5);
glVertex2f(0.15, -0.5);
glEnd();
glFlush ();
}
|