OpenGL
OpenGL
1 Task
Write a program that displays a triangle ,translates and rotates it to be some angle (for example 45 degree in 2D space)and shows the triangle in new shape and position(Figure 5)You can use the functions glTranslatef,glRotate functions for translations. You can use glutbitmapString function to draw the string on GLUT window.
#include <windows.h>
#include <GL/glut.h>
/* Initialize OpenGL Graphics */
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
whenever the window needs to be re-painted. */
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-0.5f, 0.4f, 0.0f);
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.3f, -0.3f);
glVertex2f( 0.3f, -0.3f);
glVertex2f( 0.3f, 0.3f);
glVertex2f(-0.3f, 0.3f);
glEnd();
glTranslatef(0.1f, -0.7f, 0.0f);
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(-0.3f, -0.3f);
glVertex2f( 0.3f, -0.3f);
glVertex2f( 0.3f, 0.3f);
glVertex2f(-0.3f, 0.3f);
glEnd();
glTranslatef(-0.3f, -0.2f, 0.0f);
glBegin(GL_QUADS);
glColor3f(0.2f, 0.2f, 0.2f);
glVertex2f(-0.2f, -0.2f);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f( 0.2f, -0.2f);
glColor3f(0.2f, 0.2f, 0.2f);
glVertex2f( 0.2f, 0.2f);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(-0.2f, 0.2f);
glEnd();
glTranslatef(1.1f, 0.2f, 0.0f);
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(-0.3f, -0.2f);
glVertex2f( 0.3f, -0.2f);
glVertex2f( 0.0f, 0.3f);
glEnd();
glTranslatef(0.2f, -0.3f, 0.0f);
glRotatef(180.0f, 0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.3f, -0.2f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f( 0.3f, -0.2f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f( 0.0f, 0.3f);
glEnd();
glRotatef(-180.0f, 0.0f, 0.0f, 1.0f);
glTranslatef(-0.1f, 1.0f, 0.0f);
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex2f(-0.1f, -0.2f);
glVertex2f( 0.1f, -0.2f);
glVertex2f( 0.2f, 0.0f);
glVertex2f( 0.1f, 0.2f);
glVertex2f(-0.1f, 0.2f);
glVertex2f(-0.2f, 0.0f);
glEnd();
glFlush();
}
}
Task 1
Write a program that displays a semi oval (eclipse) shape
#include <windows.h>
#include <GL/glut.h>
void Draweclipse(float cx, float cy, float r, int num_segments)
{
float theta = 2 * 3.1415926 / float(num_segments);
float c = cosf(theta);
float s = sinf(theta);
float t;
float x = r;//we start at angle = 0
float y = 0;
glBegin(GL_LINE_LOOP);
for(int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x + cx, y + cy);
t = x;
x = c * x – s * y;
y = s * t + c * y;
}
glEnd();
}
Task 2
Write a program that displays two overlapping triangles
#include <GL/glut.h>
#include <stdlib.h>
static int leftFirst = GL_TRUE;
static void init(void)
{
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glShadeModel (GL_FLAT);
glClearColor (0.0, 0.0, 0.0, 0.0);
}
static void drawLeftTriangle(void)
{
glBegin (GL_TRIANGLES);
glColor4f(1.0, 1.0, 0.0, 0.75);
glVertex3f(0.1, 0.9, 0.0);
glVertex3f(0.1, 0.1, 0.0);
glVertex3f(0.7, 0.5, 0.0);
glEnd();
}
static void drawRightTriangle(void)
{
glBegin (GL_TRIANGLES);
glColor4f(0.0, 1.0, 1.0, 0.75);
glVertex3f(0.9, 0.9, 0.0);
glVertex3f(0.3, 0.5, 0.0);
glVertex3f(0.9, 0.1, 0.0);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
if (leftFirst) {
drawLeftTriangle();
drawRightTriangle();
}
else {
drawRightTriangle();
drawLeftTriangle();
}
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D (0.0, 1.0, 0.0, 1.0*(GLfloat)h/(GLfloat)w);
else
gluOrtho2D (0.0, 1.0*(GLfloat)w/(GLfloat)h, 0.0, 1.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case ‘t’:
case ‘T’:
leftFirst = !leftFirst;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
/* Main Loop
* Open window with initial window size, title bar,
* RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (200, 200);
glutCreateWindow (argv[0]);
init();
glutReshapeFunc (reshape);
glutKeyboardFunc (keyboard);
glutDisplayFunc (display);
glutMainLoop();
return 0;
}
Task 2
Write a program to calculate the sum and the difference of two matrices .Use this program to verify the answers of task1.Show the screenshot of output of each step while verifying the task 1.
#include <stdio.h>
#include <stdlib.h>
void readmatA();
void printmatA();
void readmatB();
void printmatB();
void sum();
void diff();
int a[10][10], b[10][10], sumarray[10][10], arraydiff[10][10];
int i, j, row1, column1, row2, column2;
void main()
{
printf(“Enter the order of the matrix A \n”);
scanf(“%d %d”, &row1, &column1);
printf(“Enter the order of the matrix B \n”);
scanf(“%d %d”, &row2, &column2);
if (row1 != row2 && column1 != column2)
{
printf(“Addition and subtraction are possible \n”);
exit(1);
}
else
{
printf(“Enter the elements of matrix A \n”);
readmatA();
printf(“MATRIX A is \n”);
printmatA();
printf(“Enter the elements of matrix B \n”);
readmatB();
printf(“MATRIX B is \n”);
printmatB();
sum();
diff();
}
}
/* Function to read a matrix A */
void readmatA()
{
for (i = 0; i < row1; i++)
{
for (j = 0; j < column1; j++)
{
scanf(“%d”, &a[i][j]);
}
}
return;
}
/* Function to read a matrix B */
void readmatB()
{
for (i = 0; i < row2; i++)
{
for (j = 0; j < column2; j++)
{
scanf(“%d”, &b[i][j]);
}
}
}
/* Function to print a matrix A */
void printmatA()
{
for (i = 0; i < row1; i++)
{
for (j = 0; j < column1; j++)
{
printf(“%3d”, a[i][j]);
}
printf(“\n”);
}
}
/* Function to print a matrix B */
void printmatB()
{
for (i = 0; i < row2; i++)
{
for (j = 0; j < column2; j++)
{
printf(“%3d”, b[i][j]);
}
printf(“\n”);
}
}
/* Function to do the sum of elements of matrix A and Matrix B */
void sum()
{
for (i = 0; i < row1; i++)
{
for (j = 0; j < column2; j++)
{
sumarray[i][j] = a[i][j] + b[i][j];
}
}
printf(“Sum matrix is \n”);
for (i = 0; i < row1; i++)
{
for (j = 0; j < column2; j++)
{
printf(“%3d”, sumarray[i][j]) ;
}
printf(“\n”);
}
return;
}
/* Function to do the difference of elements of matrix A and Matrix B */
void diff()
{
for (i = 0; i < row1; i++)
{
for (j = 0; j < column2; j++)
{
arraydiff[i][j] = a[i][j] – b[i][j];
}
}
printf(“Difference matrix is \n”);
for (i = 0; i < row1; i++)
{
for (j = 0; j < column2; j++)
{
printf(“%3d”, arraydiff[i][j]);
}
printf(“\n”);
}
return;
}2
Task 1
Write a program in OpenGL that displays a triangle as show in figure 1
#include <GL/glut.h>
#include <stdlib.h>
void display(void)
{
glClearColor(1,1,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(0.5,0,0);
glVertex2f(300.0,210.0);
glVertex2f(340.0,215.0);
glVertex2f(320.0,250.0);
glEnd();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,500);
glutInitWindowPosition(1,1);
glutCreateWindow(“Triangle”);
glutDisplayFunc(display);
glutMainLoop();
return EXIT_SUCCESS;
}
Task 2
Write a program in OpenGL that displays a smiling face as shown in Figure 2
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;
void *p;
initgraph(&gd, &gm, “C:\\TC\\BGI”);
setcolor(YELLOW);
circle(50, 100, 25);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(50, 100, YELLOW);
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
fillellipse(44, 85, 2, 6);
fillellipse(56, 85, 2, 6);
ellipse(50, 100, 205, 335, 20, 9);
ellipse(50, 100, 205, 335, 20, 10);
ellipse(50, 100, 205, 335, 20, 11);
area = imagesize(left, top, left + 50, top + 50);
p = malloc(area);
setcolor(WHITE);
settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 2);
outtextxy(155, 451, “Smiling Face Animation”);
setcolor(BLUE);
rectangle(0, 0, 639, 449);
while(!kbhit())
{
temp1 = 1 + random (588);
temp2 = 1 + random (380);
getimage(left, top, left + 50, top + 50, p);
putimage(left, top, p, XOR_PUT);
putimage(temp1 , temp2, p, XOR_PUT);
delay(100);
left = temp1;
top = temp2;
}
getch();
closegraph();
return 0;
}