Very simple OpenGL

Monkey Forums/Monkey Programming/Very simple OpenGL

consty(Posted 2014) [#1]
Hi everyone, I try to fit some OpenGL to Monkey to see how it goes.

Any ideas how this can work?

Strict

Import mojo
Import opengl.gles20

'http://www.opentk.com/node/2292

Class Game Extends App
	Field vertices:Float[]
	Field buffer:Int
	Field dataBuffer:DataBuffer

	Method OnCreate:Int()
		glClearColor(0, 0, 0.5, 1)
		
		vertices = [-1.0, -1, 0, 1, -1, 0, 0, 1, 0]
		
		dataBuffer = New DataBuffer(vertices.Length * 4)

		For Local i:Int = 0 Until vertices.Length
			dataBuffer.PokeFloat(i * 4, vertices[i])
		Next
		
		buffer = glCreateBuffer()
		glBindBuffer(GL_ARRAY_BUFFER, buffer)
		glBufferData(GL_ARRAY_BUFFER, vertices.Length * 4, dataBuffer, GL_STATIC_DRAW)
				
		Return 0
	End

	Method OnRender:Int()
		glClear(GL_COLOR_BUFFER_BIT)
		
		glEnableVertexAttribArray(0)
		glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, dataBuffer)
		
		glDrawArrays(GL_TRIANGLES, 0, 3)
		
		Return 0
	End
End

Function Main:Int()
	Local game:Game = New Game()
	Return 0
End




AdamRedwoods(Posted 2014) [#2]
depends on your target, but the opengl context is usually only given in OnRender(). so you'll have to init first, then run your main draw loop.


consty(Posted 2014) [#3]
Hi Adam, I am a fan of your work.

I don't know if I do this correct.

I created a boolean variable inside OnRender and then when run for the first time I called every OpenGL command.

However I still get no graphic.


MikeHart(Posted 2014) [#4]
Located in the bananas/Mak folder, you will find an example for OpenGLES from Mark himself.


consty(Posted 2014) [#5]
Hello guys, thanks for the response and the help you offered. I had today some free time to study more and find out why there was a problem.

The problem was on this line: glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, 0)


I was confused with the fixed pipeline http://www.opengl.org/sdk/docs/man2/xhtml/glVertexPointer.xml
and thus resulting to broken code. Now in the new OpenGL the command glVertexAttribPointer needs integer offsets instead.

https://www.khronos.org/opengles/sdk/docs/man/xhtml/glVertexAttribPointer.xml
pointer is treated as a byte offset into the buffer object's data store.

So that is the new and improved code that shows a triangle on screen.



DruggedBunny(Posted 2014) [#6]
Nice, worked perfectly here on Windows desktop anyway!


Danilo(Posted 2014) [#7]
Unfortunately it just shows a blue screen on GLFW Mac OS X, Android, and iOS Simulator. No triangle.


consty(Posted 2014) [#8]
I use Windows 8 as well so I could not test in each platform.

One thing that AdamRedwoods said was that glContext is initialized OnRender and not OnCreate (if I understand correctly).

I have made a correction to the code to allow that, unfortunately I have not a Linux system available now to test but logically this is what would work.

Strict
Import mojo
Import opengl.gles20

Class MyApp Extends App
	Field isInit:Bool = False

	Method Init:Void()
		glClearColor(0, 0, 0.5, 1)

		Local vertices:Float[] = [-1.0, -1, 0, 1, -1, 0, 0, 1, 0]
		
		Local dataBuffer:DataBuffer = New DataBuffer((vertices.Length) * 4)
		For Local i:Int = 0 Until vertices.Length
			dataBuffer.PokeFloat(i * 4, vertices[i])
		Next
		
		Local vbo:Int = glCreateBuffer()
		glBindBuffer(GL_ARRAY_BUFFER, vbo)
		glBufferData(GL_ARRAY_BUFFER, dataBuffer.Length, dataBuffer, GL_STATIC_DRAW);
	End

	
	Method OnRender:Int()
		If isInit == False
			Init()
			isInit = True
		End

		If isInit == True
			glClear(GL_COLOR_BUFFER_BIT)
			glEnableVertexAttribArray(0)
			glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, 0)
			glDrawArrays(GL_TRIANGLES, 0, 3)
		End
		Return 0
	End
End

Function Main:Int()
	Local application:MyApp = new MyApp()
	Return 0
End


By weekend I will setup a virtual machine, if anyone else is interested to look at it and give us some ideas is welcomed.


Danilo(Posted 2014) [#9]
It works on GLFW (Mac) without the vbo stuff:

On Android I get an exception:
E/AndroidRuntime(18137): FATAL EXCEPTION: GLThread 4125
E/AndroidRuntime(18137): Process: com.monkeycoder.monkeygame, PID: 18137
E/AndroidRuntime(18137): java.lang.IllegalArgumentException: Must use a native order direct Buffer
E/AndroidRuntime(18137): 	at android.opengl.GLES20.glVertexAttribPointerBounds(Native Method)
E/AndroidRuntime(18137): 	at android.opengl.GLES20.glVertexAttribPointer(GLES20.java:1903)
E/AndroidRuntime(18137): 	at com.monkeycoder.monkeygame.bb_opengl_gles20._glVertexAttribPointer(MonkeyGame.java:1983)
E/AndroidRuntime(18137): 	at com.monkeycoder.monkeygame.c_MyApp.p_OnRender(MonkeyGame.java:3228)
E/AndroidRuntime(18137): 	at com.monkeycoder.monkeygame.c_GameDelegate.RenderGame(MonkeyGame.java:3277)
E/AndroidRuntime(18137): 	at com.monkeycoder.monkeygame.BBGame.RenderGame(MonkeyGame.java:597)
E/AndroidRuntime(18137): 	at com.monkeycoder.monkeygame.BBAndroidGame.onDrawFrame(MonkeyGame.java:1357)
E/AndroidRuntime(18137): 	at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1523)
E/AndroidRuntime(18137): 	at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)

I think it is because DataBuffer is no native Float array.