Neang Cheysophal G1 HW: Lab3: Royal University of Phnom Penh Faculty of Engineering Homework
Neang Cheysophal G1 HW: Lab3: Royal University of Phnom Penh Faculty of Engineering Homework
Neang Cheysophal G1 HW: Lab3: Royal University of Phnom Penh 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
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 stream.str();
}
int menu;
int submenu;
int submenuUnitColor;
int submenuGeomatric;
int submenuDrawStar;
int window;
int indexValue = 1;
int geometricType = GL_LINES;
bool drawedStar = false;
1
Royal University of Phnom Penh Computer Graphics
Faculty of Engineering Homework
!
}
//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);
glColor3f(1.0,0,0.0);
//Draw Axis
drawAxis();
//Draw Unit;
drawUnit();
//Plot sin Wave
plotSineWave(0.4,10,5);
drawedStar = false;
drawStarOrWave();
drawStarOrWave();
3
Royal University of Phnom Penh Computer Graphics
Faculty of Engineering Homework
!
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
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();
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();
drawStarOrWave();
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
drawStarOrWave();
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
drawStarOrWave();
}
//Draw a Star
else if (indexValue == 11){
glColor3f(Raxis,Gaxis,Baxis);
//Draw Axis
drawAxis();
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;
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();
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){
7
Royal University of Phnom Penh Computer Graphics
Faculty of Engineering Homework
!
void plotSineWave(float amplitude,float frequency, float phase){
float x1 = Start;
float y1 = Amplitude * sin( x1 * (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);
}