• Create Account

#Actualjohnmarinelli

Posted 30 August 2013 - 08:13 PM

Hi everyone,

I know this question has been asked a few times before, but I still can't get my program to work.

I'm working on an Asteroids clone in OpenGL, and I'm starting off by simply getting my spaceship to move around.  I also have 4 quads set up in each respective corner so I know if the movement is working or not.

However, it seems like I'm moving the quads around, and not my spaceship.  Here's the code:

void renderScene()
{
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

glPushMatrix();

gSpaceship.render();

glPopMatrix();

glPushMatrix();
//move to bottom right corner
glTranslatef(SPACE_WIDTH / 2.f, SPACE_HEIGHT / 2.f, 0.f);

glColor3f(0.f, 1.f, 0.f);
glVertex2f(0.f, 0.f);
glVertex2f(50.f, 0.f);
glVertex2f(50.f, 50.f);
glVertex2f(0.f, 50.f);
glEnd();

//move to bottom left corner
glTranslatef(-SPACE_WIDTH, 0.f, 0.f);

glColor3f(0.f, 0.f, 1.f);
glVertex2f(0.f, 0.f);
glVertex2f(50.f, 0.f);
glVertex2f(50.f, 50.f);
glVertex2f(0.f, 50.f);
glEnd();

//move to top left corner
glTranslatef(0.f, -SPACE_HEIGHT, 0.f);

glColor3f(1.f, 1.f, 1.f);
glVertex2f(0.f, 0.f);
glVertex2f(50.f, 0.f);
glVertex2f(50.f, 50.f);
glVertex2f(0.f, 50.f);
glEnd();

//move to top right corner
glTranslatef(SPACE_WIDTH, 0.f, 0.f);

glColor3f(1.f, 0.f, 1.f);
glVertex2f(0.f, 0.f);
glVertex2f(50.f, 0.f);
glVertex2f(50.f, 50.f);
glVertex2f(0.f, 50.f);
glEnd();
glPopMatrix();

glFlush();

glutSwapBuffers();
}


and heres' gSpaceship.render(), and consequently, JTexture.render():

void Spaceship::render()
{
//mGraphics is a JTexture
//m_xPos and m_yPos start out in the center of the 4 quads
mGraphics.render(m_xPos, m_yPos);
}

void JTexture::render(GLfloat x, GLfloat y)
{
if (mTextureID != 0)
{
glTranslatef(x, y, 0.f);

glBindTexture(GL_TEXTURE_2D, mTextureID);

glTexCoord2f(0.f, 0.f); glVertex2f(0.f, 0.f );
glTexCoord2f(1.f, 0.f); glVertex2f(mTextureWidth, 0.f );
glTexCoord2f(1.f, 1.f); glVertex2f(mTextureWidth, mTextureHeight);
glTexCoord2f(0.f, 1.f); glVertex2f(0.f, mTextureHeight);
glEnd();
}
}


also, I'd like to eventually be able to rotate the texture and move in the direction its facing; I know how to rotate a texture, but I know I will be very lost when it comes to moving in a direction thats not axis-aligned.  How does one go about this?

Thanks a lot!

#1johnmarinelli

Posted 30 August 2013 - 08:01 PM

Hi everyone,

I know this question has been asked a few times before, but I still can't get my program to work.

I'm working on an Asteroids clone in OpenGL, and I'm starting off by simply getting my spaceship to move around.  I also have 4 quads set up in each respective corner so I know if the movement is working or not.

However, it seems like I'm moving the quads around, and not my spaceship.  Here's the code:

void renderScene()
{
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);

glPushMatrix();

gSpaceship.render();

glPopMatrix();

glPushMatrix();
//move to bottom right corner
glTranslatef(SPACE_WIDTH / 2.f, SPACE_HEIGHT / 2.f, 0.f);

glColor3f(0.f, 1.f, 0.f);
glVertex2f(0.f, 0.f);
glVertex2f(50.f, 0.f);
glVertex2f(50.f, 50.f);
glVertex2f(0.f, 50.f);
glEnd();

//move to bottom left corner
glTranslatef(-SPACE_WIDTH, 0.f, 0.f);

glColor3f(0.f, 0.f, 1.f);
glVertex2f(0.f, 0.f);
glVertex2f(50.f, 0.f);
glVertex2f(50.f, 50.f);
glVertex2f(0.f, 50.f);
glEnd();

//move to top left corner
glTranslatef(0.f, -SPACE_HEIGHT, 0.f);

glColor3f(1.f, 1.f, 1.f);
glVertex2f(0.f, 0.f);
glVertex2f(50.f, 0.f);
glVertex2f(50.f, 50.f);
glVertex2f(0.f, 50.f);
glEnd();

//move to top right corner
glTranslatef(SPACE_WIDTH, 0.f, 0.f);

glColor3f(1.f, 0.f, 1.f);
glVertex2f(0.f, 0.f);
glVertex2f(50.f, 0.f);
glVertex2f(50.f, 50.f);
glVertex2f(0.f, 50.f);
glEnd();
glPopMatrix();

glFlush();

glutSwapBuffers();
}


and heres' gSpaceship.render(), and consequently, JTexture.render():

void Spaceship::render()
{
//mGraphics is a JTexture
mGraphics.render(m_xPos, m_yPos);
}

void JTexture::render(GLfloat x, GLfloat y)
{
if (mTextureID != 0)
{
glTranslatef(x, y, 0.f);

glBindTexture(GL_TEXTURE_2D, mTextureID);

glTexCoord2f(0.f, 0.f); glVertex2f(0.f, 0.f );
glTexCoord2f(1.f, 0.f); glVertex2f(mTextureWidth, 0.f );
glTexCoord2f(1.f, 1.f); glVertex2f(mTextureWidth, mTextureHeight);
glTexCoord2f(0.f, 1.f); glVertex2f(0.f, mTextureHeight);
glEnd();
}
}


also, I'd like to eventually be able to rotate the texture and move in the direction its facing; I know how to rotate a texture, but I know I will be very lost when it comes to moving in a direction thats not axis-aligned.  How does one go about this?

Thanks a lot!

PARTNERS