pass data (xyz, uv) with 1 buffer to shader,

bugs:
2 triangles are not painted, one is wrong positioned
This commit is contained in:
Anakin 2016-11-01 11:51:04 +01:00
parent a875820f48
commit 9d35634c0c
3 changed files with 50 additions and 52 deletions

View File

@ -6,6 +6,23 @@
#include <vector>
#include "Object.h"
#define VERTEX_INDEX_XYZ 0
#define VERTEX_INDEX_UV 1
#define VERTEX_COMPONENTS_XYZ 3
#define VERTEX_COMPONENTS_UV 4
#define VERTEX_SIZE_XYZ (sizeof(float) * VERTEX_COMPONENTS_XYZ)
#define VERTEX_SIZE_UV (sizeof(float) * VERTEX_COMPONENTS_UV)
#define VERTEX_OFFSET_XYZ 0
#define VERTEX_OFFSET_UV (VERTEX_SIZE_XYZ)
struct Vertex {
GLfloat position[3];
GLfloat uv[2];
};
class OpenGLController
{
////////////////////////////////////////////////////////////////////////////////////////////
@ -35,10 +52,10 @@ private:
// IDs ==========================
//object data
GLuint gluiVertexArrayID;
GLuint gluiInstanceBufferID;
GLuint gluiVertexBufferID;
//obj color
GLuint gluiUVBufferID;
GLuint gluiTextureID;
GLuint gluiShaderPrgmID;
GLuint gluiSamplerID;

View File

@ -3,6 +3,7 @@
// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV;
//layout(location = 2) in mat4 mvp;
// Output
out vec2 UV;

View File

@ -7,12 +7,11 @@
#include <glm\gtc\matrix_transform.hpp>
#include "shader.hpp"
#include "Texture.h"
#include <iostream>
#define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr"
#define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr"
//#define TEXTURE_NAME "Textures/texture32R.tga"
/////////////////////////////////////////////////////////////////////////
// public constructor/destructor
@ -25,7 +24,7 @@ OpenGLController* OpenGLController::getInstance(int oglMajor, int oglMinor)
OpenGLController::~OpenGLController()
{
glDeleteBuffers(1, &gluiUVBufferID);
glDeleteBuffers(1, &gluiInstanceBufferID);
glDeleteBuffers(1, &gluiVertexBufferID);
glDeleteVertexArrays(1, &gluiVertexArrayID);
glDeleteProgram(gluiShaderPrgmID);
@ -72,7 +71,7 @@ void OpenGLController::initDefault()
iAntiAliasingLevel = 4;
gluiUVBufferID = 0;
gluiInstanceBufferID = 0;
gluiTextureID = 0;
gluiShaderPrgmID = 0;
gluiSamplerID = 0;
@ -113,7 +112,16 @@ void OpenGLController::processInit()
glBindVertexArray(gluiVertexArrayID);
glGenBuffers(1, &gluiVertexBufferID);
glGenBuffers(1, &gluiUVBufferID);
glGenBuffers(1, &gluiInstanceBufferID);
// open attribute position
glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
glVertexAttribPointer(VERTEX_INDEX_XYZ, VERTEX_COMPONENTS_XYZ, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)VERTEX_OFFSET_XYZ);
glVertexAttribPointer(VERTEX_INDEX_UV, VERTEX_COMPONENTS_UV, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)VERTEX_OFFSET_UV);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
// get the painter ready
try
@ -274,24 +282,9 @@ void OpenGLController::updateScene()
// tell sampler to use texture unit 0
glUniform1i(gluiSamplerID, 0);
// open attribute position
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
// open attribute uv
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, gluiUVBufferID);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
//draw objects
//// TODO: for all
glDrawArrays(GL_TRIANGLES, 0, vModels.front()->meshSize);
//close attributes
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glfwSwapBuffers(pWindow);
glfwPollEvents();
@ -357,48 +350,35 @@ void OpenGLController::loadMsh(const char * path)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
////TODO: for all
std::vector<GLfloat> tempVertex;
std::vector<Vertex> tempBufferData;
for (unsigned int i = 0; i < vModels.front()->meshSize; i++)
{
tempVertex.push_back((GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3]);
tempVertex.push_back((GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3 + 1]);
tempVertex.push_back((GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3 + 2]);
}
Vertex tempVertex;
tempVertex.position[0] = (GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3];
tempVertex.position[1] = (GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3 + 1];
tempVertex.position[2] = (GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3 + 2];
glBufferData(
GL_ARRAY_BUFFER,
sizeof(tempVertex) * tempVertex.size(),
tempVertex.data(),
GL_STATIC_DRAW
);
////TODO: for all
std::vector<GLfloat> tempUV;
if (vModels.front()->uv == NULL)
{
for (unsigned int i = 0; i < vModels.front()->meshSize; i++)
tempUV.push_back(1.0);
}
else
{
for (unsigned int i = 0; i < vModels.front()->meshSize; i++)
if (vModels.front()->uv == NULL)
{
tempUV.push_back((GLfloat)vModels.front()->uv[vModels.front()->mesh[i] * 2]);
tempUV.push_back((GLfloat)vModels.front()->uv[vModels.front()->mesh[i] * 2 + 1]);
tempVertex.uv[0] = 1.0;
tempVertex.uv[1] = 1.0;
}
else
{
tempVertex.uv[0] = (GLfloat)vModels.front()->uv[vModels.front()->mesh[i] * 2];
tempVertex.uv[1] = (GLfloat)vModels.front()->uv[vModels.front()->mesh[i] * 2 + 1];
}
tempBufferData.push_back(tempVertex);
}
glBindBuffer(GL_ARRAY_BUFFER, gluiUVBufferID);
glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(tempUV) * tempUV.size(),
tempUV.data(),
sizeof(tempBufferData) * tempBufferData.size(),
tempBufferData.data(),
GL_STATIC_DRAW
);
}