OpenGL绘制几个基本初等函数图像

1.x三次方和x三分之一次方
在这里插入图片描述

#include <GL/glut.h>
#include <math.h>

void myDisplay(void)
{
	int i;
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(1.0f, 1.0f, 0.5f, 1.0f);
	glColor3f(0.5f, 0.1f, 1.0f);
	glBegin(GL_LINE_STRIP);
	for (i = 0; i < 1000; i++)  // 画 x 三分之一次方
			glVertex2f(i * 0.001, pow(i * 0.001, 0.33333333));
	glEnd();
	glBegin(GL_LINE_STRIP); // 画x三次方的
	for (i = -1000; i < 0; i++)

		glVertex2f(i * 0.001, pow(i * 0.001, 3));
	glEnd();
	glFlush();
}
int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(400, 400);
	glutCreateWindow("复合函数");
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}

运行结果
在这里插入图片描述

2半立方抛物线

#include <GL/glut.h>
#include <math.h>

void myDisplay(void)
{
	int i;
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(0.1f, 0.7f, 0.2f, 0.5f);
	glColor3f(0.0f, 0.0f, 1.0f);
	glBegin(GL_LINE_STRIP);
	for (i = 0; i < 1000; i++) 
			glVertex2f(i * 0.001, sqrt(pow(0.9 * i * 0.001, 3)));
	glEnd();
	glBegin(GL_LINE_STRIP);
	for (i = 0; i < 1000; i++)  
		glVertex2f(i * 0.001, -sqrt(pow(0.9 * i * 0.001, 3)));
	glEnd();


	glFlush();
}
int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(400, 400);
	glutCreateWindow("半立方抛物线");
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}

在这里插入图片描述
3.概率曲线

#include <GL/glut.h>
#include <math.h>

void myDisplay(void)
{
	int i;
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(0.0f, 1.0f, 0.5f, 1.0f);
	glColor3f(0.5f, 0.0f, 1.0f);
	glBegin(GL_LINE_STRIP);
	for (i = -1000; i < 1000; i++)  
			glVertex2f(i * 0.001, pow(2.718281828459, -pow(i * 0.001, 2)) - 0.2);
	glEnd();
	glFlush();
}
int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(400, 400);
	glutCreateWindow("概率曲线y=e^-(x^2)");
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}

在这里插入图片描述

4.ln(|x|)


```cpp
#include <GL/glut.h>
#include <math.h>

void myDisplay(void)
{
	int i;
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(0.0f, 0.5f, 0.7f, 0.5f);
	glColor3f(0.9f, 0.7f, 1.0f);
    gluOrtho2D(-3, 3, -3, 3);
	glBegin(GL_LINE_STRIP);
    for (i = -30000; i < 30000; i++){
        if (i == 0) continue;
		glVertex2f(i * 0.001, log(fabs(i * 0.001)));
    }
    glEnd();
	glFlush();
}
int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(400, 400);
	glutCreateWindow("ln(|x|)");
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}

在这里插入图片描述

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
OpenGL本身并没有提供直接绘制数字的函数,但可以通过绘制纹理贴图的方式来实现绘制数字的效果。 以下是一个简单的绘制数字的函数示例: ```c++ void drawNumber(int num, GLuint textureID, int width, int height) { int digits[10]; int count = 0; if (num == 0) { digits[count++] = 0; } else { while (num != 0) { digits[count++] = num % 10; num /= 10; } } glPushMatrix(); glTranslatef((count - 1) * width * 0.5f, 0, 0); for (int i = count - 1; i >= 0; i--) { int digit = digits[i]; float u = (float)digit / 10.0f; float u2 = u + 0.1f; GLfloat vertices[] = { (GLfloat)i * width, 0.0f, 0.0f, u, 1.0f, (GLfloat)i * width + width, 0.0f, 0.0f, u2, 1.0f, (GLfloat)i * width + width, (GLfloat)height, 0.0f, u2, 0.0f, (GLfloat)i * width, (GLfloat)height, 0.0f, u, 0.0f }; glBindTexture(GL_TEXTURE_2D, textureID); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vertices); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vertices + 3); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); glTranslatef(-width, 0, 0); } glPopMatrix(); } ``` 该函数接受三个参数:要绘制的数字、数字的纹理贴图ID、数字的宽度和高度。函数内部将数字拆分成各个位的数字,并使用绑定的纹理贴图绘制每个数字。 在主循环中,可以使用该函数绘制数字到屏幕上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值