Object.cpp 3.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <gl\glew.h>
#include <gl\glfw3.h>
#include <glm\gtc\matrix_transform.hpp>
#include "Object.h"
#include "shader.hpp"
#include "import.h"
#include "Texture.h"

#define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr"
#define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr"
#define TEXTURE_NAME "Textures/dice.tga"


/////////////////////////////////////////////////////////////////////////
// public constructor/destructor

Object::Object(const char* path) :
	fRotationX(0),
	fRotationY(0),
	fRotationZ(0)
{
	m4x4Model = glm::mat4(1.0f);

	glGenVertexArrays(1, &gluiVertexArrayID);

	glGenBuffers(1, &gluiVertexBufferID);
	glGenBuffers(1, &gluiUVBufferID);

	gluiShaderPrgmID = LoadShaders(VERTEX_SHADER, FRAGMENT_SHADER);

	vfVertices = loadData();
	vfUV = loadUV();
	
	processTexture();
	loadMesh2OGL();
}

Object::~Object()
{
	glDeleteBuffers(1, &gluiUVBufferID);
	glDeleteBuffers(1, &gluiVertexBufferID);
	glDeleteVertexArrays(1, &gluiVertexArrayID);
	glDeleteProgram(gluiShaderPrgmID);
}


/////////////////////////////////////////////////////////////////////////
// private functions

void Object::processTexture()
{
	glGenTextures(1, &gluiTextureID);
	glBindTexture(GL_TEXTURE_2D, gluiTextureID);

	TextureTGA tempTex(TEXTURE_NAME);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tempTex.getWidth(), tempTex.getHeight(), 0, GL_BGR, GL_UNSIGNED_BYTE, tempTex.getData().data());

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glGenerateMipmap(GL_TEXTURE_2D);
}

void Object::calcMatrix()
{
	m4x4Model = glm::rotate(m4x4Model, fRotationX, glm::vec3(1, 0, 0));
	m4x4Model = glm::rotate(m4x4Model, fRotationY, glm::vec3(0, 1, 0));
	m4x4Model = glm::rotate(m4x4Model, fRotationZ, glm::vec3(0, 0, 1));
}

void Object::loadMesh2OGL()
{
	// load object to OGL
	glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
	glBufferData(
		GL_ARRAY_BUFFER,
		sizeof(vfVertices) * vfVertices.size(),
		vfVertices.data(),
		GL_STATIC_DRAW
	);

	// load UV to OGL
	glBindBuffer(GL_ARRAY_BUFFER, gluiUVBufferID);
	glBufferData(
		GL_ARRAY_BUFFER,
		sizeof(vfUV) * vfUV.size(),
		vfUV.data(),
		GL_STATIC_DRAW
	);
}


/////////////////////////////////////////////////////////////////////////
// public getter

glm::mat4 Object::getMatrix()
{
	calcMatrix();
	return m4x4Model;
}

GLuint Object::getShader() const
{
	return gluiShaderPrgmID;
}

GLuint Object::getTextureID() const
{
	return gluiTextureID;
}

GLuint Object::getVertexBufferID() const
{
	return gluiVertexBufferID;
}

GLuint Object::getUVBufferID() const
{
	return gluiUVBufferID;
}

int Object::getVertexNumber() const
{
	return 12*3;
}


/////////////////////////////////////////////////////////////////////////
// public functions

void Object::add2x(float value)
{
	fRotationX += value;
}

void Object::add2y(float value)
{
	fRotationY += value;
}

void Object::add2z(float value)
{
	fRotationZ += value;
}