0% found this document useful (0 votes)
90 views

Coding Solution

This document describes a program that creates two OpenGL windows. The first window displays a teapot that can be colored differently using a right-click menu. The second window displays a wireframe teapot that can be translated, rotated, or scaled using another right-click menu. The program initializes GLUT, creates the two windows at different positions, sets up display and menu functions for each window, and enters the main loop.

Uploaded by

Sen Sokha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Coding Solution

This document describes a program that creates two OpenGL windows. The first window displays a teapot that can be colored differently using a right-click menu. The second window displays a wireframe teapot that can be translated, rotated, or scaled using another right-click menu. The program initializes GLUT, creates the two windows at different positions, sets up display and menu functions for each window, and enters the main loop.

Uploaded by

Sen Sokha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Royal University of Phnom Penh Subject: Computer Graphic 2

Faculty of Engineering Lecturer: Kor Sokchea

Name : Tan Samnieng


Group : G1 (Paid)
Department: ITE

Lab2: Two Windows

#include<iostream>
#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>
using namespace std;

GLdouble translate[16] = { 1.0, 0.0, 0.0, 0.0,


0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 };
static int window;
static int menu_id;
static int value = 0;
void menu(int num){
if (num == 0){
glutDestroyWindow(window);
exit(0);
}
else{
value = num;
}
glutPostRedisplay();
}
void createMenu(void){
menu_id = glutCreateMenu(menu);
glutAddMenuEntry("Red", 1);
glutAddMenuEntry("Green", 2);
glutAddMenuEntry("Yellow", 3);
glutAddMenuEntry("Blue", 4);
glutAddMenuEntry("magenta",5 );
glutAddMenuEntry("Cyan", 6);
glutAddMenuEntry("Quit", 0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void createMenu2(void){
menu_id = glutCreateMenu(menu);
glutAddMenuEntry("Translation axis x", 1);
glutAddMenuEntry("Translation axis y", 2);
glutAddMenuEntry("Rotate", 3);
glutAddMenuEntry("scale", 4);
glutAddMenuEntry("Quit", 0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void init(){
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(500, 500, 0, 500);
}
void display()
{
Semester 1 - Year 4 15/10/2017
/* clear window */
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0); // white
Royal University of Phnom Penh Subject: Computer Graphic 2
Faculty of Engineering Lecturer: Kor Sokchea

void display()
{
/* clear window */
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0); // white
if (value == 1){
glColor3f(1.0, 0.0, 0.0); // red
}else if (value == 2){
glColor3f(0.0, 1.0, 0.0); // green
}
else if (value == 3){
glColor3f(1.0, 1.0, 0.0); // yellow
}
else if (value == 4){
glColor3f(0.0, 0.0, 1.0); // blue
}
else if (value == 5){
glColor3f(1.0, 0.0, 1.0); // magenta
}
else if (value == 6){
glColor3f(0.0, 1.0, 1.0); // cyan
}
/* draw scene */
glutSolidTeapot(0.5);
glutSwapBuffers();
}
void display1()
{
glClear(GL_COLOR_BUFFER_BIT);
if (value == 1){
translate[12] += 0.1;
glMultMatrixd(translate);
value = 0;
}
else if (value == 2){
translate[13] += 0.1;
glMultMatrixd(translate);
value = 0;
}
else if (value == 3){
glRotatef(20.0, 1.0, 0.0, 0.0);
value = 0;
}
else if (value == 4){
glScalef(0.1, 0.1, 0.1);
value = 0;
}
glColor3f(0.0, 1.0, 0.0);
glutWireTeapot(0.5);
glutSwapBuffers();
}
int main(int argc, char** argv){

//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500); //Set the window size
glutInitWindowPosition(3, 5);
//Create the window
glutCreateWindow("Window1");
init();
createMenu();
glutDisplayFunc(display);
glutInitWindowPosition(530, 5);
glutCreateWindow("Window2");
Semester init();
1 - Year 4 15/10/2017
createMenu2();
glutDisplayFunc(display1);
glutMainLoop();
Royal University of Phnom Penh Subject: Computer Graphic 2
Faculty of Engineering Lecturer: Kor Sokchea

glutCreateWindow("Window2");
init();
createMenu2();
glutDisplayFunc(display1);
glutMainLoop();
return 0;

Semester 1 - Year 4 15/10/2017

You might also like