您尚未登录。

楼主 #1 2021-01-05 09:20:32

流氓兔
会员
注册时间: 2020-02-01
已发帖子: 121
积分: 109.5

Visual C++ 编译一个OpenGL Demo

来自: https://learnopengl.com/Getting-started/Hello-Window

https://learnopengl.com/code_viewer_gh.php?code=src/1.getting_started/1.2.hello_window_clear/hello_window_clear.cpp

hello_window_clear.cpp:

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <iostream>

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main()
{
    // glfw: initialize and configure
    // ------------------------------
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

    // glfw window creation
    // --------------------
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // glad: load all OpenGL function pointers
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }    

    // render loop
    // -----------
    while (!glfwWindowShouldClose(window))
    {
        // input
        // -----
        processInput(window);

        // render
        // ------
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        // -------------------------------------------------------------------------------
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // glfw: terminate, clearing all previously allocated GLFW resources.
    // ------------------------------------------------------------------
    glfwTerminate();
    return 0;
}

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
    if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    // make sure the viewport matches the new window dimensions; note that width and 
    // height will be significantly larger than specified on retina displays.
    glViewport(0, 0, width, height);
}

nuget 安装 nupengl 软件包:
2021-01-05_091206.png

添加 include\glad\glad.h   include\KHR\khrplatform.h   src\glad.c 文件:
2021-01-05_091600.png

2021-01-05_091636.png


glad.c 添加到工程:
2021-01-05_091844.png

运行结果:
2021-01-05_092004.png

离线

楼主 #2 2021-01-05 09:23:48

流氓兔
会员
注册时间: 2020-02-01
已发帖子: 121
积分: 109.5

Re: Visual C++ 编译一个OpenGL Demo

Visual C++ 2017工程打包: Project7_opengl_glfw_clear.7z

离线

#3 2021-01-05 11:39:20

拉轰的脚踏车
会员
注册时间: 2020-03-20
已发帖子: 288
积分: 222

Re: Visual C++ 编译一个OpenGL Demo

OpenGL 画虚线代码:

//画 虚线 ,点线,通过glLineStipple设置虚线模型,然后enable GL_LINE_STIPPLE开关
glLineStipple(2, 0x5555);
glEnable(GL_LINE_STIPPLE);

glBegin(GL_LINES);
glVertex3f(0.25,0.85,0.6);
glVertex3f(0.75,0.85,0.6);
glEnd();

//结束画虚线
glDisable(GL_LINE_STIPPLE);

离线

#4 2021-01-05 11:58:59

拉轰的脚踏车
会员
注册时间: 2020-03-20
已发帖子: 288
积分: 222

Re: Visual C++ 编译一个OpenGL Demo

glOrtho 设置原点




glGenRenderbuffers OpenGL离屏渲染

https://stackoverflow.com/questions/12157646/how-to-render-offscreen-on-opengl

离线

#5 2021-01-06 11:35:14

拉轰的脚踏车
会员
注册时间: 2020-03-20
已发帖子: 288
积分: 222

Re: Visual C++ 编译一个OpenGL Demo

2021-01-06_113439.png

https://www.cnblogs.com/1024Planet/p/5650131.html

#include <stdio.h>
#include <Windows.h>
#include <gl/glut.h>

/*
 功能描述:使用OpenGL简单画一个虚心水壶
 缩放变换函数glScalef
 */

//输出模式,0-单缓存模式;非0双缓存模式
#define OUTPUT_MODE 1

float scale = 0.0;
bool add = true;

unsigned char axle = 'x';

void init(void)
{
    //glClearColor函数设置好清除颜色,glClear利用glClearColor函数设置好的当前清除颜色设置窗口颜色
    glClearColor(1.0, 1.0, 0.8, 1.0);
}

void display(void)
{
    printf("scale=%f, axle=%c\n", scale, axle);
    glClear(GL_COLOR_BUFFER_BIT);

    //配置缩放比例大小scale
    scale = (add ? scale + 0.1 : scale - 0.1);
    if (scale >= 2.0)
    {
        add = false;
    } else if (scale <= 0.0) {
        add = true;
    }
    //开始绘画
    glPushMatrix();
    {
        if (axle == 'x') {
            glScalef(scale, 1, 1);
        } else if (axle == 'y') {
            glScalef(1, scale, 1);
        } else if (axle == 'z') {
            glScalef(1, 1, scale);
        } else {
            glScalef(scale, scale, scale);
        }
        glColor3f (1.0, 0.0, 1.0); //画笔梅红色
        glBegin(GL_POLYGON);
        glVertex2f(-0.2, -0.2);
        glVertex2f(-0.2, 0.2);
        glVertex2f(0.2, 0.2);
        glVertex2f(0.2, -0.2);
        glEnd();
    }
    glPopMatrix();

    glLoadIdentity();  //加载单位矩阵
    glColor3f (0.0, 0.0, 1.0); //画笔蓝色
    //--------画直线START--------
    //画直线
    glBegin(GL_LINES);
    glVertex2f(-0.5, 0);
    glVertex2f(0.5, 0);
    glVertex2f(0, 0.5);
    glVertex2f(0, -0.5);
    glEnd();
    //--------画直线E N D--------

    if (OUTPUT_MODE == 0) {
        glFlush();//单缓存GLUT_SINGLE时使用
    } else {
        glutSwapBuffers();//因为使用的是双缓存GLUT_DOUBLE,所以这里必须要交换缓存才会显示
    }

    Sleep(50);
}

void reshape(int w, int h)
{
    int offset = 50;
    int dis = (w > h ? h : w) - offset * 2;

    //配置显示物体屏幕的大小
    glViewport(offset, offset, (GLsizei)dis, (GLsizei)dis);
    printf("reshape: w=%d, h=%d, dis=%d\n", w, h, dis);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(-1.5, 1.5, -1.5, 1.5, 0, 10);
    //gluOrtho2D(-1.5, 1.5, -1.5, 1.5);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
    switch (key) {
        case 'x':
        case 'X':
            axle = 'x';
            glutPostRedisplay();
            break;
        case 'y':
        case 'Y':
            axle = 'y';
            glutPostRedisplay();
            break;
        case 'z':
        case 'Z':
            axle = 'z';
            glutPostRedisplay();
            break;
        case 'a':
        case 'A':
            axle = 'a';
            glutPostRedisplay();
            break;
        default:
            break;
    }
    printf("按键%c\n", key);
}

int main(int argc, char *argv[])
{
    printf("可通过按键xyza控制图形按哪一轴缩放\n");
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB | (OUTPUT_MODE == 0 ? GLUT_SINGLE : GLUT_DOUBLE));
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 400);
    glutCreateWindow("第一个 OpenGL 程序");

    init();
    glutDisplayFunc(&display);
    glutIdleFunc(display);  //设置不断调用显示函数
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}

离线

#6 2021-01-06 11:56:49

拉轰的脚踏车
会员
注册时间: 2020-03-20
已发帖子: 288
积分: 222

Re: Visual C++ 编译一个OpenGL Demo

2021-01-06_115616.png

#include <GL/glut.h>
//窗口长宽
int width = 400, height = 400;
//鼠标点击位置
int hit_pos_x, hit_pos_y;
//鼠标拖动位置
int move_pos_x, move_pos_y;
//鼠标操作种类
int button_kind = -1;


void display()
{
	// 清除屏幕
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	//重新设置OpenGL窗口:原点位置为左上角,x轴从左到右,y轴从上到下,坐标值与像素坐标值相同
	glViewport(0, 0, (GLsizei)width, (GLsizei)height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0, width, height, 0);

	if (button_kind == 0)	//左键点击
	{
		//画一个蓝色的点
		glPointSize(20);
		glBegin(GL_POINTS);
		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex2f(hit_pos_x, hit_pos_y);
		glEnd();
	}
	else if (button_kind == 2)	//右键点击
	{
		//画一个绿色的点
		glPointSize(20);
		glBegin(GL_POINTS);
		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex2f(hit_pos_x, hit_pos_y);
		glEnd();
	}
	else if (button_kind == 3)	//鼠标拖动
	{
		//沿拖动轨迹画一条红色的线
		glLineWidth(5);
		glColor3f(1.0f, 0.0f, 0.0f);
		glBegin(GL_LINES);
		glVertex2f(hit_pos_x, hit_pos_y);
		glVertex2f(move_pos_x, move_pos_y);
		glEnd();
	}

	//双缓存交换缓存以显示图像
	glutSwapBuffers();
	//每次更新显示
	glutPostRedisplay();
}

void mouse_hit(int button, int state, int x, int y)
{
	//鼠标操作种类赋值
	button_kind = button;

	//鼠标操作基本结构
	switch (button)
	{
	case GLUT_LEFT_BUTTON:	//左键操作,也可为数字0
		if (state == GLUT_DOWN)	//左键按下时
		{
			//记录按键位置
			hit_pos_x = x;
			hit_pos_y = y;
		}
		break;
	case GLUT_RIGHT_BUTTON:	//右键操作,也可为数字1
		if (state == GLUT_DOWN)	//右键按下时
		{
			//记录按键位置
			hit_pos_x = x;
			hit_pos_y = y;
		}
		break;
	default:
		break;
	}
}

void mouse_move(int x, int y)
{
	//鼠标移动时操作种类设为3(0 1 2分别为左键、中键、右键)
	button_kind = 3;
	//记录拖动位置
	move_pos_x = x;
	move_pos_y = y;
}

void main(int argc, char** argv)
{
	//初始化GL
	glutInit(&argc, argv);
	//设置显示参数(双缓存,RGB格式)
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	//设置窗口尺寸:width*height
	glutInitWindowSize(width, height);
	//设置窗口位置:在屏幕左上角像素值(100,100)处
	glutInitWindowPosition(100, 100);
	//设置窗口名称
	glutCreateWindow("OpenGL");
	//显示函数,display事件需要自行编写
	glutDisplayFunc(display);
	//鼠标点击函数,mouse_hit事件需要自行编写
	glutMouseFunc(mouse_hit);
	//鼠标拖动函数,mouse_move事件需要自行编写
	glutMotionFunc(mouse_move);


	//重复循环GLUT事件
	glutMainLoop();
}

https://zhuanlan.zhihu.com/p/24735850

OpenGL鼠标操作

离线

#7 2021-01-06 15:13:09

拉轰的脚踏车
会员
注册时间: 2020-03-20
已发帖子: 288
积分: 222

Re: Visual C++ 编译一个OpenGL Demo

glEnable(GL_POINT_SMOOTH); //打圆形点

glDisable(GL_POINT_SMOOTH); //打方形点


打点过程:

		//画一个蓝色的点
		glPointSize(20);
		glBegin(GL_POINTS);
		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex2f(hit_pos_x, hit_pos_y);
		glEnd();

离线

#8 2021-01-06 15:43:57

拉轰的脚踏车
会员
注册时间: 2020-03-20
已发帖子: 288
积分: 222

Re: Visual C++ 编译一个OpenGL Demo

glut 的设计有问题, 单核CPU占用率100%, 双核占用率50%, 这里有讨论:

https://community.khronos.org/t/100-cpu-usage/63231/6

glutIdleFunc()在单核上占用100%的CPU,在双核上占用50%的CPU。

如何在不使用Shrieks Win32“ Sleep()”函数的情况下解决CPU使用率过高的问题?

我可以经常使用glutTimerFunc()来glutPostRedisplay(),但我想知道是否有更清洁的方法吗?



https://gamedev.net/forums/topic/388780-glut-100-cpu-issue/3571642/

嘿,希望有人能希望能帮助我解决我遇到的问题。基本上,我创建了一个GLUT应用程序,每次我编译并运行它时都会达到100%的CPU使用率。奇怪而令人困惑的是,它在任何其他PC上都能正常工作,如果我在PC上运行任何其他GLUT应用程序,它只会使我的CPU耗尽。有任何想法吗?

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn