Untitled

Anonymous
plain_text
03/01/2026 6:52 PM
26.8 KB
9
Indexable
//osnovne sablonske fje
void CGLRenderer::PrepareScene(CDC *pDC)
{
	wglMakeCurrent(pDC->m_hDC, m_hrc);
	//---------------------------------
	glClearColor(1.0, 1.0, 1.0, 1.0);
	glEnable(GL_DEPTH_TEST);

	slika = LoadTexture("");
	
	//---------------------------------
	wglMakeCurrent(NULL, NULL);
}

void CGLRenderer::DrawScene(CDC *pDC)
{
	wglMakeCurrent(pDC->m_hDC, m_hrc);
	//---------------------------------
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	if (beta > 1.5)
		beta = 1.5;
	if (beta < 0.1)
		beta = 0.1;
	double camx = dist * cos(beta) * sin(alfa);
	double camy = dist * sin(beta);
	double camz = dist * cos(beta) * cos(alfa);
	gluLookAt(camx, camy, camz, 0, 0, 0, 0, 1, 0);
	DrawAxes();
	DrawEnvCube(100);

	


	glFlush();
	SwapBuffers(pDC->m_hDC);
	//---------------------------------
	wglMakeCurrent(NULL, NULL);
}

void CGLRenderer::Reshape(CDC *pDC, int w, int h)
{
	wglMakeCurrent(pDC->m_hDC, m_hrc);
	//---------------------------------
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(55, (double)w / (double)h, 0.1, 2000);
	glMatrixMode(GL_MODELVIEW);
	//---------------------------------
	wglMakeCurrent(NULL, NULL);
}

void CGLRenderer::DestroyScene(CDC *pDC)
{
	wglMakeCurrent(pDC->m_hDC, m_hrc);
	// ... 
	wglMakeCurrent(NULL,NULL); 
	if(m_hrc) 
	{
		wglDeleteContext(m_hrc);
		m_hrc = NULL;
	}
}

void CGLRenderer::DrawAxes()
{
	glColor3f(1, 1, 1);
	glLineWidth(2.0f);
	glBegin(GL_LINES);
	{
		glColor3f(0, 0, 1);
		glVertex3f(0, 0, 0);
		glVertex3f(10, 0, 0);

		glColor3f(1, 0, 0);
		glVertex3f(0, 0, 0);
		glVertex3f(0, 10, 0);

		glColor3f(0, 1, 0);
		glVertex3f(0, 0, 0);
		glVertex3f(0, 0, 10);

	}
	glEnd();
}

UINT CGLRenderer::LoadTexture(char* fileName)
{
	UINT texID;
	DImage img;
	img.Load(CString(fileName));

	glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

	glGenTextures(1, &texID);
	glBindTexture(GL_TEXTURE_2D, texID);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, img.Width(), img.Height(), GL_BGRA_EXT, GL_UNSIGNED_BYTE, img.GetDIBBits());


	return texID;
}

