Silverfrost Forums

Welcome to our forums

Texture in OpenGL?

2 May 2009 9:25 (Edited: 2 May 2009 9:34) #4521

Hello,

I'm trying to apply a 1D texture to a triangle in OpenGL. However I'm just getting a white triangle at the moment which apparently implies that the texture is somehow incomplete. Can anyone see if some of the calls are incorrect or indeed if the order of the calls is wrong. Has anyone succesfully applied a texture using FTN95?

The code should produce a triangle with 4 vertical stripes, 2 red ones on the left and two blue ones on the right, on a black background.

Albert

winapp

      use openglmod
      include <clearwin.ins>,nolist
      integer i
      integer window
      i=winio@('%es%ca[Double Buffering]&')
      i=winio@('%sp%ww[no_border]%pv%^og[double]%lw',0, 0, 400, 400,opengl_proc,window)
      call Display() 
      end

Please see next post for openglmod, for some reason I can't fit it all on one post

2 May 2009 9:33 #4522
module openglmod
      real,dimension(4,3)::arrcol
      integer:: texture1

      data arrcol(1,:)/0.76,0.2,0.02/
      data arrcol(2,:)/0.88,0.6,0.06/
      data arrcol(3,:)/1.0,1.0,0.01/
      data arrcol(4,:)/0.04,0.44,0.67/

