完成画三角形范例,测试完成

This commit is contained in:
hyzboy 2018-12-07 16:04:27 +08:00
parent d5bfd51a87
commit fde7e56b13
5 changed files with 46 additions and 43 deletions

View File

@ -1,4 +1,5 @@
#include<hgl/graph/RenderDevice.h>
#include<hgl/graph/RenderDriver.h>
#include<hgl/graph/RenderWindow.h>
#include<iostream>
#include<GLEWCore/glew.h>
@ -12,19 +13,9 @@ using namespace hgl::graph;
constexpr uint screen_width=1280;
constexpr uint screen_height=720;
Matrix4f ortho_2d_matrix;
void InitMatrix()
{
ortho_2d_matrix=ortho2d(screen_width,screen_height, //2D画面宽高
true); //使用top为0,bottom为height的方式
}
constexpr char vertex_shader[]=R"(
#version 330 core
uniform mat4 ModelViewProjectionMatrix;
in vec2 Vertex;
in vec3 Color;
@ -32,13 +23,9 @@ out vec4 FragmentColor;
void main()
{
vec4 Position;
FragmentColor=vec4(Color,1.0);
Position=vec4(Vertex,0.0,1.0);
gl_Position=Position*ModelViewProjectionMatrix;
gl_Position=vec4(Vertex,0.0,1.0);
})";
constexpr char fragment_shader[]=R"(
@ -68,33 +55,45 @@ bool InitShader()
if(!shader.Use())
return(false);
if(!shader.SetUniformMatrix4fv("ModelViewProjectionMatrix",ortho_2d_matrix))
return(false);
return(true);
}
VB2i *vb_vertex=nullptr;
VB2f *vb_vertex=nullptr;
VB3f *vb_color=nullptr;
VertexArray *va=nullptr;
constexpr int vertex_data[]={100,100, 200,100, 200,200 };
constexpr float vertex_data[]={0.0f,0.5f, -0.5f,-0.5f, 0.5f,-0.5f };
constexpr float color_data[]={1,0,0, 0,1,0, 0,0,1 };
void BindVBO2VAO(const int vao,const int binding_index,const int shader_location,VertexBufferBase *vb)
{
glVertexArrayAttribBinding(vao,shader_location,binding_index);
glVertexArrayAttribFormat(vao,shader_location,vb->GetComponent(),vb->GetDataType(),GL_FALSE,0);
glEnableVertexArrayAttrib(vao,shader_location);
glVertexArrayVertexBuffer(vao,shader_location,vb->GetBufferIndex(),0,vb->GetStride());
}
void InitVertexBuffer()
{
vb_vertex=new VB2i(4,vertex_data);
vb_vertex=new VB2f(4,vertex_data);
vb_color=new VB3f(4,color_data);
va=new VertexArray(GL_TRIANGLES, //画三角形
2); //两个属性
const int vertex_location=shader.GetAttribLocation("Vertex"); ///<取得顶点流数据输入流对应的shader地址
const int color_localtion=shader.GetAttribLocation("Color"); ///<取得颜色流数据输入流对应的shader地址
const int color_location=shader.GetAttribLocation("Color"); ///<取得颜色流数据输入流对应的shader地址
int binding_index=0; //绑定点
glVertexArrayAttribBinding(va->Get
const int vao=va->GetVAO();
va->SetVertexBuffer(vb_vertex);
va->SetColorBuffer(vb_color,HGL_PC_RGB);
BindVBO2VAO(vao,binding_index,vertex_location,vb_vertex);
++binding_index;
BindVBO2VAO(vao,binding_index,color_location,vb_color);
}
constexpr GLfloat clear_color[4]=
@ -111,6 +110,8 @@ void draw()
{
glClearBufferfv(GL_COLOR,0,clear_color);
glClearBufferfv(GL_DEPTH,0,&clear_depth);
va->Draw();
}
int main(void)
@ -139,7 +140,8 @@ int main(void)
win->MakeToCurrent(); //切换当前窗口到前台
InitMatrix();
InitOpenGLDebug(); //初始化OpenGL调试输出
if(!InitShader())
{
std::cerr<<"init shader failed."<<std::endl;

View File

@ -23,6 +23,8 @@ namespace hgl
virtual void ClearColorBuffer()=0;
virtual void ClearDepthBuffer()=0;
};//class RenderDriver
void InitOpenGLDebug();
}//namespace graph
}//namespace hgl
#endif//HGL_RENDER_DRIVER_INCLUDE

View File

@ -21,8 +21,6 @@ namespace hgl
ObjectList<VertexBufferBase> vertex_buffer_list; ///<顶点数据缓冲区
int *shader_location;
int vertex_compoment; ///<顶点属性格式
PixelCompoment color_compoment; ///<颜色属性格式
@ -37,7 +35,8 @@ namespace hgl
static int GetMaxVertexAttrib();
uint GetPrimitive ()const{return primitive;} ///<取得要绘制的图元类型
const uint GetPrimitive ()const{return primitive;} ///<取得要绘制的图元类型
const GLuint GetVAO ()const{return vao;} ///<取得VAO对象
public: //通用顶点缓冲区设置

