OpenGL 3.x need help with syntax

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
h4tt3n
Posts: 623
Joined: Oct 22, 2005 21:12
Location: Denmark

OpenGL 3.x need help with syntax

Postby h4tt3n » Mar 20, 2017 14:02

Hello folks,

I am currently translating the sample code for a couple of SDL2 and OpenGL tutorials from different c dialects to freebasic, and I could use a bit of advice on getting the syntax right. For instance, when translating these lines:

Code: Select all

   //Get vertex source
   const GLchar* vertexShaderSource[] =
   {
      "#version 140\nin vec2 LVertexPos2D; void main() { gl_Position = vec4( LVertexPos2D.x, LVertexPos2D.y, 0, 1 ); }"
   };

   //Set vertex source
   glShaderSource( vertexShader, 1, vertexShaderSource, NULL );


All I have been able to compile without error is something less than elegant like:

Code: Select all

   '' Get vertex source
   Dim As String vertex = "#version 140\nin vec2 LVertexPos2D void main() { gl_Position = vec4( LVertexPos2D.x, LVertexPos2D.y, 0, 1 ) }"
   
   Dim As GLchar Ptr vertexShaderSource = StrPtr( vertex )
   
   '' Set vertex source
   glShaderSource( vertexShader, 1, @vertexShaderSource, NULL )


And lines like this:

Code: Select all

               //VBO data
               GLfloat vertexData[] =
               {
                  -0.5f, -0.5f,
                   0.5f, -0.5f,
                   0.5f,  0.5f,
                  -0.5f,  0.5f
               };

               //Create VBO
               glBufferData( GL_ARRAY_BUFFER, 2 * 4 * sizeof(GLfloat), vertexData, GL_STATIC_DRAW );


Is translated to:

Code: Select all

               '' VBO data
               Dim As GLfloat vertexData(...) = { -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f }

               '' Create VBO
               glBufferData( GL_ARRAY_BUFFER, 2 * 4 * sizeof(GLfloat), @vertexData(0), GL_STATIC_DRAW )


Although the compiler allows these, the program still crashes on startup, probably due to a pointer error. Can anyone here give some advice or link to already existing code samples showing how to implement these things in fb?

Thanks in advance,
Mike
srvaldez
Posts: 1161
Joined: Sep 25, 2005 21:54

Re: OpenGL 3.x need help with syntax

Postby srvaldez » Mar 20, 2017 19:50

perhaps seeing a bit more of the C source might help, have you looked at the examples that come with FB? (in the examples\graphics folder)
D.J.Peters
Posts: 6958
Joined: May 28, 2005 3:28
Location: Germany

Re: OpenGL 3.x need help with syntax

Postby D.J.Peters » Mar 21, 2017 2:02

First you have to mark the shader string with "!" it includes escape sequence "\n"
Secondly @vertex returns the address of the fbstring type not to the chars !

Joshy

Code: Select all

Dim As String vertex = !"#version 140\nin vec2 LVertexPos2D void main() { gl_Position = vec4( LVertexPos2D.x, LVertexPos2D.y, 0, 1 ) }"
   dim as GLchar ptr pCode=strptr(vertex)
   glShaderSource( vertexShader, 1, @pCode, NULL )
h4tt3n
Posts: 623
Joined: Oct 22, 2005 21:12
Location: Denmark

Re: OpenGL 3.x need help with syntax

Postby h4tt3n » Mar 21, 2017 17:41

The example is from this SDL2 tutorial. It's lesson 51 on advanced opengl.

By searching the forum I've dug out a few similar examples that I think will help me solve the issue. Check out these GLSL shader examples, they're actually pretty cool and deserve some attention:

viewtopic.php?f=7&t=24436&p=223558&hilit=glCreateProgram#p223558

viewtopic.php?f=17&t=24490&hilit=glCreateProgram

viewtopic.php?f=7&t=24462&p=216693&hilit=glCreateProgram#p216693

viewtopic.php?f=15&t=21260&hilit=glCreateProgram

Cheers,
Mike

Return to “Libraries”

Who is online

Users browsing this forum: No registered users and 1 guest