Real Software Forums

[Solved] OpenGL with skybox texture example issue
Page 1 of 1

Author:  eugenedakin [ Sat Oct 27, 2012 10:38 pm ]
Post subject:  [Solved] OpenGL with skybox texture example issue

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.


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:

Has anyone else had success?

Thanks for your help,


Author:  doofus [ Mon Oct 29, 2012 9:00 am ]
Post subject:  Re: OpenGL with Environment Box (skybox) texture example iss

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...

//*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()
End Sub

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

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)

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)
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

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

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


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)
OpenGL.glViewport 0, 0, disp.Width, disp.Height

//projection transform (camera)
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.glClearColor 1, 1, 0, 0 'yellow
OpenGL.glEnable OpenGL.GL_DEPTH_TEST
End Sub

Author:  eugenedakin [ Mon Oct 29, 2012 10:06 pm ]
Post subject:  Re: OpenGL with Environment Box (skybox) texture example iss

Thanks for your kind help.

I will give it a go this weekend!



Author:  doofus [ Wed Oct 31, 2012 11:10 am ]
Post subject:  Re: OpenGL with Environment Box (skybox) texture example iss

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.glTranslated(XPosition, YPosition, ZPosition) //center on camera eye
drawSkybox(50) 'draw environment cube

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

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

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

Author:  eugenedakin [ Sat Nov 03, 2012 1:08 pm ]
Post subject:  Re: OpenGL with Environment Box (skybox) texture example iss

Thank you for all of your help,

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



Author:  eugenedakin [ Sat Nov 03, 2012 6:36 pm ]
Post subject:  Re: OpenGL with Environment Box (skybox) texture example iss

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)


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



Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group