void CGLRenderer::DrawEnvCube(double a)
{
	double h = a / 2;
	glEnable(GL_TEXTURE_2D);
	//front
	glColor3f(1, 1, 1);
	glBindTexture(GL_TEXTURE_2D, front);
	glBegin(GL_QUADS);
	{
		glTexCoord2f(0, 0);
		glVertex3d(-h, a, -h);
		glTexCoord2f(0, 1);
		glVertex3d(-h, 0, -h);
		glTexCoord2f(1, 1);
		glVertex3d(h, 0, -h);
		glTexCoord2f(1, 0);
		glVertex3d(h, a, -h);
	}
	glEnd();

	//back
	glColor3f(1, 1, 1);
	glBindTexture(GL_TEXTURE_2D, back);
	glBegin(GL_QUADS);
	{
		glTexCoord2f(0, 0);
		glVertex3d(h, a, h);
		glTexCoord2f(0, 1);
		glVertex3d(h, 0, h);
		glTexCoord2f(1, 1);
		glVertex3d(-h, 0, h);
		glTexCoord2f(1, 0);
		glVertex3d(-h, a, h);
	}
	glEnd();

	//left
	glColor3f(1, 1, 1);
	glBindTexture(GL_TEXTURE_2D, left);
	glBegin(GL_QUADS);
	{
		glTexCoord2f(0, 0);
		glVertex3d(-h, a, h);
		glTexCoord2f(0, 1);
		glVertex3d(-h, 0, h);
		glTexCoord2f(1, 1);
		glVertex3d(-h, 0, -h);
		glTexCoord2f(1, 0);
		glVertex3d(-h, a, -h);
	}
	glEnd();

	//right
	glColor3f(1, 1, 1);
	glBindTexture(GL_TEXTURE_2D, right);
	glBegin(GL_QUADS);
	{
		glTexCoord2f(0, 0);
		glVertex3d(h, a, -h);
		glTexCoord2f(0, 1);
		glVertex3d(h, 0, -h);
		glTexCoord2f(1, 1);
		glVertex3d(h, 0, h);
		glTexCoord2f(1, 0);
		glVertex3d(h, a, h);
	}
	glEnd();

	//bot
	glColor3f(1, 1, 1);
	glBindTexture(GL_TEXTURE_2D, bot);
	glBegin(GL_QUADS);
	{
		glTexCoord2f(0, 0);
		glVertex3d(-h, 0, -h);
		glTexCoord2f(0, 1);
		glVertex3d(-h, 0, h);
		glTexCoord2f(1, 1);
		glVertex3d(h, 0, h);
		glTexCoord2f(1, 0);
		glVertex3d(h, 0, -h);
	}
	glEnd();
	//top
	glColor3f(1, 1, 1);
	glBindTexture(GL_TEXTURE_2D, top);
	glBegin(GL_QUADS);
	{
		glTexCoord2f(0, 0);
		glVertex3d(-h, a, -h);
		glTexCoord2f(0, 1);
		glVertex3d(-h, a, h);
		glTexCoord2f(1, 1);
		glVertex3d(h, a, h);
		glTexCoord2f(1, 0);
		glVertex3d(h, a, -h);
	}
	glEnd();
	glDisable(GL_TEXTURE_2D);
}
//za bager poligoni
void CGLRenderer::DrawPolygon(POINTF* points, POINTF* texCoords, int n)
{
	glEnable(GL_TEXTURE_2D);
	glColor3f(1, 1, 1);
	glBegin(GL_TRIANGLE_FAN);
	for (int i = 0; i < n; i++)
	{
		glTexCoord2f(texCoords[i].x, texCoords[i].y);
		glVertex3f(points[i].x, points[i].y, 0);
	}
	glEnd();
}

void CGLRenderer::DrawExtrudedPolygon(POINTF* points, POINTF* texCoords, int n, float zh, float r, float g, float b)
{
	glPushMatrix();

	DrawPolygon(points, texCoords, n);
	glDisable(GL_TEXTURE_2D);
	glBegin(GL_QUAD_STRIP);
		for (int i = 0; i < n; i++)
		{
			glColor3f(r, g, b);
			glVertex3f(points[i].x, points[i].y, 0);
			glVertex3f(points[i].x, points[i].y, zh);
		}
		glVertex3f(points[0].x, points[0].y, 0);
		glVertex3f(points[0].x, points[0].y, zh);
	glEnd();
	glEnable(GL_TEXTURE_2D);

	glTranslatef(0, 0, zh);
	DrawPolygon(points, texCoords, n);
	glPopMatrix();
}

void CGLRenderer::DrawBody()
{
	glPushMatrix();
	glTranslatef(0, 0, -2);

	glColor3f(1, 1, 1);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, excavator);

	int n = 5;
	POINTF points[5] = { {-2, 4}, {-2, 0}, {2, 0}, {2, 2}, {0, 4} };
	POINTF texCoords[5] = { {0.5, 0}, {0.5, 0.5}, {1, 0.5}, {1, 0.25}, {0.75, 0} };
	DrawExtrudedPolygon(points, texCoords, n, 4, 0.96, 0.5, 0.12);
}
//za lokvanj

