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

发布于:2024-06-10 ⋅ 阅读:(135) ⋅ 点赞:(0)

写在前面: 

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.1416

int 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); // red
	glVertex2f(0.0, 1.0);

	glColor3f(0.0, 1.0, 0.0); // green
	glVertex2f(1.0, -1.0);

	glColor3f(0.0, 0.0, 1.0); // blue
	glVertex2f(-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;
}

参考链接:

三角形代码:三角形绘制

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

问题一参考

问题二参考


网站公告

今日签到

点亮在社区的每一天
去签到