返回顶部

收藏

C++操作OpenGL

更多

[C/C++]代码

// opengl2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/glut.h>

// function that creates the stipple patterns and rectangular polygons.
void display(void)
{
    // Stipple pattern of my initials (EP)
GLubyte myInitial[] = {
    0xff, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0xff, 0x01, 0xff, 0x01, 0x00, 0x01, 0x01, 0x01,
    0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
    0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
    0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
    0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
    0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
    0xff, 0x00, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};

    // A stipple pattern of a fly
GLubyte fly[] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x03, 0x80, 0x01, 0xC0, 0x06, 0xC0, 0x03, 0x60,
    0x04, 0x60, 0x06, 0x20, 0x04, 0x30, 0x0C, 0x20,
    0x04, 0x18, 0x18, 0x20, 0x04, 0x0C, 0x30, 0x20,
    0x04, 0x06, 0x60, 0x20, 0x44, 0x03, 0xC0, 0x22,
    0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
    0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
    0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
    0x66, 0x01, 0x80, 0x66, 0x33, 0x01, 0x80, 0xCC,
    0x19, 0x81, 0x81, 0x98, 0x0C, 0xC1, 0x83, 0x30,
    0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0,
    0x03, 0x31, 0x8c, 0xc0, 0x03, 0x33, 0xcc, 0xc0,
    0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30,
    0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08,
    0x10, 0x63, 0xC6, 0x08, 0x10, 0x30, 0x0c, 0x08,
    0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08};

    // Another stipple pattern
GLubyte halftone[] = {
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};

    // Clear the Color Buffer
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);           // Set the color to white 
    // draw one solid, unstippled rectangle
    glRectf(25.0, 25.0, 125.0, 125.0);
    glEnable(GL_POLYGON_STIPPLE);           // Enable POLYGON STIPPLE
    glPolygonStipple(fly);                      // Pass the stipple array 
    glRectf (125.0, 25.0, 225.0, 125.0);    // draw the first rectangle 
                    // with the first stipple
    glPolygonStipple(halftone);                 // pass the second stipple array
    glRectf(225.0, 25.0, 325.0, 125.0);     // draw the rectangle

    // My own code starts here
    glColor3f(1.0, 0.0,0.0);                // Set change the color to red  
    glPolygonStipple(myInitial);            // Pass the stipple my initial (EP) 
    glRectf(25.0, 125.0, 325.0, 225.0);     // Draw a bigger rectangle
    glDisable(GL_POLYGON_STIPPLE);          // Disable the POLYGON STIPPLE
    glFlush();
}
void init (void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);       // Clear the background set it to black
    glShadeModel(GL_FLAT);                  // set the shading model to FLAT
}
// Function that resets the window, each time when the window size is changed
void reshape(int w, int h)
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);    
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}
// Main entry point of the program
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(350, 250);    // set up the window size
    glutCreateWindow(argv[0]);       // create the window
    init();                          // initialize the window
    glutDisplayFunc(display);        // call the display to draw the rectangles
    glutReshapeFunc(reshape);        // call the reshape function 
                // (each time when the window size is changed)
    glutMainLoop();
    return 0;
}  

[C/C++]代码

// opengl2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/glut.h>

// function that creates the stipple patterns and rectangular polygons.
void display(void)
{
    // Stipple pattern of my initials (EP)
GLubyte myInitial[] = {
    0xff, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
    0xff, 0x01, 0xff, 0x01, 0x00, 0x01, 0x01, 0x01,
    0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
    0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
    0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
    0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
    0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01,
    0xff, 0x00, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};

    // A stipple pattern of a fly
GLubyte fly[] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x03, 0x80, 0x01, 0xC0, 0x06, 0xC0, 0x03, 0x60,
    0x04, 0x60, 0x06, 0x20, 0x04, 0x30, 0x0C, 0x20,
    0x04, 0x18, 0x18, 0x20, 0x04, 0x0C, 0x30, 0x20,
    0x04, 0x06, 0x60, 0x20, 0x44, 0x03, 0xC0, 0x22,
    0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
    0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
    0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
    0x66, 0x01, 0x80, 0x66, 0x33, 0x01, 0x80, 0xCC,
    0x19, 0x81, 0x81, 0x98, 0x0C, 0xC1, 0x83, 0x30,
    0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0,
    0x03, 0x31, 0x8c, 0xc0, 0x03, 0x33, 0xcc, 0xc0,
    0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30,
    0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08,
    0x10, 0x63, 0xC6, 0x08, 0x10, 0x30, 0x0c, 0x08,
    0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08};

    // Another stipple pattern
GLubyte halftone[] = {
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};

    // Clear the Color Buffer
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);           // Set the color to white 
    // draw one solid, unstippled rectangle
    glRectf(25.0, 25.0, 125.0, 125.0);
    glEnable(GL_POLYGON_STIPPLE);           // Enable POLYGON STIPPLE
    glPolygonStipple(fly);                      // Pass the stipple array 
    glRectf (125.0, 25.0, 225.0, 125.0);    // draw the first rectangle 
                    // with the first stipple
    glPolygonStipple(halftone);                 // pass the second stipple array
    glRectf(225.0, 25.0, 325.0, 125.0);     // draw the rectangle

    // My own code starts here
    glColor3f(1.0, 0.0,0.0);                // Set change the color to red  
    glPolygonStipple(myInitial);            // Pass the stipple my initial (EP) 
    glRectf(25.0, 125.0, 325.0, 225.0);     // Draw a bigger rectangle
    glDisable(GL_POLYGON_STIPPLE);          // Disable the POLYGON STIPPLE
    glFlush();
}
void init (void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);       // Clear the background set it to black
    glShadeModel(GL_FLAT);                  // set the shading model to FLAT
}
// Function that resets the window, each time when the window size is changed
void reshape(int w, int h)
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);    
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}
// Main entry point of the program
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(350, 250);    // set up the window size
    glutCreateWindow(argv[0]);       // create the window
    init();                          // initialize the window
    glutDisplayFunc(display);        // call the display to draw the rectangles
    glutReshapeFunc(reshape);        // call the reshape function 
                // (each time when the window size is changed)
    glutMainLoop();
    return 0;
}  

标签:OpenGL,c++

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. wysaid 发表 2014-08-09 19:07:10 【正式转战WebGL】以及终于下定决心好好学OpenGL了。
  2. ericmitt 发表 2015-07-10 19:24:49 OpenCV HighGui module available for WinRT
  3. Scott Meyers 发表 2011-04-01 05:32:02 Appearing and Disappearing consts in C++
  4. Herb Sutter 发表 2012-04-30 21:59:18 World’s youngest C++ programmer?
  5. Herb Sutter 发表 2012-09-12 19:00:39 VC++ 2012 Desktop Express (Free)
  6. Herb Sutter 发表 2013-01-16 00:08:30 Java vulnerabilities
  7. Andy Rich 发表 2013-03-04 23:04:00 Connecting to Live with the C++ REST SDK
  8. bystander 发表 2013-04-11 00:45:38 并查集(C++实现)
  9. bystander 发表 2013-05-22 08:16:24 引用和指针(C++)
  10. mortoray 发表 2013-06-03 04:25:59 Overloading the broken universal reference ‘T&&’
  11. Jim Hogg 发表 2013-07-04 20:00:00 Optimizing C++ Code : Constant-Folding
  12. Potato 发表 2013-06-14 07:39:18 C++对Posix线程API进行封装

发表评论