I'm just getting blank output when I try to execute. Please help me. My suspicion is on Reshape Function
Code:
#include"stdio.h"
#include"stdlib.h"
#include"GL/glut.h"
GLfloat vm[][3] = { { 100.0, 300.0, 20.0 }, { 100.0, 300.0, 15.0 }, { 100.0, 300.0, -20.0 }, { 100.0, 300.0, -15.0 }, { 400.0, 300.0, 20.0 },
{ 400.0, 300.0, 15.0 }, { 400.0, 300.0, -20.0 }, { 400.0, 300.0, -15.0 }, { 100.0, 380.0, 20.0 }, { 100.0, 380.0, 15.0 },
{ 100.0, 380.0, -20.0 }, { 100.0, 380.0, -15.0 }, { 400.0, 380.0, 20.0 }, { 400.0, 380.0, 15.0 }, { 400.0, 380.0, -20.0 },
{ 400.0, 380.0, -15.0 }, { 100.0, 400.0, 15.0 }, { 100.0, 400.0, -15.0 }, { 100.0, 400.0, 15.0 }, { 100.0, 400.0, -15.0 } };
GLfloat vs[][3] = { { 80.0, 280.0, 20.0 }, { 80.0, 280.0, -20.0 }, { 80.0, 300.0, 20.0 }, { 80.0, 300.0, 15.0 }, { 80.0, 300.0, -15.0 },
{ 80.0, 300.0, -20.0 }, { 80.0, 320.0, 12.0 }, { 80.0, 320.0, -12.0 }, { 80.0, 360.0, 12.0 }, { 80.0, 360.0, -12.0 },
{ 80.0, 380.0, 15.0 }, { 80.0, 380.0, -15.0 }, { 550.0, 300.0, 20.0 }, { 550.0, 300.0, 15.0 }, { 550.0, 300.0, -15.0 },
{ 550.0, 300.0, -20.0 }, { 550.0, 380.0, 15.0 }, { 550.0, 380.0, -15.0 }, { 550.0, 280.0, 20.0 }, { 550.0, -280.0, 20.0 },
{ 510.0, 250.0, 12.0 }, { 510.0, 250.0, -12.0 }, { 560.0, 100.0, 18.0 }, { 560.0, 100.0, -18.0 }, { 480.0, 100.0, 12.0 },
{ 480.0, 100.0, -12.0 }, { 150.0, 250.0, 12.0 }, { 150.0, 250.0, -12.0 }, { 460.0, 280.0, 12.0 }, { 460.0, 280.0, -12.0 } };
GLfloat color[][3] = { { 0.1, 0.5, 0.6 }, { 0.0, 0.6, 0.3 } };
void draw_gun_barrel()
{
glColor3fv(color[0]);
glBegin(GL_POLYGON); glVertex3fv(vm[1]);
glVertex3fv(vm[0]);
glVertex3fv(vm[8]);
glVertex3fv(vm[16]);
glVertex3fv(vm[17]);
glVertex3fv(vm[11]);
glVertex3fv(vm[3]);
glVertex3fv(vm[2]);
glVertex3fv(vm[10]);
glVertex3fv(vm[9]);
glEnd();
glColor3fv(color[0]);
glBegin(GL_POLYGON); glVertex3fv(vm[0]);
glVertex3fv(vm[8]);
glVertex3fv(vm[12]);
glVertex3fv(vm[4]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON); glVertex3fv(vm[8]);
glVertex3fv(vm[16]);
glVertex3fv(vm[18]);
glVertex3fv(vm[12]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON); glVertex3fv(vm[3]);
glVertex3fv(vm[11]);
glVertex3fv(vm[15]);
glVertex3fv(vm[7]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON); glVertex3fv(vm[11]);
glVertex3fv(vm[17]);
glVertex3fv(vm[19]);
glVertex3fv(vm[5]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON); glVertex3fv(vm[5]);
glVertex3fv(vm[4]);
glVertex3fv(vm[12]);
glVertex3fv(vm[18]);
glVertex3fv(vm[19]);
glVertex3fv(vm[15]);
glVertex3fv(vm[7]);
glVertex3fv(vm[6]);
glVertex3fv(vm[14]);
glVertex3fv(vm[13]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON); glVertex3fv(vm[16]);
glVertex3fv(vm[17]);
glVertex3fv(vm[19]);
glVertex3fv(vm[18]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON); glVertex3fv(vm[9]);
glVertex3fv(vm[10]);
glVertex3fv(vm[14]);
glVertex3fv(vm[13]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON); glVertex3fv(vm[2]);
glVertex3fv(vm[10]);
glVertex3fv(vm[14]);
glVertex3fv(vm[6]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON); glVertex3fv(vm[1]);
glVertex3fv(vm[9]);
glVertex3fv(vm[13]);
glVertex3fv(vm[5]);
glEnd();
}
void draw_gun_frame()
{
glColor3f(0.0, 0.6, 0.3);
glBegin(GL_POLYGON);
glVertex3fv(vs[0]);
glVertex3fv(vs[1]);
glVertex3fv(vs[2]);
glVertex3fv(vs[3]);
glVertex3fv(vs[10]);
glVertex3fv(vs[11]);
glVertex3fv(vs[4]);
glVertex3fv(vs[5]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON);
glVertex3fv(vs[6]);
glVertex3fv(vs[7]);
glVertex3fv(vs[9]);
glVertex3fv(vs[8]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON);
glVertex3fv(vs[2]);
glVertex3fv(vs[10]);
glVertex3fv(vs[13]);
glVertex3fv(vs[16]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON);
glVertex3fv(vs[2]);
glVertex3fv(vs[10]);
glVertex3fv(vs[18]);
glVertex3fv(vs[13]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON);
glVertex3fv(vs[2]);
glVertex3fv(vs[3]);
glVertex3fv(vs[13]);
glVertex3fv(vs[12]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON);
glVertex3fv(vs[18]);
glVertex3fv(vs[20]);
glVertex3fv(vs[22]);
glVertex3fv(vs[24]);
glVertex3fv(vs[26]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON);
glVertex3fv(vs[4]);
glVertex3fv(vs[11]);
glVertex3fv(vs[17]);
glVertex3fv(vs[14]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON);
glVertex3fv(vs[5]);
glVertex3fv(vs[1]);
glVertex3fv(vs[19]);
glVertex3fv(vs[15]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON);
glVertex3fv(vs[4]);
glVertex3fv(vs[5]);
glVertex3fv(vs[15]);
glVertex3fv(vs[14]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON);
glVertex3fv(vs[19]);
glVertex3fv(vs[21]);
glVertex3fv(vs[23]);
glVertex3fv(vs[25]);
glVertex3fv(vs[27]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON);
glVertex3fv(vs[26]);
glVertex3fv(vs[27]);
glVertex3fv(vs[25]);
glVertex3fv(vs[24]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON);
glVertex3fv(vs[0]);
glVertex3fv(vs[1]);
glVertex3fv(vs[27]);
glVertex3fv(vs[26]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON);
glVertex3fv(vs[22]);
glVertex3fv(vs[23]);
glVertex3fv(vs[25]);
glVertex3fv(vs[24]);
glEnd();
glColor3f(0.1, 0.5, 0.6);
glBegin(GL_POLYGON);
glVertex3fv(vs[11]);
glVertex3fv(vs[12]);
glVertex3fv(vs[17]);
glVertex3fv(vs[16]);
glEnd();
}
static GLfloat theta[] = { 0.0, 0.0, 0.0 };
static GLint axis = 2;
static GLdouble viewer[] = { 0.0, 0.0, 1.0 };
void display()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(viewer[0], viewer[1], viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
draw_gun_frame();
draw_gun_barrel();
glFlush();
glutSwapBuffers();
}
void Reshape(int w, int h)
{
if (h == 0)
h = 1;
GLfloat aspect = w/h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glFrustum(-2.0, 2.0, -2.0*(GLfloat)h / (GLfloat)w, 2.0*(GLfloat)w / (GLfloat)w, 2.0, 20.0);
else
glFrustum(-2.0, 2.0, -2.0*(GLfloat)w / (GLfloat)h, 2.0*(GLfloat)w / (GLfloat)h, 2.0, 20.0);
glMatrixMode(GL_MODELVIEW);
gluPerspective(45.0, aspect, 0.1, 1000.0);
}
void main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1200, 600);
glutCreateWindow("Gun");
glutReshapeFunc(Reshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}