当前位置: 首页 > news >正文

OpenGL相关库下载并解决三个入门问题

写在前面: 

1.本篇文章将解决 freeglut.h 和 glut.h两个库在VS2019下的配置问题,并解决三个OpenGL课本上的三个问题:

利用OpenGL实现折线和矩形的橡皮筋绘制技术,并采用右键菜单实现功能的选择。

利用OpenGL,分别用点和折线模式实现正弦和余弦曲线的绘制。 
利用OpenGL程序绘制实线、虚线和点画线。

此外,还将绘制一个三角形。

代码参考:

问题一代码:
 

#include "freegult.h"
int iPointNum = 0;
int iMode = 0;
int x1 = 0, y1 = 0, x2 = 0, y2 = 0;int width = 500, height = 500;void MousePlot(GLint button, GLint action, GLint xMouse, GLint yMouse) {if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN) {if (iPointNum == 0 || iPointNum == 2) {iPointNum = 1;x1 = xMouse;y1 = height - yMouse;} else {iPointNum = 2;x2 = xMouse;y2 = height - yMouse;glutPostRedisplay();}}
}void PassiveMouseMove(GLint xMouse, GLint yMouse) {if (iPointNum == 1) {x2 = xMouse;y2 = height - yMouse;glutPostRedisplay();}
}void ProcessMenu(int value) {iMode = value;x1 = 0;y1 = 0;x2 = 0;y2 = 0;glutPostRedisplay();
}void processNormalKeys(unsigned char key, int x, int y) {switch (key) {case 99:iMode = 0;iPointNum = 0;break;case 27:exit(0);}glutPostRedisplay();
}void myinit(void) {glClearColor(1.0, 1.0, 1.0, 1.0);glutCreateMenu(ProcessMenu);glutAddMenuEntry("line", 1);glutAddMenuEntry("broken_line", 2);glutAddMenuEntry("rectangle", 3);glutAttachMenu(GLUT_RIGHT_BUTTON);
}void myReshape(GLsizei w, GLsizei h) {width = w;height = h;glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0, width, 0.0, height);
}void display(void) {glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0, 0.0, 0.0);if (iMode == 1) {glBegin(GL_LINES);glVertex2i(x1, y1);glVertex2i(x2, y2);glEnd();} else if (iMode == 2) {glBegin(GL_LINE_STRIP);glVertex2i(x1, y1);glVertex2i(x2, y1);glVertex2i(x2, y2);glEnd();} else if (iMode == 3) {glBegin(GL_LINE_LOOP);glVertex2i(x1, y1);glVertex2i(x2, y1);glVertex2i(x2, y2);glVertex2i(x1, y2);glEnd();}glutSwapBuffers();
}int main(int argc, char* argv[]) {glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);glutInitWindowSize(500, 500);glutInitWindowPosition(100, 100);glutCreateWindow("luoxiaoc");myinit();glutKeyboardFunc(processNormalKeys);glutMouseFunc(MousePlot);glutPassiveMotionFunc(PassiveMouseMove);glutReshapeFunc(myReshape);glutDisplayFunc(display);glutMainLoop();return 0;
}

问题二代码:
 

#include <GL/freeglut.h>
#include <math.h>
#define PI 3.1416int winWidth = 500, winHeight = 500;void myinit(void) {glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}void myReshape(int w, int h) {winWidth = w;winHeight = h;glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0, w, 0.0, h);
}void display(void) {glClear(GL_COLOR_BUFFER_BIT);glColor3f(0.0, 1.0, 0.0);glBegin(GL_LINES);glVertex2f(0, 250.0);glVertex2f(500.0, 250.0);glVertex2f(250.0, 0);glVertex2f(250.0, 500.0);glEnd();glColor3f(1.0, 0.0, 0.0);glTranslatef(250, 250, 0);glBegin(GL_POINTS);for (float x = -PI; x < PI; x += 2 * PI / 180) {float y = sin(x);glVertex2f(50 * x, 50 * y);}glEnd();glBegin(GL_LINE_STRIP);for (float x = -PI; x < PI; x += 2 * PI / 180) {float y = sin(x);glVertex2f(50 * x, 50 * y);}glEnd();glBegin(GL_POINTS);for (float x = -PI; x < PI; x += 2 * PI / 180) {float y = cos(x);glVertex2f(50 * x, 50 * y);}glEnd();glBegin(GL_LINE_STRIP);for (float x = -PI; x < PI; x += 2 * PI / 180) {float y = cos(x);glVertex2f(50 * x, 50 * y);}glEnd();glutSwapBuffers();
}int main(int argc, char* argv[]) {glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);glutInitWindowSize(500, 500);glutInitWindowPosition(100, 100);glutCreateWindow("");myinit();glutReshapeFunc(myReshape);glutDisplayFunc(display);glutMainLoop();return 0;
}

【注】display函数中四个画线函数每次运行一个即可; 

问题三代码:
 

