Jump to content

Recommended Posts

Posted

Boas.... estou com um problema em openGL, no Z-buffer e gostava de saber se alguém me consegue ajudar;

#include <GL/glut.h>
#define ESCALA_MOVIMENTO 1
GLint IDWindow;
GLint RotacaoY;
GLint WireStatus;
GLint DepthStatus;

static GLfloat COR_VERMELHO[] = {1, 0, 0};
static GLfloat COR_VERDE[] = {0, 1, 0};

void ValoresIniciaisObjectos() {
RotacaoY = 0;
WireStatus = 0;
DepthStatus = 0;
}

void ActiveDepth(void) {
if(!DepthStatus) {
	glEnable(GL_DEPTH_TEST);
	DepthStatus = 1;
} else{
	glDisable(GL_DEPTH_TEST);
	DepthStatus = 0;
}
}

void tecladoNormal(unsigned char tecladoKey, int ratoX, int ratoY) {
switch(tecladoKey) {
	case 'd':
	case 'D': ActiveDepth();
		break;
	case 'z':
	case 'Z': RotacaoY-= 5;
		break;
	case 'x':
	case 'X': RotacaoY+= 5;
		break;
	case 'r':
	case 'R': ValoresIniciaisObjectos();
		break;
	case 's':
	case 'S':
			if(WireStatus)
				WireStatus = 0;
			else
				WireStatus = 1;
		break;
}
glutPostRedisplay();
}

void EscolherMatrix(int IDMatrix) {
switch(IDMatrix) {
	case 1: glMatrixMode(GL_PROJECTION);
		break;
	case 2: glMatrixMode(GL_MODELVIEW);
		break;
}
glLoadIdentity();
}

void LimparEcran() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void desenhar(GLfloat *cor1, GLfloat *cor2) {
glPushMatrix();

	glRotatef((GLfloat)RotacaoY, 0, 1, 0);

	glPushMatrix();
		glColor3fv(cor1);
		if(WireStatus)
			glBegin(GL_LINE_LOOP);
		else
			glBegin(GL_QUADS);
				glVertex3f(-1000, -1000, 0);
				glVertex3f(1000, -1000, 0);
				glVertex3f(1000, 0, 0);
				glVertex3f(-1000, 0, 0);
			glEnd();
	glPopMatrix();


	glPushMatrix();
		glColor3fv(cor2);

		if(WireStatus)
			glBegin(GL_LINE_LOOP);
		else
			glBegin(GL_QUADS);
				glVertex3f(-1000, -500, 50);
				glVertex3f(1000, -500, 50);
				glVertex3f(1000, 500, 50);
				glVertex3f(-1000, 500, 50);
			glEnd();
	glPopMatrix();

glPopMatrix();
}



void DrawAll() {
LimparEcran();

desenhar(COR_VERDE, COR_VERMELHO);

glutSwapBuffers();
}


void Janela(GLint largura, GLint altura) {
if(altura == 0)
	altura = 1;

LimparEcran();

EscolherMatrix(1);
glViewport(0, 0, (GLsizei)largura, (GLsizei)altura);
gluPerspective(45, (GLdouble)largura/altura, 0.1, 100000);

EscolherMatrix(2);
gluLookAt(
	0, 5000, 10000,
	0, 0, 0,
	0, 1, 0
);
}

void init() {
	glClearColor(0, 0, 0, 0);
}

int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(700, 500);
IDWindow = glutCreateWindow("TESTE");

glutDisplayFunc(DrawAll);
glutReshapeFunc(Janela);
glutKeyboardFunc(tecladoNormal);

init();

glutMainLoop();

   return 0;
}

quando tento aplicar o efeito de profundidade para fazer com que cada objecto seja desenhado consoante a  posição onde se encontra, se dois objectos estão demasiado próximos, o efeito não fica o mais adequado (parece que distorce as o desenho dos dois objectos), se afastar demasiado os objectos um do outro para evitar esse problema, tira o realismo do pretendido....

o que gostaria de saber, é se há maneira de calibrar o efeito de profundidade, de maneira a quando haja pouca distancia entre dois objectos, continue a funcionar correctamente... no caso do programa em cima, quando rodo o cenário, que cada quadrado seja preenchido correctamente...

desde já muito obrigado

Posted

o teu código parece correcto

os objectos não são desenhados por ordem, não é isso que acontece, eles são sempre desenhados pela ordem que o teu código os define. a correcção de esconder o que se encontra por traz é o trabalho do depth_test.

podes fazer post de uma imagem da resultado "errado" que te referes ? porque como te disse parece estar correcto.

nota : apresenta a imagem com o depth_buffer ligado 😉

IRC : sim, é algo que ainda existe >> #p@p

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.