contains
      
      subroutine display()
      include <opengl.ins>,nolist
      texture1=0
      
      call glClear(ior(GL_COLOR_BUFFER_BIT,GL_DEPTH_BUFFER_BIT))
      call glEnable(GL_TEXTURE_1D)
      call glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
      call glGenTextures(1,texture1)
      call glBindTexture(GL_TEXTURE_1D,texture1)
      call glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP)
      call glTexParameterf(GL_TEXTURE_1D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
      call glTexParameterf(GL_TEXTURE_1D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)
      call glTexImage1D(GL_TEXTURE_1D,0,3,4,0,GL_RGB,GL_UNSIGNED_BYTE,arrcol)
      call glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
      
      call glEnable(GL_TEXTURE_GEN_S)
      
      call glEnable(GL_TEXTURE_1D)
      call glBindTexture(GL_TEXTURE_1D,texture1)
      call glPushMatrix ()
      
      call glBegin(GL_TRIANGLES)!
      
      call glTexCoord1f(0.0)
	  call glVertex3f(-25.0, -25.0, 0.0)
	  call glTexCoord1f(0.5)
      call glVertex3f( 0.0, 25.0, 0.0)
	  call glTexCoord1f(1.0)
      call glVertex3f( 25.0,  -25.0, 0.0)
      
	  call glEnd()
      
      call glPopMatrix ()
      call glFlush()
      call swap_opengl_buffers()
      call glDisable(GL_TEXTURE_1D)
      end subroutine
      

      subroutine myinit ()
      include <opengl.ins>,nolist
      call glClearColor (0.0, 0.0, 0.0, 0.0)
      call glShadeModel (GL_SMOOTH)
      call glMatrixMode(GL_PROJECTION)
      call glEnable(GL_DEPTH_TEST)  
      call glLoadIdentity()
      
      call glOrtho (-50d0, 50d0, -50d0, 50d0, -50d0, 50d0)
      call glMatrixMode(GL_MODELVIEW)
      call glLoadIdentity ()
      
      end subroutine

      subroutine myReshape(w, h)
      include <opengl.ins>,nolist
      integer w,h
      double precision aspect_ratio
      if (h.NE.0) then
         aspect_ratio=real(w)/h

         call glViewport(0, 0, w, h)
         call glMatrixMode(GL_PROJECTION)
         call glLoadIdentity()
         if (w .LE. h) then 
             call glOrtho (-50d0, 50d0, -50d0/aspect_ratio, &
     &                      50d0/aspect_ratio, -50d0, 50d0)
         else 
             call glOrtho (-50d0*aspect_ratio, &
     &                      50d0*aspect_ratio, -50d0, 50d0, -50d0, 50d0)
         endif
         
         call glMatrixMode(GL_MODELVIEW)
         call glLoadIdentity ()

      endif
      end subroutine
      
      integer function opengl_proc()
      include <clearwin.ins>,nolist
      character*256 reason
      integer w,h
      reason=clearwin_string@('CALL_BACK_REASON')
      if(reason.eq.'SETUP')then
        call myinit()
      else if(reason.eq.'RESIZE')then
        w=clearwin_info@('OPENGL_WIDTH')
        h=clearwin_info@('OPENGL_DEPTH')
        call myReshape(w,h)
      else if(reason.eq.'DIRTY')then
        call display()
      end if
      
      opengl_proc=2
      end function
end module openglmod      
3 May 2009 5:58 #4524

So I made some progress last night and now have a small example of how to lay a 1D texture on a triangle in OpenGL. There were several mistakes to find, the first was the shape of the array holding the pixel RGB values, second was replacing GL_UNSIGNED_BYTE with GL_FLOAT in the call to glTexImage1D. The third was the most frustrating, turning off hardware acceleration . The learning curve with OpenGL is quite steep, Does anybody know how to get debug information? regards Albert

winapp

      use openglmod
      include <clearwin.ins>,nolist
      integer i
      integer window
      i=winio@('%es%ca[1DTexture on a Triangle]&')
      i=winio@('%sp%ww[no_border]%pv%^og[double]%lw',0, 0, 400, 400,opengl_proc,window)
      call Display()
      end

Module is in next post

3 May 2009 6:03 #4525
module openglmod
      real(kind=1),dimension(3,4)::arrcol
      integer::texture1
      
      data arrcol(:,1)/0.7 ,0.0,0.0/
      data arrcol(:,2)/1.0,0.0,0.0/
      data arrcol(:,3)/0.0,0.0,1.0/
      data arrcol(:,4)/0.0,0.0,0.4/ 
contains
      
      subroutine display()
      include <opengl.ins>,nolist
      
      call glClear(ior(GL_COLOR_BUFFER_BIT,GL_DEPTH_BUFFER_BIT))
      call glPushMatrix ()
      
      call glBegin(GL_TRIANGLES)!
      
      call glTexCoord1f(0.0)
      call glVertex3f(-30.0, -30.0, 0.0)
      call glTexCoord1f(0.5)
      call glVertex3f( 0.0, 30.0, 0.0)
      call glTexCoord1f(1.0)
      call glVertex3f( 30.0,  -30.0, 0.0)
      
      call glEnd()
      
      call glPopMatrix ()
      call glFlush()
      call swap_opengl_buffers()
      
      end subroutine
      
      
      subroutine myinit ()
      include <opengl.ins>,nolist
      call glClearColor (1.0, 1.0, 1.0, 0.0)
      call glShadeModel (GL_SMOOTH)
      call glMatrixMode(GL_PROJECTION)
      call glEnable(GL_DEPTH_TEST)  
      call glLoadIdentity()
      call glOrtho (-50d0, 50d0, -50d0, 50d0, -50d0, 50d0)
      call glMatrixMode(GL_MODELVIEW)
      call glLoadIdentity ()
      texture1=0
      call glEnable(GL_TEXTURE_1D)
      call glGenTextures(1,texture1)
      call glBindTexture(GL_TEXTURE_1D,texture1)
      call glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
      call glTexImage1D(GL_TEXTURE_1D,0,3,4,0,GL_RGB,GL_FLOAT,arrcol)
      call glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP)
      call glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
      call glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)
      call glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
      
      end subroutine

      subroutine myReshape(w, h)
      include <opengl.ins>,nolist
      integer w,h
      double precision aspect_ratio
      if (h.NE.0) then
         aspect_ratio=real(w)/h

         call glViewport(0, 0, w, h)
         call glMatrixMode(GL_PROJECTION)
         call glLoadIdentity()
         if (w .LE. h) then 
             call glOrtho (-50d0, 50d0, -50d0/aspect_ratio, 50d0/aspect_ratio, -50d0, 50d0)
         else 
             call glOrtho (-50d0*aspect_ratio, 50d0*aspect_ratio, -50d0, 50d0, -50d0, 50d0)
         endif
         
         call glMatrixMode(GL_MODELVIEW)
         call glLoadIdentity ()
         
      endif
      end subroutine
      
      integer function opengl_proc()
      include <clearwin.ins>,nolist
      character*256 reason
      integer w,h
      reason=clearwin_string@('CALL_BACK_REASON')
      if(reason.eq.'SETUP')then
        call myinit()
      else if(reason.eq.'RESIZE')then
        w=clearwin_info@('OPENGL_WIDTH')
        h=clearwin_info@('OPENGL_DEPTH')
        call myReshape(w,h)
      else if(reason.eq.'DIRTY')then
        call display()
      end if
      
      opengl_proc=2
      end function
