Texture an object in OpenGL

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by LJ.

Having trouble translating NeHe's tutorial lesson 6 which deals with texturing in OpenGL into PureBasic. Have also checked the archives here on the web site, no luck. Does anyone know how to apply a texture to an object in OpenGL using PureBasic?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by raphael.

this is what traumatic sent me some time ago.
it always worked for me...

Code: Select all

Procedure _CreateTexture(pData.l, mode.s, mipmapping.b, bmpWidth.l, bmpHeight.l)

  If pData = 0
    MessageBox_(0, "unable to load texture", ":PBglLib", #MB_OK | #MB_ICONERROR)
  Else

  glGenTextures_(1, @texture)
  glBindTexture_(#GL_TEXTURE_2D, texture)
  glTexEnvi_(#GL_TEXTURE_ENV, #GL_TEXTURE_ENV_MODE, #GL_MODULATE)       ; Texture blends with object background
;  glTexEnvi_(#GL_TEXTURE_ENV, #GL_TEXTURE_ENV_MODE, #GL_DECAL)         ; Texture does NOT blend with object background

  ; Select a filtering type. BiLinear filtering produces very good results with little performance impact
  ;   #GL_NEAREST               - Basic texture (grainy looking texture)
  ;   #GL_LINEAR                - BiLinear filtering
  ;   #GL_LINEAR_MIPMAP_NEAREST - Basic mipmapped texture
  ;   #GL_LINEAR_MIPMAP_LINEAR  - BiLinear Mipmapped texture
  
  glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_LINEAR)  ; only first two can be used
  
  If mipmapping = 1
    glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR_MIPMAP_LINEAR)  ; all of the above can be used
  Else
    glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR)  ; all of the above can be used
  EndIf


  Select mode
    Case LCase("rgb")
      If mipmapping = 1
        gluBuild2DMipmaps_(#GL_TEXTURE_2D, #GL_RGB, bmpWidth, bmpHeight, #GL_RGB, #GL_UNSIGNED_BYTE, pData):
      Else
        glTexImage2D_(#GL_TEXTURE_2D, 0, #GL_RGB, bmpWidth, bmpHeight, 0, #GL_RGB, #GL_UNSIGNED_BYTE, pData)   ; Use when not wanting mipmaps to be built by openGL
      EndIf
    
    Case LCase("rgba")
      If mipmapping = 1
        gluBuild2DMipmaps_(#GL_TEXTURE_2D, #GL_RGBA, bmpWidth, bmpHeight, #GL_RGBA, #GL_UNSIGNED_BYTE, pData)   ;
      Else
        glTexImage2D_(#GL_TEXTURE_2D, 0, #GL_RGBA, bmpWidth, bmpHeight, 0, #GL_RGBA, #GL_UNSIGNED_BYTE, pData)   ; Use when not wanting mipmaps to be built by openGL
      EndIf

    Case LCase("luminance")
      If mipmapping = 1
        gluBuild2DMipmaps_(#GL_TEXTURE_2D, #GL_LUMINANCE, bmpWidth, bmpHeight, #GL_LUMINANCE, #GL_UNSIGNED_BYTE, pData)
      Else
        glTexImage2D_(#GL_TEXTURE_2D, 0, #GL_LUMINANCE, bmpWidth, bmpHeight, 0, #GL_LUMINANCE, #GL_UNSIGNED_BYTE, pData)   ; Use when not wanting mipmaps to be built by openGL
      EndIf
      
  EndSelect

  EndIf

  ProcedureReturn texture
EndProcedure


;
; load BMPs from memory-locations (IncludeBinary)
; loads 8, 16 and 24bit bmps
;
Procedure loadBMPTextureMem(memloc.l, mode.s, mipmapping.b)
  FileHeader.BITMAPFILEHEADER
  InfoHeader.BITMAPINFOHEADER

  ; read the bitmap file header
  FileHeader\bfType                 = PeekW(memloc +0) ;2
  FileHeader\bfSize                 = PeekL(memloc +2) ;4
  FileHeader\bfReserved1            = PeekW(memloc +6) ;2
  FileHeader\bfReserved2            = PeekW(memloc +8) ;2
  FileHeader\bfOffBits              = PeekL(memloc+10) ;4

  ; check if it's a valid bmp-file
  If FileHeader\bfType  $4D42
    ; MessageBox_(0, "invalid bmpfile @"+Str(memlocation), ":PBglLib", #MB_OK)
    ProcedureReturn 0
  EndIf


  ; read the bitmap information header
  InfoHeader\biSize          = PeekL(memloc+14) ;4
  InfoHeader\biWidth         = PeekL(memloc+18) ;4
  InfoHeader\biHeight        = PeekL(memloc+22) ;4
  InfoHeader\biPlanes        = PeekW(memloc+26) ;2
  InfoHeader\biBitCount      = PeekW(memloc+28) ;2
  InfoHeader\biCompression   = PeekL(memloc+30) ;4
  InfoHeader\biSizeImage     = PeekL(memloc+34) ;4
  InfoHeader\biXPelsPerMeter = PeekL(memloc+38) ;4
  InfoHeader\biYPelsPerMeter = PeekL(memloc+42) ;4
  InfoHeader\biClrUsed       = PeekL(memloc+46) ;4
  InfoHeader\biClrImportant  = PeekL(memloc+50) ;4

  If InfoHeader\biSizeImage = 0
    InfoHeader\biSizeImage = (InfoHeader\biWidth * InfoHeader\biHeight * InfoHeader\biBitCount / 8)
  EndIf

  ; allocate enough mem to store the bitmap
;  Dim bitmapImage.b (InfoHeader\biSizeImage)
  AllocateMemory(0, InfoHeader\biSizeImage+2, 0)

  ; read in the bitmap image data
  For i.l=FileHeader\bfOffBits To InfoHeader\biSizeImage
    CopyMemory(memloc+i, UseMemory(0)+i2, 1)
    i2.l+1
  Next


  If InfoHeader\biBitCount>8
    ; swap BGR to RGB
    AllocateMemory(1,1,0)
    
    For i.l=0 To InfoHeader\biSizeImage Step 3
      CopyMemory(UseMemory(0)+i, UseMemory(1), 1)
      CopyMemory(UseMemory(0)+i+2, UseMemory(0)+i, 1)
      CopyMemory(UseMemory(1), UseMemory(0)+i+2, 1)
    Next
    
;    FreeMemory(1)
  EndIf 

  ; create texture
  texture = _CreateTexture(UseMemory(0), mode.s, mipmapping.b, InfoHeader\biWidth, InfoHeader\biHeight)

  ; cleanup
  FreeMemory(0)

  ProcedureReturn texture
EndProcedure


; example:
myTex = LoadBMPTextureMem(?texlabel, "rgb", 0)

BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by LJ.

Hi Raphael:
Uggghhhh... my eyes are bugging out, I've textured the cube pure black I think, something is wrong. Here's the code from Nehe's Lesson 6 Texturing in OpenGL with traumatic sent you, any suggestions?:

IncludeFile "OpenGL.pbi"

Global myTex
Global rquad.f

Procedure _CreateTexture(pData.l, mode.s, mipmapping.b, bmpWidth.l, bmpHeight.l)

If pData = 0
MessageBox_(0, "unable to load texture", ":PBglLib", #MB_OK | #MB_ICONERROR)
Else

glGenTextures_(1, @texture)
glBindTexture_(#GL_TEXTURE_2D, texture)
glTexEnvi_(#GL_TEXTURE_ENV, #GL_TEXTURE_ENV_MODE, #GL_MODULATE) ; Texture blends with object background

glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_LINEAR) ; only first two can be used

If mipmapping = 1
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR_MIPMAP_LINEAR) ; all of the above can be used
Else
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR) ; all of the above can be used
EndIf


Select mode
Case LCase("rgb")
If mipmapping = 1
gluBuild2DMipmaps_(#GL_TEXTURE_2D, #GL_RGB, bmpWidth, bmpHeight, #GL_RGB, #GL_UNSIGNED_BYTE, pData):
Else
glTexImage2D_(#GL_TEXTURE_2D, 0, #GL_RGB, bmpWidth, bmpHeight, 0, #GL_RGB, #GL_UNSIGNED_BYTE, pData) ; Use when not wanting mipmaps to be built by openGL
EndIf

Case LCase("rgba")
If mipmapping = 1
gluBuild2DMipmaps_(#GL_TEXTURE_2D, #GL_RGBA, bmpWidth, bmpHeight, #GL_RGBA, #GL_UNSIGNED_BYTE, pData) ;
Else
glTexImage2D_(#GL_TEXTURE_2D, 0, #GL_RGBA, bmpWidth, bmpHeight, 0, #GL_RGBA, #GL_UNSIGNED_BYTE, pData) ; Use when not wanting mipmaps to be built by openGL
EndIf

Case LCase("luminance")
If mipmapping = 1
gluBuild2DMipmaps_(#GL_TEXTURE_2D, #GL_LUMINANCE, bmpWidth, bmpHeight, #GL_LUMINANCE, #GL_UNSIGNED_BYTE, pData)
Else
glTexImage2D_(#GL_TEXTURE_2D, 0, #GL_LUMINANCE, bmpWidth, bmpHeight, 0, #GL_LUMINANCE, #GL_UNSIGNED_BYTE, pData) ; Use when not wanting mipmaps to be built by openGL
EndIf

EndSelect

EndIf

ProcedureReturn texture
EndProcedure


;
; load BMPs from memory-locations (IncludeBinary)
; loads 8, 16 and 24bit bmps
;
Procedure loadBMPTextureMem(memloc.l, mode.s, mipmapping.b)
FileHeader.BITMAPFILEHEADER
InfoHeader.BITMAPINFOHEADER

; read the bitmap file header
FileHeader\bfType = PeekW(memloc +0) ;2
FileHeader\bfSize = PeekL(memloc +2) ;4
FileHeader\bfReserved1 = PeekW(memloc +6) ;2
FileHeader\bfReserved2 = PeekW(memloc +8) ;2
FileHeader\bfOffBits = PeekL(memloc+10) ;4

; check if it's a valid bmp-file
If FileHeader\bfType $4D42
; MessageBox_(0, "invalid bmpfile @"+Str(memlocation), ":PBglLib", #MB_OK)
ProcedureReturn 0
EndIf


; read the bitmap information header
InfoHeader\biSize = PeekL(memloc+14) ;4
InfoHeader\biWidth = PeekL(memloc+18) ;4
InfoHeader\biHeight = PeekL(memloc+22) ;4
InfoHeader\biPlanes = PeekW(memloc+26) ;2
InfoHeader\biBitCount = PeekW(memloc+28) ;2
InfoHeader\biCompression = PeekL(memloc+30) ;4
InfoHeader\biSizeImage = PeekL(memloc+34) ;4
InfoHeader\biXPelsPerMeter = PeekL(memloc+38) ;4
InfoHeader\biYPelsPerMeter = PeekL(memloc+42) ;4
InfoHeader\biClrUsed = PeekL(memloc+46) ;4
InfoHeader\biClrImportant = PeekL(memloc+50) ;4

If InfoHeader\biSizeImage = 0
InfoHeader\biSizeImage = (InfoHeader\biWidth * InfoHeader\biHeight * InfoHeader\biBitCount / 8)
EndIf

; allocate enough mem to store the bitmap
; Dim bitmapImage.b (InfoHeader\biSizeImage)
AllocateMemory(0, InfoHeader\biSizeImage+2, 0)

; read in the bitmap image data
For i.l=FileHeader\bfOffBits To InfoHeader\biSizeImage
CopyMemory(memloc+i, UseMemory(0)+i2, 1)
i2.l+1
Next


If InfoHeader\biBitCount>8
; swap BGR to RGB
AllocateMemory(1,1,0)

For i.l=0 To InfoHeader\biSizeImage Step 3
CopyMemory(UseMemory(0)+i, UseMemory(1), 1)
CopyMemory(UseMemory(0)+i+2, UseMemory(0)+i, 1)
CopyMemory(UseMemory(1), UseMemory(0)+i+2, 1)
Next

; FreeMemory(1)
EndIf

; create texture
texture = _CreateTexture(UseMemory(0), mode.s, mipmapping.b, InfoHeader\biWidth, InfoHeader\biHeight)

; cleanup
FreeMemory(0)

ProcedureReturn texture
EndProcedure

Procedure InitGL()
glClearColor_(0.0, 0.0, 0.0, 0.0)
glDepthFunc_(#GL_LESS)
glEnable_(#GL_DEPTH_TEST)
glEnable_(#GL_TEXTURE_2D);
glShadeModel_(#GL_SMOOTH)

glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_()

gluPerspectivef_(45.0, 640/480, 0.1, 100.0)
glMatrixMode_(#GL_MODELVIEW)
EndProcedure

Procedure DrawScene(hDC)
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)

glLoadIdentity_();
glTranslatef_(0,0.0,-7.0); // Move Right And Into The Screen

glRotatef_(rquad,1.0,1.0,1.0); // Rotate The Cube On X, Y & Z

glBindTexture_(#GL_TEXTURE_2D, myTex);
glBegin_(GL_QUADS);
; // Front Face
glTexCoord2f_(0.0, 0.0): glVertex3f_(-1.0, -1.0, 1.0); // Bottom Left Of The Texture and Quad
glTexCoord2f_(1.0, 0.0): glVertex3f_( 1.0, -1.0, 1.0); // Bottom Right Of The Texture and Quad
glTexCoord2f_(1.0, 1.0): glVertex3f_( 1.0, 1.0, 1.0); // Top Right Of The Texture and Quad
glTexCoord2f_(0.0, 1.0): glVertex3f_(-1.0, 1.0, 1.0); // Top Left Of The Texture and Quad
;// Back Face
glTexCoord2f_(1.0, 0.0): glVertex3f_(-1.0, -1.0, -1.0); // Bottom Right Of The Texture and Quad
glTexCoord2f_(1.0, 1.0): glVertex3f_(-1.0, 1.0, -1.0); // Top Right Of The Texture and Quad
glTexCoord2f_(0.0, 1.0): glVertex3f_( 1.0, 1.0, -1.0); // Top Left Of The Texture and Quad
glTexCoord2f_(0.0, 0.0): glVertex3f_( 1.0, -1.0, -1.0); // Bottom Left Of The Texture and Quad
;// Top Face
glTexCoord2f_(0.0, 1.0): glVertex3f_(-1.0, 1.0, -1.0); // Top Left Of The Texture and Quad
glTexCoord2f_(0.0, 0.0): glVertex3f_(-1.0, 1.0, 1.0); // Bottom Left Of The Texture and Quad
glTexCoord2f_(1.0, 0.0): glVertex3f_( 1.0, 1.0, 1.0); // Bottom Right Of The Texture and Quad
glTexCoord2f_(1.0, 1.0): glVertex3f_( 1.0, 1.0, -1.0); // Top Right Of The Texture and Quad
;// Bottom Face
glTexCoord2f_(1.0, 1.0): glVertex3f_(-1.0, -1.0, -1.0); // Top Right Of The Texture and Quad
glTexCoord2f_(0.0, 1.0): glVertex3f_( 1.0, -1.0, -1.0); // Top Left Of The Texture and Quad
glTexCoord2f_(0.0, 0.0): glVertex3f_( 1.0, -1.0, 1.0); // Bottom Left Of The Texture and Quad
glTexCoord2f_(1.0, 0.0): glVertex3f_(-1.0, -1.0, 1.0); // Bottom Right Of The Texture and Quad
;// Right face
glTexCoord2f_(1.0, 0.0): glVertex3f_( 1.0, -1.0, -1.0); // Bottom Right Of The Texture and Quad
glTexCoord2f_(1.0, 1.0): glVertex3f_( 1.0, 1.0, -1.0); // Top Right Of The Texture and Quad
glTexCoord2f_(0.0, 1.0): glVertex3f_( 1.0, 1.0, 1.0); // Top Left Of The Texture and Quad
glTexCoord2f_(0.0, 0.0): glVertex3f_( 1.0, -1.0, 1.0); // Bottom Left Of The Texture and Quad
;// Left Face
glTexCoord2f_(0.0, 0.0): glVertex3f_(-1.0, -1.0, -1.0); // Bottom Left Of The Texture and Quad
glTexCoord2f_(1.0, 0.0): glVertex3f_(-1.0, -1.0, 1.0); // Bottom Right Of The Texture and Quad
glTexCoord2f_(1.0, 1.0): glVertex3f_(-1.0, 1.0, 1.0); // Top Right Of The Texture and Quad
glTexCoord2f_(0.0, 1.0): glVertex3f_(-1.0, 1.0, -1.0); // Top Left Of The Texture and Quad
glEnd_();

rquad = rquad - 0.45; // Decrease The Rotation Variable For The Quad

EndProcedure

pfd.PIXELFORMATDESCRIPTOR

hWnd = OpenWindow(0, 0, 0, 800, 580, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "OpenGL Test")

hdc = GetDC_(hWnd)

pfd\nSize = SizeOf(PIXELFORMATDESCRIPTOR)
pfd\nVersion = 1
pfd\dwFlags = #PFD_SUPPORT_OPENGL | #PFD_DOUBLEBUFFER | #PFD_DRAW_TO_WINDOW
pfd\iLayerType = #PFD_MAIN_PLANE
pfd\iPixelType = #PFD_TYPE_RGBA
pfd\cColorBits = 24
pfd\cDepthBits = 16

pixformat = ChoosePixelFormat_(hdc, pfd)
SetPixelFormat_(hdc, pixformat, pfd)
hrc = wglCreateContext_(hdc)
wglMakeCurrent_(hdc,hrc)

InitGL()

myTex = LoadBMPTextureMem(?texlabel, "rgb", 0)

While Quit = 0

Repeat
EventID = WindowEvent()

Select EventID
Case #PB_EventCloseWindow
Quit = 1
EndSelect

Until EventID = 0

DrawScene(hDC)
SwapBuffers_(hDC)

Wend

texlabel:
IncludeBinary "NeHe.bmp"
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by dmoc.

At a quick glance:

glBegin_(GL_QUADS); >>> glBegin_(#GL_QUADS);

PS: Are you using the code tags? It would make it easier for forum users to read.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by raphael.
Originally posted by LJ

Hi Raphael:
Uggghhhh... my eyes are bugging out, I've textured the cube pure black I think, something is wrong. Here's the code from Nehe's Lesson 6 Texturing in OpenGL with traumatic sent you, any suggestions?:
at a quick look i'd say you're having trouble with perspective.
the second look tells me, you didn't convert nehe #6.
well, i did that for you... here're the changed procedures:

Code: Select all

Global xrot.f, yrot.f, zrot.f

Procedure InitGL()
 glEnable_(#GL_TEXTURE_2D)
 glShadeModel_(#GL_SMOOTH)
 glClearColor_(0.0, 0.0, 0.0, 0.5)
 glEnable_(#GL_DEPTH_TEST)
 glDepthFunc_(#GL_LEQUAL)
 glHint_(#GL_PERSPECTIVE_CORRECTION_HINT, #GL_NICEST)
 glViewport_(0,0,800,580)
 glMatrixMode_(#GL_PROJECTION)
 glLoadIdentity_()
 ; Calculate The Aspect Ratio Of The Window
 gluPerspective(45.0, 800/580, 0.1, 100.0) ; EXPLAINED LATER ON!
 glMatrixMode_(#GL_MODELVIEW)
 glLoadIdentity_()
EndProcedure


Procedure DrawScene(hDC)
 glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
 glLoadIdentity_()
 glTranslatef_(0.0,0.0,-5.0)

 glRotatef_(xrot.f,1.0,0.0,0.0)
 glRotatef_(yrot.f,0.0,1.0,0.0)
 glRotatef_(zrot.f,0.0,0.0,1.0)

 glEnable_(#GL_TEXTURE_2D)
 glBindTexture_(#GL_TEXTURE_2D, myTex)

 glBegin_(#GL_QUADS)
  ; Front Face
  glTexCoord2f_(0.0, 0.0) : glVertex3f_(-1.0, -1.0,  1.0)
  glTexCoord2f_(1.0, 0.0) : glVertex3f_( 1.0, -1.0,  1.0)
  glTexCoord2f_(1.0, 1.0) : glVertex3f_( 1.0,  1.0,  1.0)
  glTexCoord2f_(0.0, 1.0) : glVertex3f_(-1.0,  1.0,  1.0)
  ; Back Face
  glTexCoord2f_(1.0, 0.0) : glVertex3f_(-1.0, -1.0, -1.0)
  glTexCoord2f_(1.0, 1.0) : glVertex3f_(-1.0,  1.0, -1.0)
  glTexCoord2f_(0.0, 1.0) : glVertex3f_( 1.0,  1.0, -1.0)
  glTexCoord2f_(0.0, 0.0) : glVertex3f_( 1.0, -1.0, -1.0)
  ; Top Face
  glTexCoord2f_(0.0, 1.0) : glVertex3f_(-1.0,  1.0, -1.0)
  glTexCoord2f_(0.0, 0.0) : glVertex3f_(-1.0,  1.0,  1.0)
  glTexCoord2f_(1.0, 0.0) : glVertex3f_( 1.0,  1.0,  1.0)
  glTexCoord2f_(1.0, 1.0) : glVertex3f_( 1.0,  1.0, -1.0)
  ; Bottom Face
  glTexCoord2f_(1.0, 1.0) : glVertex3f_(-1.0, -1.0, -1.0)
  glTexCoord2f_(0.0, 1.0) : glVertex3f_( 1.0, -1.0, -1.0)
  glTexCoord2f_(0.0, 0.0) : glVertex3f_( 1.0, -1.0,  1.0)
  glTexCoord2f_(1.0, 0.0) : glVertex3f_(-1.0, -1.0,  1.0)
  ; Right face
  glTexCoord2f_(1.0, 0.0) : glVertex3f_( 1.0, -1.0, -1.0)
  glTexCoord2f_(1.0, 1.0) : glVertex3f_( 1.0,  1.0, -1.0)
  glTexCoord2f_(0.0, 1.0) : glVertex3f_( 1.0,  1.0,  1.0)
  glTexCoord2f_(0.0, 0.0) : glVertex3f_( 1.0, -1.0,  1.0)
  ; Left Face
  glTexCoord2f_(0.0, 0.0) : glVertex3f_(-1.0, -1.0, -1.0)
  glTexCoord2f_(1.0, 0.0) : glVertex3f_(-1.0, -1.0,  1.0)
  glTexCoord2f_(1.0, 1.0) : glVertex3f_(-1.0,  1.0,  1.0)
  glTexCoord2f_(0.0, 1.0) : glVertex3f_(-1.0,  1.0, -1.0)
 glEnd_()

 xrot+0.3
 yrot+0.2
 zrot+0.4
EndProcedure
three things left to say:

1. if you don't put an 'End' before 'IncludeBinary' PB will crash on exit...
2. your main loop looks strange to me. why repeat/until AND while/wend ?
3. in order to use gluPerspective() PB had to have doubles.
well, it hasn't, so you can either convert floats to double with the 'Doubles-Library' (somewhere on the resourcesite) or use the following lib (again done by traumatic):

http://files.connection-refused.org/GLWrapper

with this library you can simply use floats.
it's of course not as accurate and... well... cheating.

it does the following

extern _stdcall void PB_gluPerspective(float fovy, float aspect, float zNear, float zFar)
{
gluPerspective(fovy, aspect, zNear, zFar);
}

'GLWrapper' has also some more commands, just find them...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by LJ.

Thanks Dmoc and Raphael.

Dmoc: That was it! A simple # sign in front of the Quad command got the code working! Thank you.

Raphael: Too many questions, but thank you.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by raphael.
Dmoc: That was it! A simple # sign in front of the Quad command got the code working! Thank you.
hmm, this doesn't work for me
Raphael: Too many questions, but thank you.
:):)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by LJ.

Code: Select all

; NeHe Lesson 6 OpenGL Texture Tutorial
; Translated to PureBasic by Lance Jepsen
; [url]http://nehe.gamedev.net/[/url]
; Thanks to Traumatic for the Load Texture procedures,
; Raphael for sharing them and with pointers :wink:
; and Dmoc for finding the missing # in glBegin_(#GL_QUADS)
; Without these guys this wouldn't be possible.
; Enjoy!!

IncludeFile "OpenGL.pbi"

Global texture
Global rquad.f

Procedure _CreateTexture(pData.l, mode.s, mipmapping.b, bmpWidth.l, bmpHeight.l)

  If pData = 0
    MessageBox_(0, "unable to load texture", ":PBglLib", #MB_OK | #MB_ICONERROR)
  Else

  glGenTextures_(1, @texture)
  glBindTexture_(#GL_TEXTURE_2D, texture)
  glTexEnvi_(#GL_TEXTURE_ENV, #GL_TEXTURE_ENV_MODE, #GL_MODULATE)       ; Texture blends with object background
  glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_LINEAR)  ; only first two can be used
  
  If mipmapping = 1
    glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR_MIPMAP_LINEAR)  ; all of the above can be used
  Else
    glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR)  ; all of the above can be used
  EndIf

  Select mode
    Case LCase("rgb")
      If mipmapping = 1
        gluBuild2DMipmaps_(#GL_TEXTURE_2D, #GL_RGB, bmpWidth, bmpHeight, #GL_RGB, #GL_UNSIGNED_BYTE, pData):
      Else
        glTexImage2D_(#GL_TEXTURE_2D, 0, #GL_RGB, bmpWidth, bmpHeight, 0, #GL_RGB, #GL_UNSIGNED_BYTE, pData)   ; Use when not wanting mipmaps to be built by openGL
      EndIf
    
    Case LCase("rgba")
      If mipmapping = 1
        gluBuild2DMipmaps_(#GL_TEXTURE_2D, #GL_RGBA, bmpWidth, bmpHeight, #GL_RGBA, #GL_UNSIGNED_BYTE, pData)   ;
      Else
        glTexImage2D_(#GL_TEXTURE_2D, 0, #GL_RGBA, bmpWidth, bmpHeight, 0, #GL_RGBA, #GL_UNSIGNED_BYTE, pData)   ; Use when not wanting mipmaps to be built by openGL
      EndIf

    Case LCase("luminance")
      If mipmapping = 1
        gluBuild2DMipmaps_(#GL_TEXTURE_2D, #GL_LUMINANCE, bmpWidth, bmpHeight, #GL_LUMINANCE, #GL_UNSIGNED_BYTE, pData)
      Else
        glTexImage2D_(#GL_TEXTURE_2D, 0, #GL_LUMINANCE, bmpWidth, bmpHeight, 0, #GL_LUMINANCE, #GL_UNSIGNED_BYTE, pData)   ; Use when not wanting mipmaps to be built by openGL
      EndIf
      
  EndSelect

  EndIf

  ProcedureReturn texture
EndProcedure
;
; load BMPs from memory-locations (IncludeBinary)
; loads 8, 16 and 24bit bmps
;
Procedure loadBMPTextureMem(memloc.l, mode.s, mipmapping.b)
  FileHeader.BITMAPFILEHEADER
  InfoHeader.BITMAPINFOHEADER

  ; read the bitmap file header
  FileHeader\bfType                 = PeekW(memloc +0) ;2
  FileHeader\bfSize                 = PeekL(memloc +2) ;4
  FileHeader\bfReserved1            = PeekW(memloc +6) ;2
  FileHeader\bfReserved2            = PeekW(memloc +8) ;2
  FileHeader\bfOffBits              = PeekL(memloc+10) ;4

  ; check if it's a valid bmp-file
  If FileHeader\bfType  $4D42
    ; MessageBox_(0, "invalid bmpfile @"+Str(memlocation), ":PBglLib", #MB_OK)
    ProcedureReturn 0
  EndIf

  ; read the bitmap information header
  InfoHeader\biSize          = PeekL(memloc+14) ;4
  InfoHeader\biWidth         = PeekL(memloc+18) ;4
  InfoHeader\biHeight        = PeekL(memloc+22) ;4
  InfoHeader\biPlanes        = PeekW(memloc+26) ;2
  InfoHeader\biBitCount      = PeekW(memloc+28) ;2
  InfoHeader\biCompression   = PeekL(memloc+30) ;4
  InfoHeader\biSizeImage     = PeekL(memloc+34) ;4
  InfoHeader\biXPelsPerMeter = PeekL(memloc+38) ;4
  InfoHeader\biYPelsPerMeter = PeekL(memloc+42) ;4
  InfoHeader\biClrUsed       = PeekL(memloc+46) ;4
  InfoHeader\biClrImportant  = PeekL(memloc+50) ;4

  If InfoHeader\biSizeImage = 0
    InfoHeader\biSizeImage = (InfoHeader\biWidth * InfoHeader\biHeight * InfoHeader\biBitCount / 8)
  EndIf

; allocate enough mem to store the bitmap
;  Dim bitmapImage.b (InfoHeader\biSizeImage)
AllocateMemory(0, InfoHeader\biSizeImage+2, 0)

  ; read in the bitmap image data
For i.l=FileHeader\bfOffBits To InfoHeader\biSizeImage
   CopyMemory(memloc+i, UseMemory(0)+i2, 1)
   i2.l+1
Next

If InfoHeader\biBitCount>8
    ; swap BGR to RGB
    AllocateMemory(1,1,0)
    
    For i.l=0 To InfoHeader\biSizeImage Step 3
      CopyMemory(UseMemory(0)+i, UseMemory(1), 1)
      CopyMemory(UseMemory(0)+i+2, UseMemory(0)+i, 1)
      CopyMemory(UseMemory(1), UseMemory(0)+i+2, 1)
    Next

EndIf 

; create texture
  texture = _CreateTexture(UseMemory(0), mode.s, mipmapping.b, InfoHeader\biWidth, InfoHeader\biHeight)

; cleanup
FreeMemory(0)

  ProcedureReturn texture
EndProcedure

Procedure InitGL()
glClearColor_(0.0, 0.0, 0.0, 0.0)
glDepthFunc_(#GL_LESS)
glEnable_(#GL_DEPTH_TEST)
glEnable_(#GL_TEXTURE_2D);
glShadeModel_(#GL_SMOOTH)
glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_()
gluPerspectivef_(45.0, 640/480, 0.1, 100.0)
glMatrixMode_(#GL_MODELVIEW)
EndProcedure

Procedure DrawScene(hDC)
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glLoadIdentity_();
glTranslatef_(0,0.0,-7.0);			// Move Right And Into The Screen
glRotatef_(rquad,1.0,1.0,1.0);		// Rotate The Cube On X, Y & Z
glBindTexture_(#GL_TEXTURE_2D, texture);
glBegin_(#GL_QUADS);
 ; // Front Face
  glTexCoord2f_(0.0, 0.0): glVertex3f_(-1.0, -1.0,  1.0);	// Bottom Left Of The Texture and Quad
  glTexCoord2f_(1.0, 0.0): glVertex3f_( 1.0, -1.0,  1.0);	// Bottom Right Of The Texture and Quad
  glTexCoord2f_(1.0, 1.0): glVertex3f_( 1.0,  1.0,  1.0);	// Top Right Of The Texture and Quad
  glTexCoord2f_(0.0, 1.0): glVertex3f_(-1.0,  1.0,  1.0);	// Top Left Of The Texture and Quad
  ;// Back Face
  glTexCoord2f_(1.0, 0.0): glVertex3f_(-1.0, -1.0, -1.0);	// Bottom Right Of The Texture and Quad
  glTexCoord2f_(1.0, 1.0): glVertex3f_(-1.0,  1.0, -1.0);	// Top Right Of The Texture and Quad
  glTexCoord2f_(0.0, 1.0): glVertex3f_( 1.0,  1.0, -1.0);	// Top Left Of The Texture and Quad
  glTexCoord2f_(0.0, 0.0): glVertex3f_( 1.0, -1.0, -1.0);	// Bottom Left Of The Texture and Quad
  ;// Top Face
  glTexCoord2f_(0.0, 1.0): glVertex3f_(-1.0,  1.0, -1.0);	// Top Left Of The Texture and Quad
  glTexCoord2f_(0.0, 0.0): glVertex3f_(-1.0,  1.0,  1.0);	// Bottom Left Of The Texture and Quad
  glTexCoord2f_(1.0, 0.0): glVertex3f_( 1.0,  1.0,  1.0);	// Bottom Right Of The Texture and Quad
  glTexCoord2f_(1.0, 1.0): glVertex3f_( 1.0,  1.0, -1.0);	// Top Right Of The Texture and Quad
  ;// Bottom Face
  glTexCoord2f_(1.0, 1.0): glVertex3f_(-1.0, -1.0, -1.0);	// Top Right Of The Texture and Quad
  glTexCoord2f_(0.0, 1.0): glVertex3f_( 1.0, -1.0, -1.0);	// Top Left Of The Texture and Quad
  glTexCoord2f_(0.0, 0.0): glVertex3f_( 1.0, -1.0,  1.0);	// Bottom Left Of The Texture and Quad
  glTexCoord2f_(1.0, 0.0): glVertex3f_(-1.0, -1.0,  1.0);	// Bottom Right Of The Texture and Quad
  ;// Right face
  glTexCoord2f_(1.0, 0.0): glVertex3f_( 1.0, -1.0, -1.0);	// Bottom Right Of The Texture and Quad
  glTexCoord2f_(1.0, 1.0): glVertex3f_( 1.0,  1.0, -1.0);	// Top Right Of The Texture and Quad
  glTexCoord2f_(0.0, 1.0): glVertex3f_( 1.0,  1.0,  1.0);	// Top Left Of The Texture and Quad
  glTexCoord2f_(0.0, 0.0): glVertex3f_( 1.0, -1.0,  1.0);	// Bottom Left Of The Texture and Quad
  ;// Left Face
  glTexCoord2f_(0.0, 0.0): glVertex3f_(-1.0, -1.0, -1.0);	// Bottom Left Of The Texture and Quad
  glTexCoord2f_(1.0, 0.0): glVertex3f_(-1.0, -1.0,  1.0);	// Bottom Right Of The Texture and Quad
  glTexCoord2f_(1.0, 1.0): glVertex3f_(-1.0,  1.0,  1.0);	// Top Right Of The Texture and Quad
  glTexCoord2f_(0.0, 1.0): glVertex3f_(-1.0,  1.0, -1.0);	// Top Left Of The Texture and Quad
 glEnd_();

rquad = rquad - 0.75;			// Decrease The Rotation Variable For The Quad 

EndProcedure

pfd.PIXELFORMATDESCRIPTOR
hWnd = OpenWindow(0, 0, 0, 800, 580, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "NeHe OpenGL Lesson 6")
hdc = GetDC_(hWnd)
pfd\nSize        = SizeOf(PIXELFORMATDESCRIPTOR)
pfd\nVersion     = 1
pfd\dwFlags      = #PFD_SUPPORT_OPENGL | #PFD_DOUBLEBUFFER | #PFD_DRAW_TO_WINDOW
pfd\iLayerType  = #PFD_MAIN_PLANE
pfd\iPixelType   = #PFD_TYPE_RGBA
pfd\cColorBits   = 24
pfd\cDepthBits   = 16   

pixformat = ChoosePixelFormat_(hdc, pfd)
SetPixelFormat_(hdc, pixformat, pfd)
hrc = wglCreateContext_(hdc)
wglMakeCurrent_(hdc,hrc)

InitGL()

texture = LoadBMPTextureMem(?texlabel, "rgb", 0)

While Quit = 0

  Repeat 
    EventID = WindowEvent()
    
    Select EventID
      Case #PB_EventCloseWindow
        Quit = 1
    EndSelect
  
  Until EventID = 0
  
  DrawScene(hDC)
  SwapBuffers_(hDC)

Wend
End

texlabel:
IncludeBinary "NeHe.bmp"
Post Reply