Archived

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

psyjax

OpenGL Using OpenGL for 2D game.

Recommended Posts

psyjax    156
Hey folks, I''m an SDL user and I also posted this question to the mailing list, but I figured this would be a good place to ask as well. In any case: I wanted to know what would be the best approach to my current game project which I am considering using OpenGL for.My game is an RPG which shifts between two modes, an Isometric perspective like Diablo, and a nice 3D dungeon crawl mode (using GL), much like the old Eye of the Beholder games. I''m considering doing both the 2D (isometric) portion and the 3D portion using OpenGL. But I am wondering if I will see good speed results this way? Will it be faster to do the 2d Part using regular blitting through SDL(a great 2D graphics library for those who don''t know), or OpenGL? One of the main reasons I want to use OpenGL for this portion, is for the simplicity in implementing lighting, scaling, rotation, etc. Also, I want to do neat 3d spell effects, the whole gamut of nice 3d eye candy on a 2d game board. What would be the best method for going about this in OpenGL? Would the best method be to render the 2D tile map onto an OpenGL surface every frame, as you would in regular 2D? Or, am I better off just sticking to 2D methods all together? I have never attempted something like this before, so excuse my ignorance. Any suggestions, or pros and cons you could give me would be gerat, as I am still in the initial design stages. I don''t wanna commit to something less I know it will work Thanks alot!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
GL for 2d stuff works great, or at least for the stuff I''ve tried.

What you might want to play with is Orth projection and set the camera on the z axis (with y as up). this way you can work using just x and y coords like a 2d screen.

Is this clear, or should I try this again?

Armand

Share this post


Link to post
Share on other sites
Chacha    138
Like the AP said, you can render in 2D if you change the current projection matrix to the orthographic projection, instead of the perspective projection. Here are two useful functions for switching between 2D and 3D rendering:


// Enables ortho mode for 2D rendering

void Enable2D()
{
int viewPort[4];
glGetIntegerv(GL_VIEWPORT, viewPort);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glOrtho(0, viewPort[2], 0, viewPort[3], -1, 1);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
}

//////////////////////////////////////////////////////


// Disables ortho mode and returns back to

// perspective projection mode for 3D rendering

void Disable2D()
{
glMatrixMode(GL_PROJECTION);
glPopMatrix();

glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}


So whenever you want to render in 2D simply call 'Enable2D()', and then when you're finished your 2D rendering, return back to 3D rendering with 'Disable2D()'. Note that when you're in 2D mode, you supply 2D coordinates such as 'glVertex2f(x, y)' instead of 'glVertex3f(x, y, z)'.

[edited by - chacha on October 9, 2003 12:31:08 AM]

Share this post


Link to post
Share on other sites
CraZeE    217
just a minor comment on this method. If u plan to do 2d in opengl, itrs tru ortho is the way to go. But if u wanna harness some of the additional power of opengl for effects which MAY not be limited to a 2D plane (say a 3d-explosion seen from birds-eye-view) then u may need to tweak the ortho settings.

chacha''s code example specifies the ortho''s Z-near and Z-far parameters as -1, 1 which is also wat happend if u use gluOrtho2D(...). But u may not want to limit your z-range between those values. U can still have depth for purposes of any special features by increasing the range for Z. Dun worry though, ocz the ortho projection will maintain the top-down-like view irrespective of the z-range.

Also, you CAN still use glVertex3f if u want, but its pointless unless u really need the z-coords. And if u need to use it anyway, u''ll probably need to change the z-range for projection anyway

Share this post


Link to post
Share on other sites
MARS_999    1627
If you want to use glOrtho() go ahead, and use glVertex3f() if you want. The Z axis is still their and you can move objects on Z if you want. Problem is objects will not be perspective corrected, again objects appear the same regardless of distance on Z. glOrtho is the way to go with 2D games because the screen coordinates map 1to1 with the screen resolution. If you do use glOrtho() and use glVertex3f() and use the Z axis and plan on moving objects on Z make sure you expand near and far planes to somethign like 2000 -2000 so you can see your objects or else they will be cut in half or not show up at all.

Share this post


Link to post
Share on other sites
Chacha    138
quote:
Original post by CraZeE
chacha's code example specifies the ortho's Z-near and Z-far parameters as -1, 1 which is also wat happend if u use gluOrtho2D(...). But u may not want to limit your z-range between those values. U can still have depth for purposes of any special features by increasing the range for Z. Dun worry though, ocz the ortho projection will maintain the top-down-like view irrespective of the z-range.

Also, you CAN still use glVertex3f if u want, but its pointless unless u really need the z-coords. And if u need to use it anyway, u'll probably need to change the z-range for projection anyway

Well said, I forgot to mention the alternatives.

[edited by - chacha on October 10, 2003 3:18:52 AM]

Share this post


Link to post
Share on other sites
psyjax    156
quote:
Original post by Raduprv
Why not making it isometric 3D instead?


I did actually

I already built the engine using full 3D polygons. But I decidede that tiles are alot prettier.

Take a look at the tiles I wan't to use:

http://www.starmars.com/tiles.png

and here is what the engine curently looks like (full 3d):

http://www.starmars.com/worldScreen.png

Granted, with some better models etc. Im sure the 3d one would look nice and spiffy But that's not the look I wan't! I wan't nice pre-rendered sprites and tiles in this game view.

Thank you all for your suggestions by the way. I am deffinetly going to use GL, and GLOrtho.

One more question, when rendering the tiles, how should it be done? Should I render them to individual Quads and then let OpenGL handle them as polygons, or is there a betterm faster method?

Remember, I wan't to take advantage of lighting, rotation, etc.

Thanks alot!

EDIT: What's the code for hyperlinks on this board? :D

[edited by - psyjax on October 10, 2003 11:05:47 AM]

Share this post


Link to post
Share on other sites
jcspray    122
You'll be feeding GL your sprites as textured quads, yes. Of course, you still have access to all the other stuff, so you're welcome to use triangles etc. Quads just generally make the most sense. The only project that springs to mind that uses 2D OpenGL extensively is Chromium, makbe you'd like to have a glance at the way the author of Chromium handled GL.

As for links, see the faq.


[edited by - jcspray on October 10, 2003 5:59:33 PM]

Share this post


Link to post
Share on other sites
Raduprv    997
quote:
Original post by psyjax
quote:
Original post by Raduprv
Why not making it isometric 3D instead?


I did actually



Good
Look at some screenshots here (from my game), just to give you some insight on what you can do with the OpenGL ortho mode:

http://www.eternal-lands.com/index.php?main=screenshots.php

Height Map Editor | Eternal Lands | Fast User Directory

Share this post


Link to post
Share on other sites
psyjax    156
Wow! looks really nice. I agree it looks very good, but it''s not exactly what I wan''t for my project. I want a nice Iso view kinda like civilization III. With 3D effects on top.

Most of you seem to suggest mapping the tiles onto Quads then blitting them to screen. This would be easy for square tiles, but what about isometric raster tiles? Like the ones I show in my screenshot above.

This one is giving me a run for my money :D

Thanks to everyone, for your comments so far.

Share this post


Link to post
Share on other sites
Raduprv    997
I highly suggest you to go in real 3D, but isometric, like I do. So map the tiles on quads.
The reason behind this is that it will be much easier for you to keep the consistency of the world, rather than render every thing via a different method.
Second, giving the user the ability to rotate the camera can''t hurt

Height Map Editor | Eternal Lands | Fast User Directory

Share this post


Link to post
Share on other sites

  • Similar Content

    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

  • Popular Now