C++ error C4700: uninitialized local variable 'i' used - c++

1>------ Build started: Project: RageBotGamingEngine, Configuration: Debug Win32 ------
1> Sprite.cpp
1>c:\users\nha\documents\visual studio 2013\projects\ragebotgamingengine\ragebotgamingengine\sprite.cpp(51): error C4700: uninitialized local variable 'i' used
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Above is what the output logs say.
Below are some related files that could be involved with this problem:
Sprite.cpp
#include "Sprite.h"
#include "Vertex.h"
#include <cstddef>
Sprite::Sprite()
{
_vboID = 0;
}
Sprite::~Sprite()
{
if (_vboID != 0) {
glDeleteBuffers(1, &_vboID);
}
}
void Sprite::init(float x, float y, float width, float height) {
_x = x;
_y = y;
_width = width;
_height = height;
if (_vboID == 0) {
glGenBuffers(1, &_vboID);
}
Vertex vertexData[6];
//First Triangle
vertexData[0].position.x = x + width;
vertexData[0].position.y = y + width;
vertexData[1].position.x = x;
vertexData[1].position.y = y + height;
vertexData[2].position.x = x;
vertexData[2].position.y = y;
//Second Triangle
vertexData[3].position.x = x;
vertexData[3].position.y = y;
vertexData[4].position.x = x + width;
vertexData[4].position.y = y;
vertexData[5].position.x = x + width;
vertexData[5].position.y = y + height;
for (int i; i < 6; i++) {
vertexData[i].color.r = 255;
vertexData[i].color.g = 0;
vertexData[i].color.b = 255;
vertexData[i].color.a = 255;
}
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Sprite::draw() {
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
glEnableVertexAttribArray(0);
//This is position attribute pointer
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
//This is color attribute pointer
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, color));
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
I have tried to find the problem but I'm relatively new to OpenGL, SDL, & C++ & as you can imagine it has been a huge hassle because of the fact that the OpenGL wiki isn't exactly easy to follow & consistent. Just about any help would be very much needed as well as appreciated.
I'm trying to create a Game Engine (obviously I'm pretty much un-skilled & lack the time to develop this project)

for (int i; i < 6; i++)
should be
for (int i=0; i < 6; i++)
You declared variable 'i' but forgot to initialise the variable, so it could have any arbitrary value, not necessarily '0'

Related

glBufferSubData calling multiple times overrides the first data

I am using glBufferSubData to change positions of vertices.
Everything works fine, when I call the code below once.
If I run this whole code two times to add two pointclouds the vertex positions is updated only for the last geometry.
Please look at only following methods of the class, because the rest of the code is just a constructor to initialize the clouds:
void opengl_init()
void opengl_draw()
I think something is wrong with winding to the vao buffer.
Question:
How can I correctly update vertex positions glBufferSubData so that both pointclouds would move?
class pointcloud {
public:
int id = 0;
std::vector<vertex> vertices;
std::vector<unsigned int> indices;//pairs
unsigned int vao, vbo, ibo;
//individual polylines
pointcloud(const std::vector<float>& coord, const std::vector<float>& colors)//, std::vector<Texture> textures
{
vertices.reserve(coord.size());
indices.reserve((coord.size() - 1) * 2);
for (int i = 0; i < coord.size(); i += 3) {
vertex vertex;
vertex.position = glm::vec3(coord[i + 0], coord[i + 1], coord[i + 2]);
vertex.color = glm::vec3(colors[i + 0], colors[i + 1], colors[i + 2]);
vertices.emplace_back(vertex);
}
for (int i = 0; i < (coord.size() / 3) - 1; i++) {
indices.emplace_back(i + 0);
indices.emplace_back(i + 1);
}
// now that we have all the required data, set the vertex buffers and its attribute pointers.
//setup_polyline();
}
//merged polylines
pointcloud(const std::vector<std::vector<float>>& coord, const std::vector<std::vector<float>>& colors)//, std::vector<Texture> textures
{
//reserve memory
int v_count = 0;
int i_count = 0;
for (int i = 0; i < coord.size(); i++) {
v_count += coord[i].size();
i_count += coord[i].size() - 1;
}
vertices.reserve(v_count);
indices.reserve(i_count);
//fill vertics and indices lists
for (int i = 0; i < coord.size(); i++) {
for (int j = 0; j < coord[i].size(); j += 3) {
vertex vertex;
vertex.position = glm::vec3(coord[i][j + 0], coord[i][j + 1], coord[i][j + 2]);
vertex.color = glm::vec3(colors[i][j + 0], colors[i][j + 1], colors[i][j + 2]);
vertices.emplace_back(vertex);
}
}
v_count = 0;
for (int i = 0; i < coord.size(); i++) {
for (int j = 0; j < (coord[i].size() / 3) - 1; j++) {
//std::cout << v_count + j + 0 << " " << v_count + j + 1 << std::endl;
indices.emplace_back(v_count + j + 0);
indices.emplace_back(v_count + j + 1);
}
v_count += (coord[i].size() / 3);
}
//std::cout << vertices.size() << std::endl;
// now that we have all the required data, set the vertex buffers and its attribute pointers.
//setup_polyline();
}
//// initializes all the buffer objects/arrays
void opengl_init(bool draw_dynamic = true, int _id = 0)
{
id = _id + 1;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// init vertex-array and vertex-array-buffer
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//vertex array
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//bind vertex-array-buffer to the vertex-array
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//declare array with data or empty array depending how data will be displayed
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
auto type = !draw_dynamic ? GL_STATIC_DRAW : GL_STREAM_DRAW;
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertex), &vertices[0], type); // target | size | data (poinnting to first element e.g. glm::value_ptr(vertices[0])) | usage
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// set attributes that corresponds to layout id in the vertex shader
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// vertex Positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (void*)0);
// vertex normals
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (void*)offsetof(vertex, color));
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//bind buffers vao | vbo | ibo
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//glBindVertexArray(0);
}
// render the mesh
void opengl_draw(opengl_shaders::shader& shader, bool draw_dynamic = true)
{
//update
//https://learnopengl.com/Advanced-OpenGL/Advanced-Data
if (draw_dynamic) {
for (auto& v : vertices)
v.position.y += 0.001;
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(vertex), &vertices[0]);
}
//draw
glBindVertexArray(vao);
glDrawArrays(GL_POINTS, 0, vertices.size());
//glBindVertexArray(0);
}
void opengl_clear(opengl_shaders::shader& shader) {
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
shader.delete_shader();
}
};
glBufferSubData updates a subset of the data store of a buffer object that is currently bound to the specified target. You must bind the buffer before you can modify its data:
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(vertex), &vertices[0]);
The GL_ARRAY_BUFFER binding is a global state. This binding is maintained until it is changed. As for your code it works for 1 pointcloud, but it doesn't work if you have more than 1 pointcloud and vertex buffer.

How to generate procedural terrain with Perlin Noise in OpenGL?

I need to generate procedural terrain using Noise (using Perlin noise) in OpenGL. Each time the application runs a new terrain, it needs to be generated using a new seed. (Do not use external library.) Is there a method/requirement needed when making a class for noise terrains. What functions/calculation i need to call and in which order ?
PS: I use Visual Studio 2019.
// Copy the array data into a float array, and scale and offset the heights.
mHeightmap.resize(NumRows * NumCols, 0);
for( int i = 0; i < NumRows * NumCols; ++i)
{
mHeightmap[i] = (float)in[i] * HeightScale;
}
// A height for each vertex
{
std::vector<unsigned char> in(NumRows * NumCols);
// Open the file.
std::ifstream inFile;
inFile.open(heightmapName.c_str(), std::ios_base::binary);
if (inFile)
{
// Read the RAW bytes.
inFile.read((char*)&in[0], (std::streamsize)in.size());
// Done with file.
inFile.close();
}
// Copy the array data into a float array, and scale and offset the heights.
mHeightmap.resize(NumRows * NumCols, 0);
for( int i = 0; i < NumRows * NumCols; ++i)
{
mHeightmap[i] = (float)in[i] * HeightScale;
}
void Terrain::CreateVAO()
{
std::vector<GLfloat> vertices;
vertices.reserve(NumCols * NumRows * 8);
float invTwoDX = 1.0f / (2.0f * CellSpacing);
float invTwoDZ = 1.0f / (2.0f * CellSpacing);
//vertices
for ( int z = 0; z < NumRows; z++)
{
for ( int x = 0; x < NumCols; x++)
{
//vertex data
int i = z * NumCols + x;
vertices.push_back((float)x*CellSpacing);
vertices.push_back(mHeightmap[i]);
vertices.push_back((float)z * CellSpacing);
//normal data
glm::vec3 _N = { 0.0f,1.0f, 0.0f };
if(z >= 1 && z < NumRows -1 && x >= 1 && z < NumCols - 1)
{
float t = mHeightmap[(z - 1) * NumCols + x];
float b = mHeightmap[(z + 1) * NumCols + x];
float l = mHeightmap[z * NumCols + x - 1];
float r = mHeightmap[z * NumCols + x + 1];
glm::vec3 tanZ(0.0f, (b - t) * invTwoDZ, 1.0f);
glm::vec3 tanX(1.0f, (r - l) * invTwoDX, 0.0f);
glm::vec3 _C, _N;
_C = glm::cross(tanZ, tanX);
_N = glm::normalize(_C);
}
vertices.push_back(_N.x);
vertices.push_back(_N.y);
vertices.push_back(_N.z);
vertices.push_back((float)x);
vertices.push_back((float)z);
}
}
std::vector<GLuint> indices;
vertices.reserve((NumCols-1)*(NumRows -1)*6);
//indices
for ( int z = 0; z < NumRows-1; z++)
{
for ( int x = 0; x < NumCols-1; x++)
{
GLint a = z * NumCols + x;
GLint b = (z +1) * NumCols + x;
GLint c = z * NumCols + (x+1);
GLint d = (z+1) * NumCols + (x+1);
indices.push_back(c);
indices.push_back(a);
indices.push_back(b);
indices.push_back(c);
indices.push_back(b);
indices.push_back(d);
}
}
indexcount = indices.size();
GLuint VBO, EBO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), GL_STATIC_DRAW);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(GLfloat), vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
0,
3,
GL_FLOAT,
GL_FALSE,
8 * sizeof(GLfloat), //Strude of the single vertex(pos)
(GLvoid*)0); //Offset from beginning of Vertex
glEnableVertexAttribArray(0);
glVertexAttribPointer(
1,
3,
GL_FLOAT,
GL_FALSE,
8 * sizeof(GLfloat), //Strude of the single vertex(pos+color)
(GLvoid*)(3 * sizeof(GLfloat))); //Offset from beginning of Vertex
glEnableVertexAttribArray(1);
glVertexAttribPointer(
2,
2, //2 float component for coordinates
GL_FLOAT,
GL_FALSE,
8 * sizeof(GLfloat), //Strude of the single vertex(pos+color+texture)
(GLvoid*)(6 * sizeof(GLfloat)));//Offset from beginning of Vertex
glEnableVertexAttribArray(2);
I'm not sure if I see usage of Perlin noise in your code. Try this lightweight, easy to integrate library:
https://github.com/Auburn/FastNoise which has Perlin and tons of other useful stuff like a visualizer.
Usage is as simple as
noise.GetNoise((float)x, (float)y); which you can plug into your height function

How to draw a cube in OpenGL using IBOs and VBOs

I've got a basic opengl setup working with shaders, IBOs, and VBOs. And can do simple things such as drawing a 2D rectangle or square or anything of that sense. However, when I implement a vbo and an ibo containing data for a cube nothing shows up on the screen. I've created a cube class which stores the vertex and indices for the cube.
Note: I have not implemented a projection matrix in the shader yet, I am waiting till I get the distorted 3d cube on the screen.
Here's my code-
main.cpp
#include <iostream>
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>
#include <glm\gtc\type_ptr.hpp>
#include "shader.h"
#include "cube.h"
#undef main
int main() {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* mainWindow = SDL_CreateWindow("game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 512, 512, SDL_WINDOW_OPENGL);
SDL_GLContext mainContext = SDL_GL_CreateContext(mainWindow);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
glewExperimental = GL_TRUE;
glewInit();
GLuint VBO, VAO, IBO;
cube block1(0, 0, -2.5, .2);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*36, block1.indices, GL_STATIC_DRAW);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 24, block1.vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
shader sqreProg("shaders/shader.vert", "shaders/shader.frag");
bool running = true;
while (running) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
sqreProg.useShader();
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glUseProgram(0);
SDL_GL_SwapWindow(mainWindow);
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = false;
break;
}
}
}
SDL_GL_DeleteContext(mainContext);
SDL_DestroyWindow(mainWindow);
SDL_Quit();
}
Shader.h
#pragma once
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <string>
#include <fstream>
#include <streambuf>
#include <iostream>
class shader {
public:
shader(const char* vShader, const char* fShader) {
vShaderCode = readFile(vShader);
fShaderCode = readFile(fShader);
shaderProgramID = glCreateProgram();
addShader(vShaderCode.c_str(), GL_VERTEX_SHADER);
addShader(fShaderCode.c_str(), GL_FRAGMENT_SHADER);
glLinkProgram(shaderProgramID);
}
void useShader() { glUseProgram(shaderProgramID); }
private:
GLuint shaderProgramID;
std::string vShaderCode;
std::string fShaderCode;
std::string readFile(const char* fileName) {
std::ifstream file(fileName);
std::string fileContents;
fileContents.assign((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return fileContents;
}
void addShader(const char* code, GLenum type) {
GLuint shaderID = glCreateShader(type);
const GLchar* theCode[1];
theCode[0] = code;
GLint codeLength[1];
codeLength[0] = strlen(code);
glShaderSource(shaderID, 1, theCode, codeLength);
glCompileShader(shaderID);
GLint result = 0;
GLchar eLog[1024] = { 0 };
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &result);
if (!result) {
glGetShaderInfoLog(shaderID, 1024, NULL, eLog);
fprintf(stderr, "error compile the %d shader: '%s'\n", type, eLog);
return;
}
glAttachShader(shaderProgramID, shaderID);
}
};
cube.h
#pragma once
#include <GL\glew.h>
class cube {
public:
cube() {
x = 0;
y = 0;
z = 0;
width = 0;
vertices = 0;
indices = 0;
}
cube(GLfloat X, GLfloat Y, GLfloat Z, float w) {
x = X;
y = Y;
z = Z;
width = w;
vertices = new GLfloat[24];
//1
vertices[0] = x;
vertices[1] = y;
vertices[2] = z;
//2
vertices[3] = x + width;
vertices[4] = y;
vertices[5] = z;
//3
vertices[6] = x;
vertices[7] = y - width;
vertices[8] = z;
//4
vertices[9] = x + width;
vertices[10] = y - width;
vertices[11] = z;
//5
vertices[12] = x;
vertices[13] = y;
vertices[14] = z - width;
//6
vertices[15] = x + width;
vertices[16] = y;
vertices[17] = z - width;
//7
vertices[18] = x;
vertices[19] = y - width;
vertices[20] = z - width;
//8
vertices[21] = x + width;
vertices[22] = y - width;
vertices[23] = z - width;
indices = new unsigned int[36];
//0
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
//1
indices[3] = 1;
indices[4] = 2;
indices[5] = 3;
//2
indices[6] = 4;
indices[7] = 5;
indices[8] = 6;
//3
indices[9] = 5;
indices[10] = 6;
indices[11] = 7;
//4
indices[12] = 4;
indices[13] = 0;
indices[14] = 1;
//5
indices[15] = 4;
indices[16] = 5;
indices[17] = 1;
//6
indices[18] = 6;
indices[19] = 2;
indices[20] = 3;
//7
indices[21] = 6;
indices[22] = 7;
indices[23] = 3;
//8
indices[24] = 1;
indices[25] = 5;
indices[26] = 3;
//9
indices[27] = 5;
indices[28] = 7;
indices[29] = 3;
//10
indices[30] = 4;
indices[31] = 0;
indices[32] = 2;
//11
indices[33] = 4;
indices[34] = 6;
indices[35] = 2;
}
GLfloat* vertices;
unsigned int* indices;
private:
GLfloat x;
GLfloat y;
GLfloat z;
float width;
};

3d point cloud of an object using Kinect

I've got a point cloud of an object using kinect and I want to create a 3d representation of the point cloud and save the image. I'm not sure how I can approach this task. I want it to be able to detect an object and derive the point cloud of the object. For example for 3d scanning purposes, it would be able to 3d scan the object from all directions and then get a point cloud from it.
main.cpp
#include "main.h"
#include "glut.h"
#include <cmath>
#include <cstdio>
#include <Windows.h>
#include <Ole2.h>
#include <NuiApi.h>
#include <NuiImageCamera.h>
#include <NuiSensor.h>
// OpenGL Variables
long depthToRgbMap[width*height*2];
// We'll be using buffer objects to store the kinect point cloud
GLuint vboId;
GLuint cboId;
// Kinect variables
HANDLE depthStream;
HANDLE rgbStream;
INuiSensor* sensor;
bool initKinect() {
// Get a working kinect sensor
int numSensors;
if (NuiGetSensorCount(&numSensors) < 0 || numSensors < 1) return false;
if (NuiCreateSensorByIndex(0, &sensor) < 0) return false;
// Initialize sensor
sensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH | NUI_INITIALIZE_FLAG_USES_COLOR);
sensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH, // Depth camera or rgb camera?
NUI_IMAGE_RESOLUTION_640x480, // Image resolution
0, // Image stream flags, e.g. near mode
2, // Number of frames to buffer
NULL, // Event handle
&depthStream);
sensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, // Depth camera or rgb camera?
NUI_IMAGE_RESOLUTION_640x480, // Image resolution
0, // Image stream flags, e.g. near mode
2, // Number of frames to buffer
NULL, // Event handle
&rgbStream);
return sensor;
}
void getDepthData(GLubyte* dest) {
float* fdest = (float*) dest;
long* depth2rgb = (long*) depthToRgbMap;
NUI_IMAGE_FRAME imageFrame;
NUI_LOCKED_RECT LockedRect;
if (sensor->NuiImageStreamGetNextFrame(depthStream, 0, &imageFrame) < 0) return;
INuiFrameTexture* texture = imageFrame.pFrameTexture;
texture->LockRect(0, &LockedRect, NULL, 0);
if (LockedRect.Pitch != 0) {
const USHORT* curr = (const USHORT*) LockedRect.pBits;
for (int j = 0; j < height; ++j) {
for (int i = 0; i < width; ++i) {
// Get depth of pixel in millimeters
USHORT depth = NuiDepthPixelToDepth(*curr++);
// Store coordinates of the point corresponding to this pixel
Vector4 pos = NuiTransformDepthImageToSkeleton(i, j, depth<<3, NUI_IMAGE_RESOLUTION_640x480);
*fdest++ = pos.x/pos.w;
*fdest++ = pos.y/pos.w;
*fdest++ = pos.z/pos.w;
// Store the index into the color array corresponding to this pixel
NuiImageGetColorPixelCoordinatesFromDepthPixelAtResolution(
NUI_IMAGE_RESOLUTION_640x480, NUI_IMAGE_RESOLUTION_640x480, NULL,
i, j, depth<<3, depth2rgb, depth2rgb+1);
depth2rgb += 2;
}
}
}
texture->UnlockRect(0);
sensor->NuiImageStreamReleaseFrame(depthStream, &imageFrame);
}
void getRgbData(GLubyte* dest) {
float* fdest = (float*) dest;
long* depth2rgb = (long*) depthToRgbMap;
NUI_IMAGE_FRAME imageFrame;
NUI_LOCKED_RECT LockedRect;
if (sensor->NuiImageStreamGetNextFrame(rgbStream, 0, &imageFrame) < 0) return;
INuiFrameTexture* texture = imageFrame.pFrameTexture;
texture->LockRect(0, &LockedRect, NULL, 0);
if (LockedRect.Pitch != 0) {
const BYTE* start = (const BYTE*) LockedRect.pBits;
for (int j = 0; j < height; ++j) {
for (int i = 0; i < width; ++i) {
// Determine rgb color for each depth pixel
long x = *depth2rgb++;
long y = *depth2rgb++;
// If out of bounds, then don't color it at all
if (x < 0 || y < 0 || x > width || y > height) {
for (int n = 0; n < 3; ++n) *(fdest++) = 0.0f;
}
else {
const BYTE* curr = start + (x + width*y)*4;
for (int n = 0; n < 3; ++n) *(fdest++) = curr[2-n]/255.0f;
}
}
}
}
texture->UnlockRect(0);
sensor->NuiImageStreamReleaseFrame(rgbStream, &imageFrame);
}
void getKinectData() {
GLubyte* ptr;
glBindBuffer(GL_ARRAY_BUFFER, vboId);
ptr = (GLubyte*) glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
if (ptr) {
getDepthData(ptr);
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, cboId);
ptr = (GLubyte*) glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
if (ptr) {
getRgbData(ptr);
}
glUnmapBuffer(GL_ARRAY_BUFFER);
}
void rotateCamera() {
static double angle = 0.;
static double radius = 3.;
double x = radius*sin(angle);
double z = radius*(1-cos(angle)) - radius/2;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(x,0,z,0,0,radius/2,0,1,0);
angle += 0.05;
}
void drawKinectData() {
getKinectData();
rotateCamera();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glVertexPointer(3, GL_FLOAT, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, cboId);
glColorPointer(3, GL_FLOAT, 0, NULL);
glPointSize(1.f);
glDrawArrays(GL_POINTS, 0, width*height);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
int main(int argc, char* argv[]) {
if (!init(argc, argv)) return 1;
if (!initKinect()) return 1;
// OpenGL setup
glClearColor(0,0,0,0);
glClearDepth(1.0f);
// Set up array buffers
const int dataSize = width*height * 3 * 4;
glGenBuffers(1, &vboId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, dataSize, 0, GL_DYNAMIC_DRAW);
glGenBuffers(1, &cboId);
glBindBuffer(GL_ARRAY_BUFFER, cboId);
glBufferData(GL_ARRAY_BUFFER, dataSize, 0, GL_DYNAMIC_DRAW);
// Camera setup
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, width /(GLdouble) height, 0.1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,0,0,0,1,0,1,0);
// Main loop
execute();
return 0;
}

OpenGL (core profile) model's triangles wrong rendering

Here is an issue of my project, please look at the screenshots:
problems
original(correct)
My object is build in a wrong way. The vertices are not connected properly.
I suspect that it has something to do with the indices of the model. Anyway here is the code that constructs the mesh for me:
mesh model::processMesh(aiMesh * mesh_, const aiScene * scene)
{
std::vector<vertex> vertices;
std::vector<GLuint> indices;
std::vector<texture> textures;
//vertices
for (GLuint i = 0; i < mesh_->mNumVertices; i++)
{
vertex vert;
glm::vec3 vector;
//positions
vector.x = mesh_->mVertices[i].x;
vector.y = mesh_->mVertices[i].y;
vector.z = mesh_->mVertices[i].z;
vert.position = vector;
//normals
vector.x = mesh_->mNormals[i].x;
vector.y = mesh_->mNormals[i].y;
vector.z = mesh_->mNormals[i].z;
vert.normal = vector;
//texture coords
if (mesh_->mTextureCoords[0])
{
glm::vec2 vector_;
vector_.x = mesh_->mTextureCoords[0][i].x;
vector_.y = mesh_->mTextureCoords[0][i].y;
vert.texCoords = vector_;
}
else vert.texCoords = glm::vec2(0.0f, 0.0f);
vertices.push_back(vert);
}
//indices
for (GLuint i = 0; i < mesh_->mNumFaces; i++)
{
aiFace face = mesh_->mFaces[i];
for (GLuint j = 0; j < mesh_->mNumFaces; j++) indices.push_back(face.mIndices[j]);
}
//textures
if (mesh_->mMaterialIndex >= 0)
{
aiMaterial* material = scene->mMaterials[mesh_->mMaterialIndex];
//diffuse
std::vector<texture> diffuseMaps = this->loadMaterialTextures(material, aiTextureType_DIFFUSE, TEX_DIFF_NAME);
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
//specular
std::vector<texture> specularMaps = this->loadMaterialTextures(material, aiTextureType_SPECULAR, TEX_SPEC_NAME);
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
}
return mesh(vertices, indices, textures);
}
In this function I set up all objects:
void mesh::setupMesh()
{
//buffers
glGenVertexArrays(1, &this->vao);
glGenBuffers(1, &this->vbo);
glGenBuffers(1, &this->ebo);
glBindVertexArray(this->vao);
glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(vertex), &this->vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(GLuint), &this->indices[0], GL_STATIC_DRAW);
//attributes
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)offsetof(vertex, normal));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), (GLvoid*)offsetof(vertex, texCoords));
//unbind
glBindVertexArray(0);
}
And here is the render
void mesh::draw(shader* shader)
{
GLuint tex_diffNumber = 1;
GLuint tex_specNumber = 1;
for (GLuint i = 0; i < this->textures.size() ; i++)
{
//load target texture
glActiveTexture(GL_TEXTURE0 + i);
std::stringstream sstream;
std::string number;
std::string name = this->textures[i].type;
if (name == TEX_DIFF_NAME)
sstream << tex_diffNumber++;
else if (name == TEX_SPEC_NAME)
sstream << tex_specNumber++;
number = sstream.str();
glBindTexture(GL_TEXTURE_2D, this->textures[i].id);
glUniform1i(glGetUniformLocation(shader->shaderProgID, (name + number).c_str()), i);
}
//set shininess
//glUniform1f(glGetUniformLocation(shader->shaderProgID, "material.shininess"), 16.0f);
//draw
glBindVertexArray(this->vao);
glDrawElements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
//release
for (GLuint i = 0; i < this->textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
The question is why does my model have wrong built triangles?
Ask any additional info.
Thank you for help in advance, guys!
for (GLuint i = 0; i < mesh_->mNumFaces; i++)
{
aiFace face = mesh_->mFaces[i];
for (GLuint j = 0; j < mesh_->mNumFaces; j++)
indices.push_back(face.mIndices[j]);
}
This looks wrong to me. I don't think the inner loop should run over all faces again. Instead it should iterate over all indices in the face:
for (GLuint i = 0; i < mesh_->mNumFaces; i++)
{
aiFace face = mesh_->mFaces[i];
for (GLuint j = 0; j < face->mNumIndices; j++)
indices.push_back(face.mIndices[j]);
}
Note, that the original version was reading outside of the available memory, since the loop was most probably going till j = 5 (six faces), but there are only faces with a maximum of four indices. If one would have attached a debugger and stepped through it, this should have been easily visible.