Is it possible to add a project or zip to the thread? Seems that would be easier than trying to copy the relevant bits here.
I’m positive this will be a one line fix when it’s done… Sigh.
again here’s the scenario spelled out a little more clear (maybe it will jog something…)
** If anyone helps me solve this and is going to XDC there will surely be consumable rewards for the help!!! (?)
Situation 1
- Add two polygons
- Render
- They show properly
Situation 2
- add two polygons
- set them to the same texture (and only load one texture)
- all is ok
Situation 3
- add two polygons
- load two textures
- Assign one texture to each polygon.
- The first loaded texture shows on both polygons
Situation 4
- Rerun 2 or 3 (above)
- add another non-textured polygon
- It won’t show at all… If I comment out the textured polygons the non-textured will appear again.
It’s both very odd and distressing. I’ve lost over a day toiling with this.
I have this in the open event of my OpenG3Surface
[code]X3_Initialize
Dim m As new X3Core.X3Model()
m.Vertex.Append new X3Core.X3Vector( -1, -1, 0 ) // 0
m.Vertex.Append new X3Core.X3Vector( -1,1,0) // 1
m.Vertex.Append new X3Core.X3Vector( 1, 1, 0) // 2
m.Vertex.Append new X3Core.X3Vector( 1, -1, 0 ) // 3
dim p as new X3Core.X3Polygon( 0, 0, 1)
p.VIndex.Append 0
p.VIndex.Append 2
p.VIndex.Append 1
p.UVMap.Append new X3Core.X3UVCoordinate(0,1)
p.UVMap.Append new X3Core.X3UVCoordinate(1,0)
p.UVMap.Append new X3Core.X3UVCoordinate(0,0)
dim t as new X3Core.X3Texture( imageb ) // this is the only texture that will show regardless of what texture is loaded after.
p.Texture = t
m.AppendPolygon p
dim p2 as new X3Core.X3Polygon( 0, 0, 1)
p2.VIndex.Append 0
p2.VIndex.Append 3
p2.VIndex.Append 2
p2.UVMap.Append new X3Core.X3UVCoordinate(0,1)
p2.UVMap.Append new X3Core.X3UVCoordinate(1,1)
p2.UVMap.Append new X3Core.X3UVCoordinate(1,0)
dim t2 as new X3Core.X3Texture( imagea )
p2.Texture = t2
m.AppendPolygon p2
model.Append m
setMyPerspective
Render
[/code]
Using the X thoughtful examples from www.xojo3d.com, the constructor of the texture wrapper is as follows.
[code]’ www.Xojo3D.com
Dim x, y, offset As Integer
Dim textCol As Color
Dim textMaskCol As Color
Dim alpha As Byte
dim m as picture
if texture.HasAlphaChannel then
m = texture.CopyMask
end if
’ convert pictures to raw formats
Width = texture.Width
Height = texture.Height
RGBABitmap = new MemoryBlock(Height * Width * 4) ’ create a MemoryBlock for the OpenGL RGBA format
’ loop through all the pixels of the picture
offset = 0
for y = 0 to Height - 1
for x = 0 to Width - 1
' read the values of the current pixel
textCol = texture.RGBSurface.Pixel(x,y) ' get the color of the current pixel
if m <> nil then
textMaskCol = m.RGBSurface.Pixel(x, y) ' get the mask (alpha) color of the current pixel
end if
' calculate the OpenGL alpha values, using the mask values of the pixel
alpha = 255 - (textMaskCol.Red + textMaskCol.Green + textMaskCol.Blue) / 3
' store the color and alpha values into our OpenGL texture bitmap
RGBABitmap.Byte(offset) = textCol.Red
RGBABitmap.Byte(offset + 1) = textCol.Green
RGBABitmap.Byte(offset + 2) = textCol.Blue
RGBABitmap.Byte(offset + 3) = alpha
offset = offset + 4 ' move to the next pixel in our OpenGL texture bitmap
next x
next y
’ load texture into OpenGL
OGLName = X3_LoadRGBATexture(RGBABitmap, Width, Height)[/code]
This is how the OGLName is created (many asked if this was the source of the problem)
I did have some issues with masks due to the old way vs new way Xojo implements, but the sample I have doesn’t use any masks.
Public Function X3_LoadRGBATexture(RGBABitmap As MemoryBlock, width As Integer, height As Integer) as Integer
' www.Xojo3D.com
// IMPORTANT: Image dimensions must be in power of 2 (e.g. 8x8, 16x16, 32x32, 64x64, ...)
Dim idMB As MemoryBlock
Dim oglName As Integer
' ask OpenGL for an ID that we can use for our texture
idMB = new MemoryBlock(4) ' create a memory block into which OpenGL will store the ID value
OpenGL.glGenTextures(1, idMB) ' get the ID from OpenGL
oglName = idMB.Long(0) ' store the value returned by OpenGL in an integer
' specify to OpenGL how this texture should be rendered
OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, OGLName) ' select the texture id that OpenGL allocated for our texture
OpenGL.glTexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR) // set up settings
OpenGL.glTexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR) // set up some more settings
' now we load the image bitmap
OpenGL.glTexImage2d(OpenGL.GL_TEXTURE_2D, 0, 4, width, height , 0, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, RGBABitmap)
return oglName
End Function
Finally the Render method and RenderModel that is called from it.
[code]Function Render() Handles Render as Boolean
OpenGL.glClearColor(1,1,1,1)
OpenGL.glClear(OpenGL.GL_COLOR_BUFFER_BIT + OpenGL.GL_DEPTH_BUFFER_BIT)
OpenGL.glPushMatrix
OpenGL.glTranslatef 0, -0.05, zoom
dim stepSize as double
dim i, j as integer
if model.Ubound = -1 then return true
X3_SetRotation model(0).Rotation
for i = 0 to model.Ubound
X3_RenderModel Model(i)
next
OpenGL.glPopMatrix ’ restore matrix
End Function
[/code]
Public Sub X3_RenderModel(model As X3Core.X3Model)
' www.Xojo3D.com
Dim i, j As Integer
Dim poly As X3Core.X3Polygon
dim col as X3Core.X3Color
if model = nil then return
'X3_SetRotation model.Rotation
if model.visible = false then return
if model.Polygon.Ubound > -1 then
OpenGL.glBegin OpenGL.GL_TRIANGLES ' start drawing triangle polygons
openGL.glPolygonMode openGL.GL_FRONT, openGL.GL_FILL
end if
' draw model polygons
for i = 0 to model.Polygon.Ubound ' loop through all the polygons
poly = model.Polygon(i) ' get the next polygon
' set the normal of the polygon
OpenGL.glNormal3d poly.Normal.X, poly.Normal.Y, poly.Normal.Z
' is this polygon mapped with a texture, and if so, is the UV coordinates configured?
'if (poly.TIndex >-1 ) and (poly.UVMap.Ubound >= poly.VIndex.Ubound) then
if poly.Texture <> nil then
' yes, a texture is used
OpenGL.glColor4d(1, 1, 1, 1) ' reset color to pure white
OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, poly.Texture.OGLName) ' bind to the polygon's texture
' loop through all the vertexes of the polygon
for j = 0 to poly.VIndex.Ubound
OpenGL.glTexCoord2d poly.UVMap(j).U, poly.UVMap(j).V ' set the texture UV-coordinates for next vertex
OpenGL.glVertex3d model.Vertex(poly.vindex(j)).X, model.vertex(poly.vindex(j)).Y,model.vertex( poly.vindex(j)).Z ' add the vertex to the OpenGL vertex list
next j
OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, 0) ' unbind from the polygon's texture
else
' set the color of the polygon
if poly.FillColor <> nil then ' is the color object set
' yes, so set the color
OpenGL.glColor4d(poly.FillColor.Red, poly.FillColor.Green, poly.FillColor.Blue, poly.FillColor.Alpha)
end if
' loop through all the vertexes of the polygon
for j = 0 to poly.VIndex.Ubound
col = model.Vertex( poly.VIndex(j)).pointColor
if col <> nil then
openGL.glColor4D( col.Red, col.Green, col.Blue, 1)
end if
OpenGL.glVertex3d model.Vertex(poly.vindex(j)).X, model.vertex(poly.vindex(j)).Y,model.vertex( poly.vindex(j)).Z ' add the vertex to the OpenGL vertex list
next j
end if
next i
OpenGL.glEnd ' end drawing of polygons
End Sub