• Advertisement
Sign in to follow this  

OpenGL my second step with openGL: improving perf.

This topic is 4345 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

Finally I was able to make my 3ds importer work (more or less) and now I can render some more interesting model. But framerate drops down to 10 with a 100/150 polys model (I own a geForce 6600); ok. The model I use is covered by a 512x512 texture, using uv coords. I use 2 lights, linear mapping (no mip mapping), perspective correction to nicest and so on. Then this is my rendering code:
bool Render()
{
    //OpenGL initialization commands
    glClearColor(0.0, 0.0, 0.0, 0.5);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //Reset the modelview matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //Rotate the triangle
    glTranslatef(0.0f,-1.0f,-25.0f);
    glRotatef(angle,0.0,1, 0.0);
    angle += 0.2;

	glBegin(GL_TRIANGLES);// Drawing Using Triangles
	int c = 0;

	for(int i = 0; i < numfaces; ++i)
	{
        glNormal3f(normals[i*3], normals[i*3+1], normals[i*3+2]);
     
        float v[4]; v[0] = matt.diffuse_rgb[0]/256.0; v[1] = matt.diffuse_rgb[1]/256.0; v[2] = matt.diffuse_rgb[2]/256.0; v[3] = 1.0;
       
        glTexCoord2f(mapcoords[faceslist[c]*2], mapcoords[faceslist[c]*2+1]);
        glVertex3f( geom[faceslist[c]*3], geom[faceslist[c]*3+1], geom[faceslist[c]*3+2]);
		c++;
     
        glTexCoord2f(mapcoords[faceslist[c]*2], mapcoords[faceslist[c]*2+1]);
        glVertex3f( geom[faceslist[c]*3], geom[faceslist[c]*3+1], geom[faceslist[c]*3+2]);
		c++;
		
		glTexCoord2f(mapcoords[faceslist[c]*2], mapcoords[faceslist[c]*2+1]);
		glVertex3f( geom[faceslist[c]*3], geom[faceslist[c]*3+1], geom[faceslist[c]*3+2]);
		c++;
    }
	glEnd();// Finished Drawing The Triangle

    SDL_GL_SwapBuffers();
    frames++;
    return true;
}


I understand that these are my first steps in opengl (I did few things some years ago, but just to experiment, nothing more) and wonder how can these lines be a problem for a card that runs quake 4 at 40 fps :-) EDIT: I use SDL, if that matters. And with the 'cube' model (without textures and so on) I reach 150 fps.

Share this post


Link to post
Share on other sites
Advertisement
I think 150 poly model should be able to be drawn in immediate mode (i.e. calls to glVertex as opposed to glDrawElements) at 100's of fps. IMO there must be something wrong in another part of your code.
I am using a Geforce fx 5600 and am drawing a 3000+ polygon model in immediate mode at 50+ fps, using exactly the same technique you are (except for no SDL).

Share this post


Link to post
Share on other sites
One optimisation I see is this line:

float v[4]; v[0] = matt.diffuse_rgb[0]/256.0; v[1] = matt.diffuse_rgb[1]/256.0; v[2] = matt.diffuse_rgb[2]/256.0; v[3] = 1.0;

Can you divide by 256 in the initialization code instead? This will make it a little faster, but it should not be responsible for the huge performance loss you are seeing.

Also, you don't need to call glClearColor each frame. You can also probably take out the call to glMatrixMode as it should still be set to GL_MODELVIEW from the previous frame.

Just make sure you do those two things somewhere in your code.

Share this post


