Browse Source

default cstr, use unified pointToViewport/distanceToViewport, reduce texture to 1x1, render() GL error output, resize/pointToViewport/distanceToViewport moved out

pull/2/head
Odilitime 5 years ago
parent
commit
a8650a9992
  1. 169
      src/graphics/components/BoxComponent.cpp

169
src/graphics/components/BoxComponent.cpp

@ -2,26 +2,43 @@ @@ -2,26 +2,43 @@
#include <cmath>
#include <iostream>
BoxComponent::BoxComponent() {
//std::cout << "BoxComponent::BoxComponent - empty" << std::endl;
}
BoxComponent::BoxComponent(const float rawX, const float rawY, const float rawWidth, const float rawHeight, const int passedWindowWidth, const int passedWindowHeight) {
x = rawX;
y = rawY;
//std::cout << "BoxComponent::BoxComponent - data" << std::endl;
//std::cout << "BoxComponent::BoxComponent - window: " << windowWidth << "x" << windowHeight << " passed " << passedWindowWidth << "x" << passedWindowHeight << std::endl;
boundToPage = false;
// set up state
windowWidth = passedWindowWidth;
windowHeight = passedWindowHeight;
width = rawWidth;
height = rawHeight;
for (int py = 0; py < 1024; py++) {
for (int px = 0; px < 1024; px++) {
for (int i = 0; i < 3; i++) {
data[1023 - py][px][i] = 0x88;
}
data[1023 - py][px][3] = 0xff;
}
x = rawX;
y = rawY;
// copy initial state
initialX = x;
initialY = y;
initialWidth = width;
initialHeight = height;
initialWindowWidth = windowWidth;
initialWindowHeight = windowHeight;
for (int i = 0; i < 3; i++) {
data[0][0][i] = 0x88; // set RGB color
}
data[0][0][3] = 0xff; // set alpha
float vx = rawX;
float vy = rawY;
//std::cout << "placing box at " << (int)vx << "x" << (int)vy << " size: " << (int)rawWidth << "x" << (int)rawHeight << std::endl;
float vWidth = rawWidth;
float vHeight = rawHeight;
pointToViewport(vx, vy, windowWidth, windowHeight);
pointToViewport(vx, vy);
/*
// if has rawWidth > 1
@ -40,8 +57,10 @@ BoxComponent::BoxComponent(const float rawX, const float rawY, const float rawWi @@ -40,8 +57,10 @@ BoxComponent::BoxComponent(const float rawX, const float rawY, const float rawWi
*/
// converts 512 to 1 and 1 to 2
//std::cout << "vWidth before: " << (int)vWidth << std::endl;
distanceToViewport(vWidth, vHeight, windowWidth, windowHeight);
distanceToViewport(vWidth, vHeight);
//std::cout << "vWidth after: " << (int)vWidth << std::endl;
//std::cout << "placing box at GL " << (int)vx << "x" << (int)vy << " size: " << (int)(vWidth*10000) << "x" << (int)(vHeight*10000) << std::endl;
vertices[(0 * 5) + 0] = vx;
vertices[(0 * 5) + 1] = vy + vHeight;
@ -73,8 +92,10 @@ BoxComponent::BoxComponent(const float rawX, const float rawY, const float rawWi @@ -73,8 +92,10 @@ BoxComponent::BoxComponent(const float rawX, const float rawY, const float rawWi
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 1024, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glBindVertexArray(0); // protect what we created against any further modification
}
BoxComponent::~BoxComponent() {
@ -85,60 +106,94 @@ BoxComponent::~BoxComponent() { @@ -85,60 +106,94 @@ BoxComponent::~BoxComponent() {
}
void BoxComponent::render() {
//std::cout << "BoxComponent::render" << std::endl;
int glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "BoxComponent::render - start not ok: " << glErr << std::endl;
}
if (verticesDirty) {
//std::cout << "BoxComponent::render - update dirty vertex" << std::endl;
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "BoxComponent::render - glBindBuffer not ok: " << glErr << std::endl;
}
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "BoxComponent::render - glBufferData not ok: " << glErr << std::endl;
}
verticesDirty = false;
}
glBindVertexArray(vertexArrayObject);
glBindTexture(GL_TEXTURE_2D, texture);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
}
void BoxComponent::resize() {
float vx = x;
float vy = y;
float vWidth = width;
float vHeight = height;
pointToViewport(vx, vy, windowWidth, windowHeight);
distanceToViewport(vWidth, vHeight, windowWidth, windowHeight);
vertices[(0 * 5) + 0] = vx;
vertices[(0 * 5) + 1] = vy + vHeight;
vertices[(1 * 5) + 0] = vx + vWidth;
vertices[(1 * 5) + 1] = vy + vHeight;
vertices[(2 * 5) + 0] = vx + vWidth;
vertices[(2 * 5) + 1] = vy;
vertices[(3 * 5) + 0] = vx;
vertices[(3 * 5) + 1] = vy;
verticesDirty = true;
}
void BoxComponent::pointToViewport(float &rawX, float &rawY, const int passedWindowWidth, const int passedWindowHeight) const {
if (rawX < 0) {
rawX += windowWidth;
glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "BoxComponent::render - glBindVertexArray not ok: " << glErr << std::endl;
}
if (rawY < 0) {
rawY += windowHeight;
}
if (rawX > 1) {
rawX /= windowWidth;
glBindTexture(GL_TEXTURE_2D, texture);
glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "BoxComponent::render - glBindTexture not ok: " << glErr << std::endl;
}
if (rawY > 1) {
rawY /= windowHeight;
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "BoxComponent::render - glDrawElements not ok: " << glErr << std::endl;
}
rawX = (rawX * 2) - 1;
rawY = (rawY * 2) - 1;
glBindVertexArray(0);
// can actuall delete vertices here
}
void BoxComponent::distanceToViewport(float &rawX, float &rawY, const int passedWindowWidth, const int passedWindowHeight) const {
if (std::abs(rawX) > 1) {
rawX /= windowWidth;
}
if (std::abs(rawY) > 1) {
rawY /= windowHeight;
/*
void BoxComponent::resize(const int passedWindowWidth, const int passedWindowHeight) {
std::cout << "BoxComponent::resize" << std::endl;
windowWidth = passedWindowWidth;
windowHeight = passedWindowHeight;
if (boundToPage) {
std::cout << "BoxComponent::resize - boundToPage doing nothing" << std::endl;
} else {
// figure out our percentages
// 0 / WW = 0
// 768 / 1024 = 75%
float xPer = initialX / (float)initialWindowWidth;
float yPer = initialY / (float)initialWindowHeight;
// adjust width
float wPer = initialWidth / (float)initialWindowWidth;
//float hPer = initialHeight / (float)initialWindowHeight;
float vx = xPer * windowWidth;
float vy = yPer * windowHeight;
// nah I don't think we want things to stretch
//float vWidth = wPer * windowWidth;
//float vHeight = hPer * windowHeight;
// ugh these just need to be relaid out
//float vWidth = width;
float vWidth = wPer * windowWidth;
float vHeight = height;
//std::cout << "initial: " << initialX << "x" << initialY << " size: " << initialWidth << "x" << initialHeight << " window size: " << initialWindowWidth << "x" << initialWindowHeight << std::endl;
//std::cout << "scaled to: " << (int)vx << "x" << (int)vy << " size: " << (int)vWidth << "x" << (int)vHeight << " window size: " << windowWidth << "x" << windowHeight << std::endl;
pointToViewport(vx, vy);
distanceToViewport(vWidth, vHeight);
vertices[(0 * 5) + 0] = vx;
vertices[(0 * 5) + 1] = vy + vHeight;
vertices[(1 * 5) + 0] = vx + vWidth;
vertices[(1 * 5) + 1] = vy + vHeight;
vertices[(2 * 5) + 0] = vx + vWidth;
vertices[(2 * 5) + 1] = vy;
vertices[(3 * 5) + 0] = vx;
vertices[(3 * 5) + 1] = vy;
verticesDirty = true;
}
rawX *= 2;
rawY *= 2;
}
*/

Loading…
Cancel
Save