|
|
#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); }
|