關於我自己

2014年2月24日 星期一

OpenGL 二維向量(glVertex2f) 與 三維向量(glVertex3f) Ch002

1.二維向量(glVertex2f)

說明: 二維向量就是指只有X軸跟Y軸  

  glVertex2f(X軸,Y軸) ;

 

程式碼:

#include <GL/glut.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/glu.h>
void display()
{
  glClear(GL_COLOR_BUFFER_BIT);
  glBegin(GL_QUADS) ;
  glVertex2f(-0.5,-0.5) ;
  glVertex2f(-0.5,0.5) ;
  glVertex2f(0.5,0.5) ;
  glVertex2f(0.5,-0.5) ;
  glEnd() ;

glFlush();
}
/* Program entry point */
int main(int argc, char *argv[])
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);
  glutInitWindowSize(500, 500); 
  glutCreateWindow("OpenGL");
  glutDisplayFunc( display );
  glutMainLoop();
  return 0;
}

執行後的圖:

2.三維向量(glVertex3f)

說明: 三維向量就是指只有X軸跟Y軸跟Z軸

glVertex3f(X軸,Y軸,Z軸);

Code:


#include <GL/glut.h>

#include <stdlib.h>
static GLfloat xRot = 45.0f;
static GLfloat yRot = 45.0f;



void RenderScene(void)
 {
 //清除
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 //保存矩陣
 glPushMatrix();

 //旋轉
 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
 glRotatef(yRot, 0.0f, 1.0f, 0.0f);



 glBegin(GL_QUADS);

 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);

 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);



 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);

 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);

 
 
 
 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);

 

 glEnd();

    glPopMatrix();

 glutSwapBuffers();
 }

void SetupRC()
 {
 glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

 glEnable(GL_DEPTH_TEST);
 glEnable(GL_DITHER);
 glShadeModel(GL_SMOOTH);
 }

void SpecialKeys(int key, int x, int y)
 {
 if(key == GLUT_KEY_UP)
  xRot-= 5.0f;

 if(key == GLUT_KEY_DOWN)
  xRot += 5.0f;

 if(key == GLUT_KEY_LEFT)
  yRot -= 5.0f;

 if(key == GLUT_KEY_RIGHT)
  yRot += 5.0f;

    if(key > 356.0f)
        xRot = 0.0f;

    if(key < -1.0f)
        xRot = 355.0f;

    if(key > 356.0f)
        yRot = 0.0f;

    if(key < -1.0f)
        yRot = 355.0f;

    // Refresh the Window
    glutPostRedisplay();
    }


void ChangeSize(int w, int h)
{
 GLfloat fAspect;

 if(h == 0)
  h = 1;

 glViewport(0, 0, w, h);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();

 fAspect = (GLfloat)w / (GLfloat)h;
 gluPerspective(35.0f, fAspect, 1.0f, 1000.0f);

 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 glTranslatef(0.0f, 0.0f, -400.0f);
}

int main(int argc, char* argv[])
{
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
 glutInitWindowSize(800,600);
 glutCreateWindow("RGB Cube");
 glutReshapeFunc(ChangeSize);
 glutSpecialFunc(SpecialKeys);
 glutDisplayFunc(RenderScene);
 SetupRC();
 glutMainLoop();

 return 0;
}

執行出來的圖: (故加線條讓使用者比較方便明白)

解說:因為用GL_QUADS,可以參考上一張的圖,這個用意

 

看一定很奇怪 明明圖只有八個角度,為什麼設定的參數有24個,

以下圖片一一為大家說明


 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);
 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);

 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);

 

首先紅色部分CODE執行 (有加入X跟Y跟Z做標指示方便了解)

 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);


 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);

首先藍色部分CODE執行 (有加入X跟Y跟Z做標指示方便了解)

 

glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);


 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);

首先綠色部分CODE執行 (有加入X跟Y跟Z做標指示方便了解)

 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);

 所以才組成

 

 

沒有留言:

張貼留言