View File

@ -1,5 +1,5 @@
#include<hgl/graph/Shader.h>
//#include<hgl/LogInfo.h>
#include<hgl/LogInfo.h>
#include<hgl/type/Smart.h>
#include<malloc.h>
#include<GLEWCore/glew.h>
@ -65,11 +65,11 @@ namespace hgl
glGetShaderInfoLog(shader,log_length,&char_writen,log);
// LOG_HINT(UTF8String(name)+U8_TEXT(" shader compile error\n\n")+ UTF8String(log));
LOG_HINT(UTF8String(name)+U8_TEXT(" shader compile error\n\n")+ UTF8String(log));
delete[] log;
//LOG_ERROR(shader_source);
LOG_ERROR(shader_source);
glDeleteShader(shader);
@ -148,7 +148,7 @@ namespace hgl
glGetProgramInfoLog(program,log_length,&char_written,log);
// LOG_ERROR(u8"Shader program link error\n\n"+UTF8String(log));
LOG_ERROR(u8"Shader program link error\n\n"+UTF8String(log));
delete[] log;
@ -175,7 +175,7 @@ namespace hgl
{
if(!program)
{
// LOG_ERROR(u8"GetAttribLocation("+UTF8String(name)+u8"),program=0");
LOG_ERROR(u8"GetAttribLocation("+UTF8String(name)+u8"),program=0");
return(-1);
}
@ -212,7 +212,7 @@ namespace hgl
{
if(!program)
{
// LOG_ERROR(u8"GetUniformLocation("+UTF8String(name)+u8"),program=0");
LOG_ERROR(u8"GetUniformLocation("+UTF8String(name)+u8"),program=0");
return(-1);
}
@ -222,7 +222,7 @@ namespace hgl
{
const int gl_error=glGetError();
// LOG_ERROR(u8"GetUniformLocation("+UTF8String(name)+u8"),program="+UTF8String(program)+u8",result=-1,gl_error="+UTF8String(gl_error));
LOG_ERROR(u8"GetUniformLocation("+UTF8String(name)+u8"),program="+UTF8String(program)+u8",result=-1,gl_error="+UTF8String(gl_error));
}
return(result);
@ -230,13 +230,13 @@ namespace hgl
#define HGL_GLSL_CHECK_PROGRAM_AND_LOCATION(func) if(!program) \
{ \
/*LOG_ERROR(u8"Shader::SetUniform" #func ",program=0"); */\
LOG_ERROR(u8"Shader::SetUniform" #func ",program=0"); \
return(false); \
} \
\
if(location<0) \
{ \
/*LOG_ERROR(u8"Shader::SetUniform" #func ",location="+UTF8String(location));*/ \
LOG_ERROR(u8"Shader::SetUniform" #func ",location="+UTF8String(location)); \
return(false); \
}
@ -287,13 +287,13 @@ namespace hgl
\
if(location<0) \
{ \
/*LOG_ERROR(u8"Shader::SetUniform" #func ",location="+UTF8String(location));*/ \
LOG_ERROR(u8"Shader::SetUniform" #func ",location="+UTF8String(location)); \
return(false); \
} \
\
if(!value) \
{ \
/*LOG_ERROR(u8"Shader::SetUniform" #func ",value=nullptr");*/ \
LOG_ERROR(u8"Shader::SetUniform" #func ",value=nullptr"); \
return(false); \
} \
\
@ -322,19 +322,19 @@ namespace hgl
{ \
if(!program) \
{ \
/*LOG_ERROR(u8"Shader::SetUniformMatrix" #func ",program=0"); */ \
LOG_ERROR(u8"Shader::SetUniformMatrix" #func ",program=0"); \
return(false); \
} \
\
if(location<0) \
{ \
/*LOG_ERROR(u8"Shader::SetUniformMatrix" #func ",location="+UTF8String(location)); */ \
LOG_ERROR(u8"Shader::SetUniformMatrix" #func ",location="+UTF8String(location)); \
return(false); \
} \
\
if(!mat) \
{ \
/*LOG_ERROR(u8"Shader::SetUniformMatrix" #func ",mat=nullptr"); */ \
LOG_ERROR(u8"Shader::SetUniformMatrix" #func ",mat=nullptr"); \
return(false); \
} \
\

View File

@ -33,12 +33,10 @@ namespace hgl
element_buffer=nullptr;
glCreateVertexArrays(1,&vao);
shader_location=new int[max_vertex_attrib];
}
VertexArray::~VertexArray()
{
delete[] shader_location;
glDeleteVertexArrays(1,&vao);
}
@ -107,6 +105,8 @@ namespace hgl
bool VertexArray::Draw()
{
glBindVertexArray(vao);
if(element_buffer)
glDrawElements(primitive,element_buffer->GetCount(),element_buffer->GetDataType(),nullptr);
else