Link to post
Share on other sites
Thank you for the help. Of course, the code I posted is not a real project, only a small app made to test the 3ds importer. If I set off the texture, I get 90 fps. With the textures on, I get 10. The texture is a tga file, so BGRA (because it contains an alpha channel. The main problem is then something related to textures, but I don't know what.

Share this post


Link to post
Share on other sites
Have you updated your drivers recently? I know that with ATI cards, not having catalyst makes OpenGL crawl at abnormal speeds, particularly in immediate mode. I figure GeForce might behave the same way.

If you still have your default drivers, upgrade. It's worth a shot; my game runs well over 1500 FPS in immediate mode and features ~1000 textured polies, badly-optimized billboarding (read: multiple unecessary world matrix manipulations), multiple background layers (old-school style, which covers the entire BG), no poly sorting, and a few other things. If a 100 poly model crawls, something is wrong and it almost certainly isn't your code...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
...The texture is a tga file, so BGRA...


BGRA? I don't know much about OpenGL but that raises alarm bells for me. Try swapping the red and the blue channels and then use GL_RGBA for glTexImage2D and see if that improves your performance.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
...The texture is a tga file, so BGRA...


BGRA? I don't know much about OpenGL but that raises alarm bells for me. Try swapping the red and the blue channels and then use GL_RGBA for glTexImage2D and see if that improves your performance.


Drivers are updated to the latest version (anyway, I installed them 1 month ago). I tried using RGBA but I get exactly the same results...

Share this post


Link to post
Share on other sites
What are your computer specs and what OS are you compiling and running this on?

BGR or RGB won't make any difference. TGA is BGR, so you should be using that or else inverting all your color data in the texture.

The problem here is most likely your computer. Download some of the Nehe tutorials and tell us what FPS you get on those. If they are still low, the issue is with your computer and not with the code. If those are fast then the issue is with your code and not the computer.

Share this post


Link to post
Share on other sites
Quote:
Original post by wyled
What are your computer specs and what OS are you compiling and running this on?

BGR or RGB won't make any difference. TGA is BGR, so you should be using that or else inverting all your color data in the texture.

The problem here is most likely your computer. Download some of the Nehe tutorials and tell us what FPS you get on those. If they are still low, the issue is with your computer and not with the code. If those are fast then the issue is with your code and not the computer.


Well, athlon xp 2600Mhz+, 512Mb ram and geForce 6600. I run quake 4 and far cry without problems... As soon as I can I will try to compile a nehe example...

Share this post


Link to post
Share on other sites
everything you've described should run much much faster.
make sure you are only loading the texture once, creating the texture once etc. other than that it's anyones guess without seeing the code.

Share this post


Link to post
Share on other sites
okay, here are a couple of things I would try:

1) Remove the glClearColor call from the render loop. You don't need to do that every frame, besides it's an expensive operation.

2) Try using vertex arrays or buffers to draw the polygons. They are faster than immediate mode rendering esp. when complex geometry is involved.

Share this post


Link to post
Share on other sites
Quote:
Original post by Specchum
okay, here are a couple of things I would try:

1) Remove the glClearColor call from the render loop. You don't need to do that every frame, besides it's an expensive operation.

2) Try using vertex arrays or buffers to draw the polygons. They are faster than immediate mode rendering esp. when complex geometry is involved.


I know that there could be many changes to do to improve perf., but I think that I should be able to play HL2 and still get some dozens of fps from my program in background :-)
I think that opengl is running in software mode: glGetString(GL_RENDERER); returns "GDI Generiversion: 3".

Now my question is: how can I set on the HW mode? (I'm using SDL by the way)

Share this post


Link to post
Share on other sites
Original post by cignox1
Quote:
Original post by Specchum
I think that opengl is running in software mode: glGetString(GL_RENDERER); returns "GDI Generiversion: 3".

Now my question is: how can I set on the HW mode? (I'm using SDL by the way)

You're right, that is the problem. How are you setting up the OpenGL context (I think that's the call the SDL_SetVideoMode or something similar)?

Share this post


Link to post
Share on other sites
This is my initialization code.

//Initialize sdl and the video sublib
if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER)<0) exit(0);

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); //Use at least 5 bits of Red
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); //Use at least 5 bits of Green
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); //Use at least 5 bits of Blue
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); //Use at least 16 bits for the depth buffer
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); //Enable double buffering

screen = SDL_SetVideoMode(width, height, 32, SDL_OPENGL | SDL_HWSURFACE/* | SDL_FULLSCREEN*/);





EDIT: In addition, I used GL_BGRA when building the texture, but I've just read somewhere that this flag was added only in OpenGL 1.2. But since OGL 1.1 is the latest version providen on Windows, why donsn't the compiler complain about it? Am I using a newer header? But then I have header and library of different versions, could this be the reason?

[Edited by - cignox1 on March 29, 2006 4:46:18 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by cignox1
EDIT: In addition, I used GL_BGRA when building the texture, but I've just read somewhere that this flag was added only in OpenGL 1.2. But since OGL 1.1 is the latest version providen on Windows, why donsn't the compiler complain about it? Am I using a newer header? But then I have header and library of different versions, could this be the reason?


If you include SDL/SDL_opengl it will define these extensions for you. And since your graphics card supports OpenGL extensions, it all works.

In your initialization code, you don't need the SDL_HWSURFACE flag. It should not cause a problem, but it's unnecesary.

Also, if you run this in fullscreen, do you still get the same problems?

Share this post


Link to post
Share on other sites
Ok, solved the problem (as usual, it was my fault): now I render a 24000 polys model with a 1024x1024 texture at 35-40 fps without changing a line of code (I had a opengl32.dll in the same folder of the exe and the app was using it instead than the one provided with the drivers).

EDIT: 60fps when full screen :-)

