Android&OpenGL&WebOS

GL makes good life

OpenGL坐标系

 本文说法有错误。其实OpenGL ES使用的是右手坐标系:

http://www.learnopengles.com/understanding-opengls-matrices/left_right_hand/


坐标原点(0,0,0)位于屏幕的中央。

OpenGL坐标系

 

如果想要很好的理解这个坐标系。写一段代码,测试下面的函数就可以了:

 

 

void DrawCube()

{

  GLfloat cubeColor[] = { 1.0f, 1.0f, 1.0f, 1.0 };

  glMaterialfv(GL_FRONT, GL_SPECULAR, cubeColor);

  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cubeColor);

  glMaterialf(GL_FRONT, GL_SHININESS, 50.0);

 

  glTranslatef(0.0, 2.0, 0.0);

  glRotatef(g_rotationAngle, 1.0, 0.5, 1.0);

 

 

  glEnable(GL_TEXTURE_2D);

  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

  glBindTexture(GL_TEXTURE_2D, g_cubeTexture);

  glBegin(GL_QUADS);

 

  // front

  glNormal3f(0.0, 0.0, 1.0);

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

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

  glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 1.0);

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

 

  // back

  glNormal3f(0.0, 0.0, -1.0);

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

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

  glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);

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

 

  // top

  glNormal3f(0.0, 1.0, 0.0);

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

  glTexCoord2f(1.0, 0.0); glVertex3f(1.0, 1.0, 1.0);

  glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, -1.0);

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

 

  // bottom

  glNormal3f(0.0, -1.0, 0.0);

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

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

  glTexCoord2f(1.0, 1.0); glVertex3f(1.0, -1.0, 1.0);

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

 

  // left

  glNormal3f(-1.0, 0.0, 0.0);

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

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

  glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, 1.0);

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

 

  // right

  glNormal3f(1.0, 0.0, 0.0);

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

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

  glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, -1.0);

  glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 1.0);

 

  glEnd();

 

  glDisable(GL_TEXTURE_2D);

 

 

这里提供一个测试代码,在VS2008上编译通过:

 

#include <GL/glaux.h>

#include <GL/glut.h>

//#include <windows.h>

#include <GL/gl.h>

#include <GL/glu.h>

//#include <iostream > 

//using   namespace   std; 

 

void Initialize();

void Reshape(int width, int height);

void Display();

void LoadTexture(char *filename, GLuint &texture);

void DrawCube();

 

// index for the texture we'll load for the cube

GLuint g_cubeTexture;

// how much to rotate the cube around an axis

GLfloat g_rotationAngle = 0.0;

 

int main(int argc, char **argv)

{

  // Setup the basic GLUT stuff

  glutInit (&argc, argv);

  glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL);

 

  // Create the window

  glutInitWindowSize(512, 512);

  glutInitWindowPosition(400, 350);

  glutCreateWindow("Chapter 1");

 

  Initialize();

  // Register the event callback functions

  glutDisplayFunc(Display); 

  glutReshapeFunc(Reshape);

 

  // At this point, control is relinquished to the GLUT event handler.

  // Control is returned as events occur, via the callback functions.

  glutMainLoop();   

 

  return 0;

} // end main()

 

void Initialize()

{

 

  // set the background color

  glClearColor(0.0, 0.0, 0.0, 0.0);

 

  // set the shading model

  glShadeModel(GL_SMOOTH);

 

  // set up a single white light

  GLfloat lightColor[] = { 1.0f, 1.0f, 1.0f, 1.0 };

 

  glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);

  glLightfv(GL_LIGHT0, GL_SPECULAR, lightColor);

 

  glEnable(GL_LIGHTING);

  glEnable(GL_LIGHT0);

  glEnable(GL_DEPTH_TEST);

 

  // load the texture for the cube

  //opengl.bmp:256X256

  LoadTexture("opengl.bmp", g_cubeTexture);

 

  // make the modelview matrix active, and initialize it

  glMatrixMode(GL_MODELVIEW);

} // end Initialize()

void Reshape(int width, int height)

{

  if (height == 0)

    return;

  glViewport(0, 0, (GLsizei) width, (GLsizei) height);

  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();

  gluPerspective(90.0, width/height, 1.0, 100.0);

 

  glMatrixMode(GL_MODELVIEW);

} // end Reshape

 

void Display()

{

  g_rotationAngle  = 0;

  glLoadIdentity();

  gluLookAt(0.0, 1.0, 6.0,

            0.0, 0.0, 0.0,

            0.0, 1.0, 0.0);

 

  // clear the screen

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 

  //glRotatef(-g_rotationAngle/8.0, 0.0, 1.0, 0.0);

 

  DrawCube();

 

  // draw everything and swap the display buffer

  glFlush();

  glutSwapBuffers();

} // end Display()

 

void LoadTexture(char *filename, GLuint &texture)

{

  AUX_RGBImageRec *image[1];

  memset(image, 0, sizeof(void *));

 

  // if the file can be read, load the texture

  if (image[0] = auxDIBImageLoad(filename))

  {

    glGenTextures(1, &texture);

    glBindTexture(GL_TEXTURE_2D, texture);

 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, 3, image[0]->sizeX, image[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image[0]->data);

  }

 

  // free memory if we need to

  if (image[0])

  {

    if (image[0]->data)

    {

      free(image[0]->data);

    }

    free(image[0]);

  }

} // end LoadTexture()

 

void DrawCube()

{

  GLfloat cubeColor[] = { 1.0f, 1.0f, 1.0f, 1.0 };

  glMaterialfv(GL_FRONT, GL_SPECULAR, cubeColor);

  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cubeColor);

  glMaterialf(GL_FRONT, GL_SHININESS, 50.0);

 

  glTranslatef(0.0, 2.0, 0.0);

  glRotatef(g_rotationAngle, 1.0, 0.5, 1.0);

 

 

  glEnable(GL_TEXTURE_2D);

  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

  glBindTexture(GL_TEXTURE_2D, g_cubeTexture);

  glBegin(GL_QUADS);

 

  // front

  glNormal3f(0.0, 0.0, 1.0);

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

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

  glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 1.0);

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

 

  // back

  glNormal3f(0.0, 0.0, -1.0);

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

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

  glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);

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

 

  // top

  glNormal3f(0.0, 1.0, 0.0);

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

  glTexCoord2f(1.0, 0.0); glVertex3f(1.0, 1.0, 1.0);

  glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, -1.0);

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

 

  // bottom

  glNormal3f(0.0, -1.0, 0.0);

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

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

  glTexCoord2f(1.0, 1.0); glVertex3f(1.0, -1.0, 1.0);

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

 

  // left

  glNormal3f(-1.0, 0.0, 0.0);

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

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

  glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, 1.0);

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

 

  // right

  glNormal3f(1.0, 0.0, 0.0);

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

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

  glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, -1.0);

  glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 1.0);

 

  glEnd();

 

  glDisable(GL_TEXTURE_2D);

 使用该测试程序时,请提供一个256X256的bmp图片。然后修改Display()函数的g_rotationAngle值来理解

图形和坐标系之间的关系。

 

 在g_rotationAngle为0时,效果如下图:

OpenGL

 

阅读更多
个人分类: OpenGL
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