• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By codelyoko373
      I wasn't sure if this would be the right place for a topic like this so sorry if it isn't.
      I'm currently working on a project for Uni using FreeGLUT to make a simple solar system simulation. I've got to the point where I've implemented all the planets and have used a Scene Graph to link them all together. The issue I'm having with now though is basically the planets and moons orbit correctly at their own orbit speeds.
      I'm not really experienced with using matrices for stuff like this so It's likely why I can't figure out how exactly to get it working. This is where I'm applying the transformation matrices, as well as pushing and popping them. This is within the Render function that every planet including the sun and moons will have and run.
      if (tag != "Sun") { glRotatef(orbitAngle, orbitRotation.X, orbitRotation.Y, orbitRotation.Z); } glPushMatrix(); glTranslatef(position.X, position.Y, position.Z); glRotatef(rotationAngle, rotation.X, rotation.Y, rotation.Z); glScalef(scale.X, scale.Y, scale.Z); glDrawElements(GL_TRIANGLES, mesh->indiceCount, GL_UNSIGNED_SHORT, mesh->indices); if (tag != "Sun") { glPopMatrix(); } The "If(tag != "Sun")" parts are my attempts are getting the planets to orbit correctly though it likely isn't the way I'm meant to be doing it. So I was wondering if someone would be able to help me? As I really don't have an idea on what I would do to get it working. Using the if statement is truthfully the closest I've got to it working but there are still weird effects like the planets orbiting faster then they should depending on the number of planets actually be updated/rendered.
    • By Jens Eckervogt
      Hello everyone, 
      I have problem with texture
      using System; using OpenTK; using OpenTK.Input; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL4; using System.Drawing; using System.Reflection; namespace Tutorial_05 { class Game : GameWindow { private static int WIDTH = 1200; private static int HEIGHT = 720; private static KeyboardState keyState; private int vaoID; private int vboID; private int iboID; private Vector3[] vertices = { new Vector3(-0.5f, 0.5f, 0.0f), // V0 new Vector3(-0.5f, -0.5f, 0.0f), // V1 new Vector3(0.5f, -0.5f, 0.0f), // V2 new Vector3(0.5f, 0.5f, 0.0f) // V3 }; private Vector2[] texcoords = { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) }; private int[] indices = { 0, 1, 3, 3, 1, 2 }; private string vertsrc = @"#version 450 core in vec3 position; in vec2 textureCoords; out vec2 pass_textureCoords; void main(void) { gl_Position = vec4(position, 1.0); pass_textureCoords = textureCoords; }"; private string fragsrc = @"#version 450 core in vec2 pass_textureCoords; out vec4 out_color; uniform sampler2D textureSampler; void main(void) { out_color = texture(textureSampler, pass_textureCoords); }"; private int programID; private int vertexShaderID; private int fragmentShaderID; private int textureID; private Bitmap texsrc; public Game() : base(WIDTH, HEIGHT, GraphicsMode.Default, "Tutorial 05 - Texturing", GameWindowFlags.Default, DisplayDevice.Default, 4, 5, GraphicsContextFlags.Default) { } protected override void OnLoad(EventArgs e) { base.OnLoad(e); CursorVisible = true; GL.GenVertexArrays(1, out vaoID); GL.BindVertexArray(vaoID); GL.GenBuffers(1, out vboID); GL.BindBuffer(BufferTarget.ArrayBuffer, vboID); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * Vector3.SizeInBytes), vertices, BufferUsageHint.StaticDraw); GL.GenBuffers(1, out iboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, iboID); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(int)), indices, BufferUsageHint.StaticDraw); vertexShaderID = GL.CreateShader(ShaderType.VertexShader); GL.ShaderSource(vertexShaderID, vertsrc); GL.CompileShader(vertexShaderID); fragmentShaderID = GL.CreateShader(ShaderType.FragmentShader); GL.ShaderSource(fragmentShaderID, fragsrc); GL.CompileShader(fragmentShaderID); programID = GL.CreateProgram(); GL.AttachShader(programID, vertexShaderID); GL.AttachShader(programID, fragmentShaderID); GL.LinkProgram(programID); // Loading texture from embedded resource texsrc = new Bitmap(Assembly.GetEntryAssembly().GetManifestResourceStream("Tutorial_05.example.png")); textureID = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, textureID); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, texsrc.Width, texsrc.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); System.Drawing.Imaging.BitmapData bitmap_data = texsrc.LockBits(new Rectangle(0, 0, texsrc.Width, texsrc.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, texsrc.Width, texsrc.Height, PixelFormat.Bgra, PixelType.UnsignedByte, bitmap_data.Scan0); texsrc.UnlockBits(bitmap_data); GL.Enable(EnableCap.Texture2D); GL.BufferData(BufferTarget.TextureBuffer, (IntPtr)(texcoords.Length * Vector2.SizeInBytes), texcoords, BufferUsageHint.StaticDraw); GL.BindAttribLocation(programID, 0, "position"); GL.BindAttribLocation(programID, 1, "textureCoords"); } protected override void OnResize(EventArgs e) { base.OnResize(e); GL.Viewport(0, 0, ClientRectangle.Width, ClientRectangle.Height); } protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); keyState = Keyboard.GetState(); if (keyState.IsKeyDown(Key.Escape)) { Exit(); } } protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); // Prepare for background GL.Clear(ClearBufferMask.ColorBufferBit); GL.ClearColor(Color4.Red); // Draw traingles GL.EnableVertexAttribArray(0); GL.EnableVertexAttribArray(1); GL.BindVertexArray(vaoID); GL.UseProgram(programID); GL.BindBuffer(BufferTarget.ArrayBuffer, vboID); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture3D, textureID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, iboID); GL.DrawElements(BeginMode.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0); GL.DisableVertexAttribArray(0); GL.DisableVertexAttribArray(1); SwapBuffers(); } protected override void OnClosed(EventArgs e) { base.OnClosed(e); GL.DeleteVertexArray(vaoID); GL.DeleteBuffer(vboID); } } } I can not remember where do I add GL.Uniform2();
    • By Jens Eckervogt
      Hello everyone
      For @80bserver8 nice job - I have found Google search. How did you port from Javascript WebGL to C# OpenTK.?
      I have been searched Google but it shows f***ing Unity 3D. I really want know how do I understand I want start with OpenTK But I want know where is porting of Javascript and C#?
       
      Thanks!
    • By mike44
      Hi
      I draw in a OpenGL framebuffer. All is fine but it eats FPS (frames per second), hence I wonder if I could execute the framebuffer drawing only every 5-10th loop or so?
      Many thanks
       
    • By cebugdev
      hi all,
      how to implement this type of effect ? 
      Also what is this effect called? this is considered volumetric lighting?
      what are the options of doing this? 
      a. billboard? but i want this to have the 3D effect that when we rotate the camera we can still have that 3d feel.
      b. a transparent 3d mesh? and we can animate it as well?
      need your expert advise.
       
      additional:
      2. how to implement things like fireball projectile (shot from a monster) (billboard texture or a 3d mesh)?
       
      Note: im using OpenGL ES 2.0 on mobile. 
       
      thanks!
  • Advertisement
  • Advertisement
Sign in to follow this  

OpenGL Slow speed when camera nears to the model

This topic is 4331 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have create a simple program that contains a box with texture, when i move far away from the box then everything works smooth but as i come near to the model box , my movement became stucky, i think fps gets too much low at that time... why is this happening ? is there anyway to make it fine? below is my source

// Gl_Engine.cpp: implementation of the Gl_Engine class.
//
//////////////////////////////////////////////////////////////////////

#include "Gl_Engine.h"
#include <stdio.h>
#include <math.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Gl_Engine::Gl_Engine()
{
	m_hRC=NULL;
	m_RotDeg =m_RotDeg2 = 10;
}

Gl_Engine::~Gl_Engine()
{

}

int Gl_Engine::InitGL(GLvoid)
{

						// Enables Smooth Shading

	

	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);					// Black Background

	glClearDepth(1.0f);							// Depth Buffer Setup

	glEnable(GL_DEPTH_TEST);						// Enables Depth Testing
	
	glDepthFunc(GL_LESS);							// The Type Of Depth Test To Do

	glShadeModel(GL_SMOOTH);

	glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
	

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);			// Really Nice Perspective Calculations


	StrArray tmpvar;
	tmpvar.push_back("C:\\Kasamba clinets\\OpenGlScene\\Textures\\NeHe.bmp");
	LoadTexture(tmpvar);
	//AddLight();

	return TRUE;								// Initialization Went OK

}


int Gl_Engine::DrawGLScene(GLvoid)								// Here's Where We Do All The Drawing
{

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);			// Clear The Screen And The Depth Buffer
	
//	glDisable(GL_LIGHTING);
	
	glLoadIdentity();							// Reset The Current Modelview Matrix


	RotateWorldObjects();
	SetWorldObjectDirection();

	// Reset The Current Modelview Matrix
	Build3dHouse();
	
	//Sleep(30);
return TRUE;								// Everything Went OK 

}


int Gl_Engine::CreateGlContext(HDC &r_hdc)
{

	if (!(m_hRC=wglCreateContext(r_hdc)))					// Are We Able To Get A Rendering Context?
	{
		MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;							// Return FALSE
	}

	if(!wglMakeCurrent(r_hdc,m_hRC))						// Try To Activate The Rendering Context
	{
		MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;							// Return FALSE
	}

return TRUE;
}

void Gl_Engine::CloseGl_Engine()
{

	if (!wglMakeCurrent(NULL,NULL))					// Are We Able To Release The DC And RC Contexts?
	{

		MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);

	}


	if (!wglDeleteContext(m_hRC))					// Are We Able To Delete The RC?
	{
		MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
	}



	this->m_hRC=NULL;

}


void Gl_Engine::ReSizeGLScene(GLsizei width, GLsizei height)				// Resize And Initialize The GL Window
{
	if (height==0)								// Prevent A Divide By Zero By
	{
		height=1;							// Making Height Equal One
	}

	glViewport(0, 0, width, height);					// Reset The Current Viewport
	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();							// Reset The Projection Matrix

	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

	glMatrixMode(GL_MODELVIEW);						// Select The Modelview Matrix
	glLoadIdentity();							// Reset The Modelview Matrix
}


int Gl_Engine::LoadTexture(StrArray strFileNames)
{

	int iSize = strFileNames.size();

	if(iSize<1)
	{
		return 0;
	}


	memset(TextureImage,0,sizeof(void *)*10); //

	for(int Loop=0; Loop<iSize; Loop++)
	{

		if(LoadBMPS((char*)strFileNames[Loop].c_str(),TextureImage[0])==FALSE)
		{

			string strTmp = "Problem in loading file from ";
			strTmp+=strFileNames[Loop].c_str();
			MessageBox(NULL,strTmp.c_str(),"Error",MB_OK);
		}

		
		glGenTextures(iSize, &m_Texture[Loop]); // Create The Texture

		// Typical Texture Generation Using Data From The Bitmap
		glBindTexture(GL_TEXTURE_2D, m_Texture[Loop]);
		
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering

		
		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[Loop]->sizeX, TextureImage[Loop]->sizeY, 0, GL_RGB,
		GL_UNSIGNED_BYTE, TextureImage[Loop]->data);
	
		

		/*
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
		gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[Loop]->sizeX, TextureImage[Loop]->sizeY, GL_RGB,
							GL_UNSIGNED_BYTE, TextureImage[Loop]->data); 
*/


	}
    


	for(int Loop2=0; Loop2<iSize; Loop2++)
	{
		if (TextureImage[Loop2]) // If Texture Exists
		{
			if (TextureImage[Loop2]->data) // If Texture Image Exists
			{
				free(TextureImage[Loop2]->data); // Free The Texture Image Memory
			}
			
			free(TextureImage[Loop2]); // Free The Image Structure
		}

	}


