Sign in to follow this  
biscuitz

(Solved) Need help with glTranslate()

Recommended Posts

When I use it the triangle disappears:
glViewport(0,0,width,height);
glFrustum(1.0,1.0,1.0,1.0,1.0,10000.0);
glMatrixMode(GL_PROJECTION);

bool loop=1;
while(loop)
{
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();
  glTranslatef(0.0,0.0,-4.0);

  glBegin(GL_TRIANGLES);
    
  glVertex3f(0.0,1.0,0.0);
  glVertex3f(1.0,-1.0,0.0);
  glVertex3f(-1.0,-1.0,0.0);
    
  glEnd();
  glFlush();
I'm just trying to move the triangle away 4 units. [Edited by - biscuitz on August 3, 2005 6:22:51 AM]

Share this post


Link to post
Share on other sites
You might be moving it 4 units towards the camera, try changing:

glTranslatef(0.0,0.0,-4.0);

to

glTranslatef(0.0,0.0,4.0);

also, try putting glPushMatrix(), glPopMatrix() functions in there like so:


glPushMatrix();
glTranslatef(0.0,0.0,-4.0);



glBegin(GL_TRIANGLES);



glVertex3f(0.0,1.0,0.0);

glVertex3f(1.0,-1.0,0.0);

glVertex3f(-1.0,-1.0,0.0);

glEnd();
glPopMatrix();



Share this post


Link to post
Share on other sites
Tried it and still shows blank screen.

??? I thought OpenGL has Z axis positive going out of the screen. So moving it (-4) would move it into screen. I was learning from the NeHe tutorials.

Share this post


Link to post
Share on other sites
I just looked in one of my projects, and the negative Z pulled back towards the screen.

Lemme look at our code again...

EDIT: I just compiled it, and it worked for me. Try using a smaller number(like 0.1) and see if it shows up then or not.



Share this post


Link to post
Share on other sites
You are using the wrong matrixmode.


glViewport(0,0,width,height);
glFrustum(1.0,1.0,1.0,1.0,1.0,10000.0);
glMatrixMode(GL_MODELVIEW);

bool loop=1;

while(loop)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-4.0);

glBegin(GL_TRIANGLES);
glVertex3f(0.0,1.0,0.0);
glVertex3f(1.0,-1.0,0.0);
glVertex3f(-1.0,-1.0,0.0);
glEnd();

glFlush();

Share this post


Link to post
Share on other sites
glFrustum(1.0,1.0,1.0,1.0,1.0,10000.0);

The 2 numbers at the end are the clipping plane. (1-10,000)


Alright. Here's the full code. Copy/pasted it straight from the testing program and the screen is blank. (Rebuilt project to make sure) Also I compiled Lesson 2 from NeHe and it works but this doesn't:
#include <SDL/SDL.h>
#include <gl/gl.h>

//Initalize variables
int width=800,height=600;
SDL_Event event;

int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(width,height,0,SDL_OPENGL|SDL_HWSURFACE);

//Setup OpenGL
glViewport(0,0,width,height);
glFrustum(1.0,1.0,1.0,1.0,1.0,10000.0);
glMatrixMode(GL_MODELVIEW);

bool loop=1;
while(loop)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0,0.0,-4.0);

glBegin(GL_TRIANGLES);

glVertex3f(0.0,1.0,0.0);
glVertex3f(1.0,-1.0,0.0);
glVertex3f(-1.0,-1.0,0.0);

glEnd();
glPopMatrix();
glFlush();

if(SDL_PollEvent(&event))
switch(event.type)
{
case SDL_QUIT:
loop=0;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
loop=0;
break;
}
}

SDL_GL_SwapBuffers();
}
SDL_Quit();
return(0);
}

Share this post


Link to post
Share on other sites
The default matrix mode is GL_MODELVIEW, so you are calling glFrustum on the ModelView matrix, which you probably don't want to do. You should have glMatrixMode(GL_PROJECTION) somewhere before you call glFrustum, then set it back to GL_MODELVIEW afterwards.

Share this post


Link to post
Share on other sites
Blank screen!
  //Setup OpenGL
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(1.0,1.0,1.0,1.0,1.0,10000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

bool loop=1;
while(loop)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-2.0);

glBegin(GL_TRIANGLES);

glVertex3f(0.0,1.0,0.0);
glVertex3f(1.0,-1.0,0.0);
glVertex3f(-1.0,-1.0,0.0);

glEnd();
glFlush();

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Try this:

glFrustum(-1.0,1.0,-1.0,1.0,1.0,10000.0)

Share this post


Link to post
Share on other sites
oh,what a terrible word "while(loop)" here. There will be some problem here:
first, it will finish or refresh the screen very fastly,so you can see nothing.
second,you LoadIdentity(),and then translate() with a same number"-4", that mean you do not move a little;

maybe you can do it like this;


float Z_TRANSLATE = -4.0f;

while(loop)
{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,Z_TRANSLATE);

glBegin(GL_TRIANGLES);

glVertex3f(0.0,1.0,0.0);
glVertex3f(1.0,-1.0,0.0);
glVertex3f(-1.0,-1.0,0.0);

glEnd();
glFlush();

Z_TRANSLATE -=5.0f; ///////////////////////////////////////

sleep(1000);///////////////////////////////
.....
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this