openGL学习

由小到大绘制多个点,排成螺旋

点击(此处)折叠或打开

void RenderScene(void)
{
    GLfloat x, y, z, angle;
    z = -50.0f;
    GLfloat zStep = 0.5f;
    GLfloat sizes[2];
    GLfloat sizeStep;
    GLfloat curSize;
   
    glGetFloatv(GL_POINT_SIZE_RANGE, sizes);
    glGetFloatv(GL_POINT_SIZE_GRANULARITY, &sizeStep);

    curSize = sizes[0] + sizeStep;
   
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glRotatef(40.0f, 10.0f, -10.0f, 0.0f);
   
   

    for (angle = 0.0f; angle <= (2.0f * GL_PI) * 3.0f; angle += 0.1f)
    {
        x = 50.0f * sin(angle);
        y = 50.0f * cos(angle);
        z += zStep;
        glPointSize(curSize);
        glBegin(GL_POINTS);
        glVertex3f(x, y, z);
        glEnd();
        if (curSize < 10.0f) curSize += sizeStep;
       
    }
   
   
    glPopMatrix();
    glutSwapBuffers();
}

void ChangeSize(GLsizei w, GLsizei h)
{
    GLfloat nRange = 100.0f;
    if (h == 0) h = 1;
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
   
   
    if (w <= h)
    {
        glOrtho(-nRange, nRange, -nRange * h / w, nRange * h / w, -nRange, nRange);
    }
    else
    {
        glOrtho(-nRange * w / h, nRange * w / h, -nRange, nRange, -nRange, nRange);
    }
   
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}



绘制直线、折线

点击(此处)折叠或打开

void RenderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glRotatef(40.0f, 10.0f, -10.0f, 0.0f);
   
    GLfloat x, y, z, angle;

    glBegin(GL_LINE_STRIP); //如果用GL_LINE_LOOP,会自动加一条线,形成环,如果用GL_LINES,则只是画一条线
    glVertex3f(x, y, z);
    glVertex3f(x + 30, y + 30, z + 30);
    glVertex3f(x + 30, y + 60, z + 30);
    glVertex3f(x + 20, y + 40, z + 80);
    glEnd();

    glPopMatrix();
    glutSwapBuffers();
}

用折线画螺旋,并且用上下左右控制旋转

点击(此处)折叠或打开

/*
* File: main.c
* Author: root
*
* Created on August 30, 2013, 12:57 PM
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <math.h>


#define GL_PI 3.1415f
#define KEY_UP 101
#define KEY_DOWN 103
#define KEY_LEFT 102
#define KEY_RIGHT 100

GLfloat rotateX = 0.0f;
GLfloat rotateY = 0.0f;

void RenderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glRotatef(rotateX, 1.0f, 0.0f, 0.0f);
    glRotatef(rotateY, 0.0f, 1.0f, 0.0f);
   
   
    GLfloat x, y, z, angle;
    z = -50.0f;
    glBegin(GL_LINE_STRIP);
    for(angle = 0.0f; angle < (2.0f * GL_PI) * 4; angle += 0.1f)
    {
        x = 50.0f * sin(angle);
        y = 50.0f * cos(angle);
        z += 0.5f;
       
        glVertex3f(x, y, z);
        
    }
    glEnd();
   
   
   
    glPopMatrix();
    glutSwapBuffers();
}

void ChangeSize(GLsizei w, GLsizei h)
{
    GLfloat nRange = 100.0f;
    if (h == 0) h = 1;
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
   
   
    if (w <= h)
    {
        glOrtho(-nRange, nRange, -nRange * h / w, nRange * h / w, -nRange, nRange);
    }
    else
    {
        glOrtho(-nRange * w / h, nRange * w / h, -nRange, nRange, -nRange, nRange);
    }
   
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

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

void myTimer(int i)
{

}

void keyAction (int key, int x, int y)
{
    switch (key)
    {
        case KEY_UP:
            rotateX += 1.5f;
            break;
        case KEY_DOWN:
            rotateX -= 1.5f;
            break;
        case KEY_LEFT:
            rotateY -= 1.5f;
            break;
        case KEY_RIGHT:
            rotateY += 1.5f;
            break;
        default:
            break;
    }
    RenderScene();
}


/*
*
*/
int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(399, 400);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutCreateWindow("asd");
    SetupRC();
    glutDisplayFunc(RenderScene);
    glutReshapeFunc(ChangeSize);
    glutSpecialFunc(keyAction);
    glutMainLoop();
    return (EXIT_SUCCESS);
}


用三角形画圆锥
两个扇面组成封口的锥形

点击(此处)折叠或打开

  1. void RenderScene(void)
  2. {
  3.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  4.     glPushMatrix();
  5.    
  6.     glRotatef(rotateX, 10.0f, 0.0f, 0.0f);
  7.     glRotatef(rotateY, 0.0f, 10.0f, 0.0f);

  8.     char p0[3] = {0.0f, 50.0f, 0.0f};
  9.     char p1[3] = {20.0f, 0.0, 20.0f};
  10.     GLfloat angle;
  11.     glShadeModel(GL_SMOOTH);
  12.     glEnable(GL_CULL_FACE); //开启多边形剔除,不渲染多边形的背面,这样如果要显示一个正面朝外的封闭多边形,则背面超前的地方根本就不渲染。如果是创建不发呢关闭的集合形体,如纸,就不能打开这个选项。
  13.     glEnable(GL_DEPTH_TEST); // 开启深度测试,可以避免晚描绘的像素错误遮挡之前描绘的Z坐标靠前的像素
  14.    
  15.     glPolygonMode(GL_BACK, GL_FILL); // 显示填充好的多边形,如果选GL_LINE,则只显示每个多边形的轮廓。
  16.    
  17.     glBegin(GL_TRIANGLE_FAN);
  18.     glVertex3f(p0[0], p0[1], p0[2]);
  19.     for (angle = 0; angle <= 2*GL_PI + 0.1f; angle += 0.1f)
  20.     {
  21.         glColor3f(sin(angle), 0.0f, 0.5f);
  22.         glVertex3f(p1[0] * sin(angle), p1[1], p1[2] * cos(angle));
  23.     }
  24.     glEnd();

  25.     glBegin(GL_TRIANGLE_FAN);
  26.     glColor3f(1.0f, 1.0f, 1.0f);
  27.     glVertex3f(p0[0], p1[1], p0[2]);
  28.     for (angle = 0; angle <= 2*GL_PI + 0.1f; angle += 0.1f)
  29.     {
  30.         glVertex3f(p1[0] * cos(angle), p1[1], p1[2] * sin(angle));
  31.     }
  32.     glEnd();
  33.    
  34.     glPopMatrix();
  35.     glutSwapBuffers();
  36. }


用GL_QUAD_STRIP绘制连续的四边形。提供顶点的方向是折线形的
提供顶点的顺序逆时针,则为正面。

点击(此处)折叠或打开

void RenderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glRotatef(rotateX, 1.0f, 0.0f, 0.0f);
    glRotatef(rotateY, 0.0f, 1.0f, 0.0f);
    glEnable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);
    glPolygonMode(GL_BACK, GL_FILL);

    GLfloat r = 2.0f;
   
    glPushMatrix();
   
    glBegin(GL_QUAD_STRIP);
    glColor3f(1.0, 0.0f, 0.0f);
   
    glVertex3f(r, -r, -r);
    glVertex3f(r, -r, r);
    glVertex3f(-r, -r, -r);
    glVertex3f(-r, -r, r);
   
    glColor3f(0.0, 1.0f, 0.0f);
    glVertex3f(-r, r, -r);
    glVertex3f(-r, r, r);

    glColor3f(0.0, 0.0f, 1.0f);
    glVertex3f(r, r, -r);
    glVertex3f(r, r, r);

    glColor3f(0.0, 1.0f, 1.0f);
    glVertex3f(r, -r, -r);
    glVertex3f(r, -r, r);
    glEnd();
   
    glBegin(GL_QUADS);
    glColor3f(1.0, 1.0f, 1.0f);
    glVertex3f(-r, -r, r);
    glVertex3f(r, -r, r);
    glVertex3f(r, r, r);
    glVertex3f(-r, r, r);
   
    glEnd();
   
    glBegin(GL_QUADS);
    glColor3f(1.0, 0.0f, 1.0f);
    glVertex3f(r, -r, -r);
    glVertex3f(-r, -r, -r);
    glVertex3f(-r, r, -r);
    glVertex3f(r, r, -r);
   
   
    glEnd();
   
    glPopMatrix();
    glutSwapBuffers();
}


透视效果

点击(此处)折叠或打开

void ChangeSize(GLsizei w, GLsizei h)
{
    if (h == 0) h = 1;
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
   
    gluPerspective(80.0f, (GLfloat)w/(GLfloat)h, 11.0, 30.0);
   
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -15.0f); 把坐标向x轴负方向推,使得图像装到gluPerspective的后两个参数范围里
}



方块回弹

点击(此处)折叠或打开

/*
* File: main.c
* Author: root
*
* Created on August 30, 2013, 12:57 PM
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <GL/glut.h>
#include <GL/gl.h>


GLfloat rsize = 25.0f;
GLfloat x = 200.0f;
GLfloat y = 25.0f;

GLfloat xStep = 5.0f;
GLfloat yStep = 5.0f;

GLfloat windowWidth;
GLfloat windowHight;

void RenderScene(void)
{
   
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0f, 0.0f, 0.0f);
    glRectf(x , y , x + rsize, y - rsize);
   
   
    glutSwapBuffers();
}

void ChangeSize(GLsizei w, GLsizei h)
{
    windowWidth = w;
    windowHight = h;
    glViewport(0, 0, windowWidth, windowHight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, windowWidth, 0, windowHight, 20.0, -20.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void SetupRC(void)
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// glColor3f(0.0f, 1.0f, 0.0f);
}

void myTimer(int i)
{
    if (x > windowWidth - rsize || x < 0) xStep *= -1;
    if (y > windowHight || y < rsize) yStep *= -1;
    x += xStep;
    y += yStep;
   
    glutPostRedisplay();
   
    glutTimerFunc(30, myTimer, i);
}

/*
*
*/
int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(399, 400);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutCreateWindow("asd");
// glEnable(GL_LIGHTING);
    glutDisplayFunc(RenderScene);
    glutReshapeFunc(ChangeSize);
    glutTimerFunc(30, myTimer, 1);
    SetupRC();
    glutMainLoop();
    return (EXIT_SUCCESS);
}








来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26239116/viewspace-2125609/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26239116/viewspace-2125609/

你可能感兴趣的:(openGL学习)