Commit 9d35634c authored by Anakin's avatar Anakin
Browse files

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

bugs:
2 triangles are not painted, one is wrong positioned
parent a875820f
...@@ -6,6 +6,23 @@ ...@@ -6,6 +6,23 @@
#include <vector> #include <vector>
#include "Object.h" #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 class OpenGLController
{ {
//////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////
...@@ -35,10 +52,10 @@ private: ...@@ -35,10 +52,10 @@ private:
// IDs ========================== // IDs ==========================
//object data //object data
GLuint gluiVertexArrayID; GLuint gluiVertexArrayID;
GLuint gluiInstanceBufferID;
GLuint gluiVertexBufferID; GLuint gluiVertexBufferID;
//obj color //obj color
GLuint gluiUVBufferID;
GLuint gluiTextureID; GLuint gluiTextureID;
GLuint gluiShaderPrgmID; GLuint gluiShaderPrgmID;
GLuint gluiSamplerID; GLuint gluiSamplerID;
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// Input vertex data, different for all executions of this shader. // Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace; layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV; layout(location = 1) in vec2 vertexUV;
//layout(location = 2) in mat4 mvp;
// Output // Output
out vec2 UV; out vec2 UV;
......
...@@ -7,12 +7,11 @@ ...@@ -7,12 +7,11 @@
#include <glm\gtc\matrix_transform.hpp> #include <glm\gtc\matrix_transform.hpp>
#include "shader.hpp" #include "shader.hpp"
#include "Texture.h" #include "Texture.h"
#include <iostream> #include <iostream>
#define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr" #define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr"
#define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr" #define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr"
//#define TEXTURE_NAME "Textures/texture32R.tga"
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// public constructor/destructor // public constructor/destructor
...@@ -25,7 +24,7 @@ OpenGLController* OpenGLController::getInstance(int oglMajor, int oglMinor) ...@@ -25,7 +24,7 @@ OpenGLController* OpenGLController::getInstance(int oglMajor, int oglMinor)
OpenGLController::~OpenGLController() OpenGLController::~OpenGLController()
{ {
glDeleteBuffers(1, &gluiUVBufferID); glDeleteBuffers(1, &gluiInstanceBufferID);
glDeleteBuffers(1, &gluiVertexBufferID); glDeleteBuffers(1, &gluiVertexBufferID);
glDeleteVertexArrays(1, &gluiVertexArrayID); glDeleteVertexArrays(1, &gluiVertexArrayID);
glDeleteProgram(gluiShaderPrgmID); glDeleteProgram(gluiShaderPrgmID);
...@@ -72,7 +71,7 @@ void OpenGLController::initDefault() ...@@ -72,7 +71,7 @@ void OpenGLController::initDefault()
iAntiAliasingLevel = 4; iAntiAliasingLevel = 4;
gluiUVBufferID = 0; gluiInstanceBufferID = 0;
gluiTextureID = 0; gluiTextureID = 0;
gluiShaderPrgmID = 0; gluiShaderPrgmID = 0;
gluiSamplerID = 0; gluiSamplerID = 0;
...@@ -113,7 +112,16 @@ void OpenGLController::processInit() ...@@ -113,7 +112,16 @@ void OpenGLController::processInit()
glBindVertexArray(gluiVertexArrayID); glBindVertexArray(gluiVertexArrayID);
glGenBuffers(1, &gluiVertexBufferID); 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 // get the painter ready
try try
...@@ -274,24 +282,9 @@ void OpenGLController::updateScene() ...@@ -274,24 +282,9 @@ void OpenGLController::updateScene()
// tell sampler to use texture unit 0 // tell sampler to use texture unit 0
glUniform1i(gluiSamplerID, 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 //draw objects
//// TODO: for all
glDrawArrays(GL_TRIANGLES, 0, vModels.front()->meshSize); glDrawArrays(GL_TRIANGLES, 0, vModels.front()->meshSize);
//close attributes
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glfwSwapBuffers(pWindow); glfwSwapBuffers(pWindow);
glfwPollEvents(); glfwPollEvents();
...@@ -357,48 +350,35 @@ void OpenGLController::loadMsh(const char * path) ...@@ -357,48 +350,35 @@ void OpenGLController::loadMsh(const char * path)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
////TODO: for all ////TODO: for all
std::vector<GLfloat> tempVertex; std::vector<Vertex> tempBufferData;
for (unsigned int i = 0; i < vModels.front()->meshSize; i++) for (unsigned int i = 0; i < vModels.front()->meshSize; i++)
{ {
tempVertex.push_back((GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3]); Vertex tempVertex;
tempVertex.push_back((GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3 + 1]); tempVertex.position[0] = (GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3];
tempVertex.push_back((GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3 + 2]); 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) if (vModels.front()->uv == NULL)
{ {
for (unsigned int i = 0; i < vModels.front()->meshSize; i++) tempVertex.uv[0] = 1.0;
tempUV.push_back(1.0); tempVertex.uv[1] = 1.0;
} }
else else
{
for (unsigned int i = 0; i < vModels.front()->meshSize; i++)
{ {
tempUV.push_back((GLfloat)vModels.front()->uv[vModels.front()->mesh[i] * 2]); tempVertex.uv[0] = (GLfloat)vModels.front()->uv[vModels.front()->mesh[i] * 2];
tempUV.push_back((GLfloat)vModels.front()->uv[vModels.front()->mesh[i] * 2 + 1]); 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( glBufferData(
GL_ARRAY_BUFFER, GL_ARRAY_BUFFER,
sizeof(tempUV) * tempUV.size(), sizeof(tempBufferData) * tempBufferData.size(),
tempUV.data(), tempBufferData.data(),
GL_STATIC_DRAW GL_STATIC_DRAW
); );
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment