OpenGL开发环境

写在前面

  • 基于Python
  • 基于CMake

基于Python

依赖库:

  • PyOpenGL
  • PyOpenGL_accelerate

Windows下需要下载相应版本的依赖库,pip默认下载32位,个人平台Windows64使用32位库文件会报错。(Linux平台未发现问题)

https://www.lfd.uci.edu/~gohlke/pythonlibs/上找到python版本相对应的库文件:

PyOpenGL-3.1.5-cp38-cp38-win_amd64.whl  # cp38表示python3.8版本
PyOpenGL_accelerate-3.1.5-cp38-cp38-win_amd64.whl

本地安装。

pip install PyOpenGL PyOpenGL_accelerate

测试文件:

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

w, h= 500,500

def DDALine(x0, y0, x1, y1):
    dx = x1 - x0
    dy = y1 - y0

    x = x0
    y = y0

    eps1 = 0
    if abs(dx) > abs(dy):
        eps1 = abs(dx)
    else:
        eps1 = abs(dy)

    xIncre = dx / eps1
    yIncre = dy / eps1

    for k in range(0, eps1 + 1):
        point(x + 0.5, y + 0.5)
        x+= xIncre
        y+=yIncre


def point(x, y):
    glBegin(GL_POINTS)
    glVertex2f(x, y)
    glEnd()


def square():
    glBegin(GL_QUADS)
    glVertex2f(0, 0)
    glVertex2f(0, 100)
    glVertex2f(100, 100)
    glVertex2f(100, 0)
    glEnd()

def iterate():
    glViewport(0, 0, 500, 500)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0.0, w, 0.0, h, 0.0, 1.0)
    glMatrixMode (GL_MODELVIEW)
    glLoadIdentity()

def display():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    iterate()
    glColor3f(1.0, 0.0, 3.0)
    square()
    point(200, 100)
    DDALine(300, 300, 350, 400)
    glutSwapBuffers()

glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(w, h)
glutCreateWindow("OpenGL")
glutDisplayFunc(display)
glutMainLoop()

基于CMake

CMake文件:

cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 11)

project(blood)

find_package(GLUT REQUIRED)
include_directories(${GLUT_INCLUDE_DIRS})
link_directories(${GLUT_LIBRARY_DIRS})
add_definitions(${GLUT_DEFINITIONS})
if(NOT GLUT_FOUND)
  message(ERROR"GLUT not found")
endif(NOT GLUT_FOUND)

find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIRS})
link_directories(${OpenGL_LIBRARY_DIRS})
add_definitions(${OpenGL_DEFINITIONS})
if(NOT OpenGL_FOUND)
  message(ERROR"OpenGL not found")
endif(NOT OpenGL_FOUND)


file(GLOB_RECURSE SOURCES "code/*.cpp")
file(GLOB_RECURSE HEADERS "code/*.h")

add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES})

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/")

target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})

测试文件:

#include <GL/glu.h>
#include <cmath>
#include <iostream>
#include <GL/freeglut_std.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glut.h>
#include <math.h>

