Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm just getting blank output when I try to execute. Please help me. My suspicion is on Reshape Function


Code:

C++
#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); //Front
	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); //Side(Left)
	glVertex3fv(vm[0]);
	glVertex3fv(vm[8]);
	glVertex3fv(vm[12]);
	glVertex3fv(vm[4]);
	glEnd();
	glColor3f(0.1, 0.5, 0.6);
	glBegin(GL_POLYGON); //Top(Left)
	glVertex3fv(vm[8]);
	glVertex3fv(vm[16]);
	glVertex3fv(vm[18]);
	glVertex3fv(vm[12]);
	glEnd();
	glColor3f(0.1, 0.5, 0.6);
	glBegin(GL_POLYGON); //Side(Right)
	glVertex3fv(vm[3]);
	glVertex3fv(vm[11]);
	glVertex3fv(vm[15]);
	glVertex3fv(vm[7]);
	glEnd();
	glColor3f(0.1, 0.5, 0.6);
	glBegin(GL_POLYGON); //Top(Right)
	glVertex3fv(vm[11]);
	glVertex3fv(vm[17]);
	glVertex3fv(vm[19]);
	glVertex3fv(vm[5]);
	glEnd();
	glColor3f(0.1, 0.5, 0.6);
	glBegin(GL_POLYGON); //Rear
	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); //Top
	glVertex3fv(vm[16]);
	glVertex3fv(vm[17]);
	glVertex3fv(vm[19]);
	glVertex3fv(vm[18]);
	glEnd();
	glColor3f(0.1, 0.5, 0.6);
	glBegin(GL_POLYGON); //Inside(Ceiling)
	glVertex3fv(vm[9]);
	glVertex3fv(vm[10]);
	glVertex3fv(vm[14]);
	glVertex3fv(vm[13]);
	glEnd();
	glColor3f(0.1, 0.5, 0.6);
	glBegin(GL_POLYGON); //Inside(Left)
	glVertex3fv(vm[2]);
	glVertex3fv(vm[10]);
	glVertex3fv(vm[14]);
	glVertex3fv(vm[6]);
	glEnd();
	glColor3f(0.1, 0.5, 0.6);
	glBegin(GL_POLYGON); //Inside(Right)
	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();
}
Posted
Updated 6-May-15 6:36am
v3
Comments
Richard MacCutchan 6-May-15 12:26pm    
No one is able to read this. Please format your code properly and add some useful details about your problem and where you think it occurs.
Sergey Alexandrovich Kryukov 6-May-15 12:30pm    
Help with what? This is not a question. You are not outputting anything, that's all.
—SA
KarstenK 6-May-15 12:54pm    
Try to debug via only executing of parts of the code or an simplifyed sample.
chandanadhikari 7-May-15 8:17am    
as already pointed out please try debugging the code.
Is the top level window titled "Gun" displayed correctly ?

If not then, 'Reshape' is not the culprit as it is not the first function which is called inside main .If the functions before 'Reshape' worked correctly then the top level window would come up.

If top level window comes up fine, then try calling glutDisplayFunc before glutReshapeFunc . Just a suggestion, it might work.

1 solution

Thank you so much for your replies. :) I figured out the real problem and it was with the co-ordinates. I lowered them to around 1.0 proportionately and I got some part of the output.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900