|
PRO Level 2
Joined: 09 May 2008
Posts: 11
|
Hi guys, I am a beginner to C++ and the use of Glut/OPENgl. am taking C++ in school. My prof assigned us this homework. Here is the assignment:
You are required to come up with a computer graphics application that draws:
• Lines
• Triangles
• Quads
• Circles
• Polygons
The drawings can either be filled, bordered or both. The program should have buttons to it such that when one the line button is clicked it draws the line when the polygon button is clicked it draws the polygon
So far I have managed to come up with a code that draws the various shapes am using Microsoft Visual C++ 6.0 as the compiler.
[img][/img]
| Code: |
#include <GL>
#include <iostream>
using namespace std;
void init();
void render();
void MouseHandling(int button, int state, int x, int y);
void KeyboardHandling(unsigned char key, int x, int y);
int choise = 0;
float rotation;
int main()
{
cout << "Setting display mode:\t\t\t\t\t";
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
cout << "[DONE]\nSetting window size...\t\t\t\t\t";
glutInitWindowSize(500, 500);
cout << "[DONE]\nSetting window position...\t\t\t\t";
glutInitWindowPosition(150, 150);
cout << "[DONE]\nCreating Window...\t\t\t\t\t";
glutCreateWindow("Gichane Dennis Chege 8290");
cout << "[DONE]\n";
init();
cout << "Setting the mouse call back function...\t\t\t";
glutMouseFunc(MouseHandling);
cout << "[DONE]\nSetting the keyboard call back function...\t\t";
glutKeyboardFunc(KeyboardHandling);
cout << "[DONE]\n";
cout << "Setting display function...\t\t\t\t";
glutDisplayFunc(render);
cout << "[DONE]\n";
cout << "Set the function when idle...\t\t\t\t";
glutIdleFunc(render);
cout << "[DONE]\n";
cout << "Ready.\n";
glutMainLoop();
cout << "Main loop finished.\n";
cout << "Press any key to quit.\n";
cin.get();
return 0;
}
void init()
{
cout << "Setting background color to white...\t\t\t";
glClearColor(1.0, 1.0, 0.5, 1.0);
cout << "[DONE]\nSetting matrix mode...\t\t\t\t\t";
glMatrixMode(GL_PROJECTION);
cout << "[DONE]\nMaking coordinate system...\t\t\t\t";
glOrtho(-5.0, 5.0, -5.0, 5.0, -1.0, 1.0);
cout << "[DONE]\nLoading identity...\t\t\t\t\t";
glLoadIdentity();
cout << "[DONE]\n";
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT);
if(choise == 0)
{
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0); glVertex2f(0.0, 0.5);
glColor3f(0.0, 1.0, 0.0); glVertex2f(-0.5, -0.5);
glColor3f(0.0, 0.0, 1.0); glVertex2f(0.5, -0.5);
glEnd();
}
else if(choise == 1)
{
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0); glVertex2f(-0.5, -0.5);
glColor3f(0.0, 1.0, 0.0); glVertex2f(-0.5, 0.5);
glColor3f(0.0, 0.0, 1.0); glVertex2f(0.5,0.5);
glColor3f(1.0, 0.0, 1.0); glVertex2f(0.5, -0.5);
glEnd();
}
else if(choise == 2)
{
glLineWidth(5.0f);
glBegin(GL_LINE_LOOP);
glColor3d(0.9,0.9,0.2);
glVertex3f(0.6,0.5, 0.5);
glColor3f (2.0, 0.0, 0.0);
glVertex3f (0.25, 0.25, 0.0);
glEnd();
}
else if(choise == 3)
{
glLineWidth(5.0f);
glBegin(GL_POLYGON);
glColor3f (2.0, 0.0, 0.0);
glVertex3f (0.25, 0.25, 0.0);
glColor3f (1.0, 0.0, 1.0);
glVertex3f (0.65, 0.85, 0.0);
glColor3f (0.0, 1.0, 1.0);
glVertex3f (0.65, 0.75, 0.0);
glColor3f (0.0, 1.0, 0.0);
glVertex3f (0.55, 0.75, 0.0);
glColor3f (0.0, 1.0, 0.0);
glVertex3f (0.45, 0.95, 0.0);
glColor3f (0.0, 2.0, 0.0);
glEnd();
}
else if(choise == 4)
{
glPushMatrix();
glColor3f(3.0f, 2.0f, 0.0f);
glutWireTorus(12.5, 12.5, 12.5, 12.5);
glPopMatrix(); //we are done messing with the matrix needed to draw the torus.. trash it
}
glutSwapBuffers();
}
void MouseHandling(int button, int state, int x, int y)
{
switch(button)
{
case GLUT_LEFT_BUTTON:
{
if(state == GLUT_DOWN)
{
if(choise == 1)
{
cout << "Setting variable to 0...\t";
choise = 0;
cout << "[DONE]\n";
}
else if(choise == 0)
{
cout << "Setting variable to 2...\t";
choise = 2;
cout << "[DONE]\n";;
}
else if(choise == 2)
{
cout << "Setting variable to 1...\t";
choise = 3;
cout << "[DONE]\n";;
}
else if(choise == 3)
{
cout << "Setting variable to 1...\t";
choise = 4;
cout << "[DONE]\n";;
}
else if(choise == 4)
{
cout << "Setting variable to 1...\t";
choise = 1;
cout << "[DONE]\n";;
}
}
break;
}
}
}
void KeyboardHandling(unsigned char key, int x, int y)
{
if(key == 'q')
{
cout << "Setting variable so a quad will be drawn...\t\t";
choise = 1;
cout << "[DONE]\n";
}
else if(key == 't')
{
cout << "Setting variable so a triangle will be drawn...\t\t";
choise = 0;
cout << "[DONE]\n";
}
else if(key == 'r')
{
cout << "Setting variable so a LINE will be drawn...\t\t";
choise = 2;
cout << "[DONE]\n";
}
else if(key == 'w')
{
cout << "Setting variable so a LINE will be drawn...\t\t";
choise = 3;
cout << "[DONE]\n";
}
else if(key == 'e')
{
cout << "Setting variable so a LINE will be drawn...\t\t";
choise = 4;
cout << "[DONE]\n";
}
else if(key == 'f')
{
cout << "Setting to fullscreen...\t\t\t\t";
glutFullScreen();
cout << "[DONE]\n";
}
}
|
Kindly Help
Regards
Dennis
|
|
|
|
|
|
|