Skip to content

Instantly share code, notes, and snippets.

@alekseypotapov-dev
Last active August 29, 2015 14:07
Show Gist options
  • Save alekseypotapov-dev/3f84372cae1778e68d8b to your computer and use it in GitHub Desktop.
Save alekseypotapov-dev/3f84372cae1778e68d8b to your computer and use it in GitHub Desktop.
This is a 4th example for the first OpenGL lesson
//
// main.cpp
// OpenGL-examples
//
// Created by Alexey Potapov on 27.09.14.
// Copyright (c) 2014 none. All rights reserved.
//
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <math.h>
#if defined (__APPLE__)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
float Pi = 3.1415926;
int verticesCount;
void keyboardListener (unsigned char key, int x, int y)
{
switch ((char)key)
{
case 'q':
case 27:
exit(0);
break;
default:
break;
}
}
void numberOfVertices()
{
printf("Please input integer variable!\nVertices count: ");
std::cin >> verticesCount;
while (!std::cin)
{
std::cin.clear();
std::cin.ignore(256,'\n');
printf("Once more time!\nVertices count: ");
std::cin >> verticesCount;
}
}
//inspired by http://slabode.exofire.net/circle_draw.shtml
void drawFigure(float cx, float cy, float r, int num_segments)
{
float theta = 2 * Pi / float(num_segments);
float c = cosf(theta);//precalculate the sine and cosine
float s = sinf(theta);
float t;
float x = r;//we start at angle = 0
float y = 0;
glBegin(GL_LINE_LOOP);
for(int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x + cx, y + cy);//output vertex
//apply the rotation matrix
t = x;
x = c * x - s * y;
y = s * t + c * y;
}
glEnd();
}
void displayProcess (void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
//draw cirle
glColor3f(1, 1, 1);
drawFigure(0, 0, 1, 50);
//draw red polygon
glColor3f(1, 0, 0);
drawFigure(0, 0, 1, verticesCount);
glFlush();
glutSwapBuffers();
}
void openGLfunction (int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("Polygon inside the circle");
glutDisplayFunc(displayProcess);
glutKeyboardFunc(keyboardListener);
glutMainLoop();
}
int main(int argc, char *argv[])
{
numberOfVertices();
openGLfunction(argc, argv);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment