Browse Source

使用vao

master
blobt 5 years ago
parent
commit
e5bd97f14a
  1. BIN
      .vs/shader2/v14/.suo
  2. 25
      main.cpp
  3. 10
      misc.cpp
  4. 3
      misc.h

BIN
.vs/shader2/v14/.suo

25
main.cpp

@ -122,16 +122,29 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
printf("load obj model fail\n"); printf("load obj model fail\n");
} }
/*´´½¨vao*/
GLuint vao = CreateVAOWithVBOSettings([&]()->void {
/*´´½¨vbo*/ /*´´½¨vbo*/
GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes); GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(posLocation);
glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0);
glEnableVertexAttribArray(texcoordLocation);
glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3));
glEnableVertexAttribArray(normalLocation);
glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 5));
glBindBuffer(GL_ARRAY_BUFFER, 0);
});
/*´´½¨IBO*/ /*´´½¨IBO*/
GLuint ibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indexCount, GL_STATIC_DRAW, indexes); GLuint ibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indexCount, GL_STATIC_DRAW, indexes);
/*´´½¨ÎÆÀí*/ /*´´½¨ÎÆÀí*/
GLuint mainTexture = CreateTextureFromFile("res/image/niutou.bmp"); GLuint mainTexture = CreateTextureFromFile("res/image/niutou.bmp");
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
GL_CALL(glClearColor(0.1f, 0.4f, 0.7f, 1.0f));
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glViewport(0,0, windowWidth, windowHeight); glViewport(0,0, windowWidth, windowHeight);
@ -168,15 +181,8 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
glBindTexture(GL_TEXTURE_2D, mainTexture); glBindTexture(GL_TEXTURE_2D, mainTexture);
glUniform1i(textureLocation, 0); glUniform1i(textureLocation, 0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(posLocation);
glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0);
glEnableVertexAttribArray(texcoordLocation);
glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3));
glEnableVertexAttribArray(normalLocation);
glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 5));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(vao);
model = glm::translate(-1.0f, -0.5f, -3.0f) * glm::rotate(-90.0f, 0.0f, 1.0f, 0.0f)*glm::scale(0.01f, 0.01f, 0.01f); model = glm::translate(-1.0f, -0.5f, -3.0f) * glm::rotate(-90.0f, 0.0f, 1.0f, 0.0f)*glm::scale(0.01f, 0.01f, 0.01f);
glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model)); glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model));
@ -199,6 +205,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glUseProgram(0); glUseProgram(0);
}; };

10
misc.cpp

@ -16,6 +16,16 @@ GLuint CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void
return object; return object;
} }
GLuint CreateVAOWithVBOSettings(std::function<void()> settings)
{
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
settings();
glBindVertexArray(0);
return vao;
}
char* LoadFileContent(const char *path) { char* LoadFileContent(const char *path) {
FILE* pFile = fopen(path, "rb"); FILE* pFile = fopen(path, "rb");
if (pFile) { if (pFile) {

3
misc.h

@ -1,7 +1,10 @@
#pragma once #pragma once
#include "glew.h" #include "glew.h"
#include <functional>
GLuint CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void* data = nullptr); GLuint CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void* data = nullptr);
GLuint CreateVAOWithVBOSettings(std::function<void()>settings);
char* LoadFileContent(const char *path); char* LoadFileContent(const char *path);
GLuint CompileShader(GLenum shaderType, const char* shaderPath); GLuint CompileShader(GLenum shaderType, const char* shaderPath);
GLuint CreateGPUProgram(const char* vsShaderPath, const char* fsShaderPath); GLuint CreateGPUProgram(const char* vsShaderPath, const char* fsShaderPath);

Loading…
Cancel
Save