Jump to content

  • Log In with Google      Sign In   
  • Create Account

WiredCat

Member Since 09 Apr 2011
Offline Last Active Today, 02:49 PM

Posts I've Made

In Topic: OpenGL ES - Dashed Lines

Today, 02:50 PM

http://perso.esiee.fr/~perrotol/TheRedBook-1.0/chapter02.html

 

search for: 

Stippled Lines

 

 

and sample code:

glPushAttrib(GL_ENABLE_BIT); 
# glPushAttrib is done to return everything to normal after drawing

glLineStipple(1, 0xAAAA);  # [1]
glEnable(GL_LINE_STIPPLE);
glBegin(GL_LINES);
glVertex3f(-.5,.5,-.5);
glVertex3f(.5,.5,-.5);
glEnd();

glPopAttrib();

thus i dont know what it draws


In Topic: About text in OpenGl

19 June 2016 - 04:01 PM

to use that you need to setup ortho etc. anyway you use opengl 2.0 and you use fixed function pipeline when you should use shaders.


void glWriteTEXT(int X,int Y, AnsiString text)
{
  glPushAttrib(GL_DEPTH_TEST);  // Save the current Depth test settings (Used for blending )
  glDisable(GL_DEPTH_TEST);     // Turn off depth testing (otherwise we get no FPS)


  glMatrixMode(GL_PROJECTION);  // Switch to the projection matrix
  glPushMatrix();               // Save current projection matrix
  glLoadIdentity();

//  GetWindowRect(GetDesktopWindow(), drawRect);  // Get current window size
  glOrtho(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT, -1, 1);  // Change the projection matrix using an orthgraphic projection
  glMatrixMode(GL_MODELVIEW);  // Return to the modelview matrix
  glPushMatrix();              // Save the current modelview matrix
  glLoadIdentity();
 // glColor3f(1.0, 1.0, 1.0);    // Text color

  glRasterPos2i(X, Y);                                // Position the Text
  glPushAttrib(GL_LIST_BIT);                          // Save's the current base list
  glListBase(base);                              // Set the base list to our character list
  glCallLists(text.Length(), GL_UNSIGNED_BYTE, text.c_str());  // Display the text
  glPopAttrib();                                      // Restore the old base list

  glMatrixMode(GL_PROJECTION);  //Switch to projection matrix
  glPopMatrix();                // Restore the old projection matrix
  glMatrixMode(GL_MODELVIEW);   // Return to modelview matrix
  glPopMatrix();                // Restore old modelview matrix
 // glEnable(GL_TEXTURE_2D);      // Turn on textures, don't want our text textured
 // glEnable(GL_LIGHTING);
  glPopAttrib();                // Restore depth testing


}

In Topic: Jitter when interpolating sprite from previous position to current physics po...

19 June 2016 - 03:58 PM

you could use average of times between frames (last and present) divide that by two that should smooth the movement. its shown here:

http://www.sulaco.co.za/opengl_project_Basic_3D_engine.htm

 

 

however i managed to make something diffrent but i cant remember what (i believe i used bigger precission timer)

  begin
      Inc(FPSCount);                      // Increment FPS Counter

      FrameTime := GetTickCount() - ElapsedTime - DemoStart;
      LastTime :=ElapsedTime;
      ElapsedTime :=GetTickCount() - DemoStart;     // Calculate Elapsed Time
      ElapsedTime :=(LastTime + ElapsedTime) DIV 2; // Average it out for smoother movement


In Topic: 2d images and OpenGL

06 June 2016 - 06:52 AM

this is an example of drawing 2d text as set of 2d textured images (that you load each texture for each character)


struct TFontChar
{
int id;
AnsiString txt;
AnsiString FontName;
TGLTexture texture;
};

struct TFontVertex
{
	t3dpoint<float> v;
	textpoint t;
};

struct TFont
{


	TFont()
	{
		initialized = false;
		font_color = vec3(0.0, 1.0, 0.0);
	}