#include "glut.h"void init() {glClearColor(1.0, 1.0, 1.0, 1.0);glColor3f(0.0, 0.0, 0.0);glLineWidth(2.0);
}void display() {glClear(GL_COLOR_BUFFER_BIT);glBegin(GL_LINES);glVertex2f(-0.9, 0.8);glVertex2f(0.9, 0.8);glEnd();glEnable(GL_LINE_STIPPLE);glLineStipple(1, 0x00FF);glBegin(GL_LINES);glVertex2f(-0.9, 0.5);glVertex2f(0.9, 0.5);glEnd();glDisable(GL_LINE_STIPPLE);glEnable(GL_LINE_STIPPLE);glLineStipple(1, 0x1C47);glBegin(GL_LINES);glVertex2f(-0.9, 0.2);glVertex2f(0.9, 0.2);glEnd();glDisable(GL_LINE_STIPPLE);glFlush();
}int main(int argc, char** argv) {glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(800, 600);glutInitWindowPosition(100, 100);glutCreateWindow("Line Styles in OpenGL");init();glutDisplayFunc(display);glutMainLoop();return 0;
}

三角形代码:
 

#include "freeglut.h"const GLsizei windowWidth = 800;
const GLsizei windowHeight = 800;// This function renders the scene using OpenGL.
void RenderScene() {// Clear the frame buffer by setting it to the clear color.glClear(GL_COLOR_BUFFER_BIT);// Select the Modelview Matrix and reset it to its default state.glMatrixMode(GL_MODELVIEW);glLoadIdentity();// Draw the triangle.glBegin(GL_TRIANGLES);glColor3f(1.0, 0.0, 0.0); // redglVertex2f(0.0, 1.0);glColor3f(0.0, 1.0, 0.0); // greenglVertex2f(1.0, -1.0);glColor3f(0.0, 0.0, 1.0); // blueglVertex2f(-1.0, -1.0);glEnd();// Flush the graphic buffers &// swap them (double buffering).glFlush();glutSwapBuffers();
}// This is the entry point of the program.
int main(int argc, char* argv[]) {// Initialize GLUT.glutInit(&argc, argv);// Set the display mode to use double buffering.// GLUT_SINGLE will came flichering.glutInitDisplayMode(GLUT_DOUBLE);// Create a window with the title "Hello OpenGL".int windowID = glutCreateWindow("Hello OpenGL");glutReshapeWindow(windowWidth, windowHeight);// Set the display function to render the scene.glutDisplayFunc(RenderScene);// Set the clear color to white.glClearColor(1.0, 1.0, 1.0, 1.0);// Transfer control over to GLUT.glutMainLoop();// This code is never reached.return 0;
}

参考链接:

三角形代码:三角形绘制;

环境配置:先看(从头看到精准空降的这里),然后退出来把这个视频看完安装库。

问题一参考;

问题二参考;

相关文章:

  • kettle从入门到精通 第六十四课 ETL之kettle kettle中执行SQL脚本步骤,使用需当心
  • 路由器重启真的好吗?多久重启一次更好?
  • 计算机网络 4.1双绞线
  • python-windows10普通笔记本跑bert mrpc数据样例0.1.001
  • 《深入浅出C语言:从基础到指针的全面指南》
  • C++中的if constexpr
  • 探索HTML5 Geolocation:精准定位网页的新纪元
  • 一个训练好的神经网络的模型文件的内容主要是什么信息呢?请用大白话举例说明( 百度 API 回答 )
  • Flutter与iOS原生混合开发 iOS项目集成Flutter
  • (杂交版)植物大战僵尸
  • 流批一体计算引擎-10-[Flink]中的常用算子和DataStream转换
  • OpenCV的核心数据结构
  • codereview时通常需要关注哪些
  • Mongodb---java篇
  • 简述React 和 Vue 的 diff 时间复杂度从 O(n^3) 优化 到 O(n) ,那么 O(n^3) 和 O(n) 是如何计算出来的 ?
  • JS中 map, filter, some, every, forEach, for in, for of 用法总结
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • echarts的各种常用效果展示
  • Golang-长连接-状态推送
  • Idea+maven+scala构建包并在spark on yarn 运行
  • Java读取Properties文件的六种方法
  • Less 日常用法
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • Objective-C 中关联引用的概念
  • php中curl和soap方式请求服务超时问题
  • React 快速上手 - 06 容器组件、展示组件、操作组件
  • Vue UI框架库开发介绍
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • ​插件化DPI在商用WIFI中的价值
  • # MySQL server 层和存储引擎层是怎么交互数据的?
  • #define用法
  • #Linux(帮助手册)
  • $.ajax中的eval及dataType
  • ()、[]、{}、(())、[[]]命令替换
  • (2)MFC+openGL单文档框架glFrame
  • (floyd+补集) poj 3275
  • (Oracle)SQL优化基础(三):看懂执行计划顺序
  • (vue)el-tabs选中最后一项后更新数据后无法展开
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (蓝桥杯每日一题)love
  • (原創) 是否该学PetShop将Model和BLL分开? (.NET) (N-Tier) (PetShop) (OO)
  • .a文件和.so文件
  • .net 获取某一天 在当月是 第几周 函数
  • .NET 命令行参数包含应用程序路径吗?
  • .Net组件程序设计之线程、并发管理(一)
  • :class的用法及应用
  • [20170728]oracle保留字.txt
  • [APIO2015]巴厘岛的雕塑
  • [AutoSar]BSW_Com02 PDU详解
  • [C++]18:set和map的使用
  • [C++从入门到精通] 14.虚函数、纯虚函数和虚析构(virtual)
  • [c语言]小课堂 day2
  • [DDR5 Jedec 4-1] 预充电命令 Precharge
  • [Django 0-1] Core.Handlers 模块