GL02Primitive cpp Vertex Primitive and Color Draw Simple 2D colored Sh

 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
/*
* GL02Primitive.cpp: Vertex, Primitive and Color
* Draw Simple 2D colored Shapes: quad, triangle and polygon.
*/
#define M_PI 3.14159265f
#include <windows.h> // for MS Windows
#include <GL/glut.h> // GLUT, include glu.h and gl.h
#include "math.h"
/* Initialize OpenGL Graphics */
void initGL() {
// Set "clearing" or background color
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Black and opaque
}
GLfloat ballRadius = 0.1f; // Radius of the bouncing ball
GLfloat ballX = 0.0f; // Ball's center (x, y) position
GLfloat ballY = 0.0f;
// Refresh period in milliseconds
/* Handler for window-repaint event. Call back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer with current clearing color
GLfloat ballRadius = 0.5f; // Radius of the bouncing ball
GLfloat ballX = 0.0f; // Ball's center (x, y) position
GLfloat ballY = 0.0f;
// Refresh period in milliseconds
// Define shapes enclosed within a pair of glBegin and glEnd
glBegin(GL_QUADS);
glColor3f(155.0f, 0.2f, 100.0f); // Green
glVertex2f(-0.92f, 0.92f);
glVertex2f(0.92f, 0.92f);
glVertex2f(0.92f, -0.92f);
glVertex2f(-0.92f, -0.92f);
// Each set of 4 vertices form a quad
glColor3f(0.2f, 0.2f, 0.2f); // Green
glVertex2f(-0.9f, 0.9f);
glVertex2f(0.9f, 0.9f);
glVertex2f(0.9f, -0.9f);
glVertex2f(-0.9f, -0.9f);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(155.0, 0.0, 100.0); // Blue
glVertex2f(-0.421, 0.41f);
glVertex2f(0.01f, 0.81f);
glVertex2f(0.41f, 0.41f);
// Each set of 3 vertices form a triangle
glColor3f(1.0, 1.0, 1.0); // Blue
glVertex2f(-0.4f, 0.4f);
glVertex2f(0.0f, 0.8f);
glVertex2f(0.4f, 0.4f);
glEnd();
glColor3f(155, 0, 100);
glutSolidSphere(0.35, 25, 15 );
glColor3f(1, 1, 1);
glutSolidSphere(0.1, 25, 15);
glFlush();
}
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("Vertex, Primitive & Color"); // Create window with the given title
glutInitWindowSize(1000, 1000); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display); // Register callback handler for window re-paint event
initGL(); // Our own OpenGL initialization
glutMainLoop(); // Enter the event-processing loop
return 0;
}