Skip to content

Instantly share code, notes, and snippets.

@3N4N
Created June 25, 2021 07:40
Show Gist options
  • Save 3N4N/491baf88cb319fbba7e211f52b30c62b to your computer and use it in GitHub Desktop.
Save 3N4N/491baf88cb319fbba7e211f52b30c62b to your computer and use it in GitHub Desktop.
#ifndef _GLUT_SHAPES_H_
#define _GLUT_SHAPES_H_
#include <GL/glut.h>
#include<math.h>
#define pi (2*acos(0.0))
void drawAxes(int drawaxes);
void drawSquare(double a);
void drawGrid(int drawgrid);
void drawCircle(double radius,int segments);
void drawCone(double radius,double height,int segments);
void drawSphere(double radius,int slices,int stacks);
void drawAxes(int drawaxes)
{
if(drawaxes == 1) {
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES); {
glColor3f(1.0, 0.0, 0.0);
glVertex3f( 100,0,0);
glVertex3f(-100,0,0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0,-100,0);
glVertex3f(0, 100,0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0,0, 100);
glVertex3f(0,0,-100);
} glEnd();
}
}
void drawGrid(int drawgrid)
{
int i;
if(drawgrid == 1) {
glColor3f(0.6, 0.6, 0.6); //grey
glBegin(GL_LINES); {
for(i = -8; i <= 8; i++){
if(i == 0)
continue; //SKIP the MAIN axes
//lines parallel to Y-axis
glVertex3f(i*10, -90, 0);
glVertex3f(i*10, 90, 0);
//lines parallel to X-axis
glVertex3f(-90, i*10, 0);
glVertex3f( 90, i*10, 0);
}
} glEnd();
}
}
void drawSquare(double a)
{
//glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS); {
glVertex3f( a, a, 2);
glVertex3f( a,-a, 2);
glVertex3f(-a,-a, 2);
glVertex3f(-a, a, 2);
} glEnd();
}
void drawCircle(double radius,int segments)
{
int i;
struct vector3d points[100];
glColor3f(0.7,0.7,0.7);
//generate points
for(i = 0; i <= segments; i++) {
points[i].x = radius * cos(((double)i / (double)segments) * 2*pi);
points[i].y = radius * sin(((double)i / (double)segments) * 2*pi);
}
//draw segments using generated points
for(i = 0; i < segments; i++) {
glBegin(GL_LINES); {
glVertex3f(points[i].x, points[i].y, 0);
glVertex3f(points[i+1].x, points[i+1].y, 0);
} glEnd();
}
}
void drawCone(double radius,double height,int segments)
{
int i;
double shade;
struct vector3d points[100];
//generate points
for(i = 0; i <= segments; i++) {
points[i].x = radius * cos(((double)i / (double)segments) * 2*pi);
points[i].y = radius * sin(((double)i / (double)segments) * 2*pi);
}
//draw triangles using generated points
for(i = 0; i < segments; i++) {
//create shading effect
if (i < segments/2)
shade = 2 * (double)i / (double)segments;
else
shade = 2 * (1.0 - (double)i / (double)segments);
glColor3f(shade, shade, shade);
glBegin(GL_TRIANGLES); {
glVertex3f(0, 0, height);
glVertex3f(points[i].x, points[i].y, 0);
glVertex3f(points[i+1].x, points[i+1].y, 0);
} glEnd();
}
}
void drawSphere(double radius, int slices, int stacks)
{
struct vector3d points[100][100];
int i, j;
double h, r;
//generate points
for(i = 0; i <= stacks; i++) {
h = radius * sin(((double)i / (double)stacks) * (pi/2));
r = radius * cos(((double)i / (double)stacks) * (pi/2));
for(j = 0; j <= slices; j++) {
points[i][j].x = r * cos(((double)j / (double)slices) * 2*pi);
points[i][j].y = r * sin(((double)j / (double)slices) * 2*pi);
points[i][j].z = h;
}
}
//draw quads using generated points
for(i = 0; i < stacks; i++) {
glColor3f((double)i / (double)stacks,
(double)i / (double)stacks,
(double)i / (double)stacks);
for(j = 0; j < slices; j++) {
glBegin(GL_QUADS); {
//upper hemisphere
glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
//lower hemisphere
glVertex3f(points[i][j].x,points[i][j].y,-points[i][j].z);
glVertex3f(points[i][j+1].x,points[i][j+1].y,-points[i][j+1].z);
glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,-points[i+1][j+1].z);
glVertex3f(points[i+1][j].x,points[i+1][j].y,-points[i+1][j].z);
} glEnd();
}
}
}
#endif // _GLUT_SHAPES_H_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment