How to convert 3D coordinates into 2D screen space - c++

I know stackoverflow has already some questions and answers about this topic but I cannot deal with them for my specific Problem.
Here is my Code:
int main()
{
std::vector <int> v;
double x_screen, y_screen;
double screen_width = 640.0;
double screen_height = 480.0;
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
double half_screen_width = screen_width / 2;
double half_screen_height = screen_height / 2;
double window_aspect = screen_width / screen_height;
double x_3D, y_3D, z_3D;
for (int i = 0; i < 3; i++){
v.push_back(i);
++v[i];
}
std::cout << "3D Point:" << std::endl;
for (int j = 0; j < v.size(); j++)
std::cout << v[j] << std::endl;
x_3D = v[0] - viewport[0];
y_3D = v[1] - viewport[1];
z_3D = v[2] - viewport[2];
x_screen = (+(x_3D / z_3D)+half_screen_width)*half_screen_width;
y_screen = (-(y_3D / z_3D)+half_screen_height)*half_screen_height;
if (window_aspect > 1.0)
x_screen = x_screen / window_aspect;
else
y_screen = y_screen*window_aspect;
std::cout << "2D Point :" << std::endl;
std::cout << "[" << x_screen << "," << y_screen << "]" << std::endl;
getchar();
getchar();
return 0;
}
Output:
3D Point : [1,2,3]
2D Point : [77040,57360]
The Tutorial where i got the mathematic Background is here :
http://www.flipcode.com/archives/Plotting_A_3D_Point_On_A_2D_Screen.shtml
Can anyone tell me if this result is logical? I am new on this topic and cannot interpret the result.

Related

How can I get the pixels values and coordinates inside bounding box (rect) in c++? is there any method?

I'm trying to get the pixel's values and their x and y coordinate inside the bounding box of objects.
here's my part of the code :
vector<Detector::Object> detected_objects;
for (int i = 0; i < detected_objects.size(); ++i) {
int xmin = detected_objects[i].rect.x;
int ymin = detected_objects[i].rect.y;
int width = detected_objects[i].rect.width;
int height = detected_objects[i].rect.height;
Rect rect(xmin, ymin, width, height); //The upper left coordinates (x, y) and the length (x) and width (y) of the rectangle
cv::rectangle(osrc, rect, Scalar(200, 200, 10), 1, LINE_8, 0); // set rectangle color
// std::cout << "\n coord: \n" << rect;
//std::cout << "# of contour points: " << rect.size() << std::endl;
int xmax = xmin + width;
int ymax = ymin + height;
for (size_t x = xmin; x < xmax; x++)
{
for (size_t y = ymin; y < ymax; y++)
{
}
}
}
Any help is appreciated!
v::Mat BoxValues = cv::Mat::zeros(detected_objects[i].rect.Height, detected_objects[i].rect.Width,type());
BoxValues = osrc(cv::Range(int(detected_objects[i].rect.y()),int(detected_objects[i].rect.y())+detected_objects[i].rect.Height),cv::Range(int(detected_objects[i].rect.x()),int(detected_objects[i].rect.x())+detected_objects[i].rect.Wigth));
Result BoxValues will holds the Inside the Bounding box Pixcel Values.
Regarding the coordinate hope we can get by the cv::Range.
I found a solution!
Here's the part of the code to extract the pixels values and coordinates inside of yolo bounding boxes :
for (i = 0; i < detected_objects.size(); ++i) {
xmin = detected_objects[i].rect.x;
ymin = detected_objects[i].rect.y;
width = detected_objects[i].rect.width;
height = detected_objects[i].rect.height;
xmax = xmin + width;
ymax = ymin + height;
Rect rect(xmin, ymin, width, height); //The upper left coordinates (x, y) and the length (x) and width (y) of the rectangle
cv::rectangle(osrc, rect, Scalar(200, 200, 10), 1, LINE_8, 0); // set rectangle color
std::cout << "rect number:" << rect << "[" << i << "]" << "\n"; // print the number of detected objects
for (x = xmin; x < xmax; x++)
{
for (y = ymin; y < ymax; y++)
{
// pixel_values = osrc.at<Vec3b>(x, y); // for 3 channels output
// Scalar intensity = osrc.at<uchar>(y, x);
Scalar intensity = osrc.at<uchar>(Point(x, y)); //get pixel intensity from one channel (Grayscale image)
std::cout << "Pixel intensity: " << intensity << "[" << i << "]" << "\n";
//allocate the array
int** arr = new int* [xmax];
for (int k = 0; k < xmax; k++)
arr[k] = new int[ymax];
// use the array
cout << "coords: "<<"[" << x << "," << y << "]: \n ";
//cout << arr[x][y] << endl;
//deallocate the array
for (int k = 0; k < xmax; k++)
delete[] arr[k];
delete[] arr;
// std::cout << " \npixel: \n " << Point(x,y);
// pixelValue = GetPixel(hDC, pos.x, pos.y);
// std::cout << " \pixelValue: \n" << pixelValue;
}
}
}

Eigen remove for in plane line(s) intersection method

I'm trying to do a plane line(s) intersection method using Eigen. The code works, but I want to remove the for loop (used to process each point). The goal is to make this method run as fast as possible.
Input: planeNormal, planePoint, linesP0 (3xN), linesp1 (3xN)
Output: intersetcionsPoints (3xN)
Number of points is N
Complete code:
void PlaneLineIntersect(const Vector3f &planeNormal, const Vector3f &planeP0, const Matrix3Xf &linesP0, const Matrix3Xf &linesP1, Matrix3Xf &I, float &t)
{
Matrix3Xf p0l0(linesP0.rows(), linesP0.cols());
// ################################
// ## HOW TO REMOVE THIS FOR (colwise?) ##
for (int i = 0; i < linesP0.cols(); i++)
{
float denom = planeNormal.dot(linesP1.col(i) - linesP0.col(i));
if (denom > 1e-6 || denom < -1e-6) // há uma interseção
{
Vector3f p0l0 = planeP0 - linesP0.col(i);
t = p0l0.dot(planeNormal) / denom;
p0l0 = (t * (linesP1.col(i) - linesP0.col(i))) + linesP0.col(i);
I.col(i) = p0l0;
}
}
}
int main(cli::array<System::String ^> ^args)
{
int n = 1000;
Eigen::Vector3f planeNormal = Eigen::Vector3f(0, 0, -1);
Eigen::Vector3f planeP0 = Eigen::Vector3f(0, 0, 7);
Eigen::Matrix3Xf linesP0 = Matrix3Xf::Random(3, n);
Eigen::Matrix3Xf linesP1 = Matrix3Xf::Random(3, n);
Eigen::Matrix3Xf I = Matrix3Xf::Zero(3, n);
float t;
high_resolution_clock::time_point t1 = high_resolution_clock::now();
#pragma omp parallel for
for (int i = 0; i < 100; i++)
PlaneLineIntersect(planeNormal, planeP0, linesP0, linesP1, I, t);
high_resolution_clock::time_point t2 = high_resolution_clock::now();
double duration = (double) (duration_cast<microseconds>(t2 - t1).count());
cout << "\nTempo total = " << duration / 1000 << " ms" << endl;
cout << "\nTempo / ponto = " << duration / (n * 100) << " us\n" << endl;
system("pause");
return 0;
}

Image processing : luminance weighted 2

I would like to weigh values of luminance on a new image.
I have an image (5px.jpg) of 5 pixels with these luminance :50,100,150,200,250.
I have a vector of coefficient.
I created a new Mat Z which combine luminance of 5px.jpg and the coefficient.
So, my first value of luminance is 50 (lum[0]=50) and I want it to be applied on the 5.1 (coef[0]=5.1) first pixel of my matrix. To do that, I need to weight the 6th pixel with the first and the second value of luminance. In my case,the luminance of my 6th pixel will be 95 because (0.1*50)+(0.9*100)=95
And so on...
But I do not know why my code does not works.
I had already asked a similar question for a vector here and now, I'm try to adapt to an image.
My picture in input :
My output :
#define MPI 3.14159265358979323846264338327950288419716939937510
#define RAD2DEG (180./MPI)
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <cmath>
#include <math.h>
#include <string.h>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("5px.jpg", 1);
if (image.empty())
{
cout << "Couldn't load " << image << endl;
}
else
{
cout << "Image upload, go" << endl;
}
namedWindow("ImageIn", CV_WINDOW_AUTOSIZE);
imshow("ImageIn", image);
Mat imgGrayScale;
cvtColor(image, imgGrayScale, CV_BGR2GRAY);
float *deltaP = new float[imgGrayScale.cols];
float *angle = new float[imgGrayScale.cols];
float *coeff = new float[imgGrayScale.cols];
int col;
for (col = 0; col < imgGrayScale.cols; ++col)
{
//cout << "position x = " << col << endl;
deltaP[col] = imgGrayScale.at<uchar>(0, col);
//cout << "luminance = " << deltaP[col] << endl;
angle[col] = acos(deltaP[col] / 255);
//cout << "angle =" << angle[col] << endl;
coeff[col] = (1 / cos(angle[col]));
cout << "coeff = " << coeff[col] << endl;
}
int width = imgGrayScale.size().width;
int height = imgGrayScale.size().height;
int width2 = width * 5;
int idx_coef = 0;
Mat Z = Mat::zeros(height, width2, CV_8UC1);
//for (int r = 0; r < imgGrayScale.rows; r++)
//{
//cout << "Saut de ligne " << endl << endl << endl;
for (int t = 0; t < imgGrayScale.cols; t++)
{
//cout << "Saut de colonne " << endl;
// Attribue le coeff à une variable
int c = int(coeff[idx_coef]);
//cout << "x" << t << endl;
for (int i = 0; i < c; ++i)
{
Z.at<uchar>(0, c) = imgGrayScale.at<uchar>(0, t);
}
float alpha = fmod(coeff[idx_coef], 1.f);
float beta = 1.f - alpha;
Z.at<uchar>(0, c + 1) = (alpha * imgGrayScale.at<uchar>(0, t) + beta * imgGrayScale.at<uchar>(0, t + 1));
idx_coef++;
coeff[idx_coef] = coeff[idx_coef] - beta;
if (idx_coef >= width - 1)
{
int cc = int(coeff[idx_coef]);
for (int i = 0; i < cc; ++i)
{
Z.at<uchar>(0, c) = imgGrayScale.at<uchar>(0, t);
}
idx_coef = 0;
break;
}
}
//}
namedWindow("m", CV_WINDOW_AUTOSIZE);
imshow("m", Z);
imwrite("lumianacetest.jpg", Z);
int t = waitKey();
if ((char)t == 27)
return 0;
}
You messed up with the indices while accessing the matrix Z. You shoudn't access Z at column c, but you need access the current column (as a vector::push_back would do). So you can keep the current index column in a variable, here idx_z, and increment it every time you access Z
Here your Z is CV_8U, so you lose accuracy since your values are float. You can create Z as CV_32F, and if you need to store values in CV_8U format to save the image, you can convert to CV_8U later, eventually.
The last columns of Z won't be set to any value (so I initialized them with value 0). If you need them to have the last value as in the imgGrayScale, just decomment the relevant part of the code.
Here the code:
#define MPI 3.14159265358979323846264338327950288419716939937510
#define RAD2DEG (180./MPI)
#include <opencv2\opencv.hpp>
#include <vector>
using namespace cv;
using namespace std;
int main()
{
Mat1b imgGrayScale = (Mat1b(2, 5) << 50, 100, 150, 200, 250,
50, 100, 150, 200, 250);
vector<float> deltaP(imgGrayScale.cols);
vector<float> angle(imgGrayScale.cols);
vector<float> coeff(imgGrayScale.cols);
int col;
for (col = 0; col < imgGrayScale.cols; ++col)
{
//cout << "position x = " << col << endl;
deltaP[col] = imgGrayScale.at<uchar>(0, col);
//cout << "luminance = " << deltaP[col] << endl;
angle[col] = acos(deltaP[col] / 255);
//cout << "angle =" << angle[col] << endl;
coeff[col] = (1 / cos(angle[col]));
cout << "coeff = " << coeff[col] << endl;
}
int width = imgGrayScale.size().width;
int height = imgGrayScale.size().height;
int width2 = width * 5;
Mat1f Z(height, width2, 0.f);
for (int r = 0; r < imgGrayScale.rows; r++)
{
int idx_lum = 0;
int idx_coef = 0;
int idx_z = 0;
vector<float> coef = coeff;
// Set all values in Z to the last value in imgGrayScale
Z.row(r) = imgGrayScale(r, imgGrayScale.cols-1);
while (true)
{
int c = int(coef[idx_coef]);
for (int i = 0; i < c; ++i)
{
Z(r, idx_z++) = imgGrayScale(r, idx_lum);
}
float alpha = fmod(coef[idx_coef], 1.f);
float beta = 1.f - alpha;
Z(r, idx_z++) = (alpha * imgGrayScale(r, idx_lum) + beta * imgGrayScale(r, idx_lum + 1));
idx_coef++;
idx_lum++;
coef[idx_coef] = coef[idx_coef] - beta;
if (idx_lum >= imgGrayScale.cols - 1 || idx_coef >= coef.size() - 1)
{
int cc = int(coef[idx_coef]);
for (int i = 0; i < cc; ++i)
{
Z(r, idx_z++) = imgGrayScale(r, idx_lum);
}
idx_coef = 0;
break;
}
}
}
Mat1b ZZ;
Z.convertTo(ZZ, CV_8U);
cout << "Float values:" << endl;
cout << Z << endl << endl;
cout << "Uchar values:" << endl;
cout << ZZ << endl << endl;
namedWindow("m", CV_WINDOW_AUTOSIZE);
imshow("m", Z);
imwrite("lumianacetest.png", ZZ);
waitKey();
return 0;
}

Wierd C++ Problems, OpenGL game

Aight so, I'm in the process of making a simple terrain program, not exactly a game, but hey maybe someday. I'll start with describing the basics of how my program works. I'm using an two-dimension array to store the coordinates of the vertexes which make up a grid of triangles. The x and z values are assigned in the follow code:
//Sets up the array for the vertexes
int e = 0;
int p = 0;
for (int r = 0; r < 10; r++) {
for (int x = 0; x < 5; x++) {
grid[p][0] = x * 2;
grid[p][2] = e;
p++;
}
e += 2;
}
Okay, so the grid array is the one I use for storing the values of all the vertexes in the program, the first argument (the value in the first set of []) is used to choose a point, the second set of [] is the x, y, and z values, the code snippet above only sets values for the x and z coordinates.
The y value, height, is randomised at the start of the program using the following code:
//Random heights for the terrain
if (fir == true) {
for (int h = 0; h < 64; h++) {
grid[h][1] = rand() % 3;
}
fir = false;
}
And the code for drawing the triangles:
for (int q = 0; q < 11; q++) {
a = q * 5;
b = a + 1;
c = a + 5;
glBegin(GL_TRIANGLE_STRIP);
for (int dw = 0; dw < 8; dw++) {
//a
glColor3f(grid[a][1], grid[a][1], grid[a][1]);
glVertex3f(grid[a][0], grid[a][1], grid[a][2]);
//b
glColor3f(grid[b][1], grid[b][1], grid[b][1]);
glVertex3f(grid[b][0], grid[b][1], grid[b][2]);
//c
glColor3f(1, 0, 0);
glVertex3f(grid[c][0], grid[c][1], grid[c][2]);
a = c;
t = b;
b = a + 1;
c = t;
}
glEnd();
}
glutSwapBuffers();
}
The problem I'm having is that I can't make the size of the grid bigger without the program breaking, I would like to be able to change the size of the grid of triangles to whatever I like. when I try to change the for loop which goes through the x values (first code snippet) the program breaks, my movement speed drastically increases, moving the mouse has no effect on the camera rotation. When I do manage to find a set of values to work the grid of triangles is messed up, any points after the 5th row are all set to 0, I'll post the whole program, I don't know if it will run for everyone, but I think the problem is something that requires the whole program to be looked at in order to solve.
I think the problem may be due to variables being shared by different parts of the program, I have looked and looked but can't seem to find any cause. This is my last resort to solving this nightmare of a problem...
main.cpp
/*
*/
#include <iostream>
#include <glut.h>
#include <gl\GL.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <cstdlib>
#include "vector3f.h"
using namespace std;
//Variables
const int WINDOW_WIDTH = 1280;
const int WINDOW_HEIGHT = 720;
const char* WINDOW_TITLE = "Terrain";
const float WALKING_SPEED = 5.0;
const float MOUSE_SENSITIVITY = 0.3;
const float MAX_TILT = 90.0;
float grid[200][3];
float LAST_TIME;
float CURRENT_TIME;
float DELTA_TIME;
bool KEY[256];
int a;
int b;
int c;
int t;
int MOUSE_LAST_X;
int MOUSE_LAST_Y;
int MOUSE_CURRENT_X;
int MOUSE_CURRENT_Y;
int MOUSE_DELTA_X;
int MOUSE_DELTA_Y;
float red;
float green;
float blue;
bool fir = true;
//Object
vector3f CAMERA_POSITION;
vector3f CAMERA_ROTATION;
//Functions
void initialize();
void display();
void reshape(int w, int h);
void keyboardDown(unsigned char key, int x, int y);
void keyboardUp(unsigned char key, int x, int y);
void mouseMove(int x, int y);
void movement();
double degreesToRadians(double degrees);
double dsin(double theta);
double dcos(double theta);
double dtan(double theta);
void display() {
//cout << KEY[' '] << endl;
movement();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Camera transformations
glRotatef(CAMERA_ROTATION.x, 1, 0, 0);
glRotatef(CAMERA_ROTATION.y, 0, 1, 0);
glRotatef(CAMERA_ROTATION.z, 0, 0, 1);
glTranslatef(-CAMERA_POSITION.x, -CAMERA_POSITION.y, CAMERA_POSITION.z);
//Draw the basic triangle
glBegin(GL_TRIANGLES);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, -1.0, -3.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, -3.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(10.0, -1.0, -3.0);
glEnd();
/*
1 0,0,0
2 2,0,0
3 4,0,0
4 6,0,0
5 0,0,2
6 2,0,2
7 4,0,2
8 6,0,2
9 0,0,4
10 2,0,4
11 4,0,4
12 6,0,4
13 0,0,6
14 2,0,6
15 4,0,6
16 6,0,6
*/
/*//Option 1 (2 for loops, four if statements)
for (int p = 0; p < 16; p++) {
for (int x = 0; x < 7; x += 2) {
grid[p][0] = x;
}
grid[p][1] = 0;
if (p < 4) {
grid[p][2] = 0;
}
if (p < 8 && p >= 4) {
grid[p][2] = 2;
}
if (p < 12 && p >= 8) {
grid[p][2] = 4;
}
if (p < 16 && p >= 12) {
grid[p][2] = 6;
}
}
*/
//Option 2 (three for loops)
//Sets up the array for the vertexes
int e = 0;
int p = 0;
for (int r = 0; r < 10; r++) {
for (int x = 0; x < 5; x++) {
grid[p][0] = x * 2;
grid[p][2] = e;
p++;
}
e += 2;
}
//Random heights for the terrain
if (fir == true) {
for (int h = 0; h < 64; h++) {
grid[h][1] = rand() % 3;
}
fir = false;
}
//cout << "point 0: " << grid[0][0] << ", " << grid[0][1] << ", " << grid[0][2] << endl;
//cout << "point 1: " << grid[1][0] << ", " << grid[1][1] << ", " << grid[1][2] << endl;
//cout << "point 2: " << grid[2][0] << ", " << grid[2][1] << ", " << grid[2][2] << endl;
//cout << "point 3: " << grid[3][0] << ", " << grid[3][1] << ", " << grid[3][2] << endl;
//cout << "point 4: " << grid[4][0] << ", " << grid[4][1] << ", " << grid[4][2] << endl;
//cout << "point 5: " << grid[5][0] << ", " << grid[5][1] << ", " << grid[5][2] << endl;
//cout << "point 6: " << grid[6][0] << ", " << grid[6][1] << ", " << grid[6][2] << endl;
//cout << "point 7: " << grid[7][0] << ", " << grid[7][1] << ", " << grid[7][2] << endl;
//cout << "point 8: " << grid[8][0] << ", " << grid[8][1] << ", " << grid[8][2] << endl;
//cout << "point 9: " << grid[9][0] << ", " << grid[9][1] << ", " << grid[9][2] << endl;
//cout << "point 10: " << grid[10][0] << ", " << grid[10][1] << ", " << grid[10][2] << endl;
//cout << "point 11: " << grid[11][0] << ", " << grid[11][1] << ", " << grid[11][2] << endl;
//cout << "point 12: " << grid[12][0] << ", " << grid[12][1] << ", " << grid[12][2] << endl;
//cout << "point 13: " << grid[13][0] << ", " << grid[13][1] << ", " << grid[13][2] << endl;
//cout << "point 14: " << grid[14][0] << ", " << grid[14][1] << ", " << grid[14][2] << endl;
//cout << "point 15: " << grid[15][0] << ", " << grid[15][1] << ", " << grid[15][2] << endl;
for (int q = 0; q < 11; q++) {
a = q * 5;
b = a + 1;
c = a + 5;
glBegin(GL_TRIANGLE_STRIP);
for (int dw = 0; dw < 8; dw++) {
//a
glColor3f(grid[a][1], grid[a][1], grid[a][1]);
glVertex3f(grid[a][0], grid[a][1], grid[a][2]);
//b
glColor3f(grid[b][1], grid[b][1], grid[b][1]);
glVertex3f(grid[b][0], grid[b][1], grid[b][2]);
//c
glColor3f(1, 0, 0);
glVertex3f(grid[c][0], grid[c][1], grid[c][2]);
a = c;
t = b;
b = a + 1;
c = t;
}
glEnd();
}
glutSwapBuffers();
}
void keyboardDown(unsigned char key, int x, int y) {
KEY[key] = true;
}
void keyboardUp(unsigned char key, int x, int y) {
KEY[key] = false;
}
void movement() {
CURRENT_TIME = ((float)glutGet(GLUT_ELAPSED_TIME) / 1000);
DELTA_TIME = CURRENT_TIME - LAST_TIME;
LAST_TIME = CURRENT_TIME;
MOUSE_DELTA_X = MOUSE_CURRENT_X - MOUSE_LAST_X;
MOUSE_DELTA_Y = MOUSE_CURRENT_Y - MOUSE_LAST_Y;
MOUSE_LAST_X = MOUSE_CURRENT_X;
MOUSE_LAST_Y = MOUSE_CURRENT_Y;
CAMERA_ROTATION.y += (float)MOUSE_DELTA_X * MOUSE_SENSITIVITY;
CAMERA_ROTATION.x += (float)MOUSE_DELTA_Y * MOUSE_SENSITIVITY;
if (CAMERA_ROTATION.x > MAX_TILT) {
CAMERA_ROTATION.x = MAX_TILT;
}
if (CAMERA_ROTATION.x < -MAX_TILT) {
CAMERA_ROTATION.x = -MAX_TILT;
}
if (KEY['w'] == true) {
CAMERA_POSITION.x += (WALKING_SPEED * DELTA_TIME) * dsin(CAMERA_ROTATION.y);
CAMERA_POSITION.z += (WALKING_SPEED * DELTA_TIME) * dcos(CAMERA_ROTATION.y);
CAMERA_POSITION.y += (WALKING_SPEED * DELTA_TIME) * dsin(CAMERA_ROTATION.x + 180);
}
if (KEY['s'] == true) {
CAMERA_POSITION.x += (WALKING_SPEED * DELTA_TIME) * dsin(CAMERA_ROTATION.y + 180);
CAMERA_POSITION.z += (WALKING_SPEED * DELTA_TIME) * dcos(CAMERA_ROTATION.y + 180);
CAMERA_POSITION.y += (WALKING_SPEED * DELTA_TIME) * dsin(CAMERA_ROTATION.x);
}
if (KEY['a'] == true) {
CAMERA_POSITION.x += (WALKING_SPEED * DELTA_TIME) * dsin(CAMERA_ROTATION.y + 270);
CAMERA_POSITION.z += (WALKING_SPEED * DELTA_TIME) * dcos(CAMERA_ROTATION.y + 270);
}
if (KEY['d'] == true) {
CAMERA_POSITION.x += (WALKING_SPEED * DELTA_TIME) * dsin(CAMERA_ROTATION.y + 90);
CAMERA_POSITION.z += (WALKING_SPEED * DELTA_TIME) * dcos(CAMERA_ROTATION.y + 90);
}
if (KEY[' '] == true) {
CAMERA_POSITION.y += (WALKING_SPEED * DELTA_TIME);
}
if (KEY['e'] == true) {
exit(1);
}
}
void mouseMove(int x, int y) {
MOUSE_CURRENT_X = x;
MOUSE_CURRENT_Y = y;
}
double degreesToRadians(double degrees){
return degrees * M_PI / 180;
}
double dsin(double theta) {
return sin(degreesToRadians(theta));
}
double dcos(double theta) {
return cos(degreesToRadians(theta));
}
double dtan(double theta) {
return tan(degreesToRadians(theta));
}
void reshape(int w, int h) {
//Stops the ratio from dividing by 0
if (h == 0) {
h = 1;
}
float fRatio = (float)w / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(60, fRatio, 0.1, 1000);
glMatrixMode(GL_MODELVIEW);
}
void initialize() {
glClearColor(0.0, 0.0, 102.0 / 255.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}
int main(int iArgc, char** cArgv) {
//Initialise OpenGL and GLUT
glutInit(&iArgc, cArgv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
//Setup window
glutInitWindowPosition(0, 0);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow(WINDOW_TITLE);
//Setup GLUT callback functions
initialize();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(display);
glutKeyboardFunc(keyboardDown);
glutKeyboardUpFunc(keyboardUp);
glutMotionFunc(mouseMove);
glutPassiveMotionFunc(mouseMove);
glEnable(GL_DEPTH_TEST);
//Enter main loop
glutMainLoop();
return 0;
}
Some quick remarks:
your loops access different number of points!
1st loop (r, x) initialize 50 points
2nd loop (h) initialize 64 points
3rd loop (q, dw) access 59 points
glColor3f values should be between 0 and 1 but, using your grid y values, you get 0, 1 or 2
beware of the gimbal lock problem when trying to rotate your view!

Class 2D vector problems

Alrighty. I have a class Sphere. I am trying to initialize the coordinates for a UVSphere with a given number of subdivisions. The class has a 2D vector verts that looks like this:
std::vector<std::vector<Angel::vec3>> verts;
Where vec3 is essentially a struct of three floats
I have two functions
void setupSphere();
void setupRaw();
Whose implementation looks like this:
void Sphere::setupSphere()
{
float pi = std::atan(1.0f) * 4.0f;
float subdivAngle = 2 * pi / numSubdivide;
std::vector<Angel::vec3> yVectors;
yVectors.reserve(numSubdivide);
for (int i = 0; i < numSubdivide; i ++)
{
float curAngle = subdivAngle * i;
yVectors.push_back(Angel::vec3(std::cos(curAngle), 0.0, -std::sin(curAngle)));
}
int zNumSubdivide = numSubdivide + 4;
float zAngle = 2 * pi / zNumSubdivide;
for (int i = 1; i < zNumSubdivide / 2; i ++)
{
float curAngle = pi / 2 - zAngle * i;
float yCoord = std::sin(curAngle);
float xzScale = std::cos(curAngle);
std::vector<Angel::vec3> curVector;
for (int j = 0; j < numSubdivide; j ++)
{
Angel::vec3 newPoint = yVectors[j] * xzScale;
newPoint.y = yCoord;
curVector.push_back(newPoint);
}
verts.push_back(curVector);
}
std::cout << "Size = " << verts.size() << std::endl;
setupRaw();
//setupTexture();
}
void Sphere::setupRaw()
{
std::cout << "TESTING" << std::endl;
std::cout << "Size = " << verts.size();
}
When I look at the contents of verts at the end of setupSphere() then it looks great, but in setupRaw() it crashes when I try to print the size of verts. What is going on here?