	~TFont()
	{
if(initialized)
{
	glDeleteBuffers(1, &alphabet_vbuffer);

delete FONT_SHADER;
delete FONT_EVENT_SHADER;
}
	}

AnsiString chartable;
TFontChar * Alphabet;
TFontVertex * FONT_VERTEX_BUFFER;
TShaderObject * FONT_SHADER;
TShaderObject * FONT_EVENT_SHADER;
bool initialized;
vec3 font_color;
unsigned int alphabet_vbuffer;
float sw,sh;
 void initialize_char_table(AnsiString FONT_DIR, float asw, float ash, AnsiString appdir) //additionally load default font for debugging
	{
	 sw=asw;
	 sh=ash;
CAN_LOG = true;
		FONT_SHADER = new TShaderObject();

		FONT_SHADER->LoadShaderProgram(appdir+"shaders/fonts/base_font_vp.shader",appdir+"shaders/fonts/base_font_fp.shader");

		FONT_EVENT_SHADER = new TShaderObject();

		FONT_EVENT_SHADER->LoadShaderProgram(appdir+"shaders/fonts/event_font_vp.shader",appdir+"shaders/fonts/event_font_fp.shader");



		chartable = " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-+.,=:{}()[]/*;!<>_?";
int x = chartable.length();
		Alphabet = new TFontChar[ x ];
//		ALOG("alphabet length: "+IntToStr(x));

		for (int i=0; i < chartable.length(); i++)
		{
//ALOG("FONT INDEX: "+IntToStr(i));
			Alphabet[i].texture.px_height = 32;
			Alphabet[i].texture.px_width  = 32;
		Alphabet[i].texture.LoadTGA(appdir+FONT_DIR+IntToStr(i)+".tga");
		Alphabet[i].txt = chartable[i];
		}

		FONT_VERTEX_BUFFER = new TFontVertex[ 256*4 ]; //max 256 chars...
for (int i=0; i < 256; i++)
{

	FONT_VERTEX_BUFFER[i*4+0].t = textpoint(0.0, 0.0);
	FONT_VERTEX_BUFFER[i*4+0].v = t3dpoint<float>(0.0+i*18.0, 0.0, 0.0);
	FONT_VERTEX_BUFFER[i*4+1].t = textpoint(1.0, 0.0);
	FONT_VERTEX_BUFFER[i*4+1].v = t3dpoint<float>(0.0+i*18.0+18.0, 0.0, 0.0);
	FONT_VERTEX_BUFFER[i*4+2].t = textpoint(1.0, 1.0);
	FONT_VERTEX_BUFFER[i*4+2].v = t3dpoint<float>(0.0+i*18.0+18.0, 0.0 + 18.0, 0.0);
	FONT_VERTEX_BUFFER[i*4+3].t = textpoint(0.0, 1.0);
	FONT_VERTEX_BUFFER[i*4+3].v = t3dpoint<float>(0.0, 0.0 + 18.0, 0.0);
}

		glGenBuffers(1, &alphabet_vbuffer);
		glBindBuffer(GL_ARRAY_BUFFER, alphabet_vbuffer);
		glBufferData(GL_ARRAY_BUFFER, sizeof(TFontVertex) * 256 * 4, FONT_VERTEX_BUFFER, GL_STATIC_DRAW);

		glBindBuffer(GL_ARRAY_BUFFER, 0);
		ALOG("DONE WITH LOADING FONT");
		initialized = true;
	}


