OpenGL 3.x need help with syntax

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

OpenGL 3.x need help with syntax

Post by h4tt3n »

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: 3373
Joined: Sep 25, 2005 21:54

Re: OpenGL 3.x need help with syntax

Post by srvaldez »

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: 8586
Joined: May 28, 2005 3:28
Contact:

Re: OpenGL 3.x need help with syntax

Post by D.J.Peters »

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: 698
Joined: Oct 22, 2005 21:12
Location: Denmark

Re: OpenGL 3.x need help with syntax

Post by h4tt3n »

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:

http://www.freebasic.net/forum/viewtopi ... am#p223558

http://www.freebasic.net/forum/viewtopi ... ateProgram

http://www.freebasic.net/forum/viewtopi ... am#p216693

http://www.freebasic.net/forum/viewtopi ... ateProgram

Cheers,
Mike
Post Reply