void CGLRenderer::DrawScene(CDC *pDC)
{
	wglMakeCurrent(pDC->m_hDC, m_hrc);
	//---------------------------------
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	double eyeX = cam_dist * cos(cam_alpha * 3.14 / 180.0) * sin(cam_beta * 3.14 / 180.0);
	double eyeY = cam_dist * cos(cam_beta * 3.14 / 180.0);
	double eyeZ = cam_dist * sin(cam_alpha * 3.14 / 180.0) * sin(cam_beta * 3.14 / 180.0);

	gluLookAt(
		eyeX, eyeY, eyeZ,   // pozicija posmatrača
		0.0, 0.0, 0.0,      // gleda u centar scene
		0.0, 1.0, 0.0       // vektor "gore"
	);



	GLfloat light0_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // bela difuzna
	GLfloat light0_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // bela spekularna
	GLfloat light0_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f }; // tražena ambijentalna

	// Pravac svetlosti "iz pravca posmatrača" => (0,0,1) posle transformacije kamere
	GLfloat light0_position[] = { 0.0f, 0.0f, 1.0f, 0.0f };

	glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
	glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
	glLightfv(GL_LIGHT0, GL_POSITION, light0_position);

	glEnable(GL_LIGHT0);

	GLfloat light1_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
	GLfloat light1_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
	GLfloat light1_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f };

	GLfloat light1_position[] = { 0.0f, 1.0f, 1.0f, 0.0f };

	glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
	glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
	glLightfv(GL_LIGHT1, GL_POSITION, light1_position);

	glEnable(GL_LIGHT1);

	glEnable(GL_LIGHTING);
	glEnable(GL_NORMALIZE);

	DrawAxes();

	DrawSphere(50, 30, 15);


	glPushMatrix();
	DrawFlower();
	glPopMatrix();

	glPushMatrix();
	glTranslatef(-9, 0, -9);
	DrawFlower();
	glPopMatrix();

	glPushMatrix();
	glTranslatef(9, 0, -9);
	DrawFlower();
	glPopMatrix();


	glFlush();
	SwapBuffers(pDC->m_hDC);
	//---------------------------------
	wglMakeCurrent(NULL, NULL);
}

void CGLRenderer::PolarToCartesian(double R, double alfa, double beta, double& x, double& y, double& z)
{
	x = R * sin(beta) * cos(alfa);
	y = R * cos(beta);
	z = R * sin(beta) * sin(alfa);
}

void CGLRenderer::DrawSphere(float R, int n, int m)
{
	glPushMatrix();

	glRotatef(-75, 0, 1, 0);
	glDisable(GL_LIGHTING);
	glColor3f(1, 1, 1);

	double alfa = 2 * 3.14 / n;
	double beta = 3.14 / m;


	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, env);

	

	for (int i = 0; i < n; i++) {

		for (int j = 0; j < m; j++) {
			double x1;
			double y1;
			double z1;

			PolarToCartesian(R, alfa*i, beta*j, x1, y1, z1);

			double x2;
			double y2;
			double z2;

			PolarToCartesian(R, alfa * (i+1), beta * j, x2, y2, z2);

			double x3;
			double y3;
			double z3;

			PolarToCartesian(R, alfa * (i + 1), beta * (j + 1), x3, y3, z3);


			double x4;
			double y4;
			double z4;

			PolarToCartesian(R, alfa * (i), beta * (j + 1), x4, y4, z4);

			double u1 = (double)i / n;
			double v1 = (double)j / m;
			double u2 = (double)(i + 1) / n;
			double v2 = v1;
			double u3 = u2;
			double v3 = (double)(j + 1) / m;
			double u4 = u1;
			double v4 = v3;

			glBegin(GL_QUADS);
				//glColor3f(1/x1, 1/y1, 1/z1);
				glTexCoord2f(u1, v1); glVertex3f(x1, y1, z1);
				glTexCoord2f(u2, v2); glVertex3f(x2, y2, z2);
				glTexCoord2f(u3, v3); glVertex3f(x3, y3, z3);
				glTexCoord2f(u4, v4); glVertex3f(x4, y4, z4);
			glEnd();
		}
	}

	glPopMatrix();

	glEnable(GL_LIGHTING);

}

