Which OpenGL states should be push/popped w/ Max2D

BlitzMax Forums/OpenGL Module/Which OpenGL states should be push/popped w/ Max2D

USNavyFish(Posted 2009) [#1]
The title says it all. I've been working mainly with Max2D, but came upon the need to use raw OpenGL. Specifically, I needed to draw a line that faded from one color to another.

After lots of trial and error, the function I developed is simply:

	Function DrawMultiColorLines(x0:Float , y0:Float , x1:Float , y1:Float , rgb0:Byte[] , rgb1:Byte[]) 
		glDisable GL_TEXTURE_2D
		glBegin GL_LINES
		glColor3ub(rgb0[0] , rgb0[1] , rgb0[2]) 
		glVertex2f(x0,y0)
		glColor3ub(rgb1[0] , rgb1[1] , rgb1[2]) 
		glVertex2f(x1 , y1)		
		glEnd
		glEnable GL_TEXTURE_2D
		glColor3ub(255,255,255)
	End Function


I was wondering if anyone had developed a comprehensive list of which OpenGL states Max2D plays around with. If not, I could probably dig through the brl code but why do so if someone has already?

I am hoping to use this information to create a basic Render-State push/pop class. There is one on the forums that uses Max2D commands, but I'm interested in using the raw OGL states instead.

The purpose of this class would be for programmers using Max2D who want to throw in a few lines of OGL code without having to scratch their heads figuring out which states to enable/disable etc. Simply call OGLStates.Push(), then, optionally, call OGLStates.Clear() to restore OGL defaults, and finally OGLStates.Pop() to regain the pushed settings.

Would this be useful to anyone?


Bremer(Posted 2009) [#2]
In the source for glgraphics.bmx there are two private functions that might provide information. They are called BeginOrtho() and EndOrtho(). I do not have bmax installed on my work computer, so I cannot view the code now, but I remember seeing those functions and they do the glPushAttrib and deals with the matrix's and such. So it might be a place to start at least.


nawi(Posted 2009) [#3]
Couldn't you just use GlIsEnabled to check if a state is enabled?

Like:
oldstate = GlIsEnabled(GL_TEXTURE_2D)

GlDisable GL_TEXTURE_2D
..do your stuff
if oldstate then GlEnable(GL_TEXTURE_2D) Else GlDisable(GL_TEXTURE_2D)


ImaginaryHuman(Posted 2009) [#4]
It really depends what you plan to change, as to how extensively you need to back up Max2D's state. Just thinking about what Max2D does, you probably need to look at preserving:

Anything to do with texturing
Anything to do with alpha testing and stencil windows
Anything to do with blend modes
Anything to do with the orthographic projection
Anything to do with whether or not texturing is enabled and what kind of shade model
The modelview matrix and projection matrix

etc


nawi(Posted 2009) [#5]
SuperStrict
Type GLState
 	Field GLNumber:Int
 	Field Enabled:Byte
 	Global StateList:TList
 	Function Add(GLN:Int)
		Local GLS:GLState = New GLState
	 	GLS.GLNumber = GLN
		GLS.Enabled = GlIsEnabled(GLN)
	 	StateList.AddLast(GLS)
 	End Function
 	Function SaveStates()
 		For Local GLS:GLState = EachIn StateList
			GLS.Enabled = GlIsEnabled(GLS.GLNumber)
		Next
	End Function
	Function UpdateStates()
		 	For Local GLS:GLState = EachIn StateList
			If GLS.Enabled Then 
				GlEnable(GLS.GLNumber)
			Else
				GlDisable(GLS.GLNumber)
			EndIf
		Next
	End Function
End Type
GLState.StateList = CreateList()

GLState.Add(GL_ALPHA_TEST)
GLState.Add(GL_AUTO_NORMAL)
GLState.Add(GL_BLEND)
GLState.Add(GL_COLOR_ARRAY)
GLState.Add(GL_COLOR_LOGIC_OP)
GLState.Add(GL_COLOR_MATERIAL)
GLState.Add(GL_COLOR_TABLE)
GLState.Add(GL_CONVOLUTION_1D)
GLState.Add(GL_CONVOLUTION_2D)
GLState.Add(GL_CULL_FACE)
GLState.Add(GL_DEPTH_TEST)
GLState.Add(GL_DITHER)
GLState.Add(GL_EDGE_FLAG_ARRAY)
GLState.Add(GL_FOG)
GLState.Add(GL_HISTOGRAM)
GLState.Add(GL_INDEX_ARRAY)
GLState.Add(GL_INDEX_LOGIC_OP)
GLState.Add(GL_LIGHTING)
GLState.Add(GL_LINE_SMOOTH)
GLState.Add(GL_LINE_STIPPLE)
GLState.Add(GL_MAP1_COLOR_4)
GLState.Add(GL_MAP1_INDEX)
GLState.Add(GL_MAP1_NORMAL)
GLState.Add(GL_MAP1_TEXTURE_COORD_1)
GLState.Add(GL_MAP1_TEXTURE_COORD_2)
GLState.Add(GL_MAP1_TEXTURE_COORD_3)
GLState.Add(GL_MAP1_TEXTURE_COORD_4)
GLState.Add(GL_MAP2_COLOR_4)
GLState.Add(GL_MAP2_INDEX)
GLState.Add(GL_MAP2_NORMAL)
GLState.Add(GL_MAP2_TEXTURE_COORD_1)
GLState.Add(GL_MAP2_TEXTURE_COORD_2)
GLState.Add(GL_MAP2_TEXTURE_COORD_3)
GLState.Add(GL_MAP2_TEXTURE_COORD_4)
GLState.Add(GL_MAP2_VERTEX_3)
GLState.Add(GL_MAP2_VERTEX_4)
GLState.Add(GL_MINMAX)
GLState.Add(GL_NORMAL_ARRAY)
GLState.Add(GL_NORMALIZE)
GLState.Add(GL_POINT_SMOOTH)
GLState.Add(GL_POLYGON_SMOOTH)
GLState.Add(GL_POLYGON_OFFSET_FILL)
GLState.Add(GL_POLYGON_OFFSET_LINE)
GLState.Add(GL_POLYGON_OFFSET_POINT)
GLState.Add(GL_POLYGON_STIPPLE)
GLState.Add(GL_POST_COLOR_MATRIX_COLOR_TABLE)
GLState.Add(GL_POST_CONVOLUTION_COLOR_TABLE)
GLState.Add(GL_RESCALE_NORMAL)
GLState.Add(GL_SCISSOR_TEST)
GLState.Add(GL_SEPARABLE_2D)
GLState.Add(GL_STENCIL_TEST)
GLState.Add(GL_TEXTURE_1D)
GLState.Add(GL_TEXTURE_2D)
GLState.Add(GL_TEXTURE_3D)
GLState.Add(GL_TEXTURE_COORD_ARRAY)
GLState.Add(GL_TEXTURE_GEN_Q)
GLState.Add(GL_TEXTURE_GEN_R)
GLState.Add(GL_TEXTURE_GEN_S)
GLState.Add(GL_TEXTURE_GEN_T)
GLState.Add(GL_VERTEX_ARRAY)

This excludes things like the current color etc..


USNavyFish(Posted 2009) [#6]
Excellent! Thank you Mawi, this is exactly what I was looking for.

Obviously not all situations will require checking every single state, but now that I have a foundation I can build my own Max2D extension functions and narrow down which states need to be reset for each function.


beanage(Posted 2009) [#7]
NICE stuff. beewn looking for kinda that function myself... thx nawi :)