OpenGL编程指南-环境搭建,渲染三角形

博客中使用的环境为:

visual studio 2013

freeglut-MSVC-3.0.0-2.mp.zip

glew-1.13.0-win32.zip

采用编译完成的库文件,安装方法如下

If you are using 64-bit Windows 7 or Windows 8 with Visual Studio 2013 do the following:
Header files:$(Microsoft Visual Studio 12.0)\VC\include\gl
32-bit lib files: $(Microsoft Visual Studio 12.0)\VC\lib

64-bit lib files: $(Microsoft Visual Studio 12.0)\VC\lib\amd64

32-bit dll files: C:\WINDOWS\SysWOW64


64-bit dll files: C:\WINDOWS\System32



当然,也可以每一个项目手动制定所有的库文件和头文件,只是比较麻烦。


渲染三角形

到这里,所需要的库文件都安装完成,现在来了解OpenGL里面的Hello world ---渲染三角形。

1.新建一个空的win32项目,指定好需要的lib库。

2.编写源文件,loadshaders.h 和loadshaders.cpp是红宝书里面写的一个帮助加载shader的简单封装,直接拿过来了,功能是根据文件名,读入字符串,然后编译shader。完整目录结构如下

注意:因为我自己的项目是用了预编译头,所以,在loadshaders.cpp中需要添加预编译头的!



下面是渲染三角新的代码

// triangles.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

enum VAO_IDs {Triangles,NumVAOs};
enum VBO_IDs {ArrayBuffer,NumVBOs};
enum Attrib_IDs{vPosition = 0};

GLuint VAOs[NumVAOs];
GLuint VBOs[NumVBOs];

const GLuint NumVertices = 6;

void init(){
	glGenVertexArrays(NumVAOs, VAOs);
	glBindVertexArray(VAOs[Triangles]);

	GLfloat vertices[NumVertices][2] = {
		{-0.9,-0.9},
		{ 0.85, -0.9 },
		{ -0.9, 0.85 },
		{0.9,-0.85},
		{0.9,0.9},
		{-0.85,0.9}
	};

	glGenBuffers(NumVBOs, VBOs);
	glBindBuffer(GL_ARRAY_BUFFER, VBOs[ArrayBuffer]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
	ShaderInfo shaders[] = {
		{ GL_VERTEX_SHADER, "triangles.vert" },
		{ GL_FRAGMENT_SHADER, "triangles.frag" },
		{GL_NONE,NULL}
	};
	GLuint program = LoadShaders(shaders);
	glUseProgram(program);

	glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, NULL);
	glEnableVertexAttribArray(vPosition);
}


void display(void){
	glClear(GL_COLOR_BUFFER_BIT);
	//glColor3f(0, 0, 1);
	glBindVertexArray(VAOs[Triangles]);
	glDrawArrays(GL_TRIANGLES, 0, NumVertices);
	glFlush();
}

int _tmain(int argc, char* argv[])
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_RGB);
	glutInitWindowSize(512, 512);
	//glutInitContextVersion(4, 0);
	glutInitContextProfile(GLUT_CORE_PROFILE);
	glutCreateWindow("triangles");
	if (glewInit())
	{
		cerr << "Unable to initialize GLEW ." << endl;
		exit(EXIT_FAILURE);
	}

	init();

	glutDisplayFunc(display);
	glutMainLoop();
	return 0;
}



下面是vertex shade 和 fragment shader r的函数

#version 450 core
layout(location = 0) in vec4 vPosition;
void main(){
	gl_Position = vPosition;
}

#version 450 core
out vec4 fColor;
void main(){
	fColor = vec4(0.0,0.7,1.0,1.0);
}

执行结果:

当然,也可以换一下fragment shader里面的输出颜色

#version 450 core
out vec4 fColor;
void main(){
	fColor = vec4(0.0,0.7,0.2,1.0);
}

然后输出的结果也会跟着变化。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值