(Solved) Need help with glTranslate()

Started by
9 comments, last by biscuitz 18 years, 8 months ago
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]
Advertisement
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();


My Current Project Angels 22 (4E5)
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.
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.



My Current Project Angels 22 (4E5)
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();
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 variablesint 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);}
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.
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();
Try this:

glFrustum(-1.0,1.0,-1.0,1.0,1.0,10000.0)
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);///////////////////////////////
.....
}

This topic is closed to new replies.

Advertisement