void CGLRenderer::CalcRotAxis(double x1, double y1, double z1, double x2, double y2, double z2, double& x3, double& y3, double& z3)
{

	double vx = x2 - x1;
	double vy = y2 - y1;
	double vz = z2 - z1;


	double L = sqrt(vx * vx + vy * vy + vz * vz);

	if (L != 0.0) {
		x3 = vx / L;
		y3 = vy / L;
		z3 = vz / L;
	}
	else {
		x3 = y3 = z3 = 0.0;
	}
}

void CGLRenderer::DrawSphFlower(float R, int n, int m, float factor, unsigned char R1, unsigned char G1, unsigned char B1, unsigned char R2, unsigned char G2, unsigned char B2)
{
	glPushMatrix();
	glRotatef(180, 1, 0, 0);
	glDisable(GL_TEXTURE_2D);
	glEnable(GL_LIGHTING);

	

	double alfa = 2 * 3.14 / n;
	double beta = 3.14 / m;



	for (int i = 0; i < n; i++) {
		glPushMatrix();
		for (int j = 0; j < m; j++) {

			

			double x1;
			double y1;
			double z1;

			PolarToCartesian(R, alfa * i, beta * j, x1, y1, z1);

			double x2;
			double y2;
			double z2;

			PolarToCartesian(R, alfa * (i + 1), beta * j, x2, y2, z2);

			double x3;
			double y3;
			double z3;

			PolarToCartesian(R, alfa * (i + 1), beta * (j + 1), x3, y3, z3);


			double x4;
			double y4;
			double z4;

			PolarToCartesian(R, alfa * (i), beta * (j + 1), x4, y4, z4);

			float t = (float)j / (float)m;

			// difuzna boja
			GLfloat diffuse[4];
			diffuse[0] = ((1 - t) * R2 + t * R1) / 255.0f;
			diffuse[1] = ((1 - t) * G2 + t * G1) / 255.0f;
			diffuse[2] = ((1 - t) * B2 + t * B1) / 255.0f;
			diffuse[3] = 1.0f;

			// ambijentalna boja = 20% južnog pola
			GLfloat ambient[4];
			ambient[0] = 0.2f * (R1 / 255.0f);
			ambient[1] = 0.2f * (G1 / 255.0f);
			ambient[2] = 0.2f * (B1 / 255.0f);
			ambient[3] = 1.0f;

			// spekularna = bela
			GLfloat specular[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
			GLfloat shininess = 50.0f;

			glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
			glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
			glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
			glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);


			double axisX, axisY, axisZ;

			CalcRotAxis(x1, y1, z1, x2, y2, z2, axisX, axisY, axisZ);

			glTranslatef(x1, y1, z1);
			glRotatef(angleRF * factor, axisX, axisY, axisZ);
			glTranslatef(-x1, -y1, -z1);


			glBegin(GL_QUADS);
				glNormal3f(x1/R, y1/R, z1/R); glVertex3f(x1, y1, z1);
				glNormal3f(x2 / R, y2 / R, z2 / R); glVertex3f(x2, y2, z2);
				glNormal3f(x3 / R, y3 / R, z3 / R); glVertex3f(x3, y3, z3);
				glNormal3f(x4 / R, y4 / R, z4 / R); glVertex3f(x4, y4, z4);
			glEnd();
		}
		glPopMatrix();
	}

	glPopMatrix();


}
//za pauka
void CGLRenderer::DrawCone(double r, double h, int nSeg, double texU, double texV, double texR)
{
	
	glBegin(GL_TRIANGLE_FAN);
	glTexCoord2f(texU, texV);
	glVertex3f(0, h, 0);
	for (int i = 0; i <= nSeg; i++)
	{
		double angle = 2 * 3.14 / nSeg * i;
		double x1 = r * cos(angle);
		double z1 = r * sin(angle);

		double tx = texU + texR * (x1 / r);
		double ty = texV + texR * (z1 / r);

		glTexCoord2f(tx, ty);
		glVertex3f(x1, 0, z1);
	}
	glEnd();
}