	 int find_index(char txt)
	{
		for (int i=0; i < chartable.length(); i++)
		{
if (Alphabet[i].txt[0] == txt) return i;
		}

		return 1;
	}



void DrawEventText(float x, float y, float size, vec3 color, float alpha, AnsiString text);


void DrawText(float x, float y, float size, AnsiString atext, vec3 color)
{

	glActiveTexture(GL_TEXTURE0);

	glDisable(GL_DEPTH_TEST);
	glDepthMask(GL_FALSE);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	AnsiString text = UpperCase(atext);

	//lets assume we draw from left top corner of text
	for (int i=0; i < 256; i++)
	{
		FONT_VERTEX_BUFFER[i*4+0].t = textpoint(0.0, 0.0);
		FONT_VERTEX_BUFFER[i*4+0].v = t3dpoint<float>(x+i*size, y, 0.0);

		FONT_VERTEX_BUFFER[i*4+1].t = textpoint(1.0, 0.0);
		FONT_VERTEX_BUFFER[i*4+1].v = t3dpoint<float>(x+i*size+size, y, 0.0);

		FONT_VERTEX_BUFFER[i*4+2].t = textpoint(1.0, 1.0);
		FONT_VERTEX_BUFFER[i*4+2].v = t3dpoint<float>(x+i*size+size, y + size, 0.0);


		FONT_VERTEX_BUFFER[i*4+3].t = textpoint(0.0, 1.0);
		FONT_VERTEX_BUFFER[i*4+3].v = t3dpoint<float>(x+i*size, y + size, 0.0);
	}

	glBindBuffer(GL_ARRAY_BUFFER, alphabet_vbuffer);
	glBufferSubData(GL_ARRAY_BUFFER,0, sizeof(TFontVertex) * 256 * 4, FONT_VERTEX_BUFFER);


	FONT_SHADER->Enable();
	FONT_SHADER->Send1I(glGetUniformLocation(FONT_SHADER->gProgram, "font_tex"), 0 );
	FONT_SHADER->Send3F(glGetUniformLocation(FONT_SHADER->gProgram, "FONT_COLOR"), color );
	FONT_SHADER->Send1F(glGetUniformLocation(FONT_SHADER->gProgram, "sh"), sh );
	FONT_SHADER->Send1F(glGetUniformLocation(FONT_SHADER->gProgram, "sw"), sw );


	glVertexAttribPointer(FONT_SHADER->vertex_pos, 3, GL_FLOAT, GL_FALSE, sizeof(TFontVertex), (void*)(offsetof(struct TFontVertex, v)));
	glVertexAttribPointer(FONT_SHADER->texture_coord, 2, GL_FLOAT, GL_FALSE, sizeof(TFontVertex), (void*)(offsetof(struct TFontVertex, t)));

	glEnableVertexAttribArray(FONT_SHADER->vertex_pos);
	glEnableVertexAttribArray(FONT_SHADER->texture_coord);

		for (int i=0; i < text.length(); i++)
				{
			int index = find_index(text[i]);
				glBindTexture(GL_TEXTURE_2D, Alphabet[ index ].texture.texture);
				glDrawArrays(GL_TRIANGLE_FAN, i*4 , 4);
				}


		glDisableVertexAttribArray(FONT_SHADER->vertex_pos);
		glDisableVertexAttribArray(FONT_SHADER->texture_coord);
	FONT_SHADER->Disable();
		glBindBuffer(GL_ARRAY_BUFFER, 0);


	glDisable(GL_BLEND);
	glDepthMask(GL_TRUE);
	glEnable(GL_DEPTH_TEST);
}

void DrawText(float x, float y, float size, AnsiString atext)
{
	DrawText(x, y, size, atext, font_color);
}

};

fragment shader


uniform sampler2D font_tex;
    varying vec2 texcoord;
 
uniform vec3 FONT_COLOR;
    
void main()
{
vec3 color =    texture2D( font_tex, texcoord ).rgb;
 
float intensity = 0.0;
 
if (color.x > 0.5)
intensity = 0.0;
else
intensity = 1.0;
 
 
 
 
 
gl_FragColor = vec4(FONT_COLOR, intensity);
}
 

 

vertex shader


attribute vec3 Vpos;
attribute vec2 Vtexcoord;
varying vec2 texcoord;
uniform float sw;
uniform float sh;
 
void main()
{
float x = -1.0 + (Vpos.x / sw) * 2.0;
float y = -1.0 + (Vpos.y / sh) * 2.0;
texcoord = Vtexcoord;
gl_Position = vec4(vec2(x,y), 0.0, 1.0);
}
 

 

 

 

Important thing here is that you can define image left, top, width and height in lets say pixels (knowing opengl window size)

 

so you just draw a quad for (one image) that is lets say at top left corner of ogl window:

 

in fragment shader you calculate on screen coordinate (Vpos is vertex position, sw ogl window width, sh ogl window height)

 

float x = -1.0 + (Vpos.x / sw) * 2.0;
float y = -1.0 + (Vpos.y / sh) * 2.0;
 
 
since screen coordinate in ogl is from -1..1 you will put exactly sized image whereever you want

In Topic: Having trouble using glGetTexImage with a depth texture of an FBO

03 June 2016 - 06:24 PM

to even start with depth with FBO you need to create valid renderbuffer.

 

//not sure if thats supported.

m_DepthTexture.SetFilterMode(GL_NEAREST,GL_NEAREST); 
m_DepthTexture.SetWrapMode(GL_CLAMP_TO_BORDER,GL_CLAMP_TO_BORDER);

 

//not sure if thats even deprecated
glBindTexture(GL_TEXTURE_2D, m_TextureID);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE,
                GL_COMPARE_REF_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);

void init()
{
glGenTextures(1, &SHADOW_DEPTH_TEX);
glBindTexture(GL_TEXTURE_2D, SHADOW_DEPTH_TEX);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, RENDER_BUFF_W, RENDER_BUFF_H, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);



glGenFramebuffers(1, &SHADOW_FBO1);
glGenRenderbuffers(1, &SHADOW_RBO1);
glBindRenderbuffer(GL_RENDERBUFFER, SHADOW_RBO1);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, RENDER_BUFF_W, RENDER_BUFF_H);
glBindFramebuffer(GL_FRAMEBUFFER, SHADOW_FBO1);
glBindTexture(GL_TEXTURE_2D, SHADOW_DEPTH_TEX);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                          GL_DEPTH_ATTACHMENT,
                          GL_RENDERBUFFER,
						  SHADOW_RBO1);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, SHADOW_DEPTH_TEX, 0);
}


where
unsigned int SHADOW_FBO1;
unsigned int SHADOW_RBO1;
unsigned int SHADOW_DEPTH_TEX;

thus

 

GL_INVALID_OPERATION is generated by glGetTextureImage if texture is not the name of an existing texture object.

GL_INVALID_OPERATION is generated if type is one of GL_UNSIGNED_BYTE_3_3_2GL_UNSIGNED_BYTE_2_3_3_REVGL_UNSIGNED_SHORT_5_6_5,GL_UNSIGNED_SHORT_5_6_5_REV, or GL_UNSIGNED_INT_10F_11F_11F_REV and format is not GL_RGB.

GL_INVALID_OPERATION is generated if type is one of GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_UNSIGNED_SHORT_5_5_5_1,GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_UNSIGNED_INT_8_8_8_8, GL_UNSIGNED_INT_8_8_8_8_REV, GL_UNSIGNED_INT_10_10_10_2,GL_UNSIGNED_INT_2_10_10_10_REV, or GL_UNSIGNED_INT_5_9_9_9_REV and format is neither GL_RGBA or GL_BGRA.

GL_INVALID_OPERATION is generated if format is GL_STENCIL_INDEX and the base internal format is not GL_STENCIL_INDEX or GL_DEPTH_STENCIL.

GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_PACK_BUFFER target and the buffer object's data store is currently mapped.

GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_PACK_BUFFER target and the data would be packed to the buffer object such that the memory writes required would exceed the data store size.

GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_PACK_BUFFER target and pixels is not evenly divisible into the number of bytes needed to store in memory a datum indicated by type.

GL_INVALID_OPERATION is generated by glGetTextureImage and glGetnTexImage if the buffer size required to store the requested data is greater thanbufSize.


PARTNERS