I have write the OpenGL code and it can Run but the function cannot call out, can anyone help me??
These are my codes:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <math.h>
#include <string>
#include <sstream>
int width,height,xAxis,yAxis; // Width, Height, X-axis, Y-axis
float pR=0.0; // Polygon Colour (Red)
float pG=0.0; // Polygon Colour (Green)
float pB=1.0; // Polygon Colour (Blue)
float bR=0.80; // Background Colour (Red)
float bG=0.80; // Background Colour (Green)
float bB=0.80; // Background Colour (Blue)
int i=1;
GLfloat x1[100]; //Store the Mouse Point in the Array
GLfloat Y1[100];
int click=0;
int xPos;
int yPos;
int stop=0;
int z;
static GLfloat spin = 0.0f; // Rotate, Scale, Translate Objects
static GLfloat t1 = 0.0f;
static GLfloat t2 = 0.0f;
static GLfloat t3 = 1.0f;
// Mouse Menu
int MainMenu;
int PolygonMenu;
int FillMenu;
int ColourMenu;
int bColourMenu;
int old_rot_x=0; //axis when mouse click(N)
int old_rot_y=0;
int rot_x=0; //aixs after drag the mouse, use these for rotation (N)
int rot_y=0;
int record_x=0; //record of last rotation
int record_y=0;
int draw=0; // Drawing Objects
int type=0; // Drawing Type
static GLfloat theta[] = {0.0,0.0,0.0,0.0}; // Rotation
static GLint axis = 0;
int fill=0; // Colour Fill For Polygon : 0 = Not Fill, 1 = Fill
int side; // Number of side of an object
// Cube Parameters
GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{1.0,0.0,1.0},{1.0,1.0,1.0},{0.0,1.0,1.0}};
GLfloat normals[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},{1.0,1.0,-1.0},{-1.0,1.0,-1.0},{-1.0,-1.0,1.0},{1.0,-1.0,1.0},{1.0,1.0,1.0},{-1.0,1.0,1.0}};
GLfloat vertices[][3] = {{-0.5,-0.5,-0.5},{0.5,-0.5,-0.5},{0.5,0.5,-0.5},{-0.5,0.5,-0.5},{-0.5,-0.5,0.5},{0.5,-0.5,0.5},{0.5,0.5,0.5},{-0.5,0.5,0.5}};
// End of Cube Parameters
// Function Declaration
void spinDisplay(void);
void SpecialKeys(int key, int x, int y);
void WindowSize(int , int ); // for WindowSize & drawig
void MotionMouse(int , int ); // get message when mouse move
// Draw Polygon for Cube
void polygon(int p1, int p2, int p3, int p4)
{
// Draw a polygon via list of vertices
glBegin(GL_POLYGON);
glColor3fv(colors[p1]);
glNormal3fv(normals[p1]);
glVertex3fv(vertices[p1]);
glColor3fv(colors[p2]);
glNormal3fv(normals[p2]);
glVertex3fv(vertices[p2]);
glColor3fv(colors[p3]);
glNormal3fv(normals[p3]);
glVertex3fv(vertices[p3]);
glColor3fv(colors[p4]);
glNormal3fv(normals[p4]);
glVertex3fv(vertices[p4]);
glEnd();
} // End of Draw Polygon for Cube
// Draw a Cube
void drawCube(void)
{
// Map vertices to faces
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
} // End of Draw Cube
// Draw Rectangle
void drawRectangle(void)
{
glColor3f(pR, pG, pB); // Colour of the Polygon
if (fill == 0)
glBegin(GL_LINE_LOOP); // Not Fill Colour
else
glBegin(GL_POLYGON); // Fill Colour
glVertex2f(-0.5,0.4);
glVertex2f(-0.5,-0.4);
glVertex2f(0.5,-0.4);
glVertex2f(0.5,0.4);
glEnd();
} // End of Draw Rectangle
void init(void)
{
// Clear Background Colour
glClearColor(0.0, 0.0, 0.0, 0.0);
// Initialize Viewing Values
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glShadeModel (GL_SMOOTH);
}
void display(void)
{
glClearColor(bR, bG, bB, 0.0);
// Clear All Pixels
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Reset All Settings when type = 0
if (type==0)
{
float i;
float m=1;
float n=-1;
int q;
glColor3f(pR, pG, pB); //Colour of the polygon
glPushMatrix();
// draw the rows and column lines
glBegin(GL_LINES);
for(i=-0.9 ;i<1 ;i=i+0.1)
{
glVertex3f(i,m,0.0f);
glVertex3f(i,n,0.0f);
}
glEnd();
glBegin(GL_LINES);
for(i=-0.9 ;i<1 ;i=i+0.1)
{
glVertex3f(m,i,0.0f);
glVertex3f(n,i,0.0f);
}
glEnd();
glPopMatrix();
float xPos = -1;
float yPos = 0.95;
float spacing = 0.05;
// Draw the point of user click
glPushMatrix();
glPointSize(5.0); // Increase the point size
glColor3f(pR, pG, pB); // Colour of the mouse click
glBegin(GL_POINTS);
for(q= 1; q<=click; q++)
{
glVertex3f(x1[q],Y1[q],0.0f);
}
glEnd();
glPopMatrix();
}
// Draw a Polygon when type = 1
glColor3f(pR, pG, pB); // Polygon Colour
if (type==1)
{
glPushMatrix();
gluLookAt(0,0,10.0f,0,0,0,0,1,0);//position and direction of the viewing point
glRotatef(spin + (float)rot_x, 0.0, 0.0, 1.0); // Rotate
glTranslatef(t1,t2,0.0); // Translate
glScalef(t3,t3,0.0); // Scale
glColor3f(pR, pG, pB);
glBegin(GL_POLYGON); // Draw Polygon
for (i= 1; i<=click; i++)
{
glVertex3f(x1[i],Y1[i],0.0f);
}
glEnd();
glPopMatrix();
}
// Draw a Cube when type = 2
if (type==2)
{
gluLookAt(0,0,10.0f,0,0,0,0,1,0); //position and direction of the viewing point
glRotatef(theta[0], 1.0,0.0,0.0); // Rotate Cube
glRotatef(theta[1], 0.0,1.0,0.0);
glRotatef(theta[2], 0.0,0.0,1.0);
glTranslatef(t1,t2,0.0); // Translate
glScalef(t3,t3,t3); // Scale
glColor3f(pR, pG, pB);
drawCube(); // Draw Cube
}
//Draw a Rectangle when type = 4
if (type==4)
{
glRotatef(spin, 0.0, 0.0, 1.0); // Rotate
glTranslatef(t1,t2,0.0); // Translate
glScalef(t3,t3,0.0); // Scale
glColor3f(pR, pG, pB);
drawRectangle();
}
glFlush();
glutSwapBuffers();
}
void point(int xPos, int yPos) // store the mouse click data
{
GLfloat xPosition,yPosition;
xPosition= xPos;
yPosition= yPos;
click=click+1;
x1[click]=(xPosition - width/2)/(width/2);
Y1[click]=(-yPosition + height/2)/(height/2);
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
if (stop==0)
point(x, y);
if(button == GLUT_MIDDLE_BUTTON);
if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
glutIdleFunc(NULL);
if(state)
{
record_x += x - old_rot_x;
record_y += y - old_rot_y;
rot_x = 0; //should restore to Zero
rot_y = 0;
}
else
{
old_rot_x = x;
old_rot_y = y;
}
}
void MotionMouse(int x, int y)
{
rot_x = x - old_rot_x;
rot_y = y - old_rot_y;
glutPostRedisplay();
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP){
t2 += 0.01f;
glutPostRedisplay();
printf(">> Translate Object Upwards
“);
}
if(key == GLUT_KEY_DOWN){
t2 -= 0.01f;
glutPostRedisplay();
printf(”>> Translate Object Downwards
“);
}
if(key == GLUT_KEY_LEFT){
t1 -= 0.01f;
// line = (line + 5) % 360;
glutPostRedisplay();
printf(”>> Translate Object to Left
“);
}
if(key == GLUT_KEY_RIGHT){
t1 += 0.01f;
// line = (line - 5) % 360;
glutPostRedisplay();
printf(”>> Translate Object to Right
“);
}
if(key == GLUT_KEY_F1){
t3 += 0.1f;
glutPostRedisplay();
printf(”>> Scale Object to Large
“);
}
if(key == GLUT_KEY_F2){
t3 -= 0.1f;
glutPostRedisplay();
printf(”>> Scale Object to Small
“);
}
if (key == GLUT_KEY_HOME){
pR += 0.01;
pG += 0.01;
pB += 0.01;
glutPostRedisplay();
printf(”>> Decrease Object Colour
");
}
if (key == GLUT_KEY_END){
pR -= 0.01;
pG -= 0.01;
pB -= 0.01;
glutPostRedisplay();
printf("Increase Object Colour
");
}
if (key == GLUT_KEY_PAGE_UP){
bR += 0.01;
bG += 0.01;
bB += 0.01;
glutPostRedisplay();
printf("Decrease Background Colour
“);
}
if (key == GLUT_KEY_PAGE_DOWN){
bR -= 0.01;
bG -= 0.01;
bB -= 0.01;
glutPostRedisplay();
printf(”>> Increase Background Colour
“);
}
if (key == GLUT_KEY_F8){
glutIdleFunc(NULL); // Stop
printf(”>> Stop Object
“);
}
if (key == GLUT_KEY_F3){
z=1; // Rotate to Left
printf(”>> Rotate Polygon Object to Left
“);
glutIdleFunc(spinDisplay);
}
if (key == GLUT_KEY_F4){
z=2; // Rotate to Right
glutIdleFunc(spinDisplay);
printf(”>> Rotate Polygon Object to Right
“);
}
if (key == GLUT_KEY_F5){
axis = 0;
glutIdleFunc(spinDisplay);
printf(”>> Rotate Cube Object along X-axis
“);
}
if (key == GLUT_KEY_F6){
axis = 1;
glutIdleFunc(spinDisplay);
printf(”>> Rotate Cube Object along Y-axis
“);
}
if (key == GLUT_KEY_F7){
axis = 2;
glutIdleFunc(spinDisplay);
printf(”>> Rotate Cube Object along Z-axis
“);
}
if (key == GLUT_KEY_F9){
for (i= 1; i<=click; i++) // Clear the Array
{
x1[i]=0;
Y1[i]=0;
}
click=0;
draw=0;
stop=0;
type=0;
t1=0; t2=0; t3=1;
spin=0;
glutIdleFunc(NULL);
glutPostRedisplay(); //Reset function
printf(”>> Clear object
");
}
if (key == GLUT_KEY_F10){
printf ("
---- Drawing 2D Polygon & Cube Operation Menu ----
“);
printf (”<< Mouse Right Click for Menu Control >>
“);
printf (”--------------------------------------------------
");
printf("Up Arrow : Translate Object Upwards
");
printf("Down Arrow : Translate Object Downwards
");
printf("Left Arrow : Translate Object to Left
");
printf("Right Arrow : Translate Object to Right
");
printf("F1 : Scale Object to Large
");
printf("F2 : Scale Object to Small
");
printf("F3 : Rotate Polygon Object to Left
");
printf("F4 : Rotate Polygon Object to Right
");
printf("F5 : Rotate Cube Object along X-axis
");
printf("F6 : Rotate Cube Object along Y-axis
");
printf("F7 : Rotate Cube Object along Z-axis
");
printf("F8 : Stop Object
");
printf("F9 : Clear Object
");
printf("F10 : Display Menu
");
printf("Home : Decrease Polygon Colour
");
printf("End : Increase Polygon Colour
");
printf("Page Up : Decrease Background Colour
");
printf("Page Down : Increase Background Colour
");
printf("ESC : Exit Program
“);
printf (”----------------------------------------------
");
glutPostRedisplay();
}
}
// Press Esc to Exit the Program
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
default:
break;
}
}
void spinDisplay(void)
{
// Polygon Rotation
if (z==1)
{
spin = spin + 0.1;
if (spin > 360.0)
spin = spin - 360.0;
}
if (z==2)
{
spin = spin - 0.1;
if (spin < -360.0)
spin = spin + 360.0;
}
// Cube Rotation
if (type>=2 )
{
// Idle Callback, Spin Cube 2 Degrees along Selected Axis
theta[axis] += 0.05;
if(theta[axis] > 360.0)
theta[axis] -= 360.0;
}
glutPostRedisplay();
}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void ProcessMenu(int value)
{
switch(value)
{
case 31: // Display a Cube
type=2;
t1=0; t2=0; t3=1;
axis=0;
theta[0]=0; theta[1]=0; theta[2]=0;
glutPostRedisplay();
break;
// Colour Filling
case 5: // Fill Colour
fill=1;
glutPostRedisplay();
break;
case 6: // Not Fill Colour
fill=0;
glutPostRedisplay();
break;
// Polygon Colour
case 7: // Black Colour
pR=0; pG=0; pB=0;
glutPostRedisplay();
break;
case 8: // Red Colour
pR=1; pG=0; pB=0;
glutPostRedisplay();
break;
case 9: // Green Colour
pR=0; pG=1; pB=0;
glutPostRedisplay();
break;
case 10: // Yellow Colour
pR=1; pG=1; pB=0;
glutPostRedisplay();
break;
case 11: // Blue Colour
pR=0; pG=0; pB=1;
glutPostRedisplay();
break;
case 12: // Magenta Colour
pR=1; pG=0; pB=1;
glutPostRedisplay();
break;
case 13: // Dark Grey Colour
pR=0.25; pG=0.25; pB=0.25;
glutPostRedisplay();
break;
case 14: // Light Grey Colour
pR=0.75; pG=0.75; pB=0.75;
glutPostRedisplay();
break;
case 15: // Brown Colour
pR=0.6; pG=0.4; pB=0.12;
glutPostRedisplay();
break;
case 16: // Pink Colour
pR=0.98; pG=0.04; pB=0.7;
glutPostRedisplay();
break;
case 17: // Purple Colour
pR=0.6; pG=0.4; pB=0.7;
glutPostRedisplay();
break;
case 18: // White Colour
pR=1; pG=1; pB=1;
glutPostRedisplay();
break;
// Background Colour
case 19: // Black Colour
bR=0; bG=0; bB=0;
glutPostRedisplay();
break;
case 20: // Red Colour
bR=1; bG=0; bB=0;
glutPostRedisplay();
break;
case 21: // Green Colour
bR=0; bG=1; bB=0;
glutPostRedisplay();
break;
case 22: // Yellow Colour
bR=1; bG=1; bB=0;
glutPostRedisplay();
break;
case 23: // Blue Colour
bR=0; bG=0; bB=1;
glutPostRedisplay();
break;
case 24: // Magenta Colour
bR=1; bG=0; bB=1;
glutPostRedisplay();
break;
case 25: // Dark Grey Colour
bR=0.25; bG=0.25; bB=0.25;
glutPostRedisplay();
break;
case 26: // Light Grey Colour
bR=0.75; bG=0.75; bB=0.75;
glutPostRedisplay();
break;
case 27: // Brown Colour
bR=0.6; bG=0.4; bB=0.12;
glutPostRedisplay();
break;
case 28: // Pink Colour
bR=0.98; bG=0.04; bB=0.7;
glutPostRedisplay();
break;
case 29: // Purple Colour
bR=0.6; bG=0.4; bB=0.7;
glutPostRedisplay();
break;
case 30: // White Colour
bR=1; bG=1; bB=1;
glutPostRedisplay();
break;
// Display 2D Polygon
case 1: // Display Triangle
if (draw==0)
{
type=0; //clear the scale
glutPostRedisplay(); //Hold the polygon
draw=1; //stop refresh the window
stop=1; //stop storing array
}
break;
case 2: // Display Square
if (draw==0)
{
type=0; //clear the scale
glutPostRedisplay(); //Hold the polygon
draw=1; //stop refresh the window
stop=1; //stop storing array
}
break;
case 3: // Display Rectangle
type=4;
t1=0; t2=0; t3=1;
axis=0;
theta[0]=0; theta[1]=0; theta[2]=0;
glutPostRedisplay();
break;
// case 4: // Display 5 Side
if (draw==0)
{
type=1; //clear the scale
glutPostRedisplay(); //Hold the polygon
draw=1; //stop refresh the window
stop=1; //stop storing array
}
break;
/* case 5: // Display 6 Side
type=8;
t1=0; t2=0; t3=1;
axis=0;
theta[0]=0; theta[1]=0; theta[2]=0;
glutPostRedisplay();
break;
case 6: // Display 7 Side
type=9;
t1=0; t2=0; t3=1;
axis=0;
theta[0]=0; theta[1]=0; theta[2]=0;
glutPostRedisplay();
break;
case 7: // Display 8 Side
type=10;
t1=0; t2=0; t3=1;
axis=0;
theta[0]=0; theta[1]=0; theta[2]=0;
glutPostRedisplay();
break;
case 8: // Display 9 Side
type=11;
t1=0; t2=0; t3=1;
axis=0;
theta[0]=0; theta[1]=0; theta[2]=0;
glutPostRedisplay();
break;
case 9: // Display 10 Side
for (i= 1; i<=click; i++) // clear the array
{
x1[i]=0;
Y1[i]=0;
}
click=0;
draw=0; //start the draw again
stop=0; //start to store the array
// clear=0;
t1=0;
t2=0;
t3=1;
spin=0;
glutIdleFunc(NULL);
glutPostRedisplay();
break;
*/
case 99:
for (i= 1; i<=click; i++) // Clear the Array
{
x1[i]=0;
Y1[i]=0;
}
click=0;
draw=0; // Draw Again
stop=0; // Store the Array
type=0;
t1=0; t2=0; t3=1;
spin=0;
glutIdleFunc(NULL);
glutPostRedisplay(); //Reset Function
break;
default:
break;
}
}
/* Main program to open a window and display a polygon */
int main(int argc, char** argv)
{
printf ("Please input two integers for Window Size :
");
printf (“The Width: (500) “);
scanf (”%d”, &width);
printf (“The Height: (500) “);
scanf (”%d”, &height);
printf ("Please input two integers for Window Position :
“);
printf (“The X-aixs: (750) “);
scanf (”%d”, &xAxis);
printf (“The Y-aixs: (100) “);
scanf (”%d”, &yAxis);
printf (”
---- Drawing 2D Polygon & Cube Operation Menu ----
“);
printf (”<< Mouse Right Click for Menu Control >>
“);
printf (”--------------------------------------------------
");
printf("Up Arrow : Translate Object Upwards
");
printf("Down Arrow : Translate Object Downwards
");
printf("Left Arrow : Translate Object to Left
");
printf("Right Arrow : Translate Object to Right
");
printf("F1 : Scale Object to Large
");
printf("F2 : Scale Object to Small
");
printf("F3 : Rotate Polygon Object to Left
");
printf("F4 : Rotate Polygon Object to Right
");
printf("F5 : Rotate Cube Object along X-axis
");
printf("F6 : Rotate Cube Object along Y-axis
");
printf("F7 : Rotate Cube Object along Z-axis
");
printf("F8 : Stop Object
");
printf("F9 : Clear Object
");
printf("F10 : Display Menu
");
printf("Home : Decrease Polygon Colour
");
printf("End : Increase Polygon Colour
");
printf("Page Up : Decrease Background Colour
");
printf("Page Down : Increase Background Colour
");
printf("ESC : Exit Program
“);
printf (”----------------------------------------------
");
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutInitWindowPosition(xAxis, yAxis);
glutCreateWindow("COMP407 Yani's Assignment1");
// Create Menu
PolygonMenu = glutCreateMenu(ProcessMenu);
glutAddMenuEntry(“Triangle”,1);
glutAddMenuEntry(“Square”,2);
glutAddMenuEntry(“Rectangle (Demo)”,3);
// glutAddMenuEntry(“Draw”,4);
// glutAddMenuEntry(“6 Side”,5);
// glutAddMenuEntry(“7 Side”,6);
// glutAddMenuEntry(“8 Side”,7);
// glutAddMenuEntry(“9 Side”,8);
// glutAddMenuEntry(“10 Side”,9);
FillMenu = glutCreateMenu(ProcessMenu);
glutAddMenuEntry("Yes",5);
glutAddMenuEntry("No",6);
ColourMenu = glutCreateMenu(ProcessMenu);
glutAddMenuEntry("Black",7);
glutAddMenuEntry("Red",8);
glutAddMenuEntry("Green",9);
glutAddMenuEntry("Yellow",10);
glutAddMenuEntry("Blue",11);
glutAddMenuEntry("Magenta",12);
glutAddMenuEntry("Dark Grey",13);
glutAddMenuEntry("Light Grey",14);
glutAddMenuEntry("Brown",15);
glutAddMenuEntry("Pink",16);
glutAddMenuEntry("Purple",17);
glutAddMenuEntry("White",18);
bColourMenu = glutCreateMenu(ProcessMenu);
glutAddMenuEntry("Black",19);
glutAddMenuEntry("Red",20);
glutAddMenuEntry("Green",21);
glutAddMenuEntry("Yellow",22);
glutAddMenuEntry("Blue",23);
glutAddMenuEntry("Magenta",24);
glutAddMenuEntry("Dark Grey",25);
glutAddMenuEntry("Light Grey",26);
glutAddMenuEntry("Brown",27);
glutAddMenuEntry("Pink",28);
glutAddMenuEntry("Purple",29);
glutAddMenuEntry("White",30);
MainMenu = glutCreateMenu(ProcessMenu);
glutAddMenuEntry("Cube",31);
glutAddSubMenu("2D Polygon", PolygonMenu);
glutAddSubMenu("Fill Colour", FillMenu);
glutAddSubMenu("Polygon Colour", ColourMenu);
glutAddSubMenu("Background Colour", bColourMenu);
glutAddMenuEntry("Reset",99);
glutAttachMenu(GLUT_RIGHT_BUTTON);
// End Create Menu
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(MotionMouse);
glutSpecialFunc(SpecialKeys);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}