opengl+glfw+glew绘制英文字符和数字

沿用上篇博文,加入了xyz轴的xyz文字显示

#include <iostream>
#include <Windows.h>
#define _USE_MATH_DEFINES
#include <math.h>

// GLEW    
#include <GL/glew.h>    

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

GLuint WIDTH = 400, HEIGHT = 400;

#define MAX_CHAR    128
GLuint TextFont;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void windowResize_callback(GLFWwindow* window, int width, int height);


//英文、数字
void XPrintString(const char *s)
{
	//glPushAttrib(GL_LIST_BIT);

	调用每个字符对应的显示列表,绘制每个字符
	//for (; *s != '\0'; ++s)
	//	glCallList(TextFont + *s);

	//glPopAttrib();
	glListBase(TextFont);
	glCallLists(strlen(s), GL_UNSIGNED_BYTE, s);
}

void DrawCoordinate()
{
	glBegin(GL_LINES);

	glColor3f(1.0f, 0.0f, 0.0f);
	glVertex3f(-0.8, 0, 0);
	glVertex3f(0.8, 0, 0);

	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex3f(0, -0.8, 0);
	glVertex3f(0, 0.8, 0);

	glColor3f(0.0f, 0.0f, 1.0f);
	glVertex3f(0, 0, -0.8);
	glVertex3f(0, 0, 0.8);

	glEnd();

	glColor3f(1.0f, 0.0f, 0.0f);
	glRasterPos3f(0.81, 0, 0);
	XPrintString("X");

	glColor3f(0.0f, 1.0f, 0.0f);
	glRasterPos3f(0, 0.81, 0);
	XPrintString("Y");

	glColor3f(0.0f, 0.0f, 1.0f);
	glRasterPos3f(0, 0, 0.81);
	XPrintString("Z");
}

GLfloat xRot = 0.0f;
GLfloat yRot = 0.0f;
void Draw3d(void)
{
	int ipvot = 0;
	GLfloat x, y, angle;
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除屏幕和深度缓存

	glLoadIdentity();
	//glPushMatrix();
	glRotatef(xRot, 1.0f, 0.0f, 0.0f);
	glRotatef(yRot, 0.0f, 1.0f, 0.0f);

	glBegin(GL_TRIANGLE_FAN);
	glVertex3f(0.0f, 0.0f, 0.0f);

	for (angle = 0; angle <= (2.001f * M_PI); angle += (M_PI / 32.0f))
	{
		x = 0.60f * sin(angle);
		y = 0.60f * cos(angle);

		if (ipvot % 2 == 0) glColor3f(0.0f, 1.0f, 1.0f);
		else glColor3f(0.0f, 0.0f, 0.0f);

		ipvot++;

		glVertex3f(x, y, 0.0f);
	}

	glEnd();

	glBegin(GL_TRIANGLE_FAN);
	glVertex3f(0.0f, 0.0f, 0.75f);

	for (angle = 0.0f; angle <= (2.001f * M_PI); angle += (M_PI / 32.0f))
	{
		x = 0.50f * sin(angle);
		y = 0.50f * cos(angle);

		if (ipvot % 2 == 0) glColor3f(0.0f, 1.0f, 0.0f);
		else glColor3f(1.0f, 0.0f, 0.0f);

		ipvot++;

		glVertex3f(x, y, 0.0f);

	}
	glEnd();

	DrawCoordinate();

	//glPopMatrix();
}

void render(GLFWwindow* window)
{
	glClearColor(1.0f, 0.8f, 1.0f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);

	//DrawLine();
	//DrawBezier();
	Draw3d();
}

void initWindow(GLFWwindow* window)
{
	glfwMakeContextCurrent(window);

	//设置按键回调
	glfwSetKeyCallback(window, key_callback);

	//设置窗口大小改变的回调,让绘画区域在窗口中间
	glfwSetWindowSizeCallback(window, windowResize_callback);
}

void initParam()
{
	//显示规则:窗口左下角坐标为0,0;所以下行代码表示在窗口左下角向右向上的400个像素单位作为画布
	glViewport(0, 0, WIDTH, HEIGHT);//设置显示区域400*400,但是可以拖动改变窗口大小
	glLineWidth(3.0);//设置线条宽度,3个像素

	glEnable(GL_BLEND);

	glEnable(GL_POINT_SMOOTH);
	glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);//设置点圆滑

	glEnable(GL_LINE_SMOOTH);
	glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST);//设置线光滑

	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

//启用文字,不支持汉字、unicode
void initData()
{
	TextFont = glGenLists(MAX_CHAR);
	wglUseFontBitmaps(wglGetCurrentDC(), 0, MAX_CHAR, TextFont);
}

int main()
{
	glfwInit();

	GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "OpenGL", nullptr, nullptr);
	if (window == nullptr)
	{
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return -1;
	}

	initWindow(window);

	if (glewInit() != GLEW_OK)
	{
		std::cout << "Failed to initialize GLEW" << std::endl;
		return -1;
	}

	initParam();
	initData();

	// Game loop    
	while (!glfwWindowShouldClose(window))
	{
		glfwPollEvents();

		render(window);

		glfwSwapBuffers(window);
	}

	// Terminate GLFW, clearing any resources allocated by GLFW.    
	glfwTerminate();
	return 0;
}

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
	if (key == GLFW_KEY_UP)
		xRot -= 5.0f;

	else if (key == GLFW_KEY_DOWN)
		xRot += 5.0f;

	else if (key == GLFW_KEY_LEFT)
		yRot -= 5.0f;

	else if (key == GLFW_KEY_RIGHT)
		yRot += 5.0f;
}

void windowResize_callback(GLFWwindow* window, int width, int height)
{
	WIDTH = HEIGHT = width > height ? height : width;
	//左下角是0, 0坐标, x,y说就是相对于左下角的像素距离
	glViewport((width - WIDTH) / 2, (height - HEIGHT) / 2, WIDTH, HEIGHT);
}

效果:

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值