Opengl bug texture

Résolu
cs_Guigar Messages postés 105 Date d'inscription mercredi 12 novembre 2008 Statut Membre Dernière intervention 8 juin 2011 - 23 avril 2009 à 11:29
centellasandra Messages postés 1 Date d'inscription lundi 7 mai 2007 Statut Membre Dernière intervention 16 mars 2010 - 16 mars 2010 à 00:19
Bonjour a tous petite question technique

En fait j'ai un programme en VB6 qui me lit des fichier 3ds (ainsi que les textures .bmp) et me l'affiche

Je l'ai ensuite traduit en vb net cela marche sauf les textures...

Il m'affiche toujours des choses bizarre mais jamais la vrai texture. J'ai remarqué autre chose d'encore plus bizarre. Lorsque j'ouvre un objet texturé en VB6 (cela marche) et que je l'ouvre ensuite en vb net, miracle l'objet saffiche bien en vb net.

Et quand jouvre un objetc quelquonce en vb6 et qu'ensuite jouvre mon objet texturé en vb net tout bug

Je pensais a un conflit mémoire ou autre chose... qu'en pensez vous ca me rend dingue

merci d'avance

2 réponses

cs_Guigar Messages postés 105 Date d'inscription mercredi 12 novembre 2008 Statut Membre Dernière intervention 8 juin 2011
23 avril 2009 à 13:02
probleme résolu, il s'agissait d'une utilisation des fonctions

en VB6 on écrit ca comme ca :

 Call glTexImage2D(GL_TEXTURE_2D, _
                              0, _
                              GL_COMPRESSED_RGB_ARB, _
                              Scene.Materials(matNumber).texture.width, _
                              Scene.Materials(matNumber).texture.height, _
                              0, _
                              GL_BGR_EXT, _
                              GL_UNSIGNED_BYTE, _
                              data(0))

en VB.NET on écrit ca comme ca :

Call Gl.glTexImage2D(Gl.GL_TEXTURE_2D, _
                                  0, _
                                  GL_COMPRESSED_RGB_ARB, _
                                  Scene.Materials(matNumber).texture.width, _
                                  Scene.Materials(matNumber).texture.height, _
                                  0, _
                                  Gl.GL_BGR_EXT, _
                                  Gl.GL_UNSIGNED_BYTE, _
                                  data)
sans mettre le (0) au data sinon il ne sait plus afficher la texture et prends ce qu'il trouve en mémoire (d'ou le bug bizarre cité ci dessus)

en esperant que ca serve a quelqu'un...
3
centellasandra Messages postés 1 Date d'inscription lundi 7 mai 2007 Statut Membre Dernière intervention 16 mars 2010
16 mars 2010 à 00:19
bonjour ou hello plis problem, quelqu'un peut m'aider j'ai tout essayé, j'ai cherche par tout sur internet, j'en ai besoin d'un bon programmateur pour resoudre ce problemme regarde ce code assez complexe, je pense que j'ai tout mit, j'ai mm changé le code, mais pas de bon resultat, la texture ne va pas, quelqu'un que puisse m'aider svp, je vous remerci en avance





Imports System.Runtime.InteropServices
Imports System.Drawing.Imaging

Imports Tao.OpenGl
Imports Tao.Platform.Windows


Module Module1
Public GLForm As Form ' Our Current Windows Form
Public GLWidth As Integer = 640
Public GLHeight As Integer = 480
Public GLBits As Integer = 32

Public Active As Boolean = True ' Window Active Flag, Set To True By Default
Public Done As Boolean = False ' Bool Variable To Exit Main Loop
Public hRC As System.IntPtr ' Permanent Rendering Context
Public hDC As System.IntPtr

Sub Main()
' Start OpenGL
If EnableOpenGL() Then
MainLoop() 'Main loop
KillGLWindow() ' Stop OpenGL
End If
End
End Sub

Public Function MainLoop() As Boolean
While Not Done ' Loop That Runs While done = false
Application.DoEvents() ' Process Events
If Active And (GLForm IsNot Nothing) Then ' Program Active?
' Update Screen
DrawGLScene() ' Draw The Scene
Gdi.SwapBuffers(hDC) ' Swap Buffers (Double Buffering)
End If
End While
Return True
End Function