return 1;
}

BOOL Gl_Engine::LoadBMPS(char *szFileName, AUX_RGBImageRec *&r_RGBrec)
{

	FILE *pfile=NULL;

	if(!szFileName)
	{
		return FALSE;
	}

	pfile= fopen(szFileName,"r");

	if(!pfile)
	{
		return FALSE;
	}
	else
	{
		 fclose(pfile); //it was just used to check it if file exist or not
		 AUX_RGBImageRec *Tmp_RGBrec= auxDIBImageLoad(szFileName);
		 r_RGBrec= Tmp_RGBrec;
	}

	return TRUE;
}

int Gl_Engine::AddLight()
{

	GLfloat fLightAmb[]={0.5f,0.5f,0.5f,1.0f};
	GLfloat fLightDiffuse[]={1.0f,1.0f,1.0f,1.0f};
	GLfloat fLightPosition[]={0.0f,0.0f,2.0f,0.0f};

	glLightfv(GL_LIGHT1,GL_AMBIENT,fLightAmb);
	glLightfv(GL_LIGHT1,GL_DIFFUSE,fLightDiffuse);
	glLightfv(GL_LIGHT1,GL_POSITION,fLightPosition);

	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT1);
	return 1;
}


void Gl_Engine::CameraMoveLeft()
{
	m_Camera.fYRot += 1.5f;
}