void display(){
  glClearColor(1.0 * 200 / 255, 1.0 * 200 / 255, 1.0 * 169 / 255, 1.0f);
  glClear(GL_COLOR_BUFFER_BIT);

  float hx = -0.5, hy = -0.6;
  float hheight = 0.8, hwidth = 1;
  float wx= 0.1, wy = -0.2;
  float wh = 0.3, ww = 0.3;
  float dx = -0.4, dy = -0.6;
  float dh = 0.3, dw = 0.3;
  float dcx = dx + dw / 2, dcy = dy + dh, r = dw / 2;
  float sx = 1, sy = 1, sr1 = 0.35, sr2 = 0.3, sr3 = 0.25;

  // 折线
  glColor3f(0.9, 0.6, 0.6);
  glLineWidth(8);
  glBegin(GL_LINE_LOOP);
  glVertex2f(-0.5, -0.6);
  glVertex2f(-0.5, 0.2);
  glVertex2f(0.5, 0.2);
  glVertex2f(0.5, -0.6);
  glEnd();


  // 矩形
  glColor3f(1.0 * 200 / 255, 1.0 * 100 / 255, 1.0 * 200 / 255);
  glRectf(hx, hy, hx + hwidth, hy + hheight);

  // 三角形
  glColor3f(1.0 * 50 / 255, 1.0 * 100 / 255, 1.0 * 155 / 255);
  glBegin(GL_TRIANGLES);
  glVertex2f(hx, hy + hheight);
  glVertex2f(0, 0.7);
  glVertex2f(hx + hwidth, hy + hheight);
  glEnd();

  // 窗户

  glColor3f(1.0 * 249 / 255, 1.0 * 205 / 255, 1.0 * 173 / 255);
  glRectf(wx, wy, wx + wh, wy + wh);

  glColor3f(1.0 * 252 / 255, 1.0 * 157 / 255, 1.0 * 156 / 255);
  glLineWidth(3);
  glBegin(GL_LINES);
  glVertex2f(wx, wy + wh / 2);
  glVertex2f(wx + ww, wy + wh / 2);
  glVertex2f(wx + ww / 2, wy + wh);
  glVertex2f(wx + ww / 2, wy);
  glEnd();

  // 门
  glColor3f(1.0 * 252 / 255, 1.0 * 157 / 255, 1.0 * 156 / 255);
  glRectf(dx, dy, dx + dh, dy + dh);

  glColor3f(1.0 * 252 / 255, 1.0 * 157 / 255, 1.0 * 156 / 255);
  glBegin(GL_TRIANGLE_FAN) ;
  glVertex2f(dcx, dcy);
  for(float i = dcx - r; i < dcx + r; i+=0.001){
    glVertex2f(i, std::sqrt(std::fabs(r * r - (i - dcx) * (i - dcx))) + dcy);
  }
  glEnd();

  // 门锁
  glColor3f(1.0 * 255 / 255, 1.0 * 0 / 255, 1.0 * 0 / 255);
  glPointSize(10);
  glBegin(GL_POINTS);
  glVertex2f(dx + 0.08, dy + dh / 2);
  glEnd();

  // 太阳
  glColor3f(1.0 * 255 / 255, 1.0 * 0 / 205, 1.0 * 0 / 255);
  glBegin(GL_TRIANGLE_FAN);
  glVertex2f(sx, sy);
  for(float i = sx - sr1; i < sx; i += 0.001){
    glVertex2f(i, -std::sqrt(std::fabs(sr1 * sr1 - (i - sx) * (i - sx))) + sy);
  }
  glEnd();

  glColor3f(1.0 * 232 / 255, 1.0 * 110 / 255, 1.0 * 145 / 255);
  glBegin(GL_TRIANGLE_FAN);
  glVertex2f(sx, sy);
  for(float i = sx - sr2; i < sx; i+=0.001){
    glVertex2f(i, -std::sqrt(std::fabs(sr2 * sr2 - (i - sx) * (i - sy))) + sy);
  }
  glEnd();

  glColor3f(1.0 * 255 / 255, 1.0 * 255 / 255, 1.0 * 0 / 255);
  glBegin(GL_TRIANGLE_FAN);
  glVertex2f(sx, sy);
  for(float i = sx - sr3; i < sx; i+=0.001){
    glVertex2f(i, -std::sqrt(std::fabs(sr3 * sr3 - (i - sx) * (i - sy))) + sy);
  }
  glEnd();


  glFlush();
}

const int WIDTH = 500;
const int HEIGHT = 500;
const int POS_X = 200;
const int POS_Y = 200;

int main(int argc, char *argv[])
{

  glutInit(&argc, argv);

  const int SCREEN_WIDTH = glutGet(GLUT_SCREEN_WIDTH);
  const int SCREEN_HEIGHT = glutGet(GLUT_SCREEN_HEIGHT);

  const int POS_X = (SCREEN_WIDTH - WIDTH) / 2;
  const int POS_Y = (SCREEN_HEIGHT - HEIGHT) / 2;

  // setting up the display RGB color model + Alpha channel = GLUT_RGBA
  glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);

  // configure window position and size
  glutInitWindowPosition(POS_X, POS_Y);
  glutInitWindowSize(WIDTH, HEIGHT);

  // create window
  glutCreateWindow("opengl");

  // call to the drawing function
  glutDisplayFunc(display);

  // loop require by opengl
  glutMainLoop();
  return 0;
}

comment: