欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OpenGL基础代码整理

程序员文章站 2022-07-04 12:03:05
...

3-1:画点,连成线

// OPENGL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

void init(void)
{
    glClearColor(1.0, 1.0, 0.0, 0.0);

    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(-10.0, 10.0, -10.0, 10.0);
}

void lineSegment(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(0.0, 0.4, 0.2);
    glBegin(GL_LINES);
        glVertex2i(10,10);
        glVertex2i(-10,-10);
        glVertex2i(-10, 0);
        glVertex2i(10, 0);
        glVertex2i(0, -10);
        glVertex2i(0, 10);
    glEnd();

    glFlush();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(800, 400);
    glutCreateWindow("第一个OpenGL程序");

    init();//设置背景,坐标系
    glutDisplayFunc(lineSegment);//
    glutMainLoop();
    return 0;
}

4-1

// OPENGL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define Pi 3.1415926

void init(void)
{
    glClearColor(1.0, 1.0, 1.0, 0.0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-200.0, 200.0, -200.0, 200.0);
}

//rho = a(1-sint(theta))
//x = a(1 - sin(t))*cos(t)
//y = a(1 - sin(t))*sin(t)
void circleDisplay(void)
{
    float t;
    glClear(GL_COLOR_BUFFER_BIT);

    srand((unsigned)time(NULL));

    glColor3f(1.0, 0.0, 0.0);
    glLineWidth(10.0);
    glBegin(GL_LINE_LOOP);
        for (int i = 0; i < 1000; i++)
        {
            t = -1 * Pi / 2+ 2 * i * Pi / 1000;
            glVertex2f(100*(1 - sin(t))*cos(t), 100*(1 - sin(t))*sin(t));
        }   
    glEnd();

    glFlush();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(50,100);
    glutInitWindowSize(800, 800);
    glutCreateWindow("第一个OpenGL程序");

    glClearColor(1.0, 1.0, 1.0, 0.0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-200.0, 200.0, -200.0, 200.0);

    glutDisplayFunc(circleDisplay);
    glutMainLoop();

    return 0;
}

4-2

// OPENGL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define Pi 3.1415926

float a = 60.0;

void circleDisplay(void)
{
    double rr, gg, bb;
    float x, y;
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(1.0, 0.0, 0.0);
    glLineWidth(5.0);
    //cout << "a=" << a << endl;
    glBegin(GL_LINE_STRIP);
    for (int i = 0; i <= 10000; i++)
    {
        x = -5 + 10.0 * i / 10000;
        glVertex2f(x, pow(x * x, 1.0 / 3.0) + 0.9 * sqrt(3.3 - x * x) * sin(a * Pi * x));
        //glVertex2f(x, x * x);
    }
    glEnd();

    glFlush();
}

void SpecialKeys(int key, int x, int y)
{
    if (key == GLUT_KEY_UP)
        a = a + 0.25;

    glutPostRedisplay();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(50,100);
    glutInitWindowSize(800, 800);
    glutCreateWindow("第一个OpenGL程序");

    glClearColor(1.0, 1.0, 1.0, 0.0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-3.0, 3.0, -3.0, 3.0);

    glutDisplayFunc(circleDisplay);
    //glutSpecialFunc(SpecialKeys);
    glutMainLoop();

    return 0;
}

4-3:point

// OPENGL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define Pi 3.1415926

void init(void)
{
    glClearColor(1.0, 1.0, 1.0, 0.0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-200.0, 200.0, -200.0, 200.0);
}

void circleDisplay(void)
{
    double rr, gg, bb;
    float x, y;
    glClear(GL_COLOR_BUFFER_BIT);

    srand((unsigned)time(NULL));

    glPointSize(10.0);
    glBegin(GL_POINTS);
    for (int i = 0; i < 1000; i++)
    {
        rr = rand() / double(RAND_MAX);
        gg = rand() / double(RAND_MAX);
        bb = rand() / double(RAND_MAX);

        x = (rand() % (400 + 1)) - 200;
        y = (rand() % (400 + 1)) - 200;

        glColor3d(rr, gg, bb);
        glVertex2f(x, y);
    }
    glEnd();

    glFlush();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(50,100);
    glutInitWindowSize(800, 800);
    glutCreateWindow("第一个OpenGL程序");

    glClearColor(1.0, 1.0, 1.0, 0.0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-200.0, 200.0, -200.0, 200.0);

    glutDisplayFunc(circleDisplay);
    glutMainLoop();

    return 0;
}

5-1:bitmap显示字体

// OPENGL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define Pi 3.1415926

void init(void)
{
    glClearColor(1.0, 1.0, 1.0, 0.0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-200.0, 200.0, -200.0, 200.0);
}

void circleDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(0.0, 0.0, 1.0);

    glRasterPos2i(0,0);
    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'A');
    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'B');
    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, 'C');
    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'D');


    glFlush();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(50,100);
    glutInitWindowSize(800, 800);
    glutCreateWindow("第一个OpenGL程序");

    init();
    glutDisplayFunc(circleDisplay);
    glutMainLoop();

    return 0;
}

6-1:blending

// OPENGL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define Pi 3.1415926

int windowWidth = 600, windowHeight = 600;

void init(void)
{
    glClearColor(1.0, 1.0, 1.0, 1.0);

    glEnable(GL_BLEND);
    //glBlendFunc(GL_ONE, GL_ZERO);// sFactor, dFactor
    //glBlendFunc(GL_ZERO, GL_ONE);// sFactor, dFactor
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    //glBlendFunc(GL_ONE, GL_ONE);
}

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor4f(1.0, 0.0, 0.0, 0.9);
    glRecti(-50, -50, 100, 100);

    glColor4f(0.0, 1.0, 0.0, 0.9);
    glRecti(-100, -100, 50, 50);

    glFlush();
}

void windowReshape(int w, int h)
{
    float aspectRatio;

    windowWidth = w;
    windowHeight = h;

    if (windowHeight == 0) windowHeight = 1;
    aspectRatio = (float)windowWidth / (float)windowHeight;

    glViewport(0, 0, windowWidth, windowHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    if (windowWidth >= windowHeight)
        gluOrtho2D(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0);
    else gluOrtho2D(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio);
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(windowWidth, windowHeight);
    glutCreateWindow("第一个OpenGL程序");

    init();
    glutReshapeFunc(windowReshape);
    glutDisplayFunc(myDisplay);
    glutMainLoop();

    return 0;
}

6-2:reshape

// OPENGL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define Pi 3.1415926

int windowWidth = 600, windowHeight = 600;

void init(void)
{
    glClearColor(1.0, 1.0, 1.0, 1.0);
}

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(1.0, 0.0, 0.0);
    glRectf(-50.0,-50.0, 50.0, 50.0);

    glFlush();
}

void windowReshape(int w, int h)
{
    float aspectRatio;

    windowWidth = w;
    windowHeight = h;

    aspectRatio = (float)windowWidth / (float)windowHeight;
    
    //if (windowHeight == 0) windowHeight = 1;
    //aspectRatio = (float)windowWidth / (float)windowHeight;

    glViewport(0, 0, windowWidth, windowHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    if (windowWidth >= windowHeight)
        gluOrtho2D(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0);
    else gluOrtho2D(-100.0, 100.0, -100.0 / aspectRatio, 100.0/aspectRatio);
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(windowWidth, windowHeight);
    glutCreateWindow("第一个OpenGL程序");

    init();
    glutReshapeFunc(windowReshape);
    glutDisplayFunc(myDisplay);
    glutMainLoop();

    return 0;
}

6-3:smooth

// OPENGL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define Pi 3.1415926

int windowWidth = 600, windowHeight = 600;

void init(void)
{
    glClearColor(1.0, 1.0, 1.0, 1.0);

    //glEnable(GL_BLEND);
    //glBlendFunc(GL_ONE, GL_ZERO);// sFactor, dFactor
    //glBlendFunc(GL_ZERO, GL_ONE);// sFactor, dFactor
    //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    //glBlendFunc(GL_ONE, GL_ONE);
}

void myDisplay(void)
{
    double rr, gg, bb;
    float t, x, y;

    srand((unsigned)time(NULL));

    glClear(GL_COLOR_BUFFER_BIT);

    glShadeModel(GL_SMOOTH);
    glBegin(GL_POLYGON);
    glVertex2f(0.0, 0.0);
    for (int i = 0; i <= 20; i++)
    {
        rr = rand() / double(RAND_MAX);
        gg = rand() / double(RAND_MAX);
        bb = rand() / double(RAND_MAX);

        t = Pi *i / 20;

        x = 60.0*cos(t);
        y = 60.0*sin(t);

        glColor3d(rr, gg, bb);
        glVertex2f(x, y);
    }
    glEnd();

    glFlush();
}

void windowReshape(int w, int h)
{
    float aspectRatio;

    windowWidth = w;
    windowHeight = h;

    if (windowHeight == 0) windowHeight = 1;
    aspectRatio = (float)windowWidth / (float)windowHeight;

    glViewport(0, 0, windowWidth, windowHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    if (windowWidth >= windowHeight)
        gluOrtho2D(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0);
    else gluOrtho2D(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio);
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(windowWidth, windowHeight);
    glutCreateWindow("第一个OpenGL程序");

    init();
    glutReshapeFunc(windowReshape);
    glutDisplayFunc(myDisplay);
    glutMainLoop();

    return 0;
}

7-1:antialiasing 消除锯齿

#include "stdafx.h"
#define Pi 3.1415926

int windowWidth = 600, windowHeight = 600;

void init(void)
{
    glClearColor(1.0, 1.0, 1.0, 1.0);

    glEnable(GL_LINE_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
}

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(0.0, 0.0, 1.0);
    glLineWidth(200.0);
    glBegin(GL_LINES);
    glVertex2i(-5, -1); 
    glVertex2i(5, 1);
    glEnd();

    glFlush();
}

void windowReshape(int w, int h)
{
    float aspectRatio;

    windowWidth = w;
    windowHeight = h;

    aspectRatio = (float)windowWidth / (float)windowHeight;

    glViewport(0, 0, windowWidth, windowHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    if (windowWidth >= windowHeight)
        gluOrtho2D(-10.0*aspectRatio, 10.0*aspectRatio, -10.0, 10.0);
    else gluOrtho2D(-10.0, 10.0, -10.0 / aspectRatio, 10.0 / aspectRatio);
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(windowWidth, windowHeight);
    glutCreateWindow("第一个OpenGL程序");

    init();
    glutReshapeFunc(windowReshape);
    glutDisplayFunc(myDisplay);
    glutMainLoop();

    return 0;
}

7-2:fill area

#include "stdafx.h"
#define Pi 3.1415926

int windowWidth = 600, windowHeight = 600;

void init(void)
{
    glClearColor(1.0, 1.0, 1.0, 1.0);
}

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    //glColor3f(1.0, 0.0, 0.0);
    //glBegin(GL_TRIANGLES);
    //glVertex2i(-50, -50); 
    //glVertex2i(50, -50);
    //glVertex2i(0, 50);
    //glEnd();

    //glColor3f(1.0, 0.0, 0.0);
    //glBegin(GL_POLYGON);
    //glVertex2i(-50, 0); glVertex2i(-25, -50);
    //glVertex2i(25, -50); glVertex2i(50, 0);
    //glVertex2i(25, 50); glVertex2i(-25, 50);
    //glEnd();

    //glShadeModel(GL_SMOOTH);//GL_FLAT
    //glBegin(GL_TRIANGLES);
    //glColor3f(1.0, 0.0, 0.0); glVertex2i(-50, -50);
    //glColor3f(0.0, 1.0, 0.0); glVertex2i(50, -50);
    //glColor3f(0.0, 0.0, 1.0); glVertex2i(0, 50);
    //glEnd();

    //glShadeModel(GL_SMOOTH);//GL_FLAT
    //glBegin(GL_POLYGON);
    //glColor3ub(255, 0, 0); glVertex2i(-50, 0);
    //glColor3ub(255, 97, 0); glVertex2i(-25, -50);
    //glColor3ub(255, 255, 0); glVertex2i(25, -50);
    //glColor3ub(0, 255, 0); glVertex2i(50, 0);
    //glColor3ub(0, 255, 255); glVertex2i(25, 50);
    //glColor3ub(0, 0, 255); glVertex2i(-25, 50);
    //glEnd();

    //glShadeModel(GL_SMOOTH);//GL_FLAT
    //glBegin(GL_POLYGON);
    //glColor3ub(255, 0, 0); glVertex2i(-50, 0);
    //glColor3ub(255, 255, 0); glVertex2i(-25, -50);
    //glColor3ub(255, 255, 0); glVertex2i(25, -50);
    //glColor3ub(255, 0, 0); glVertex2i(50, 0);
    //glEnd();

    glFlush();
}

void windowReshape(int w, int h)
{
    float aspectRatio;

    windowWidth = w;
    windowHeight = h;

    aspectRatio = (float)windowWidth / (float)windowHeight;

    glViewport(0, 0, windowWidth, windowHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    if (windowWidth >= windowHeight)
        gluOrtho2D(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0);
    else gluOrtho2D(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio);
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(windowWidth, windowHeight);
    glutCreateWindow("第一个OpenGL程序");

    init();
    glutReshapeFunc(windowReshape);
    glutDisplayFunc(myDisplay);
    glutMainLoop();

    return 0;
}

7-3:line-type

// OPENGL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define Pi 3.1415926

void init(void)
{
    glClearColor(1.0, 1.0, 1.0, 1.0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-10.0, 10.0, -10.0, 10.0);
}

void circleDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glEnable(GL_LINE_STIPPLE);
    
    glLineWidth(10.0f);
    glColor3f(0.0, 0.0, 0.0);
    glLineStipple(1, 0x0101);
    glBegin(GL_LINES);
    glVertex2f(-5.0,-5.0);
    glVertex2f(5.0, 5.0);
    glEnd();

    glFlush();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowPosition(50,100);
    glutInitWindowSize(800, 800);
    glutCreateWindow("第一个OpenGL程序");

    init();
    glutDisplayFunc(circleDisplay);
    glutMainLoop();

    return 0;
}