[Edited by - cignox1 on March 29, 2006 9:35:14 AM]

Share this post


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

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By LifeArtist
      Good Evening,
      I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
      First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
      I am really stucked right now because of the fundamental question:
      Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
      If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on. 
      In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
      Should I treat those debug objects as entities/components?
      For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
      Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
      Regards,
      LifeArtist
    • By QQemka
      Hello. I am coding a small thingy in my spare time. All i want to achieve is to load a heightmap (as the lowest possible walking terrain), some static meshes (elements of the environment) and a dynamic character (meaning i can move, collide with heightmap/static meshes and hold a varying item in a hand ). Got a bunch of questions, or rather problems i can't find solution to myself. Nearly all are deal with graphics/gpu, not the coding part. My c++ is on high enough level.
      Let's go:
      Heightmap - i obviously want it to be textured, size is hardcoded to 256x256 squares. I can't have one huge texture stretched over entire terrain cause every pixel would be enormous. Thats why i decided to use 2 specified textures. First will be a tileset consisting of 16 square tiles (u v range from 0 to 0.25 for first tile and so on) and second a 256x256 buffer with 0-15 value representing index of the tile from tileset for every heigtmap square. Problem is, how do i blend the edges nicely and make some computationally cheap changes so its not obvious there are only 16 tiles? Is it possible to generate such terrain with some existing program?
      Collisions - i want to use bounding sphere and aabb. But should i store them for a model or entity instance? Meaning i have 20 same trees spawned using the same tree model, but every entity got its own transformation (position, scale etc). Storing collision component per instance grats faster access + is precalculated and transformed (takes additional memory, but who cares?), so i stick with this, right? What should i do if object is dynamically rotated? The aabb is no longer aligned and calculating per vertex min/max everytime object rotates/scales is pretty expensive, right?
      Drawing aabb - problem similar to above (storing aabb data per instance or model). This time in my opinion per model is enough since every instance also does not have own vertex buffer but uses the shared one (so 20 trees share reference to one tree model). So rendering aabb is about taking the model's aabb, transforming with instance matrix and voila. What about aabb vertex buffer (this is more of a cosmetic question, just curious, bumped onto it in time of writing this). Is it better to make it as 8 points and index buffer (12 lines), or only 2 vertices with min/max x/y/z and having the shaders dynamically generate 6 other vertices and draw the box? Or maybe there should be just ONE 1x1x1 cube box template moved/scaled per entity?
      What if one model got a diffuse texture and a normal map, and other has only diffuse? Should i pass some bool flag to shader with that info, or just assume that my game supports only diffuse maps without fancy stuff?
      There were several more but i forgot/solved them at time of writing
      Thanks in advance
    • By RenanRR
      Hi All,
      I'm reading the tutorials from learnOpengl site (nice site) and I'm having a question on the camera (https://learnopengl.com/Getting-started/Camera).
      I always saw the camera being manipulated with the lookat, but in tutorial I saw the camera being changed through the MVP arrays, which do not seem to be camera, but rather the scene that changes:
      Vertex Shader:
      #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { gl_Position = projection * view * model * vec4(aPos, 1.0f); TexCoord = vec2(aTexCoord.x, aTexCoord.y); } then, the matrix manipulated:
      ..... glm::mat4 projection = glm::perspective(glm::radians(fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); ourShader.setMat4("projection", projection); .... glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp); ourShader.setMat4("view", view); .... model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); ourShader.setMat4("model", model);  
      So, some doubts:
      - Why use it like that?
      - Is it okay to manipulate the camera that way?
      -in this way, are not the vertex's positions that changes instead of the camera?
      - I need to pass MVP to all shaders of object in my scenes ?
       
      What it seems, is that the camera stands still and the scenery that changes...
      it's right?
       
       
      Thank you
       
    • By dpadam450
      Sampling a floating point texture where the alpha channel holds 4-bytes of packed data into the float. I don't know how to cast the raw memory to treat it as an integer so I can perform bit-shifting operations.

      int rgbValue = int(textureSample.w);//4 bytes of data packed as color
      // algorithm might not be correct and endianness might need switching.
      vec3 extractedData = vec3(  rgbValue & 0xFF000000,  (rgbValue << 8) & 0xFF000000, (rgbValue << 16) & 0xFF000000);
      extractedData /= 255.0f;
    • By Devashish Khandelwal
      While writing a simple renderer using OpenGL, I faced an issue with the glGetUniformLocation function. For some reason, the location is coming to be -1.
      Anyone has any idea .. what should I do?
  • Advertisement