void CGLRenderer::DrawSphere(double r, int nSeg, double texU, double texV, double texR)
{
	int i, j;
	double ang1, ang2;
	double dAng1, dAng2;
	dAng1 = 3.14 / nSeg;
	dAng2 = 2 * 3.14 / nSeg;
	ang1 = -3.14 / 2;

	for (i = 0; i < nSeg; i++)
	{
		ang2 = 0;
		glBegin(GL_QUAD_STRIP);
		for (j = 0; j < nSeg + 1; j++) 
		{
			double x1 = r * cos(ang1) * cos(ang2);
			double y1 = r * sin(ang1);
			double z1 = r * cos(ang1) * sin(ang2);

			double x2 = r * cos(ang1 + dAng1) * cos(ang2);
			double y2 = r * sin(ang1 + dAng1);
			double z2 = r * cos(ang1 + dAng1) * sin(ang2);

			double tx1 = texR * x1 / r + texU;
			double ty1 = texR * z1 / r + texV;

			double tx2 = texR * x2 / r + texU;
			double ty2 = texR * z2 / r + texV;

			glTexCoord2f(tx1, ty1);
			glVertex3d(x1, y1, z1);
			glTexCoord2f(tx2, ty2);
			glVertex3d(x2, y2, z2);
			
			ang2 += dAng2;

		}
		glEnd();
		ang1 += dAng1;
	}
}

void CGLRenderer::DrawLegSegment(double r, double h, int nSeg)
{
	glEnable(GL_TEXTURE_2D);
	glColor3f(1, 1, 1);
	glBindTexture(GL_TEXTURE_2D, spider);

	glPushMatrix();
	glTranslatef(0, r, 0);


	DrawSphere(r, 2 * nSeg, 0.25, 0.25, 0.245);
	glTranslatef(0, r / 2, 0);
	DrawCone(r, h, nSeg, 0.75, 0.75, 0.245);


	glPopMatrix();
}

void CGLRenderer::DrawLeg()
{
	glPushMatrix();
	glRotatef(45, 0, 0, 1);
	DrawLegSegment(1, 10, 5);
	glTranslatef(0, 10+1, 0);
	glRotatef(85,0,0,1);
	DrawLegSegment(1, 15, 5);
	glPopMatrix();

}

void CGLRenderer::DrawSpiderBody()
{
	glEnable(GL_TEXTURE_2D);
	glColor3f(1, 1, 1);
	glBindTexture(GL_TEXTURE_2D, spider);
	glPushMatrix();

	//glava
	glPushMatrix();
	glTranslatef(-2 - 1.5, 0, 0);
	glScalef(1, 0.5, 1);
	DrawSphere(2, 10, 0.75, 0.25, 0.245);
	glPopMatrix();

	//grudi
	glPushMatrix();
	glScalef(1.0, 0.5, 1.0);
	DrawSphere(3, 10, 0.25, 0.25, 0.245);
	glPopMatrix();

	//stomak
	glPushMatrix();
	glTranslatef(5 + 1.5, 0, 0);
	glScalef(1.0, 0.8, 1.0);
	DrawSphere(5, 10, 0.25, 0.25, 0.245);
	glPopMatrix();

	glPopMatrix();
}