Public Function InitGL() As Boolean
If Not LoadGLTextures() Then
InitGL = False
Exit Function
End If

Gl.glEnable(Gl.GL_TEXTURE_2D)

Gl.glShadeModel(Gl.GL_SMOOTH) ' Enable Smooth Shading
Gl.glClearColor(0, 0, 0, 0.5F) ' Black Background
Gl.glClearDepth(1) ' Depth Buffer Setup
Gl.glEnable(Gl.GL_DEPTH_TEST) ' Enables Depth Testing
Gl.glDepthFunc(Gl.GL_LEQUAL) ' The Type Of Depth Testing To Do
Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST) ' Really Nice Perspective Calculations
Return True
End Function

Public Sub KillGLWindow()
If hRC <> IntPtr.Zero Then ' Do We Have A Rendering Context?
If Not Wgl.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero) Then ' Are We Able To Release The DC and RC Contexts?
MessageBox.Show("Release Of DC And RC Failed.", "SHUTDOWN ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
If Not Wgl.wglDeleteContext(hRC) Then ' Are We Able To Delete The RC?
MessageBox.Show("Release Rendering Context Failed.", "SHUTDOWN ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
hRC = IntPtr.Zero ' Set RC To Null
End If

If hDC <> IntPtr.Zero Then ' Do We Have A Device Context?
If GLForm IsNot Nothing And Not GLForm.IsDisposed Then ' Do We Have A Window?
If GLForm.Handle <> IntPtr.Zero Then ' Do We Have A Window Handle?
If Not User.ReleaseDC(GLForm.Handle, hDC) Then ' Are We Able To Release The DC?
MessageBox.Show("Release Device Context Failed.", "SHUTDOWN ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If
End If

hDC = IntPtr.Zero ' Set DC To Null
End If
GLForm.Hide() ' Hide The Window
GLForm.Close() ' Close The Form
GLForm = Nothing
End Sub

#Region "OpenGL handling" ' Private GDI Device Context

Public Function EnableOpenGL() As Boolean
GLForm = Form1
GC.Collect() ' Request A Collection
Kernel.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1) ' This Forces A Swap

GLForm = New FormGL ' Show Mouse Pointer
GLForm.Width = GLWidth
GLForm.Height = GLHeight

Dim pfd As New Gdi.PIXELFORMATDESCRIPTOR() ' pfd Tells Windows How We Want Things To Be
pfd.nSize = System.Runtime.InteropServices.Marshal.SizeOf(pfd) ' Size Of This Pixel Format Descriptor
pfd.nVersion = 1 ' Version Number
' Format Must Support Window, OpenGL, Double Buffering
pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW Or Gdi.PFD_SUPPORT_OPENGL Or Gdi.PFD_DOUBLEBUFFER
pfd.iPixelType = Gdi.PFD_TYPE_RGBA ' Request An RGBA Format
pfd.cColorBits = GLBits ' Select Our Color Depth
pfd.cRedBits = 0 ' Color Bits Ignored
pfd.cRedShift = 0
pfd.cGreenBits = 0
pfd.cGreenShift = 0
pfd.cBlueBits = 0
pfd.cBlueShift = 0
pfd.cAlphaBits = 0 ' No Alpha Buffer
pfd.cAlphaShift = 0 ' Shift Bit Ignored
pfd.cAccumBits = 0 ' No Accumulation Buffer
pfd.cAccumRedBits = 0 ' Accumulation Bits Ignored
pfd.cAccumGreenBits = 0
pfd.cAccumBlueBits = 0
pfd.cAccumAlphaBits = 0
pfd.cDepthBits = 16 ' 16Bit Z-Buffer (Depth Buffer)
pfd.cStencilBits = 0 ' No Stencil Buffer
pfd.cAuxBuffers = 0 ' No Auxiliary Buffer
pfd.iLayerType = Gdi.PFD_MAIN_PLANE ' Main Drawing Layer
pfd.bReserved = 0 ' Reserved
pfd.dwLayerMask = 0 ' Layer Masks Ignored
pfd.dwVisibleMask = 0
pfd.dwDamageMask = 0

hDC = User.GetDC(GLForm.Handle) ' Attempt To Get A Device Context
If hDC = IntPtr.Zero Then ' Did We Get A Device Context?
KillGLWindow() ' Reset The Display
MessageBox.Show("Can't Create A GL Device Context.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If

Dim PixelFormat As Integer
PixelFormat = Gdi.ChoosePixelFormat(hDC, pfd) ' Attempt To Find An Appropriate Pixel Format
If PixelFormat = 0 Then ' Did Windows Find A Matching Pixel Format?
KillGLWindow() ' Reset The Display
MessageBox.Show("Unable to retrieve pixel format")
Return False
End If
If Not (Gdi.SetPixelFormat(hDC, PixelFormat, pfd)) Then ' Are We Able To Set The Pixel Format?
KillGLWindow() ' Reset The Display
MessageBox.Show("Unable to set pixel format")
Return False
End If
hRC = Wgl.wglCreateContext(hDC) ' Attempt To Get The Rendering Context
If hRC.ToInt32 = 0 Then ' Are We Able To Get A Rendering Context?
KillGLWindow() ' Reset The Display
MessageBox.Show("Unable to get rendering context")
Return False
End If
If Not (Wgl.wglMakeCurrent(hDC, hRC)) Then ' Try To Activate The Rendering Context
KillGLWindow() ' Reset The Display
MessageBox.Show("Unable to make rendering context current")
Return False
End If

GLForm.Show()
GLForm.TopMost = True

ResizeGL(GLWidth, GLHeight)

If Not InitGL() Then ' Initialize Our Newly Created GL Window
KillGLWindow() ' Reset The Display
MessageBox.Show("Initialization Failed.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If

Return True ' Success
End Function

Public Sub ResizeGL(ByVal Width As Integer, ByVal height As Integer)
If height 0 Then height 1
Gl.glViewport(0, 0, Width, height) ' Reset The Current Viewport
Gl.glMatrixMode(Gl.GL_PROJECTION) ' Select The Projection Matrix
Gl.glLoadIdentity() ' Reset The Projection Matrix
Glu.gluPerspective(45, Width / height, 0.1, 100) ' Calculate The Aspect Ratio Of The Window
Gl.glMatrixMode(Gl.GL_MODELVIEW) ' Select The Modelview Matrix
Gl.glLoadIdentity() ' Reset The Modelview Matrix
End Sub
#End Region

Public rtri As Single
Public rquad As Single

Public Function DrawGLScene() As Boolean

Gl.glClear(Gl.GL_COLOR_BUFFER_BIT Or Gl.GL_DEPTH_BUFFER_BIT) ' Clear the Screen and the Depth Buffer
Gl.glMatrixMode(Convert.ToUInt32(Gl.GL_MODELVIEW)) ' Modelview Matrix
Gl.glLoadIdentity() '// reset the current modelview matrix

Gl.glTranslatef(0.0F, 0.0F, -5.0F) '// move 1.5 Units left and 6 Units into the screen
Gl.glRotatef(rtri, 1.0F, 1.0F, 0.0F) '// rotate the Pyramid on it's Y-axis
rtri += 0.5F '// rotation angle

Gl.glBindTexture(Gl.GL_TEXTURE_2D, gaTextures(0)) ' Select Our Texture

Gl.glBegin(Gl.GL_QUADS)
' Front Face
Gl.glTexCoord2f(0.0#, 0.0#) : Gl.glVertex3f(-1.0#, -1.0#, 1.0#) ' Bottom Left Of The Texture and Quad
Gl.glTexCoord2f(1.0#, 0.0#) : Gl.glVertex3f(1.0#, -1.0#, 1.0#) ' Bottom Right Of The Texture and Quad
Gl.glTexCoord2f(1.0#, 1.0#) : Gl.glVertex3f(1.0#, 1.0#, 1.0#) ' Top Right Of The Texture and Quad
Gl.glTexCoord2f(0.0#, 1.0#) : Gl.glVertex3f(-1.0#, 1.0#, 1.0#) ' Top Left Of The Texture and Quad
' Back Face
Gl.glTexCoord2f(1.0#, 0.0#) : Gl.glVertex3f(-1.0#, -1.0#, -1.0#) ' Bottom Right Of The Texture and Quad
Gl.glTexCoord2f(1.0#, 1.0#) : Gl.glVertex3f(-1.0#, 1.0#, -1.0#) ' Top Right Of The Texture and Quad
Gl.glTexCoord2f(0.0#, 1.0#) : Gl.glVertex3f(1.0#, 1.0#, -1.0#) ' Top Left Of The Texture and Quad
Gl.glTexCoord2f(0.0#, 0.0#) : Gl.glVertex3f(1.0#, -1.0#, -1.0#) ' Bottom Left Of The Texture and Quad
' Top Face
Gl.glTexCoord2f(0.0#, 1.0#) : Gl.glVertex3f(-1.0#, 1.0#, -1.0#) ' Top Left Of The Texture and Quad
Gl.glTexCoord2f(0.0#, 0.0#) : Gl.glVertex3f(-1.0#, 1.0#, 1.0#) ' Bottom Left Of The Texture and Quad
Gl.glTexCoord2f(1.0#, 0.0#) : Gl.glVertex3f(1.0#, 1.0#, 1.0#) ' Bottom Right Of The Texture and Quad
Gl.glTexCoord2f(1.0#, 1.0#) : Gl.glVertex3f(1.0#, 1.0#, -1.0#) ' Top Right Of The Texture and Quad
' Bottom Face
Gl.glTexCoord2f(1.0#, 1.0#) : Gl.glVertex3f(-1.0#, -1.0#, -1.0#) ' Top Right Of The Texture and Quad
Gl.glTexCoord2f(0.0#, 1.0#) : Gl.glVertex3f(1.0#, -1.0#, -1.0#) ' Top Left Of The Texture and Quad
Gl.glTexCoord2f(0.0#, 0.0#) : Gl.glVertex3f(1.0#, -1.0#, 1.0#) ' Bottom Left Of The Texture and Quad
Gl.glTexCoord2f(1.0#, 0.0#) : Gl.glVertex3f(-1.0#, -1.0#, 1.0#) ' Bottom Right Of The Texture and Quad
' Right face
Gl.glTexCoord2f(1.0#, 0.0#) : Gl.glVertex3f(1.0#, -1.0#, -1.0#) ' Bottom Right Of The Texture and Quad
Gl.glTexCoord2f(1.0#, 1.0#) : Gl.glVertex3f(1.0#, 1.0#, -1.0#) ' Top Right Of The Texture and Quad
Gl.glTexCoord2f(0.0#, 1.0#) : Gl.glVertex3f(1.0#, 1.0#, 1.0#) ' Top Left Of The Texture and Quad
Gl.glTexCoord2f(0.0#, 0.0#) : Gl.glVertex3f(1.0#, -1.0#, 1.0#) ' Bottom Left Of The Texture and Quad
' Left Face
Gl.glTexCoord2f(0.0#, 0.0#) : Gl.glVertex3f(-1.0#, -1.0#, -1.0#) ' Bottom Left Of The Texture and Quad
Gl.glTexCoord2f(1.0#, 0.0#) : Gl.glVertex3f(-1.0#, -1.0#, 1.0#) ' Bottom Right Of The Texture and Quad
Gl.glTexCoord2f(1.0#, 1.0#) : Gl.glVertex3f(-1.0#, 1.0#, 1.0#) ' Top Right Of The Texture and Quad
Gl.glTexCoord2f(0.0#, 1.0#) : Gl.glVertex3f(-1.0#, 1.0#, -1.0#) ' Top Left Of The Texture and Quad


Gl.glEnd()

Return True
End Function


Public Structure RGBQUAD
Public rgbBlue As Byte
Public rgbGreen As Byte
Public rgbRed As Byte
Public rgbReserved As Byte
End Structure

Structure BITMAPFILEHEADER
Dim bfType As Short 'Integer
Dim bfSize As Integer 'Long
Dim bfReserved1 As Short 'Integer
Dim bfReserved2 As Short 'Integer
Dim bfOffBits As Integer 'Long
End Structure

Public Structure RGBTRIPLE
Public rgbBlue As Byte
Public rgbGreen As Byte
Public rgbRed As Byte
End Structure

Public Structure BITMAP
Public biStructure As Integer
Public biWidth As Integer
Public biHeight As Integer
Public biWidthBytes As Integer
Public biPlanes As Short
Public biBitsPixel As Short
Public biBits As IntPtr
End Structure

Public Structure BITMAPINFOHEADER
Public biSize As Integer
Public biWidth As Integer
Public biHeight As Integer
Public biPlanes As Short
Public biBitCount As Short
Public biCompression As Integer
Public biSizeImage As Integer
Public biXPelsPerMeter As Integer
Public biYPelsPerMeter As Integer
Public biClrUsed As Integer
Public biClrImportant As Integer
End Structure

Public Structure BITMAPINFO
Public bmiHeader As BITMAPINFOHEADER
Public bmColors() As RGBTRIPLE
End Structure

Public Const DIB_RGB_COLORS As Integer = 0


Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As IntPtr, _
ByVal hBitmap As IntPtr, _
ByVal nStartScan As Integer, _
ByVal nNumScans As Integer, _
ByVal lpBits As IntPtr, _
ByRef lpBI As BITMAPINFO, _
ByVal wUsage As Integer) _
As Integer

Private WithEvents pb As New PictureBox




'OpenGL Constants -------------------------------------

'Private Constants ------------------------------------------------------------
'Private Const TEXTURE_FILE As String = "Data\NeHe.bmp"


'Global Variables -------------------------------------------------------------
Public giOldWidth As Short
Public giOldHeight As Short
Public giOldNumBits As Short
Public giOldVRefresh As Short
Public gaTextures() As Integer 'Array to hold textures

Public Function LoadGLTextures() As Boolean

Dim Pict As PictureBox
Pict = New PictureBox
Dim bmp As System.Drawing.Bitmap = DirectCast(Image.FromFile("D:\NEHE.bmp"), System.Drawing.Bitmap)
' Dim X, Y As Integer
Dim bi24BitInfo As New BITMAPINFO
'Getting data from PictureBox directly
With bi24BitInfo.bmiHeader
.biBitCount = 24
.biCompression = 0 ' BI_RGB
.biPlanes = 1
.biSize = Marshal.SizeOf(GetType(BITMAPINFOHEADER))
.biWidth = bmp.Width
.biHeight = bmp.Height
End With
' Flattened it. Multidimension won't work here.
Dim textureImg(bmp.Width * bmp.Height * 3 - 1) As Byte
' Pin the array in memory, the garbage collector won't play with it.
Dim gch As GCHandle = GCHandle.Alloc(textureImg, GCHandleType.Pinned)
' Send it off.
Dim result As Integer = GetDIBits(Pict.CreateGraphics.GetHdc, bmp.GetHbitmap, 0, bmp.Height, gch.AddrOfPinnedObject, bi24BitInfo, 0)
gch.Free()

'Swap BGR->RGB
' For Y = 0 To bmp.Height - 1
'For X = 0 To bmp.Width - 1
'Dim index As Integer = (Y * (bmp.Width * 3)) + (X * 3)
' Dim temp As Byte = textureImg(index)
' textureImg(index) = temp 'textureImg(index + 2)
' textureImg(index + 2) = temp
' Next
' Next

' Er...
' Now what. We didn't change anything.
' We have to get the pixels back into an image now.
'Dim bmp2 As New System.Drawing.Bitmap(bmp.Width, bmp.Height, bmp.PixelFormat) ' otherwise it would be 32bppARGB
'Dim ms As New IO.MemoryStream
' Save to the stream.
'bmp2.Save(ms, ImageFormat.Bmp)
' Overwrite with the new pixels
'Dim bw As New IO.BinaryWriter(ms)
' Seek to the start of the pixeldata
' bw.BaseStream.Seek(&H36, IO.SeekOrigin.Begin)
' write the bytes
'bw.Write(textureImg)
' bw.Flush()
' bmp2.Dispose()
' bmp2 = New System.Drawing.Bitmap(ms)
' bw.Close() ' also disposes of ms and bw

'Allocate space for the texture "ptr" in the array
ReDim gaTextures(1)

'Allocate the texture for OpenGL
Gl.glGenTextures(1, gaTextures(0))

'Typical Texture Generation Using Data From The Bitmap
Gl.glBindTexture(Gl.GL_TEXTURE_2D, gaTextures(0))
'Generate The Texture
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR)
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR)

Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, 3, bi24BitInfo.bmiHeader.biWidth, bi24BitInfo.bmiHeader.biHeight, _
0, Gl.GL_RGB, Gl.GL_UNSIGNED_BYTE, textureImg(0))

'Whew! Return success if we got this far
Return True

End Function
End Module
0
Rejoignez-nous