Jump to content
  • Advertisement
Sign in to follow this  
lgguy

OpenGL using 3d models for a 2d game

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

First off, let me clarify that I am *not* asking about how to set up OpenGL to draw on the screen using 2D functions. I know about the "Last Post about 2D in OpenGL" thread referenced in the forum faq. Here is a little background on what I am trying to do, the gameplay of the game I am writing takes place on a 2D playing field. However, the project lead (in his infinite wisdom) wanted to use 3D graphics. We are writing the game from scratch and using OpenGL as the graphics API. I am an experienced programmer but my graphics experience is in 2D and this is my first real attempt at 3D graphics. The artists have given me models in 3ds format. This is my ultimate goal: the game takes place on a 24 x 18 grid. I want to encapsulate the 3D code and expose a simple API that lets me move objects around on the game grid (i.e. object->Move(4, 5); // move object to (4,5) on the grid) I have experienced no end of problems. I tried using a regular perspective projection but when I moved an object around, it would appear rotated the farther it was from the middle of the screen. I switched to an orthographic projection and that seems to work okay. However, I am having all sorts of problems with the sizes of my models. Models that look the same size in Milkshape, are drastically different sizes in my app. Even scaling models in Milkshape versus using glScalef() sometimes has different results. I know a lot of these problems are probably because of lack of experience in 3D programming, but I can't find any information on the net about using 3D models for a 2D game (only setting up 3D graphics APIs to act like 2D drawing APIs). Any help would be *much* appreciated! I was a math major in a former life so don't be afraid to throw linear algebra at me in your explanation either ;)

Share this post


Link to post
Share on other sites
Advertisement
Render wise, I would use Ortho cuz the corners will be a little distorted, but you probably already saw that. If your models are the same size in Milkshape, they should be the same size in OpenGL. Unless you or milkshape is scaling the models they should work right. Having the gameplay in 2d with 3d models is actually a good idea and should work just fine. I recommend you take all the models, get them into milkshape, then reexport them when they are the same size. Then they should all be the same. Otherwise, you'll have to figure out how much to scale each one in your program.

Share this post


Link to post
Share on other sites
Well, (again I could have made a newbie mistake I am not aware of), I did import all the models into Milkshape, scaled them to the same size and reexported. On one model, it worked fine. For a different one, when I put it in my app, it was so small it was only 4 pixels big. I keep thinking it has to be something obvious but I can't figure it out. In Milkshape, I look at the max/min vertex coordinates and the two models are in the same range ((+/- 25, +/- 25, +/- 25) to (+/- 17, +/- 17, +/- 17)).

Can anyone help me with a concrete code (or pseudocode) example? I know how all of this works in theory, I just can't do it in practice :)

Share this post


Link to post
Share on other sites
Are you scaling anywhere in your code? If you are make sure you are pushing and matrix before-hand, and then popping after you are done with it. Other than that it's pretty tough to guess what's going wrong.

Share this post


Link to post
Share on other sites
This is my render function is anyone can see any obvious problems...

Also, my call to glTranslatef to move the model along the game grid is dependent on the size of the model. I would like it to be independent but I don't know how to do it.


void Engine::Render()
{
t3DObject *pObject;
int index;
unsigned char *pColor;
unsigned int speed;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

gluLookAt(0, 0, 100, 0, 0, 0, 0, 1, 0);

for(int x = 0; x < GRID_WIDTH; x++)
{
for(int y = 0; y < GRID_HEIGHT; y++)
{
if(grid[x][y] == NULL)
continue;

glTranslatef(static_cast<GLfloat>(grid[x][y]->GetX() * TRANSLATE_SCALE_X), static_cast<GLfloat>(grid[x][y]->GetY() * TRANSLATE_SCALE_Y), 0);

speed = 16;

if(Delay(speed))
if(grid[x][y]->GetMesh()->numberOfFrames)
grid[x][y]->GetMesh()->currentFrame = (grid[x][y]->GetMesh()->currentFrame) % (grid[x][y]->GetMesh()->numberOfFrames) + 1;

if(grid[x][y]->GetMesh()->currentFrame == 0)
grid[x][y]->GetMesh()->currentFrame = 1;

for(int i = 0; i < grid[x][y]->GetMesh()->numOfObjects; i++)
{
pObject = &grid[x][y]->GetMesh()->pObject;
glPushMatrix();

if(pObject->m_ParentNode)
AnimateModel_Recursively(grid[x][y]->GetMesh(), pObject->m_ParentNode);

AnimateModel(grid[x][y]->GetMesh(), pObject);

if(pObject->m_GroupToggle)
{
glPopMatrix();
continue;
}

if(pObject->bHasTexture)
{
glEnable(GL_TEXTURE_2D);
glColor3ub(255, 255, 255);
glBindTexture(GL_TEXTURE_2D, grid[x][y]->GetTextures()[pObject->materialID]);
}
else
{
glDisable(GL_TEXTURE_2D);
glColor3ub(255, 255, 255);
}

glBegin(GL_TRIANGLES);
for(int j = 0; j < pObject->numOfFaces; j++)
{
for(int whichVertex = 0; whichVertex < 3; whichVertex++)
{
index = pObject->pFaces[j].vertIndex[whichVertex];

if(pObject->pNormals)
glNormal3f(pObject->pNormals[index].x, pObject->pNormals[index].y, pObject->pNormals[index].z);

if(pObject->bHasTexture)
if(pObject->pTexVerts)
glTexCoord2f(pObject->pTexVerts[index].x, pObject->pTexVerts[index].y);
else
{
if(grid[x][y]->GetMesh()->pMaterials.size() && pObject->materialID >= 0)
{
pColor = grid[x][y]->GetMesh()->pMaterials[pObject->materialID].color;
glColor3ub(pColor[0], pColor[1], pColor[2]);
}
}

if(pObject->pVerts)
glVertex3f(pObject->pVerts[index].x, pObject->pVerts[index].y, pObject->pVerts[index].z);
}
}
glEnd();
glPopMatrix();
}
}
}

SDL_GL_SwapBuffers();
}

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!