You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
4.1 KiB
110 lines
4.1 KiB
#include "scene.h"
|
|
#include "ggl.h"
|
|
#include "utils.h"
|
|
#include "ground.h"
|
|
|
|
GLuint vbo, ebo;
|
|
GLuint program;
|
|
GLint positionLocation, modelMatrixLocation, viewMatrixLocation, projectionMatrixLocation, colorLocation;//顶点、模型矩阵、视图矩阵、投影矩阵的位置
|
|
GLint texcoordLocation, textureLocation;//顶点坐标插槽位置、贴图插槽位置
|
|
GLuint texture;
|
|
glm::mat4 modelMatrix, viewMatrix, projectionMatrix;
|
|
Ground ground;
|
|
|
|
void Init() {
|
|
/*1.初始化vbo并为vbo赋值*/
|
|
float data[] = {
|
|
-0.2f,-0.2f,0.0f,1.0f, 1.0f,0.0f,0.0f,1.0f, 0.0f,0.0f,
|
|
0.2f,-0.2f,0.0f,1.0f, 0.0f,1.0f,0.0f,1.0f, 1.0f,0.0f,
|
|
0.0f,0.2f,0.0f,1.0f, 0.0f,0.0f,1.0f,1.0f, 0.5f,1.0f
|
|
};
|
|
glGenBuffers(1, &vbo);//创建vbo
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);//绑定下面要操作的vbo
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 30, data, GL_STATIC_DRAW);//发送cpu数据到GPU
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);//关闭绑定
|
|
|
|
/*1.2使用vbo定义绘画顺序*/
|
|
unsigned short indexes[] = { 0,1,2 };
|
|
glGenBuffers(1, &ebo);
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short) * 3, indexes, GL_STATIC_DRAW);
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
/*2.编译shader*/
|
|
int fileSize = 0;
|
|
unsigned char * shaderCode = LoadFileContent("Res/test.vs", fileSize);
|
|
GLuint vsShader = CompileShader(GL_VERTEX_SHADER, (char*)shaderCode);//编译 vertex shader
|
|
delete shaderCode;
|
|
|
|
shaderCode = LoadFileContent("Res/test.fs", fileSize);
|
|
GLuint fsShader = CompileShader(GL_FRAGMENT_SHADER, (char*)shaderCode);//编译 fragment shader
|
|
delete shaderCode;
|
|
|
|
/*3.链接 vertex shader 和 fragment shader*/
|
|
program = CreateProgram(vsShader, fsShader);
|
|
glDeleteShader(vsShader);
|
|
glDeleteShader(fsShader);
|
|
|
|
/*4.获取顶点、模型矩阵、视图矩阵、投影矩阵,在GPU中的位置,有这个位置后面才能对其进行赋值操作*/
|
|
/*
|
|
attribute vec4 position;
|
|
uniform mat4 ModelMatrix;
|
|
uniform mat4 ViewMatrix;
|
|
uniform mat4 ProjectionMatrix;
|
|
*/
|
|
positionLocation = glGetAttribLocation(program, "position");
|
|
colorLocation = glGetAttribLocation(program, "color");
|
|
texcoordLocation = glGetAttribLocation(program, "texcoord");
|
|
|
|
modelMatrixLocation = glGetUniformLocation(program, "ModelMatrix");
|
|
viewMatrixLocation = glGetUniformLocation(program, "ViewMatrix");
|
|
projectionMatrixLocation = glGetUniformLocation(program, "ProjectionMatrix");
|
|
textureLocation = glGetUniformLocation(program, "U_Texture");
|
|
modelMatrix = glm::translate(0.0f, 0.0f, -0.6f);
|
|
|
|
texture = CreateTexture2DFromBMP("Res/test.bmp");
|
|
|
|
ground.Init();
|
|
}
|
|
void SetViewPortSize(float width, float height) {
|
|
//5.1.设置投影矩阵
|
|
projectionMatrix = glm::perspective(60.0f, width / height, 0.1f, 1000.0f);
|
|
}
|
|
void Draw() {
|
|
float frameTime = GetFrameTime();
|
|
glClearColor(0.1f, 0.4f, 0.6f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
ground.Draw(viewMatrix, projectionMatrix);
|
|
|
|
glUseProgram(program);
|
|
|
|
/*5.2.把MVP矩阵赋值到显卡中*/
|
|
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix));//设置模型矩阵
|
|
glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, glm::value_ptr(viewMatrix));
|
|
glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, glm::value_ptr(projectionMatrix));
|
|
|
|
|
|
/*6.设置绘制的数据*/
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
glUniform1i(textureLocation, 0);//设置贴图
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);//绑定vbo,就是1部分设置的几个顶点数据
|
|
glEnableVertexAttribArray(positionLocation);//启用顶点插槽
|
|
glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 10, 0);//把vbo顶点数据填入顶点插槽
|
|
glEnableVertexAttribArray(colorLocation);//启用颜色插槽
|
|
glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 10, (void*)(sizeof(float) * 4));//把vbo颜色数据填入颜色插槽
|
|
glEnableVertexAttribArray(texcoordLocation);
|
|
glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 10, (void*)(sizeof(float) * 8));
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
/*7.绘制*/
|
|
//glDrawArrays(GL_TRIANGLES, 0, 3);//顺序绘制
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
|
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);//按照ebo顺序绘制
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
|
|
/*8.解除绑定*/
|
|
glUseProgram(0);
|
|
}
|