us-east-1

engine question...

BlitzMax Forums/OpenGL Module/engine question...

*(Posted 2011) [#1]
I am currently looking at creating an engine in BlitzMax and OpenGL as I have always wanted to learn how to write my own engine.

The problem I am having is im rendering everything and it works ok BUT I can only render about 350 polys per surface I was wondering if anyone else has come across this problem or has some code somewhere on how to correctly set up the OpenGL system so I can render loads of polygons per surface?

Thanks in advance...


kfprimm(Posted 2011) [#2]
How are you rendering? glBegin/glEnd, VBOs, or arrays?


*(Posted 2011) [#3]
glDrawElements


kfprimm(Posted 2011) [#4]
Mmm. That's strange. 350 is sort of random.

Any chance you could post some of the code?


*(Posted 2011) [#5]
SuperStrict
GLGraphics 800, 600

Global Vertices:Float[0] 
Global Triangles:Int[0]
Global Colors:Float[0]

Global VertexID:Long = 0
Global VertCount:Long = 0
Global TriangleCount:Long = 0

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, 1.3333, 1, 1000)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gltranslatef( 0.0, 0.0, -6.0 )
			glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );

CreateCylinder( 100 )

Global Ang:Float = 1.0

While Not KeyDown(KEY_ESCAPE)
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
	glRotatef( Ang, 0, 1, 0 )
'	Angle :+ 1

	glEnableClientState(GL_VERTEX_ARRAY)
'	glEnableClientState(GL_COLOR_ARRAY)
	
'	glColorPointer(3, GL_FLOAT, 0, Colors)
	glVertexPointer(3, GL_FLOAT, 0, Vertices)
	glDrawElements( GL_TRIANGLES, VertCount-1 , GL_UNSIGNED_INT, Triangles )
	
	glDisableClientState(GL_VERTEX_ARRAY)
'	glDisableClientState(GL_COLOR_ARRAY)

	Flip
Wend
End

	'---------------------------------------------------------
	Function CreateCylinder( segments:Long, closeEnds:Byte = True )
		'this will create a cylinder, closeEnds will either close the ends or leave them open
		Local Angle:Float = 0.0
		Local uV:Float = 0.0
		
		Local TopVert:Long
		Local BottomVert:Long
		
		If segments<3 Or segments>100 Then Return	'if the requirements are to big then null it
		
		If ( closeEnds = True )
			TopVert = AddVertex( 0.0, 1.0, 0.0 );
			BottomVert = AddVertex( 0.0, -1.0, 0.0 );
		EndIf
		
		Local Vert1:Long, Vert2:Long, Vert3:Long, Vert4:Long				'these will be used for the sides of the cylinder

		Vert1 = AddVertex( Sin( Angle ) *1.0, 1.0, Cos( Angle ) *1.0 );
		Vert2 = AddVertex( Sin( Angle ) *1.0, -1.0, Cos( Angle ) *1.0 );
		uV :+ ( 1.0 /segments )
		
		Repeat
			Angle :+ ( 360.0 /segments )			'increase it via the increments
			Vert3 = AddVertex( Sin( Angle ) *1.0, 1.0, Cos( Angle ) *1.0 )
			Vert4 = AddVertex( Sin( Angle ) *1.0, -1.0, Cos( Angle ) *1.0 )
			If ( closeEnds = True )
				'this will close off the ends of the cylinder if required
				AddTriangle( TopVert, Vert1, Vert3 )
				AddTriangle( BottomVert, Vert4, Vert2 )
			EndIf
			
			'now add the sides
			AddTriangle( Vert1, Vert2, Vert3 )
			AddTriangle( Vert3, Vert2, Vert4 )
			
			uV :+ ( 1.0 /segments )
			
			Vert1 = Vert3
			Vert2 = Vert4
		Until Angle>360
		
		Print "TC:"+TriangleCount
		Print "VC:"+VertCount
	End Function

Function AddTriangle( index1:Long, index2:Long, index3:Long )
	Triangles = Triangles[ ..TriangleCount +3 ]
	Triangles[ TriangleCount ] = index1
	Triangles[ TriangleCount +1 ] = index2
	Triangles[ TriangleCount +2 ] = index3
	TriangleCount :+ 3
End Function

Function AddVertex:Long( X:Float, Y:Float, Z:Float )
	Vertices = Vertices[ ..VertCount +3 ];
	Vertices[ VertCount ] = X
	Vertices[ VertCount +1 ] = Y
	Vertices[ VertCount +2 ] = Z
	VertCount :+ 3
	VertexID :+ 1
	
	Return VertexID -1
End Function


This is a small example I knocked up :)


Sanctus(Posted 2011) [#6]
From Ogl specs



Hence, you should use this:
glDrawElements( GL_TRIANGLES, TriangleCount , GL_UNSIGNED_INT, Triangles )

Since opengl works with pointers a lot you should try to make your engine in c++ just for learning's sake. If you think it's hard that's one more reason to do it :)


*(Posted 2011) [#7]
Its not hard but I was just wondering why it wasnt working :D

I do have a C++ version of the engine working for iOS its 100% OpenGL ES but now im looking towards Windows, Linux and OSX :)


joncom2000(Posted 2011) [#8]
I dont see any reason why you should right the engine in c++ for learnings sake "opengl works with pointer", well bmax is perfectly capable of using pointers as well. The only reason I can see edzup's code looks to be failing is hes set the vertex count rather than triangle count so its expecting more triangles than the array has, a simple mistake to make, which you have pointed out.


*(Posted 2011) [#9]
TBH its easier to write the engine in BlitzMax as you dont have to fluff around with networking, sound, input as its already catered for all you have to write is the renderer.


col(Posted 2011) [#10]
works with pointers

isnt a good enough reason to use C++. By all means if you want then go for it, but as already mentioned BlitzMax is soooo more than capable of using pointers. I've interfaced into the complete DirectX9 and Direct3DX9 libraries that use COM and pointers extensively, it works fine with no problems at all and there's not one single line of code in C++, it's all BlitzMax.

Have fun.

Last edited 2011