end module openglmod      
3 May 2009 1:37 #4526

The third was the most frustrating, turning off hardware acceleration

Albert, do you do this yourself via control panel and display, or is there a way to do this from ftn95?

3 May 2009 1:56 #4527

John, I turned off hardware acceleration through control panel and display. I don't know if it's possible through FTN95. Without turning off hardware acceleration my triangles were just black shapes. This is only when using textures though. On my laptop the code runs with hardware acceleration enabled. I don't understand why one computer should work and the other not.

I'm just starting in Opengl, the results are very good, but it is a steep learning curve. Fortunately I now have the techniques I need to swap from using Simpleplot for my animations to OpenGl.

John, I noticed through trawling through old threads, that you have used Opengl extensively, how are you debugging when using Opengl?

Regards

Albert

3 May 2009 7:15 #4528

Albert,

I don't understand why one computer should work and the other not

Unfortunately that is quite normal! The only way around it as you have found is to switch off graphics acceleration. It can be a real pain when users of the software don't know how to do this, thus my question to see if you had found an automated way. I don't know C++ but I do know that C++ can interrogate the graphics card and can open an OpenGL graphics window without hardware acceleration. There must be a way in FTN95 via the windows API ??

how are you debugging when using Opengl?

With great difficulty! As you are aware by now the FTN95 debugger doesn't touch OpenGL. Thus when things went wrong (as they did), all I did was put write statements into the code at various positions to indicate how far the program had got before the crash, then write to the screen, data the program was attempting to pass to OpenGL. I soon learnt that data has to be checked and sanitised, OpenGL is intolerant to bad data!

Yes, its a steep initial learning curve, but well worth the effort, as the graphics can be absolutely stunning, and I thought my GDI graphics using %gr were good, but there's no comparison to OpenGL!

Good Luck, John

29 Nov 2010 12:14 #7172

Although I started this thread sometime ago, I thought it might be interesting to report progress on my opengl application. The little test application i posted will still only display textures properly with hardware acceleration turned off using the Silverfrost compiler. However I've been playing with opengl in another language; Pascal. and the applications I've coded using that will run textures with hardware acceleration turned on full! This indicates that the opengl implementation in Silverfrost is not optimal.

Turning hardware acceleration off is always a problem, opengl is much slower, cpu usage often runs to 100% when using a largish viewport. And one isn't able to use any extensions that the graphics card may support, for example, my graphics card supports 3d textures, but only if hardware acceleration is turned on.

Regards

Albert

14 Jul 2011 8:15 #8577

Al,

You are one of the first people I have heard of using Pascal with SF and a video converter. Are you saying that the Pascal code is what overloaded everything and caused you to determine the issue with SF? Again, these are important questions that I can answer in my email marketing campaign and I would love to have your knowledge and even include your namesake in the next edition of my newsletter.

Please login to reply.