texture trouble :/

BlitzMax Forums/OpenGL Module/texture trouble :/

nawi(Posted 2005) [#1]
My texture renders as all black(or violet with GL_NEAREST instead of GL_LINEAR modes), Im pretty certain that the U,V coordinates are right. On fullscreen it crashes with Flushmem(), without it its the same problems as in windowed mode.

Texture type:
Type Texture
	Field GLID

	Function Create:Texture(Filename$)
		Local Txt:Texture = New Texture
		
		Local Pixmap:TPixmap = LoadPixmap(Filename$)
		If Not Pixmap Then
			Print "Could not load texture!"
			Return Null
		EndIf
		
		Txt.GLID = bglTexFromPixmap(Pixmap)
		If Not Txt.GLID Then
			Print "Could not apply texture to opengl!"
			Return Null
		EndIf
		
		
		glBindTexture GL_TEXTURE_2D,Txt.GLID
		
		glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT
		glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT		
	
		glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST
		glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST
		


		Pixmap = Null
		FlushMem
		
		Return Txt
	End Function
	
End type


Render function (parts from it)
				'Texture
				If Ent.Txt <> Null Then
					glBindTexture(GL_TEXTURE_2D,Ent.Txt.GLID)
					glEnable(GL_TEXTURE_2D)
				end if

				'Tri1
				If Ent.Alpha# <> 1.0 Then
					glColor4f(Pol.P[0].R/255.0,Pol.P[0].G/255.0,Pol.P[0].B/255.0,Ent.Alpha#)
				Else
					glColor3f(Pol.P[0].R/255.0,Pol.P[0].G/255.0,Pol.P[0].B/255.0)
				EndIf
				glTexCoord2f(Pol.P[0].U#,Pol.P[0].V#)
				glVertex3f(Pol.P[0].X#,Pol.P[0].Y#,Pol.P[0].Z#)


This is creating 2 triangle entity with my own lib
'Create entity
Local Cube:Entity = Entity.Create()
'Add(x,y,z,u,v)
Cube.Add(-1,1,-1,0,0)
Cube.Add(-1,-1,-1,0,1)
Cube.Add(1,-1,-1,1,1)

Cube.Add(1,-1,-1,0,0)
Cube.Add(1,1,-1,1,0)
Cube.Add(-1,1,-1,1,1)



TeraBit(Posted 2005) [#2]
I had something like this as BglTexFromPixmap will create Mipmaps by default.

Pass a false after the PixMap in the BglTexFromPixmap() call to prevent this.

Then each time you bind the texture call your:

glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST
glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST

I don't know if it will help. I had to play around with the settings to get my one to work.


nawi(Posted 2005) [#3]
Still doesnt work. :/


nawi(Posted 2005) [#4]
Can somebody post a simple code to display a texture?


Ibmurai(Posted 2005) [#5]
This is a simple example - hope it helps even though you've probably solved it by now... :P
Strict

Incbin "ending.png"


' Variables

Global ..
	theTexture:Int

Local ..
	thePixmap:TPixmap, ..
	time:Int = 0, ..
	timeDiff:Int = 0, ..
	timeSecond:Int = 0, ..
	fps:Int = 0, shownFPS:Int = 0


' First set up openGL

bglCreateContext(800, 600, 32, 0, BGL_BACKBUFFER|BGL_DEPTHBUFFER)
glClearColor(0, 0, 0, 64)
glClearDepth(1.0)
glDepthFunc(GL_LESS)
glEnable(GL_DEPTH_TEST)
glShadeModel(GL_SMOOTH)
glEnable(GL_CULL_FACE)
glCullFace(GL_BACK)

glMatrixMode(GL_PROJECTION)
glLoadIdentity

gluPerspective(45.0, 4.0 / 3.0, 1.0, 100.0)

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
glEnable(GL_TEXTURE_2D)
glMatrixMode(GL_MODELVIEW)


' Load the texture

thePixmap = LoadPixmap("incbin::ending.png")
theTexture = bglTexFromPixmap(thePixmap)
glBindTexture(GL_TEXTURE_2D, theTexture)


' The game loop

Repeat
	timeDiff = MilliSecs() - time
	time = MilliSecs()
	timeSecond :+ timeDiff
	If timeSecond > 1000.0 Then
		timeSecond :Mod 1000.0
		shownFPS = fps
		fps = 0
	EndIf
	fps :+ 1
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

	DrawEnding
	
	bglDrawText("FPS: " + shownFPS, 0, 0) 

	glFlush

	bglSwapBuffers
	
	FlushMem
Until KeyHit(KEY_ESCAPE)


Function DrawEnding()
	glBindTexture(GL_TEXTURE_2D, theTexture)

	glEnable(GL_TEXTURE_2D)

	glLoadIdentity
	
	glTranslatef(0.0, 0.0, -5.0)
	glScalef(4.0, 4.0, 0.0)
			
	glBegin(GL_POLYGON)
		glNormal3f(  0.0, 0.0, 1.0)
		glTexCoord3f(0.0, 1.0, 0.0) 
		glVertex3f(  -0.5, -0.5,  0.0)
		glTexCoord3f(1.0, 1.0, 0.0)
		glVertex3f(   0.5, -0.5,  0.0)
		glTexCoord3f(1.0, 0.0, 0.0)
		glVertex3f(   0.5,  0.5,  0.0)
		glTexCoord3f(0.0, 0.0, 0.0) 
		glVertex3f(  -0.5,  0.5,  0.0)
		glColor4f(1.0, 1.0, 1.0, 1.0)
	glEnd
	glDisable(GL_TEXTURE_2D)
	
	glPopMatrix
End Function



Extron(Posted 2005) [#6]
Try this nawi :
Type Texture
	Field GLID

	Function Create:Texture(Filename$)
		Local Txt:Texture = New Texture
		
		Local Pixmap:TPixmap = LoadPixmap(Filename$)
		If Not Pixmap Then
			Print "Could not load texture!"
			Return Null
		EndIf
		
		Txt.GLID = bglTexFromPixmap(Pixmap)
		If Not Txt.GLID Then
			Print "Could not apply texture to opengl!"
			Return Null
		EndIf
		
		Txt.Init()
		
		Pixmap = Null
		FlushMem

		Return Txt
	End Function

	Method Init()
		glBindTexture GL_TEXTURE_2D,Self.GLID
		
		glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT
		glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT		
	
		glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST
		glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST
	End Method
'	
End Type



nawi(Posted 2005) [#7]
Nope, still doesnt work on my engine. Really weird.

If anyone wants to try it:

library.bmx


opengl.bmx



Extron(Posted 2005) [#8]
Your UV texture is broken. ;)
To prove this, try this :
				glBegin(GL_TRIANGLES)
				
				'Tri1
				If Ent.Alpha# <> 1.0 Then
					glColor4f(Pol.P[0].R/255.0,Pol.P[0].G/255.0,Pol.P[0].B/255.0,Ent.Alpha#)
				Else
					glColor3f(Pol.P[0].R/255.0,Pol.P[0].G/255.0,Pol.P[0].B/255.0)
				EndIf
				glTexCoord2f(0.0,0.0) 'Pol.P[0].U#,Pol.P[0].V#)
				glVertex3f(Pol.P[0].X#,Pol.P[0].Y#,Pol.P[0].Z#)

				'Tri2
				If Ent.Alpha# <> 1.0 Then
					glColor4f(Pol.P[1].R/255.0,Pol.P[1].G/255.0,Pol.P[1].B/255.0,Ent.Alpha#)
				Else
					glColor3f(Pol.P[1].R/255.0,Pol.P[1].G/255.0,Pol.P[1].B/255.0)
				EndIf
				glTexCoord2f(1.0,0.0) 'Pol.P[1].U#,Pol.P[1].V#)
				glVertex3f(Pol.P[1].X#,Pol.P[1].Y#,Pol.P[1].Z#)				

				'Tri3
				If Ent.Alpha# <> 1.0 Then
					glColor4f(Pol.P[2].R/255.0,Pol.P[2].G/255.0,Pol.P[2].B/255.0,Ent.Alpha#)
				Else
					glColor3f(Pol.P[2].R/255.0,Pol.P[2].G/255.0,Pol.P[2].B/255.0)
				EndIf
				glTexCoord2f(0.0,1.0) 'Pol.P[2].U#,Pol.P[2].V#)
				glVertex3f(Pol.P[2].X#,Pol.P[2].Y#,Pol.P[2].Z#)	
				
				glEnd()