Jump to content
  • Advertisement
Sign in to follow this  
BlackMarik

OpenGL Python - OpenGL depth test problem

This topic is 2766 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello to everybody
First, sorry my bad english

I am creating a program and it uses OpenGL for 3D graphics.
When it runs, the first frame is OK, but in other pictures, it starts to turn black and the figures are half full.
When I disable the "depth test", this problem does not occur, but the elements are in front of others.
Can anyone help me?


import OpenGL, socket, math, Image
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *
from sys import argv

class World:
def init(self):
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

glClearColor (0.0, 0.0, 0.0, 0.0)

glClearDepth(1);
glEnable(GL_DEPTH_TEST)
glDepthMask(GL_TRUE)

mat_specular = [1.0, 1.0, 1.0, 1.0]
mat_shininess = [50.0]
light_position = [1.0, 50.0, -50.0, 0.0]
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_COLOR_MATERIAL);
glColor3f(1,1,1);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glMatrixMode(GL_PROJECTION)
glLoadIdentity()


def display(self):
glClear(GL_COLOR_BUFFER_BIT);
glClearDepth(1);

glMatrixMode(GL_MODELVIEW)

#Chao
glLoadIdentity()
glPushMatrix()
glColor3f(0.9,0.9,0.9)
glBegin(GL_POLYGON);
glVertex3f( -250, -50, -250);
glVertex3f( -250, -50, 250);
glVertex3f( 250, -50, 250);
glVertex3f( 250, -50, -250);
glEnd();
glPopMatrix()

#Teapot
glLoadIdentity()
glPushMatrix()
glColor3f(0.0, 0.0, 1.0)
glutSolidTeapot(50.0)
glPopMatrix()

#Cubo do robo
glLoadIdentity()
glPushMatrix()
glColor3f(0.7,0.7,0.7)
print "Aplicando rotate com os parametros", self.angle, 0, 1, 0
glRotatef(self.angle, 0, 1, 0)
glTranslatef(self.posX, 0, self.posZ + 10)
glutSolidCube(10.0)
glPopMatrix()



glLoadIdentity()

glutSwapBuffers()


print "Gerando imagem e enviando ao condutor";
#screenshot = glReadPixels( 0,0, 600, 600, GL_RGB, GL_UNSIGNED_BYTE)
#self.cs.send( screenshot )


def EspecificaParametrosVisualizacao(self):
glMatrixMode(GL_PROJECTION)

glLoadIdentity()
gluPerspective(45, self.fAspect ,0.1,500)
gluLookAt(self.posX,10,self.posZ, self.lookX,0,self.lookZ, 0,1,0)

glMatrixMode(GL_MODELVIEW)


def AlteraTamanhoJanela(self, w, h):
if h == 0:
h = 1

glViewport(0, 0, w, h)
self.fAspect = w/h
self.EspecificaParametrosVisualizacao()



def idle(self):
recv = self.cs.recv ( 1 )
if recv == "\r" or recv == "\n" or recv == "":
return

print "Recebi o comando", "\"" + recv + "\""

glMatrixMode(GL_PROJECTION)
if recv == "1":
print "Movendo o motor esquerdo para frente"

self.angle += self.angleUp
if self.angle > 180:
self.angle -= 180

ang = self.angle * self.fgr
self.posX += math.fabs(math.sin(ang) * self.steps)
self.posZ += math.fabs(math.cos(ang) * self.steps)
self.lookX -= self.upLookX
self.lookZ -= self.upLookZ

elif recv == "2":
print "Movendo o motor esquerdo para traz"

self.angle -= self.angleUp
if self.angle < 0:
self.angle += 180

ang = self.angle * self.fgr
self.posX -= math.fabs(math.sin(ang) * self.steps)
self.posZ -= math.fabs(math.cos(ang) * self.steps)
self.lookX += self.upLookX
self.lookZ += self.upLookZ

elif recv == "3":
print "Movendo o motor direito para frente"

self.angle -= self.angleUp
if self.angle < 0:
self.angle += 180

ang = self.angle * self.fgr
self.posX -= math.fabs(math.sin(ang) * self.steps)
self.posZ += math.fabs(math.cos(ang) * self.steps)
self.lookX += self.upLookX
self.lookZ += self.upLookZ


elif recv == "4":
print "Movendo o motor direito para traz"

self.angle += self.angleUp
if self.angle > 180:
self.angle -= 180

ang = self.angle * self.fgr
self.posX += math.fabs(math.sin(ang) * self.steps)
self.posZ -= math.fabs(math.cos(ang) * self.steps)
self.lookX -= self.upLookX
self.lookZ -= self.upLookZ

#print "Pozisao X", self.posX
#print "Pozisao Z", self.posZ
#print "Visao X", self.lookX
#print "Visao Z", self.lookZ
self.EspecificaParametrosVisualizacao()
glutPostRedisplay()



def __init__(self, cS):
self.cs = cS

self.fAspect = 0
self.fgr = math.pi / 180

self.angleUp = 5
self.angUp = self.angleUp * self.fgr
self.upLookX = math.fabs(math.sin(self.angUp) * 200)
self.upLookZ = math.fabs(200 - math.cos(self.angUp) * 200)
self.angle = 0

self.steps = 5
self.posX = 0
self.posZ = -200

self.lookX = 0
self.lookZ = 0

glutInit(argv)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowSize(600, 600)
glutCreateWindow("Matrix")
glutDisplayFunc(self.display)
glutIdleFunc(self.idle)
glutReshapeFunc(self.AlteraTamanhoJanela);

self.init()
glutMainLoop()


# Main Program
print "Aguardando cliente"

ss = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
ss.bind ( ( '', 31001 ) )
ss.listen ( 1 )
cS, details = ss.accept()
print "Conexao com o cliente", details
recv = cS.recv ( 1 )
print "Iniciando o mundo, pacote", recv, "recebido"

World(cS)

cS.close()
ss.close()

Share this post


Link to post
Share on other sites
Advertisement
glClearDepth doesn't actually clear the depth buffer, it only sets the value to which you want to clear to when you do clear it, similar to glClearColor.

So you aren't clearing the depth buffer each frame, which explains your problem.

Replace this:

glClear(GL_COLOR_BUFFER_BIT);
glClearDepth(1);

with this:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!