void CGLRenderer::DrawSpider()
{
	glTranslatef(0, 3, 0);
	DrawSpiderBody();

	for (int i = 1; i <= 4; i++)
	{
		glPushMatrix();
		glRotatef(30 * i, 0, 1, 0);
		DrawLeg();
		glPopMatrix();


		for (int i = 1; i <= 4; i++)
		{
			glPushMatrix();
			glRotatef(30 * i + 180, 0, 1, 0);
			DrawLeg();
			glPopMatrix();
		}
	}
}
//za lampu
void CGLRenderer::DrawCylinder(double r1, double r2, double h, int nSeg, int texMode, bool bIsOpen)
{
	if (!bIsOpen) {
		texMode = 0;
	}
	double repy1 = 0;
	double repy2 = 0;
	glEnable(GL_TEXTURE_2D);
	if (texMode == 0) {
		repy1 = 0.5;
		repy2 = 1.0;
	}
	else {
		repy1 = 0.0;
		repy2 = 0.5;
	}

	glBindTexture(GL_TEXTURE_2D, lampa);

	double alfa = 2 * 3.14 / nSeg;

	if (!bIsOpen) {
		glBegin(GL_TRIANGLE_FAN);
		glNormal3f(0, 1, 0);
		glTexCoord2f(0.5, 0.25);
		glVertex3f(0, h, 0);

		for (int i = 0; i <= nSeg; i++) {
			double x = r1 * cos(i * alfa);
			double z = r1 * sin(i * alfa);

			glTexCoord2f(0.5 + (x/r1)/4, (z/r1)/4 + 0.25);

			glVertex3f(x, h, z);
		}
		glEnd();

		glBegin(GL_TRIANGLE_FAN);
		glNormal3f(0, -1, 0);
		glTexCoord2f(0.5, 0.25);

		glVertex3f(0, 0, 0);

		for (int i = 0; i <= nSeg; i++) {
			double x = r2 * cos(i * alfa);
			double z = r2 * sin(i * alfa);

			glTexCoord2f(0.5 + (x / r2) / 4, (z / r2) / 4 + 0.25);
			glVertex3f(x, 0, z);
		}
		glEnd();

	}
	glBegin(GL_QUADS);

		for (int i = 0; i < nSeg; i++) {

			double x1 = r1 * cos(i * alfa);
			double z1 = r1 * sin(i * alfa);

			double x2 = r2 * cos(i * alfa);
			double z2 = r2 * sin(i * alfa);

			double x12 = r1 * cos((i + 1) * alfa);
			double z12 = r1 * sin((i + 1) * alfa);

			double x22 = r2 * cos((i + 1) * alfa);
			double z22 = r2 * sin((i + 1) * alfa);

			double L = sqrt((r1 - r2)*(r1 - r2) + h*h);

			double nx = (h / L) * cos(alfa * i);
			double ny = ((r1 - r2) / L);
			double nz = (h / L) * sin(alfa * i);

			glNormal3f(nx, ny, nz);

			double tx1 = (double)i / nSeg;
			double tx2 = (double)(i + 1) / nSeg;
			double ty = 0.5;
			glTexCoord2f(tx2, ty + repy1);
			glVertex3f(x12, h, z12);

			glTexCoord2f(tx2, 0 + repy1);
			glVertex3f(x22, 0, z22);

			glTexCoord2f(tx1, 0 + repy1);
			glVertex3f(x2, 0, z2);

			glTexCoord2f(tx1, ty + repy1);
			glVertex3f(x1, h, z1);
		}
	glEnd();

	
}

void CGLRenderer::DrawLampBase()
{
	DrawCylinder(7, 8, 2, 20, 1, false);
}

void CGLRenderer::DrawLampArm()
{
	glPushMatrix();
	glTranslatef(0, 0, -1);
	glRotatef(90, 1, 0, 0);
	DrawCylinder(3, 3, 2, 20, 1, false);

	glPopMatrix();

	glPushMatrix();
	DrawCylinder(1, 1, 15, 20, 1, false);
	glPopMatrix();

}

void CGLRenderer::DrawLampHead()
{
	glPushMatrix();
	glTranslatef(-1.5, 1.5, -1);
	glRotatef(angleHead, 0, 0, 1);
	glTranslatef(1.5, -1.5, 1);

	DrawCylinder(3, 3, 5, 20, 1, false);

	glPushMatrix();
	glTranslatef(0, -5, 0);
	DrawCylinder(3, 6, 5, 20, 1, true);
	glPopMatrix();

	glPushMatrix();
	glTranslatef(0, 5, 0);
	DrawCylinder(2, 3, 1, 20, 1, false);
	glPopMatrix();


	glPushMatrix();
	glTranslatef(-1.5, 1.5, -1);
	glRotatef(90, 1, 0, 0);
	DrawCylinder(3, 3, 2, 20, 1, false);
	glPopMatrix();


	glPopMatrix();

}

void CGLRenderer::DrawLamp()
{
	glPushMatrix();

	DrawLampBase();
	glRotatef(angleArm1+45, 0, 0, 1);
	DrawLampArm();
	glTranslatef(0, 15, 0);
	glRotatef(angleArm2-90, 0, 0, 1);
	DrawLampArm();
	glTranslatef(0, 15, 0);
	glTranslatef(1.5, -1.5, 1);
	glRotatef(90, 0, 0, 1);
	DrawLampHead();

	glPopMatrix();

}

//za osvetljenje neko
#include <GL/gl.h>   //ovo ti treba da bi radilo setlightparams I ZA SETMATERIAL
void  CGLRenderer::SetLightParams(GLenum glLight)
{
	// postavlja svetlo glLight tako da bude bele boje u koordinatnom pocetku
	//  usmereno ka negativnoj z - osi sa malom atenuacijom i ograniceno kupom od 45 stepeni.
	
	// Ukljuci svetlo
	glEnable(glLight);

	// Boja svetla (difuzno i spekularno)
	float diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
	float specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
	glLightfv(glLight, GL_DIFFUSE, diffuse);
	glLightfv(glLight, GL_SPECULAR, specular);

	// Pozicija svetla: koordinatni pocetak
	float position[] = { 0.0f, 0.0f, 0.0f, 1.0f }; 
	glLightfv(glLight, GL_POSITION, position);

	// Smer svetlosnog snopa ka -Z
	float direction[] = { 0.0f, 0.0f, -1.0f };
	glLightfv(glLight, GL_SPOT_DIRECTION, direction);

	// Ogranicenje svetla na 45stepeni kup
	glLightf(glLight, GL_SPOT_CUTOFF, 45.0f);

	// Mala atenuacija: svetlo slabi sa daljinom
	glLightf(glLight, GL_LINEAR_ATTENUATION, 0.02f);
}
---------------------------------------------------------------------------
void CGLRenderer::SetLightingParams()
{// koja postavlja model osvetljenja : 
	// ambijentalna svetlost na{ 0.3, 0.3, 0.3 }, lokalni posmatrac i dvostrano osvetljenje i ukljucuje osvetljenje
	
	float ambient[] = { 0.3f, 0.3f, 0.3f, 1.0f };
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);

	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);

	
	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);

	glEnable(GL_LIGHTING);
}void CGLRenderer::SetMaterial(GLfloat diffuse[])
{
	//koja postavlja difuzionu komponentu na diffuse, 
	//ambijentalnu na polovinu difuzione, spekularnu na belu, odsjaj na 64 i nema emisijonu komponentu.

	// Difuzna komponenta = prosledjena boja
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);

	float ambient[] = { diffuse[0] * 0.5f, diffuse[1] * 0.5f, diffuse[2] * 0.5f,	diffuse[3] };
	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);

	float specular[] = { 1.0,1.0,1.0,1.0 };
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);

	float shininess = 64;
	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);

	float emission[] = { 0,0,0,1 };
	glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emission);

}
Editor is loading...
Leave a Comment