Royal University of Phnom Penh Computer Graphics
Faculty of Engineering Homework
!
Neang Cheysophal
G1
HW: Lab3
#include <cstdlib>
#include <math.h>
#include <sstream>
#include <iostream>
#if _WIN32
#include <gl/glut.h>
#include <GL/freeglut_std.h>
#endif
#if __APPLE__
#include <GLUT/glut.h>
#endif
using namespace std;
void drawAxis();
void drawUnit();
void drawStarOrWave();
void plotSineWave(float amplitude,float frequency, float phase);
void drawStar (float x, float y, float radius, int numPoints);
template <typename T>
string ToString(T val)
{
stringstream stream;
stream << val;
return [Link]();
}
int menu;
int submenu;
int submenuUnitColor;
int submenuGeomatric;
int submenuDrawStar;
int window;
int indexValue = 1;
int geometricType = GL_LINES;
bool drawedStar = false;
float Raxis = 1.0, Gaxis = 0.0, Baxis = 0.0;
float Runit = 1.0, Gunit = 0.0, Bunit = 0.0;
float Rstar = 1.0, Gstar = 0.0, Bstar = 0.0;
void renderString (float pos1, float pos2, const char *str){
int strLength, i;
glRasterPos2d(pos1,pos2);
strLength = (int) strlen(str);
for(i = 0; i<strLength;i++){
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10,str[i]);
}
1
Royal University of Phnom Penh Computer Graphics
Faculty of Engineering Homework
!
}
void selectIndexOnMenu(int index){
if(index == 0){
glutDestroyWindow(window);
exit(0);
}else{
indexValue = index;
}
glutPostRedisplay();
}
//Create Sub-menu
void createMenu(void){
//Sub-menu axis
submenu = glutCreateMenu(selectIndexOnMenu);
glutAddMenuEntry("Blue",2);
glutAddMenuEntry("Green",3);
glutAddMenuEntry("Yellow",4);
//Sub-menu unit color
submenuUnitColor = glutCreateMenu(selectIndexOnMenu);
glutAddMenuEntry("Brown",5);
glutAddMenuEntry("Cyan",6);
glutAddMenuEntry("White",7);
//Sub-Menu geometric type
submenuGeomatric = glutCreateMenu(selectIndexOnMenu);
glutAddMenuEntry("Point", 8);
glutAddMenuEntry("Line Strip", 9);
glutAddMenuEntry("Line Loop", 10);
//Sub-Menu draw a star with different color
submenuDrawStar = glutCreateMenu(selectIndexOnMenu);
glutAddMenuEntry("Blue",11);
glutAddMenuEntry("Cyan",12);
glutAddMenuEntry("Yellow",13);
//Menu
menu = glutCreateMenu(selectIndexOnMenu);
glutAddMenuEntry("Reset",1);
glutAddSubMenu("Change Color",submenu);
glutAddSubMenu("Unit Color", submenuUnitColor);
glutAddSubMenu("Geomatric Type",submenuGeomatric);
glutAddSubMenu("Draw Star",submenuDrawStar);
glutAddMenuEntry("Quit",0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0,0.0);
//Draw Axis
drawAxis();
2
Royal University of Phnom Penh Computer Graphics
Faculty of Engineering Homework
!
//Draw Unit;
drawUnit();
//Plot sin Wave
//plotSineWave(0.4,10,5);
//Change Axis Color
if(indexValue == 1){
geometricType = GL_LINES;
glColor3f(1.0,0,0.0);
//Draw Axis
drawAxis();
//Draw Unit;
drawUnit();
//Plot sin Wave
plotSineWave(0.4,10,5);
drawedStar = false;
}else if(indexValue == 2){
Raxis = 0.0;
Gaxis = 0.0;
Baxis = 255.0;
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
glColor3f(Runit, Gunit, Bunit);
//Draw Unit
drawUnit();
drawStarOrWave();
} else if (indexValue == 3){
Raxis = 0.0;
Gaxis = 128.0;
Baxis = 0.0;
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
glColor3f(Runit, Gunit, Bunit);
//Draw Unit
drawUnit();
drawStarOrWave();
} else if (indexValue == 4){
Raxis = 255.0;
Gaxis = 255.0;
Baxis = 0.0;
3
Royal University of Phnom Penh Computer Graphics
Faculty of Engineering Homework
!
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
glColor3f(Runit, Gunit, Bunit);
//Draw Unit
drawUnit();
drawStarOrWave();
}
//Change Unit Color
if (indexValue == 5){
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
Runit = 165;
Gunit = 42;
Bunit = 42;
glColor3f(Runit, Gunit, Bunit);
//Draw Unit
drawUnit();
drawStarOrWave();
} else if (indexValue == 6){
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
Runit = 0;
Gunit = 255;
Bunit = 255;
glColor3f(Runit, Gunit, Bunit);
//Draw Unit
drawUnit();
drawStarOrWave();
} else if (indexValue == 7){
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
Runit = 255;
Gunit = 255;
Bunit = 255;
glColor3f(Runit, Gunit, Bunit);
//Draw Unit
drawUnit();
drawStarOrWave();
}
//Change Geometric Type
if (indexValue == 8){
4
Royal University of Phnom Penh Computer Graphics
Faculty of Engineering Homework
!
geometricType = GL_POINTS;
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
glColor3f(Runit, Gunit, Bunit);
//Draw Unit
drawUnit();
drawStarOrWave();
} else if (indexValue == 9){
geometricType = GL_LINE_STRIP;
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
glColor3f(Runit, Gunit, Bunit);
//Draw Unit
drawUnit();
drawStarOrWave();
} else if (indexValue == 10){
geometricType = GL_LINE_LOOP;
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
glColor3f(Runit, Gunit, Bunit);
//Draw Unit
drawUnit();
drawStarOrWave();
}
//Draw a Star
else if (indexValue == 11){
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
glColor3f(Runit, Gunit, Bunit);
//Draw Unit
drawUnit();
Rstar = 0.0;
Gstar = 0.0;
Bstar = 255.0;
glColor3f(Rstar,Gstar,Bstar);
5
Royal University of Phnom Penh Computer Graphics
Faculty of Engineering Homework
!
//Draw a star
drawStar(0.5, 0.7, 0.3, 8);
drawedStar = true;
} else if (indexValue == 12){
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
glColor3f(Runit, Gunit, Bunit);
//Draw Unit
drawUnit();
Rstar = 0.0;
Gstar = 255.0;
Bstar = 255.0;
glColor3f(Rstar,Gstar,Bstar);
//Draw a star
drawStar(0.5, 0.7, 0.3, 8);
drawedStar = true;
} else if (indexValue == 13){
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
glColor3f(Runit, Gunit, Bunit);
//Draw Unit
drawUnit();
Rstar = 255.0;
Gstar = 255.0;
Bstar = 0.0;
glColor3f(Rstar,Gstar,Bstar);
//Draw a star
drawStar(0.5, 0.7, 0.3, 8);
drawedStar = true;
}
glFlush();
}
// Draw axis
void drawAxis(){
glBegin(GL_LINES);
//draw y-axis
glVertex2f(0,0.9);
glVertex2f(0,-0.9);
//draw x-axis
glVertex2f(0.9,0);
6
Royal University of Phnom Penh Computer Graphics
Faculty of Engineering Homework
!
glVertex2f(-0.9,0);
//draw y-axis arrow right side
glVertex2f(0,0.9);
glVertex2f(0.05,0.85);
//draw y-axis arrow left side
glVertex2f(0,0.9);
glVertex2f(-0.05,0.85);
//draw x-axis arrow top side
glVertex2f(0.9,0);
glVertex2f(0.85,0.05);
//draw x-axis arrow bottom side
glVertex2f(0.9,0);
glVertex2f(0.85,-0.05);
glEnd();
// Draw a unit on the axis and display number
}
void drawUnit(){
// Draw a unit on the axis and display number
float step = 0.1;
for(float i = step; i < 0.9; i += step){
//draw number y-axis
renderString(0.02,-i,ToString(i).c_str());
renderString(0.02,i,ToString(i).c_str());
//draw unit y-axis
glBegin(GL_LINES);
//Right
glVertex2f(i,0.02);
glVertex2f(i,-0.02);
//Left
glVertex2f(-i,0.02);
glVertex2f(-i,-0.02);
glEnd();
//draw number x-axis
renderString(-i,0.02,ToString(i).c_str());
renderString(i,0.02,ToString(i).c_str());
//draw unit y-axis
glBegin(GL_LINES);
//Top
glVertex2f(0.02,i);
glVertex2f(-0.02,i);
//Bottom
glVertex2f(0.02,-i);
glVertex2f(-0.02,-i);
glEnd();
}
}
// Plot sin wave with the following formula
7
Royal University of Phnom Penh Computer Graphics
Faculty of Engineering Homework
!
void plotSineWave(float amplitude,float frequency, float phase){
float Start = -0.9; // coordination Ortho left
float End = 0.9; // coordination Ortho right
float Step = 0.01;
float Amplitude = amplitude;
float Frequency = frequency;
float Phase = phase;
float x1 = Start;
float y1 = Amplitude * sin( x1 * (Frequency + Phase) );
for (float x2 = Start + Step; x2 <= End; x2+=Step ){
float y2 = Amplitude * sin( x2 * (Frequency + Phase) );
glBegin(geometricType);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
x1 = x2;
y1 = y2;
}
}
// End Plot sin wave with the following formula
//Draw a Star
void drawStar (float x, float y, float radius, int numPoints)
{
const float DegToRad = 3.14159 / 180;
(geometricType==GL_LINES)?glBegin(GL_LINE_LOOP):glBegin (geometricType);
int count = 1;
// glVertex2f(x, y);
for (float i = 0; i <= 360; i+=360.0/(numPoints*2)) {
float DegInRad = i * DegToRad;
if(count%2!=0)
glVertex2d (x + cos (DegInRad) * radius, y + sin (DegInRad) * radius);
else
glVertex2d ((x + cos (DegInRad) * radius/2), (y + sin (DegInRad) * radius/2));
count++;
}
glEnd();
}
//End Draw a Star
void drawStarOrWave(){
8
Royal University of Phnom Penh Computer Graphics
Faculty of Engineering Homework
!
if(drawedStar == true){
glColor3f(Rstar,Gstar,Bstar);
//Draw Star
drawStar(0.5, 0.7, 0.3, 8);
} else{
glColor3f(1.0,0.0,0.0);
//Plot sin Wave
plotSineWave(0.4,10,5);
}
void initialization(){
glClearColor(0.0, 0.0, 0.0, 0.0);
glLineWidth(2); //Line Size
glMatrixMode(GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D(-1, 1, -1, 1);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("axis");
initialization();
createMenu();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}