Archived

This topic is now archived and is closed to further replies.

OpenGL A good OpenGL 2d Tutorial?

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

Where can I find a good 2d tutorial?? I''m playing with QUADS and textures, but I want to know if there is other good alternatives (glRect?How can I texture it?) Thanks ma0

Share this post


Link to post
Share on other sites
Thanks for the answer (U are the only who answered me)
I am now trying different solutions for a simple Tile *engine* (engine is a big word tough

- GL_QUADS + glTextCoords ( I''ve tried with gl_Rect but I dont know how to map a texture on it :-)

- gl_Drawpixels + glRasterPos

I want to test what''s the fastest solution..
Now I have some problem with the glRasterPos (I am not able to tile the entire screen..then I have to understand how to simulate the glTranslate..

I have a little question too.. how can I *create* a surface bigger than the screen, so if i translate, i always see something? I''ve tried changing the glViewport but then It changes the tile dimensions too..I don''t understand perfectly how they works.

Another BIG problem. how coordinates works? I''ve read many tutorials,but I don''t understand how to apply them. If I create a viewport of 640x480 and my cube is a 0.5,0.5,.. it will be half of the screen..how people can translate the diffent way of thinking coordinates?

Share this post


Link to post
Share on other sites
2D in OpenGL isn''t the greatest way to go. I would suggest you check out Simple DirectMedia Layer (SDL). There is a great tutorial on how to get it setup here on GameDev, its a featured article on the main page, and then go to cone3d.gamedev.net and take the SDL Graphics Tutorials.

This is a great alternative. SDL is very simple to use, and very easy to learn.

If you have any trouble with it (if you decide to use it), post your problem in the Cone3D forum, and you will be answered a.s.a.p.

SDL works with OpenGL, and there is alot of stuff you can do with it very simply, compared to how OpenGL does it.

Good luck.

Share this post


Link to post
Share on other sites
I use glOrtho with textured quads for my OpenGL-based tile engine :
http://rpg-forge.i8.com/screen_1.jpg


As you can see it looks the biz, and thanks to 3d acceleration you can do neat zooming in\out and loads of other effects.

Everyone has a 3d card nowadays.

Edited by - Terran Marine on December 14, 2001 2:34:09 AM

Edited by - Terran Marine on December 14, 2001 2:34:40 AM

Share this post


Link to post
Share on other sites
about SDL... I am using OpenGL because SDL unfortunately works fast only if I use it as root (on Linux) This is NO good.
I don''t like to use system ram instead of video ram and no hardware acceleration only because I am not root..
This is why I am trying to create some little example in OpenGL . Then I will translate it in SDL to show the difference..

ma0

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Terran Marine
I use glOrtho with textured quads for my OpenGL-based tile engine :
http://rpg-forge.i8.com/screen_1.jpg



Can I see some of your code? I have some problem to understan how glOrtho2D (i use the 2D form) and glView works together..I am using 64x64 tiles and I want to create a *terrain* bigger than the screen..but If i set glView bigger , my tiles becomes bigger..
--> How are related the -1.0..1.0 coords? they are glOrtho xmin->xmax or glView?
(example in 640x480 screen: glTranslatef(0.2,0.0,0.0) are 640/2+(1/640*0.2) ??)

Share this post


Link to post
Share on other sites
Don''t use glDrawPixels/glRasterPos - it chokes the graphics pipeline and can cause your FPS to drop about 4/5ths. Use GL_QUADs!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Generally, you will always want to set glViewport to the size of the window - that way the entire window will be used. If you enlarge the window but not the viewport, you''ll only be using a small portion of the new window.

A call to glOrtho2D is the same as a call to glOrtho with -1.0, 1.0 as the near and far clip planes. Remember, just because you are thinking in 2D, you are still drawing in 3D and the z axis does exist.

Now, you can thank of glOrtho as a way to map screen coordinates to pixel coordinates. If you reset the recatangle to start at (0,0) and use width and height of the window everytime you resize the screen, then your coordinates will range from (0,0) to (winWidth, winHeight).

So, to keep the images from stretching win the window is resized, call both glOrtho AND glViewport with the new window dimensions in your resize function. Otherwise, if you make glViewport bigger, but don''t update glOrtho, then you will have a larger viewport which maps to the original screen coordinates. That is, if you set the width/height of the viewport to (800,600), but glOrtho''s width/height are set to (640,480), then OpenGL will map the coordinates so that position (640,480) is actually (800/600) on the screen. Therefore, all of your images will be larger than you expect.

This is sometimes desirable. For example, if you have a game where you only want to display a row of 10 tiles at a time, just never call glOrtho after the first call, update glViewport each time the screen is resized, and you will always display the same number of tiles. They will be stretched to fit the window. If you have a scrolling game, you might instead update the viewport AND the projection each time the window is resized in order to show more of the map.

Share this post


Link to post
Share on other sites
It sounds like you want to be able to render a world that is say 1000x1000, but have a viewport that is 100x100, and scrolls over the world. I am assuming you want to do this because it should be faster than rendering each screen/view from scratch.

This is really just a blitting trick, and not handled by something like OpenGL. If you want to do that, then render to an offscreen surface, and then blit portions of that to the screen. This is of course assuming that you *can* render to an offscreen surface (not a backbuffer) and assuming that you dont lose the hardware acceleration if you do so.

As for being faster, sure it''s probably faster, but when modern day video cards can render millions of texture mapped triangles/second, I don''t think you''ll be stressing that limit with your average 2d game.


-Brannon

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Brannon
If you want to do that, then render to an offscreen surface, and then blit portions of that to the screen. This is of course assuming that you *can* render to an offscreen surface (not a backbuffer) and assuming that you dont lose the hardware acceleration if you do so.


Ok. How can I do this? It''s impossible in OpenGL to render an offscreen surface.. I have to use SDL to do it..or not?

Share this post


Link to post
Share on other sites
quote:
Original post by Brannon
If you want to do that, then render to an offscreen surface, and then blit portions of that to the screen. This is of course assuming that you *can* render to an offscreen surface (not a backbuffer) and assuming that you dont lose the hardware acceleration if you do so.

That''s a horrible method. Even for a 2D API that''s not that great of a method. Simply use glTranslatef and (if not only) occlusion techiques to decide what and where you should render tiles. How are tiles hard? I don''t get it. They aren''t any harder in OpenGL than any other API; they''re barely any different!

[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
How is that a horrible method? Back in the day (386/486/etc), re-drawing an entire screen every frame killed performance. You could get magnitudes of order better perf if you only redrew what changed (for 2D games).

I''m not suggesting you do that. OpenGL is designed (as far as I can tell) to redraw the entire screen each frame. So, use it as it was designed.

Sorry if you thought I was suggesting rendering to an offscreen surface. Dont do it. Redraw the screen.

On windows, you can render to a GDI surface (offscreen), and then blit it where ever you want. But you probably wont get hardware acceleration, since you are then using the generic MS OpenGL APIs, and not the video card driver''s APIs.

There is a way to render to an offscreen surface .. because you can render to a texture .. but I think it involves some nasty tricks.. something along the lines of rendering to the backbuffer, and then reading the pixels from the back buffer into your own buffer, that you then specify as a texture.


-Brannon

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can just redraw a portion of the screen in OpenGL. Look into scissor testing. Should be in one of the nehe tuts.

Sypes

Share this post


Link to post
Share on other sites
I wasn''t complaining about only redrawing a portion of the screen. I was refering to using an offscreen buffer and selecting a portion of it to draw each time. "Dirty rectangles" are still a viable technique in OpenGL, but offscreen buffers are more trouble than they''re worth.

[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites

  • Similar Content

    • By _OskaR
      Hi,
      I have an OpenGL application but without possibility to wite own shaders.
      I need to perform small VS modification - is possible to do it in an alternative way? Do we have apps or driver modifictions which will catch the shader sent to GPU and override it?
    • By xhcao
      Does sync be needed to read texture content after access texture image in compute shader?
      My simple code is as below,
      glUseProgram(program.get());
      glBindImageTexture(0, texture[0], 0, GL_FALSE, 3, GL_READ_ONLY, GL_R32UI);
      glBindImageTexture(1, texture[1], 0, GL_FALSE, 4, GL_WRITE_ONLY, GL_R32UI);
      glDispatchCompute(1, 1, 1);
      // Does sync be needed here?
      glUseProgram(0);
      glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
      glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                     GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[1], 0);
      glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
       
      Compute shader is very simple, imageLoad content from texture[0], and imageStore content to texture[1]. Does need to sync after dispatchCompute?
    • By Jonathan2006
      My question: is it possible to transform multiple angular velocities so that they can be reinserted as one? My research is below:
      // This works quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); quat quaternion2 = GEMultiplyQuaternions(quaternion1, GEQuaternionFromAngleRadians(angleRadiansVector2)); quat quaternion3 = GEMultiplyQuaternions(quaternion2, GEQuaternionFromAngleRadians(angleRadiansVector3)); glMultMatrixf(GEMat4FromQuaternion(quaternion3).array); // The first two work fine but not the third. Why? quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); vec3 vector1 = GETransformQuaternionAndVector(quaternion1, angularVelocity1); quat quaternion2 = GEQuaternionFromAngleRadians(angleRadiansVector2); vec3 vector2 = GETransformQuaternionAndVector(quaternion2, angularVelocity2); // This doesn't work //quat quaternion3 = GEQuaternionFromAngleRadians(angleRadiansVector3); //vec3 vector3 = GETransformQuaternionAndVector(quaternion3, angularVelocity3); vec3 angleVelocity = GEAddVectors(vector1, vector2); // Does not work: vec3 angleVelocity = GEAddVectors(vector1, GEAddVectors(vector2, vector3)); static vec3 angleRadiansVector; vec3 angularAcceleration = GESetVector(0.0, 0.0, 0.0); // Sending it through one angular velocity later in my motion engine angleVelocity = GEAddVectors(angleVelocity, GEMultiplyVectorAndScalar(angularAcceleration, timeStep)); angleRadiansVector = GEAddVectors(angleRadiansVector, GEMultiplyVectorAndScalar(angleVelocity, timeStep)); glMultMatrixf(GEMat4FromEulerAngle(angleRadiansVector).array); Also how do I combine multiple angularAcceleration variables? Is there an easier way to transform the angular values?
    • By dpadam450
      I have this code below in both my vertex and fragment shader, however when I request glGetUniformLocation("Lights[0].diffuse") or "Lights[0].attenuation", it returns -1. It will only give me a valid uniform location if I actually use the diffuse/attenuation variables in the VERTEX shader. Because I use position in the vertex shader, it always returns a valid uniform location. I've read that I can share uniforms across both vertex and fragment, but I'm confused what this is even compiling to if this is the case.
       
      #define NUM_LIGHTS 2
      struct Light
      {
          vec3 position;
          vec3 diffuse;
          float attenuation;
      };
      uniform Light Lights[NUM_LIGHTS];
       
       
    • By pr033r
      Hello,
      I have a Bachelor project on topic "Implenet 3D Boid's algorithm in OpenGL". All OpenGL issues works fine for me, all rendering etc. But when I started implement the boid's algorithm it was getting worse and worse. I read article (http://natureofcode.com/book/chapter-6-autonomous-agents/) inspirate from another code (here: https://github.com/jyanar/Boids/tree/master/src) but it still doesn't work like in tutorials and videos. For example the main problem: when I apply Cohesion (one of three main laws of boids) it makes some "cycling knot". Second, when some flock touch to another it scary change the coordination or respawn in origin (x: 0, y:0. z:0). Just some streng things. 
      I followed many tutorials, change a try everything but it isn't so smooth, without lags like in another videos. I really need your help. 
      My code (optimalizing branch): https://github.com/pr033r/BachelorProject/tree/Optimalizing
      Exe file (if you want to look) and models folder (for those who will download the sources):
      http://leteckaposta.cz/367190436
      Thanks for any help...

  • Popular Now