void Gl_Engine::CameraMoveRight()
{
	m_Camera.fYRot -= 1.5f;
}

void Gl_Engine::CameraMoveForward()
{
	float fRad = PIOVER;
	m_Camera.fX -= (float)sin(m_Camera.fYRot * fRad) * 1.05f;
	m_Camera.fZ -= (float)cos(m_Camera.fYRot * fRad) * 1.05f;
}

void Gl_Engine::CameraMoveBackward()
{
	float fRad = PIOVER;
	m_Camera.fX += (float)sin(m_Camera.fYRot * fRad) * 1.05f;
	m_Camera.fZ += (float)cos(m_Camera.fYRot * fRad) * 1.05f;
}

void Gl_Engine::RotateWorldObjects()
{
	GLfloat sceneroty = 360.0f - m_Camera.fYRot;
	glRotatef(sceneroty,0,1.0f,0);
}

void Gl_Engine::SetWorldObjectDirection()
{
	GLfloat worldx,worldy,worldz;
	worldx= -m_Camera.fX;
	worldz = -m_Camera.fZ;
	worldy = -0.25f;

	glTranslatef(worldx,worldy,worldz);
}

void Gl_Engine::Build3dHouse()
{

	//it is necessary to off the debth buffer for blending effects
	 glColor4f(1.0f,1.0f,1.0f,0.5f);     //Blend the object
	 glTranslatef(0.5f,0.0f,0.0f); // Move Right 3 Units
	//glRotatef(m_RotDeg2--,0.0f,1.0f,0.0f);
	
	glBindTexture(GL_TEXTURE_2D, m_Texture[0]); // Select Our Texture

	glBegin(GL_QUADS); // Draw A Quad
	//Front.
	glNormal3f(0.0f,0.0f,1.0f);
	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
	glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
	glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
	// Back Face
	glNormal3f(0.0f,0.0f,-1.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture andQuad
	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
	glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
	// Top Face
	glNormal3f(0.0f,1.0f,0.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
	glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
	glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
	// Bottom Face
	glNormal3f(0.0f,1.0f,0.0f);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
	glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
	glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
	// Right face
	glNormal3f(1.0f,0.0f,0.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
	glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
	glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
	// Left Face
	glNormal3f(-1.0f,0.0f,0.0f);
	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
	glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
	glEnd();
//	glDisable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )

}



Share this post


Link to post
Share on other sites
Advertisement
sounds like your app is fillrate limited. try looking up the latest drivers for your video card (they may add extra hardware acceleration support). other than that, build mipmaps (you have it commented out in your code).

a few other things to help if you have to live in software mode: disable blending, enable back face culling and use a display list to draw your house.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement