PDA

View Full Version : i dont know whats wrong with this code, for tessellation iam getting blank screen



saikumar
01-05-2016, 10:39 AM
please help me out, thanks in advance

code is too long

object and texture is loading correctly


include <iostream>
#include <SDL.h>
#include <gl\glew.h>
#include "display.h"
#include <glm\glm.hpp>
#include "mesh.h"
#include "texture.h"
#include <include\shader.h>

using namespace std;
using namespace glm;


int main(int, char**)
{
display disp(800, 600, "example");

unsigned int texid = 1;
unsigned int meshID = 1;
mesh objloader;
texture tex;
shader loadshader("./basics");
objloader.meshloading("./untitled2.obj", meshID);
bool y=tex.textureload("./bricks.jpg", texid, GL_RGBA, GL_RGBA, 0, 0);
if (y == false)
{
cout << "texture cannot be loaded" << endl;
}
GLfloat count = 0.11f;
while (!disp.windowclose())
{
disp.clear(0.0f, 0.0f, 0.0f, 1.0f);
tex.BindTexture(texid);
loadshader.bind();
loadshader.shaderupdate(count, glm::vec3(0.01f, 0.01f, 0.01f));
objloader.meshdraw(meshID);
disp.update();
}
return 0;
}


#include "mesh.h"

mesh::mesh()
{
}

void mesh::meshloading(const std::string& filename, const unsigned int mesh_ID)
{
if (m_meshID.find(mesh_ID) != m_meshID.end())
{
glDeleteBuffers(4, m_meshID[mesh_ID].vbo);
glDeleteVertexArrays(1, &m_meshID[mesh_ID].vao);
}
Importer importer;
const aiScene* object = importer.ReadFile(filename, aiProcess_CalcTangentSpace |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType);

if (!object)
{
std::cout << "Unable to laod mesh:" << importer.GetErrorString() << std::endl;
return;
}

for (register unsigned int i = 0; i < object->mNumMeshes; ++i)
{
meshentry(object->mMeshes[i], mesh_ID);
}
importer.FreeScene();
}

void mesh::meshentry(aiMesh* part, unsigned int mesh_ID)
{
GLuint vbo[4];
GLuint vao;
vbo[VERTEX_BUFFER] = NULL;
vbo[TEXCOORD_BUFFER] = NULL;
vbo[NORMAL_BUFFER] = NULL;
vbo[INDEX_BUFFER] = NULL;

glGenVertexArrays(1, &vao);
m_meshID[mesh_ID].vao = vao;
glBindVertexArray(vao);


if (part->HasPositions())
{
std::vector<float> vert;
for (register unsigned int i = 0; i < part->mNumVertices; ++i)
{
vert.push_back(part->mVertices[i].x);
vert.push_back(part->mVertices[i].y);
vert.push_back(part->mVertices[i].z);
}

glGenBuffers(1, &vbo[VERTEX_BUFFER]);
m_meshID[mesh_ID].vbo[VERTEX_BUFFER] = vbo[VERTEX_BUFFER];
glBindBuffer(GL_ARRAY_BUFFER, vbo[VERTEX_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vert[0])*vert.size(), &vert, GL_STATIC_DRAW);

if (gl_error = glGetError())
{
std::cout << "There was an error loading the vertices" << std::endl;
return;
}
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
}

if (part->HasTextureCoords(0))
{
std::vector<float> texCoords;
for (register unsigned int i = 0; i < part->mNumVertices; ++i)
{
texCoords.push_back(part->mTextureCoords[0][i].x);
texCoords.push_back(part->mTextureCoords[0][i].y);
}

glGenBuffers(1, &vbo[TEXCOORD_BUFFER]);
m_meshID[mesh_ID].vbo[TEXCOORD_BUFFER] = vbo[TEXCOORD_BUFFER];
glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, sizeof(texCoords[0])*texCoords.size(), &texCoords, GL_STATIC_DRAW);

if (gl_error = glGetError())
{
std::cout << "There was an error loading the texture coordinates" << std::endl;
return;
}
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(1);
}


if (part->HasNormals())
{
std::vector<float> normals;
for (register unsigned int i = 0; i < part->mNumVertices; ++i)
{
normals.push_back(part->mNormals[i].x);
normals.push_back(part->mNormals[i].y);
normals.push_back(part->mNormals[i].z);
}

glGenBuffers(1, &vbo[NORMAL_BUFFER]);
m_meshID[mesh_ID].vbo[NORMAL_BUFFER] = vbo[NORMAL_BUFFER];
glBindBuffer(GL_ARRAY_BUFFER, vbo[NORMAL_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, sizeof(normals[0])*normals.size(), &normals, GL_STATIC_DRAW);

if (gl_error = glGetError())
{
std::cout << "There was an error loading the normals" << std::endl;
return;
}
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(2);
}

if (part->HasFaces())
{
std::vector<unsigned int> indices;
for (register unsigned int i = 0; i < part->mNumFaces; ++i)
{
indices.push_back(part->mFaces[i].mIndices[0]);
indices.push_back(part->mFaces[i].mIndices[1]);
indices.push_back(part->mFaces[i].mIndices[2]);
indices.push_back(part->mFaces[i].mIndices[3]);
}

glGenBuffers(1, &vbo[INDEX_BUFFER]);
m_meshID[mesh_ID].vbo[INDEX_BUFFER] = vbo[INDEX_BUFFER];
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[INDEX_BUFFER]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices[0])*indices.size(), &indices, GL_STATIC_DRAW);

if (gl_error = glGetError())
{
std::cout << "There was an error loading the indices" << std::endl;
return;
}
elementCount = indices.size();
}
glBindVertexArray(0);
}

bool mesh::bufferunload(unsigned int it)
{
if (m_meshID.find(it) != m_meshID.end())
{
glDeleteBuffers(4, m_meshID[it].vbo);
glDeleteVertexArrays(1, &m_meshID[it].vao);
m_meshID.erase(it);
}
else
{
return false;
}
return true;
}
void mesh::meshdraw(unsigned int mesh_ID)
{
glBindVertexArray(m_meshID[mesh_ID].vao);
glPatchParameteri(GL_PATCH_VERTICES, 4);
glDrawElements(GL_PATCHES, elementCount, GL_UNSIGNED_INT, 0);
GLenum huboError = glGetError();
if (huboError)
{
std::cout << "There was an error drawing: " <<huboError<< std::endl;
}
glBindVertexArray(0);
return;
}
void mesh::allbufferunload()
{
std::map<unsigned int, vertex_ID>::iterator it = m_meshID.begin();
if (m_meshID.size() == 1)
{
bufferunload(it->first);
}
else
{
for (it; it != m_meshID.end(); ++it)
{
bufferunload(it->first);
}
}
m_meshID.clear();
}
mesh::~mesh()
{
allbufferunload();
}


#pragma once

#include <iostream>
#include <glm\glm.hpp>
#include <GL\glew.h>
#include <string>
#include <vector>
#include <assimp\mesh.h>
#include "assimp\Importer.hpp"
#include "assimp\postprocess.h"
#include <assimp\scene.h>
#include <map>


using namespace glm;
using namespace Assimp;

struct vertex_ID
{
GLuint vao;
GLuint vbo[4];
};

class mesh
{
public:
static enum BUFFERS {
VERTEX_BUFFER, TEXCOORD_BUFFER, NORMAL_BUFFER, INDEX_BUFFER
};
mesh();
void meshloading(const std::string& filename, const unsigned int mesh_ID);
bool bufferunload(unsigned int it);
void allbufferunload();
void meshdraw(unsigned int mesh_ID);
~mesh();
protected:
void meshentry(aiMesh* part, unsigned int mesh_ID);
std::map<unsigned int, vertex_ID>m_meshID;
GLenum gl_error;
unsigned int elementCount;
};




#include "texture.h"

texture::texture()
{
FreeImage_Initialise();
}


bool texture::textureload(const string& filename, const unsigned int texID, GLenum image_format, GLint internal_format, GLint level, GLint border)
{
FREE_IMAGE_FORMAT format = FreeImage_GetFileType(filename.c_str(), 0);

FIBITMAP* imagen = FreeImage_Load(format, filename.c_str());

FIBITMAP* temp = imagen;
imagen = FreeImage_ConvertTo32Bits(imagen);
FreeImage_Unload(temp);

int w = FreeImage_GetWidth(imagen);
int h = FreeImage_GetHeight(imagen);
GLuint gl_texID;
//cout << "The size of the image is: " << filename.c_str() << " is " << w << "*" << h << endl; //Some debugging code

GLubyte* texture = new GLubyte[4 * w*h];
char* pixeles = (char*)FreeImage_GetBits(imagen);
//FreeImage loads in BGR format, so you need to swap some bytes(Or use GL_BGR).

for (int j = 0; j<w*h; j++)
{
texture[j * 4 + 0] = pixeles[j * 4 + 2];
texture[j * 4 + 1] = pixeles[j * 4 + 1];
texture[j * 4 + 2] = pixeles[j * 4 + 0];
texture[j * 4 + 3] = pixeles[j * 4 + 3];
//cout<<j<<": "<<int(texture[j*4+0])<<"**"<<texture[j*4+1]<<"**"<<texture[j*4+2]<<"**"<<texture[j*4+3]<<endl;
}

//Now generate the OpenGL texture object
if (m_texID.find(texID) != m_texID.end())
glDeleteTextures(1, &(m_texID[texID]));

glGenTextures(1, &gl_texID);
m_texID[texID] = gl_texID;
glBindTexture(GL_TEXTURE_2D, gl_texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, level, internal_format, w, h, border, image_format, GL_UNSIGNED_BYTE, (GLvoid*)texture);

GLenum huboError = glGetError();
if (huboError)
{
cout << "There was an error loading the texture" << endl;
return false;
}
FreeImage_Unload(imagen);
return true;
}

texture::~texture()
{
FreeImage_DeInitialise();
UnloadAllTextures();
}

bool texture::UnloadTexture(unsigned int it)
{
//if this texture ID mapped, unload it's texture, and remove it from the map
if (m_texID.find(it) != m_texID.end())
{
glDeleteTextures(1, &(m_texID[it]));
m_texID.erase(it);
}
//otherwise, unload failed
else
{
return false;
}
return true;
}

void texture::UnloadAllTextures()
{
std::map<unsigned int, GLuint>::iterator it = m_texID.begin();
if (m_texID.size() == 1)
{
UnloadTexture(it->first);
}
//Unload the textures untill the end of the texture map is found
else
{
for (it; it != m_texID.end(); ++it)
{
UnloadTexture(it->first);
}
}
//clear the texture map
m_texID.clear();
}

bool texture::BindTexture(const unsigned int texID)
{
bool result = true;
//if this texture ID mapped, bind it's texture as current
if (m_texID.find(texID) != m_texID.end())
glBindTexture(GL_TEXTURE_2D, m_texID[texID]);
//otherwise, binding failed
else
result = false;
return result;
}


#pragma once

#include <GL\glew.h>
#include <string>
#include <iostream>
#include <imageinclude\FreeImage.h>
#include <map>

using namespace std;

class texture

{
public:
texture();
bool textureload(const string& filename,
const unsigned int texID, //arbitrary id you will reference the texture by
//does not have to be generated with glGenTextures
GLenum image_format, //format the image is in
GLint internal_format, //format to store the image in
GLint level, //mipmapping level
GLint border);
~texture();
bool texture::UnloadTexture(unsigned int it);
bool texture::BindTexture(const unsigned int texID);
void texture::UnloadAllTextures();
protected:
std::map<unsigned int, GLuint>m_texID;
};




#include <SDL.h>
#include <string>

using namespace std;

class display
{
public:

display(int width, int height, const string& title);
void clear(float r, float g, float b, float a);
void update();
bool windowclose();
~display();
private:
SDL_Window* glwindow;
SDL_GLContext glcontext;
bool close;
};



#include "display.h"
#include <iostream>
#include <gl\glew.h>

display::display(int width, int height, const string& title)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);

glwindow = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
glcontext = SDL_GL_CreateContext(glwindow);

glEnable(GL_DEPTH_TEST);

GLenum res = glewInit();
if (res != GLEW_OK)
{
cerr << "Glew failed to initialize!" << endl;
}

close = false;
}

void display::clear(float r, float g, float b, float a)
{
glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

}

void display::update()
{
SDL_GL_SwapWindow(glwindow);
SDL_Event e;
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
close = true;
}
}

bool display::windowclose()
{
return close;
}

display::~display()
{
SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(glwindow);
SDL_Quit();
}



#include "shader.h"
#include <iostream>
#include <fstream>
#include <glm\gtc\matrix_transform.hpp>
#include <glm\glm.hpp>


using namespace std;
using namespace glm;

shader::shader(const std::string& fileName)
{
m_program = glCreateProgram();

m_shaders[0] = createshader(loadshader(fileName + ".vert"), GL_VERTEX_SHADER);
m_shaders[2] = createshader(loadshader(fileName + ".tesc"), GL_TESS_CONTROL_SHADER);
m_shaders[3] = createshader(loadshader(fileName + ".tese"), GL_TESS_EVALUATION_SHADER);
m_shaders[1] = createshader(loadshader(fileName + ".frag"), GL_FRAGMENT_SHADER);
for (unsigned int i = 0; i < NUM_SHADERS; i++)
glAttachShader(m_program, m_shaders[i]);

glLinkProgram(m_program);
CheckShaderError(m_program, GL_LINK_STATUS, true, "Error linking shader program");

glValidateProgram(m_program);
CheckShaderError(m_program, GL_VALIDATE_STATUS, true, "Invalid shader program");
}

shader::~shader()
{
for (unsigned int i = 0; i < NUM_SHADERS; i++)
{
glDetachShader(m_program, m_shaders[i]);
glDeleteShader(m_shaders[i]);
}

glDeleteProgram(m_program);
}

void shader::bind()
{
glUseProgram(m_program);
}

string shader::loadshader(const string& fileName)
{
std::ifstream file;
file.open((fileName).c_str());

std::string output;
std::string line;

if (file.is_open())
{
while (file.good())
{
getline(file, line);
output.append(line + "\n");
}
}
else
{
std::cerr << "Unable to load shader: " << fileName << std::endl;
}

return output;
}

void shader::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const string& errorMessage)
{
GLint success = 0;
GLchar error[1024] = { 0 };

if (isProgram)
glGetProgramiv(shader, flag, &success);
else
glGetShaderiv(shader, flag, &success);

if (success == GL_FALSE)
{
if (isProgram)
glGetProgramInfoLog(shader, sizeof(error), NULL, error);
else
glGetShaderInfoLog(shader, sizeof(error), NULL, error);

cerr << errorMessage << ": '" << error << "'" << endl;
}
}

GLuint shader::createshader(const string& text, unsigned int type)
{
GLuint shader = glCreateShader(type);

if (shader == 0)
cerr << "Error compiling shader type " << type << endl;

const GLchar* p[1];
p[0] = text.c_str();
GLint lengths[1];
lengths[0] = text.length();

glShaderSource(shader, 1, p, lengths);
glCompileShader(shader);

CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error compiling shader!");

return shader;
}

void shader::shaderupdate(GLfloat count, glm::vec3 offset)
{
mat4 resize = glm::scale(mat4(),glm::vec3(0.5,0.5,0.5));
glm::mat4 view = glm::lookAt(glm::vec3(0.0, 0.0, 3.0), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
glm::mat4 trans = glm::translate(mat4(), offset);
glm::mat4 projection = glm::perspective(45.0f, 800.0f/800.0f, 0.1f, 10.0f);
glm::mat4 anim = glm::rotate(glm::mat4(1.0f), glm::radians(count), vec3(0.0,0.0,1.0));

mat4 ftm = projection*view*trans*anim*resize;
mat4 model = trans*anim*resize;

GLint ftm_location = glGetUniformLocation(m_program, "ftm");
glUniformMatrix4fv(ftm_location, 1, GL_FALSE, &ftm[0][0]);

GLint model_location = glGetUniformLocation(m_program, "Normal");
glUniformMatrix4fv(model_location, 1, GL_FALSE, &model[0][0]);

GLuint lightdirection = glGetUniformLocation(m_program, "lightDirection");
glUniform3f(lightdirection, 0.0f, 0.0f, 1.0f);
}


#pragma once
#include <string>
#include <GL\glew.h>
#include <iostream>
#include <glm\glm.hpp>

class shader
{
public:
shader(const std::string& fileName);
void bind();
void shaderupdate(GLfloat count, glm::vec3 offset);
~shader();


private:
static const unsigned int NUM_SHADERS = 4;
std::string loadshader(const std::string& fileName);
void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string& errorMessage);
GLuint createshader(const std::string& text, unsigned int type);

GLuint m_program;
GLuint m_shaders[NUM_SHADERS];
};






vertex shader

#version 420

in layout(location = 0) vec3 position;
in layout(location = 1) vec2 texCoord;

out vec3 vposition;
out vec2 texCoord0;

void main()
{
texCoord0 = texCoord;
gl_Position = vec4(position,1.0);
}

tcs

#version 420
layout (vertices = 4) out;
void main()
{
if(gl_InvocationID==0)
{
gl_TessLevelOuter[0] = 1.0;
gl_TessLevelOuter[1] = 1.0;
gl_TessLevelOuter[2] = 1.0;
gl_TessLevelOuter[3] = 1.0;
gl_TessLevelInner[0] = 1.0;
gl_TessLevelInner[1] = 1.0;
// and then set tessellation levels
}
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
}

tes

#version 420
layout (quads, equal_spacing, ccw) in;
uniform mat4 ftm;

void main()
{
vec4 a0 = mix(gl_in[0].gl_Position, gl_in[3].gl_Position,gl_TessCoord.x);
vec4 a1 = mix(gl_in[1].gl_Position, gl_in[2].gl_Position,gl_TessCoord.x);
vec4 a = mix(a0,a1,gl_TessCoord.y);
gl_Position= ftm * a;
}

frag shader

#version 420

in vec2 texCoord0;
out vec4 color;
uniform sampler2D sampler;
void main()
{
color= texture2D(sampler, texCoord0);
//color=vec4(1.0,0.3,0.8,1.0);
}