Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Tue Oct 17, 2017 6:51 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: [Solved] OpenGL with skybox texture example issue
PostPosted: Sat Oct 27, 2012 10:38 pm 
Offline

Joined: Mon Jan 02, 2006 11:43 am
Posts: 172
Location: Calgary, Alberta, Canada
Hello Everyone,

Does anyone have a simple skybox or environment box example for OpenGL without windows handlers? I have been trying to make one for the last couple of weeks without any success. Chuckle, I have been through many C++ and other programming language examples with OpenGL, and I am unable to port any of them to Real Studio.

Here is an example where there are 10 smaller boxes drawn, and one larger box (skycube). Press the w, s, a, and d keys to move around. Hold the left mouse button to change the camera angle.

Image

It looks like I can't seem to get the textures working. The textures for the skycube are oback6.raw, ofront6.raw, etc.

The example file can be downloaded here:
http://www.mediafire.com/?m9jpmc11xs5paxf

Has anyone else had success?

Thanks for your help,

Eugene


Last edited by eugenedakin on Sat Nov 03, 2012 6:37 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: OpenGL with Environment Box (skybox) texture example iss
PostPosted: Mon Oct 29, 2012 9:00 am 
Offline
User avatar

Joined: Thu Sep 10, 2009 2:50 am
Posts: 418
Location: Santa Cruz, CA, USA
eugenedakin wrote:
It looks like I can't seem to get the textures working. The textures for the skycube are oback6.raw, ofront6.raw, etc.
Got your code running and no textures either, but it does look OK to me: generate texture bank, store image data, bind to bank, draw with TexCoord. Not familiar with the use of .raw pictures and how you passed them into glTexImage2D; guess it works though because there's no complaining.

Here's a relatively minimal example that works for me. You may need to change the "OpenGL.framework" lib used. Once you have textures working a skybox is keeping the cube centered about the camera (can't tell if your example does this).

There are open source rb projects that do much more advanced texturing, half this code is snipped from such. I think some have skyboxes built in. A couple at hand...
http://www.jcquan.com/REALbasic/index.html
http://forums.realsoftware.com/viewtopic.php?f=4&t=33569

//*Start with OpenGLSurface1 sized and locked to window edges
//*and picture dragged into project and renamed "myPic"

//--------- window events
Sub Open()
genTexBank(0) //create bank for 1 texture (0 based)
storeImage(0, myPic) //put an image in the bank
OpenGLSurface1.Render //ready to draw
End Sub

Sub Resizing()
OpenGLSurface1.Render
End Sub

//-------- OpenGLSurface1 event
Function Render() As Boolean
initGLDrawing(me) //setup projection and clear buffers

OpenGL.glEnable(OpenGL.GL_TEXTURE_2D)
bindTo(0) //bind to image in first bank

OpenGL.glBegin(OpenGL.GL_QUADS) //draw quad

OpenGL.glTexCoord2f(1, 1)
OpenGL.glVertex3f(-1, -1, 0)
OpenGL.glTexCoord2f(1, 0)
OpenGL.glVertex3f(-1, 1, 0)
OpenGL.glTexCoord2f(0, 0)
OpenGL.glVertex3f( 1, 1, 0)
OpenGL.glTexCoord2f(0, 1)
OpenGL.glVertex3f( 1, -1, 0)

OpenGL.glEnd
End Function



//======================== texture helpers (window property and methods)

texNames(-1) As Integer //stores list of generated bank 'names'

Sub genTexBank(lastBankIdx As integer)
dim count As integer = lastBankIdx + 1

dim m As new MemoryBlock(count * 4) //array of 4 byte integers

OpenGL.glGenTextures(count, m) //retrieve ids

redim texNames(lastBankIdx) //scan values into easier to use int array
for i As integer = 0 to lastBankIdx
texNames(i) = m.Int32Value(i * 4)
next
End Sub

Sub storeImage(bankIdx As integer, p As Picture)
dim imgW As integer = p.Width
dim imgH As integer = p.Height
dim memImg As new MemoryBlock(imgW * imgH * 3)

dim surf As RGBSurface = p.RGBSurface

if surf = nil then //need a new pic with a surface
dim p2 As new Picture(imgW, imgH, 32)
p2.Graphics.DrawPicture p, 0, 0
p = p2 'repoint cause p2 goes out of scope, not necessary tho
surf = p.RGBSurface 'grab surf now, should be good
end

dim x, y, offset As integer 'scan surf into memoryblock
for y = 0 to imgH - 1
for x = 0 to imgW - 1
memImg.ColorValue(offset, 24) = surf.Pixel(x, y)
offset = offset + 3
next
next

OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, texNames(bankIdx))

OpenGL.glTexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_REPEAT)
OpenGL.glTexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_REPEAT)
OpenGL.glTexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR)'NEAREST)
OpenGL.glTexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR)'GL_NEAREST)

OpenGL.glPixelStorei(OpenGL.GL_UNPACK_ALIGNMENT, 1)
OpenGL.glTexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, imgW, imgH, 0, OpenGL.GL_BGR, OpenGL.GL_UNSIGNED_BYTE, memImg)
End Sub

Sub bindTo(bankIdx As integer)
OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, texNames(bankIdx))
End Sub


//======================= setup drawing (window method)

Sub initGLDrawing(disp As OpenGLSurface)
//viewport
OpenGL.glViewport 0, 0, disp.Width, disp.Height

//projection transform (camera)
OpenGL.glMatrixMode(OpenGL.GL_PROJECTION)
OpenGL.glLoadIdentity
dim imageRatio As single = disp.Width / disp.Height //fix for fov in x
OpenGL.gluPerspective 55 / imageRatio, imageRatio, 0.1, 100
Soft Declare sub gluLookAt lib "OpenGL.framework" (eyeX as Double, eyeY as Double, eyeZ as Double, centerX as Double, centerY as Double, centerZ as Double, upX as Double, upY as Double, upZ as Double)
gluLookAt 2, 1.5, -5, 0, 0, 0, 0, 1, 0 //cam position, lookat point, up vector
//model transform
OpenGL.glMatrixMode(OpenGL.GL_MODELVIEW)
OpenGL.glLoadIdentity

//clear
OpenGL.glClearColor 1, 1, 0, 0 'yellow
OpenGL.glClear OpenGL.GL_COLOR_BUFFER_BIT + OpenGL.GL_DEPTH_BUFFER_BIT
OpenGL.glEnable OpenGL.GL_DEPTH_TEST
End Sub


Top
 Profile  
Reply with quote  
 Post subject: Re: OpenGL with Environment Box (skybox) texture example iss
PostPosted: Mon Oct 29, 2012 10:06 pm 
Offline

Joined: Mon Jan 02, 2006 11:43 am
Posts: 172
Location: Calgary, Alberta, Canada
Thanks for your kind help.

I will give it a go this weekend!

Sincerely,

Eugene


Top
 Profile  
Reply with quote  
 Post subject: Re: OpenGL with Environment Box (skybox) texture example iss
PostPosted: Wed Oct 31, 2012 11:10 am 
Offline
User avatar

Joined: Thu Sep 10, 2009 2:50 am
Posts: 418
Location: Santa Cruz, CA, USA
Tried out your .raw technique and it works! Looking back at your code I found 5 changes to get it texturing...

1. leave lighting disabled, that's why the cubes are black. For lighting you need to setup a light and set colors with glMaterial.

2. call InitTexture in Open

3. add window property 'TextureID As integer' and remove the the dim line for it in InitTexture. This value needs to be saved.

4. in drawSkybox pass TextureID into glBindTexture instead of the constant Sky_Top. Comment out the other glBindTextures because only one texture is loaded. For 6 images generate and save 6 TextureIDs.

5. disable cullface before drawing the skybox and move the enabling lines to afterwards. When inside a skybox no faces get culled.

To finish off the skybox you'll need to translate to the cameras actual eye position, which is happily available. This is the Render code I ended up with. glColor(1,1,1) is there because GL_MODULATE tints the texture by this color (use GL_REPLACE for no tinting).

OpenGL.glClearColor(1,1,1,1) 'White Background
OpenGL.glClear(OpenGL.GL_COLOR_BUFFER_BIT) 'Clear Colour buffer
OpenGL.glClear(OpenGL.GL_DEPTH_BUFFER_BIT) 'Clear Depth buffer
OpenGL.glEnable(OpenGL.GL_DOUBLEBUFFER) 'Make the scene move quicker
OpenGL.glEnable(OpenGL.GL_DEPTH_TEST) 'Enables 3d depth testing

Call Camera

OpenGL.glDisable OpenGL.GL_CULL_FACE

OpenGL.glPushMatrix()
OpenGL.glTranslated(XPosition, YPosition, ZPosition) //center on camera eye
OpenGL.glColor3d(1,1,1)
drawSkybox(50) 'draw environment cube
OpenGL.glPopMatrix()

OpenGL.glCullFace OpenGL.GL_BACK 'Only draw quads facing us
OpenGL.glEnable OpenGL.GL_CULL_FACE 'Ignore quads away from us

OpenGL.glPushMatrix()
OpenGL.glScaled(0.35,0.35,0.35) ' scale at 35%
call DrawCube 'Draw a hundered cubes
OpenGL.glPopMatrix()


Also, no need to begin and end each quad, just keep listing 4 points.


Top
 Profile  
Reply with quote  
 Post subject: Re: OpenGL with Environment Box (skybox) texture example iss
PostPosted: Sat Nov 03, 2012 1:08 pm 
Offline

Joined: Mon Jan 02, 2006 11:43 am
Posts: 172
Location: Calgary, Alberta, Canada
Thank you for all of your help,

I have implemented all except the textures (working on that one right now).

:)

Eugene


Top
 Profile  
Reply with quote  
 Post subject: Re: OpenGL with Environment Box (skybox) texture example iss
PostPosted: Sat Nov 03, 2012 6:36 pm 
Offline

Joined: Mon Jan 02, 2006 11:43 am
Posts: 172
Location: Calgary, Alberta, Canada
Hi There,

Your examples are absolutely fantastic!! Here is a screen grab of the 10 coloured cubes with a skybox (I left the cube edges with lines so that you can see the outline of the cube)

Image

The code was great, and thank you for sharing!!!!

Sincerely,

Eugene


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group