Jump to content

  • Log In with Google      Sign In   
  • Create Account


Dark Engineer

Member Since 21 Jan 2013
Offline Last Active Apr 25 2013 12:36 PM
-----

Topics I've Started

How to use glfwSetKeyCallback?

13 April 2013 - 05:26 AM

I have this function

bool Camera::onKeyboard( int Key )
{
	bool Ret = false;

	switch (Key)
	{
	case GLFW_KEY_UP:
		{
			m_pos += (m_target * STEP_SCALE );
			Ret = true;
		} break;
	case GLFW_KEY_DOWN:
		{
			m_pos -= (m_target * STEP_SCALE );
			Ret = true;
		} break;
	case GLFW_KEY_LEFT:
		{
			glm::vec3 left = glm::cross(m_target, m_up );
			glm::normalize(left);
			left *= STEP_SCALE;
			m_pos += left;
			Ret = true;
		} break;
	case GLFW_KEY_RIGHT:
		{
			glm::vec3 right = glm::cross(m_up, m_target);
			glm::normalize(right);
			right *= STEP_SCALE;
			m_pos += right;
			Ret = true;
		} break;
	}
	return Ret;
}

Adn when i try to make function and run it

void GLFWCALL Camera::keyWrapper(int Key)
{
	onKeyboard(Key);
}
glfwSetKeyCallback(pGameCamera->keyWrapper);

it doesn't work. Can someone help me how to do it correct?


[OpenGL] render mesh with shaders

06 April 2013 - 10:01 AM

I have code where i initialize mesh:

#ifndef Mesh_h
#define Mesh_h

#include "Texture.h"
#include "Technique.h"
#include <map>
#include <vector>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <list>
#include <assimp/Importer.hpp>      // C++ importer interface
#include <assimp/scene.h>       // Output data structure
#include <assimp/postprocess.h> // Post processing flags

struct Vertex
{
    glm::vec3 m_pos;
    glm::vec2 m_tex;
    glm::vec3 m_normal;

    Vertex() {}

    Vertex(const glm::vec3& pos, const glm::vec2& tex, const glm::vec3& normal)
    {
        m_pos    = pos;
        m_tex    = tex;
        m_normal = normal;
    }
};

class Mesh : public Technique
{
	bool init();
	bool initFromScene(const aiScene* pScene, const std::string &Filename);
	void initMesh(unsigned int index, const aiMesh* paiMesh);
	bool initMaterials(const aiScene* pScene, const std::string &Filename);
	void clear();

#define INVALID_MATERIAL 0xFFFFFFFF

	struct MeshEntry
	{
		MeshEntry();

		~MeshEntry();

		void init(const std::vector<Vertex> & vertices, const std::vector<unsigned int> & indices);

		GLuint VB;
		GLuint IB;
		unsigned int numIndices;
		unsigned int materialIndex;
	};
	GLint gSamplerLocation;
	GLint WVPLocation;
	std::vector <MeshEntry> m_Entries;
	std::vector <Texture*> m_Textures;


public:
	Mesh();

	~Mesh();
	
	bool loadMesh(const std::string &filename);

	void render();
};
#endif
#include "Mesh.h"


Mesh::Mesh()
{
}

Mesh::~Mesh()
{
	clear();
}

void Mesh::clear()
{
		for(unsigned int i = 0; i < m_Textures.size(); i++)
			delete m_Textures[i];
}

Mesh::MeshEntry::MeshEntry()
{
	VB = GL_INVALID_ENUM;
	IB = GL_INVALID_ENUM;
	numIndices = 0;
	materialIndex = INVALID_MATERIAL;
}

Mesh::MeshEntry::~MeshEntry()
{
	if(VB != GL_INVALID_ENUM)
		glDeleteBuffers(1, &VB);
	if(IB != GL_INVALID_ENUM)
		glDeleteBuffers(1, &IB);
}

void Mesh::MeshEntry::init(const std::vector<Vertex> & vertices, const std::vector<unsigned int> & indices)
{
	numIndices = indices.size();
	glGenBuffers(1, &VB);
	glBindBuffer(GL_ARRAY_BUFFER, VB);
	glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);

	glGenBuffers(1, &IB);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * numIndices, &indices[0], GL_STATIC_DRAW);


}

bool Mesh::loadMesh(const std::string & filename)
{
	clear();
	
	bool Ret = false;
	Assimp::Importer importer;

	const aiScene* pScene = importer.ReadFile(filename.c_str(), aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs);
	if(pScene)
		Ret = initFromScene(pScene, filename);
	else
		std::cout << "Error passing " << filename << " : " << importer.GetErrorString() << std::endl;
	return Ret;
}

bool Mesh::init()
{
	if(!Technique::init())
		return false;
	if(!addShader(Shader::VERTEX_SHADER, "vs.glsl"))
		return false;
	if(!addShader(Shader::FRAGMENT_SHADER, "fs.glsl"))
		return false;
	if(!finalize())
		return false;

	gSamplerLocation = getUniformLocation("gSampler");
	WVPLocation = getUniformLocation("gWVP");

	if(gSamplerLocation == 0xFFFFFFFF || WVPLocation == 0xFFFFFFFF)
		return false;

	return true;
}

bool Mesh::initFromScene(const aiScene* pScene, const std::string &filename)
{
	m_Entries.resize(pScene->mNumMeshes);
	m_Textures.resize(pScene->mNumMaterials);

	for(unsigned int i = 0; i < m_Entries.size(); i++)
	{
		const aiMesh* paiMesh = pScene->mMeshes[i];
		initMesh(i, paiMesh);
	}
		

	return initMaterials(pScene, filename);
}

void Mesh::initMesh(unsigned int index, const aiMesh* paiMesh)
{
	if(!init())
		std::cout << "Failed to initialize shaders" << std::endl;
	m_Entries[index].materialIndex = paiMesh->mMaterialIndex;

	std::vector <Vertex> vertices;
	std::vector <unsigned int> indices;

	const aiVector3D zero3D(0.0f, 0.0f, 0.0f);

	for(unsigned int i = 0; i < paiMesh->mNumVertices; i++)
	{
		const aiVector3D * pPos = &(paiMesh->mVertices[i]);
		const aiVector3D *pNormal = &(paiMesh->mNormals[i]);
		const aiVector3D *pTextCoord = paiMesh->HasTextureCoords(0) ? &(paiMesh->mTextureCoords[0][i]) : &zero3D;

		Vertex v(glm::vec3(pPos->x, pPos->y, pPos->z), glm::vec2(pTextCoord->x, pTextCoord->y), glm::vec3(pNormal->x, pNormal->y, pNormal->z));
		vertices.push_back(v);
	}

	for(unsigned int i = 0; i < paiMesh->mNumFaces; i++)
	{
		const aiFace &face = paiMesh->mFaces[i];
		indices.push_back(face.mIndices[0]);
		indices.push_back(face.mIndices[1]);
		indices.push_back(face.mIndices[2]);
	}

	m_Entries[index].init(vertices, indices);
}

bool Mesh::initMaterials(const aiScene* pScene, const std::string &filename)
{
	 std::string::size_type SlashIndex = filename.find_last_of("/");
    std::string Dir;

    if (SlashIndex == std::string::npos) {
        Dir = ".";
    }
    else if (SlashIndex == 0) {
        Dir = "/";
    }
    else {
        Dir = filename.substr(0, SlashIndex);
    }

    bool Ret = true;

	for(unsigned int i = 0; i < pScene->mNumMaterials; i++)
	{
		const aiMaterial *pMaterial = pScene->mMaterials[i];
		m_Textures[i] = NULL;
		if (pMaterial->GetTextureCount(aiTextureType_DIFFUSE) > 0) 
		{
            aiString path;
				

			if(pMaterial->GetTexture(aiTextureType_DIFFUSE, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS)
			{
				std::string fullPath = Dir + "/" + path.data;

				m_Textures[i] = new Texture(GL_TEXTURE_2D, path.C_Str());
				if(!m_Textures[i]->Load())
				{
					
					std::cout << "Error Loading texture " << path.C_Str() << std::endl;
					delete m_Textures[i];
					m_Textures[i] = NULL;
					Ret = false;
				}
				else
					std::cout << "Loaded texture " << path.C_Str() << std::endl;
				
			}
		}

		if(!m_Textures[i])
		{
			std::cout << "No textures" << std::endl;
			m_Textures[i] = new Texture(GL_TEXTURE_2D, "/white.png");
			Ret = m_Textures[i]->Load();
		}
	}

	return Ret;
}

void Mesh::render()
{
	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);
	glEnableVertexAttribArray(2);

	for(unsigned int i = 0; i < m_Entries.size(); i++)
	{
		glBindBuffer(GL_ARRAY_BUFFER, m_Entries[i].VB);
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
		glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)12);
		glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)20);

		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Entries[i].IB);

		const unsigned int materialIndex = m_Entries[i].materialIndex;

		if( materialIndex < m_Textures.size() && m_Textures[materialIndex])
		{
			m_Textures[materialIndex]->Bind(GL_TEXTURE0);
		}

		glDrawElements(GL_TRIANGLES, m_Entries[i].numIndices, GL_UNSIGNED_INT, 0);
	}

	glDisableVertexAttribArray(0);
	glDisableVertexAttribArray(1);
	glDisableVertexAttribArray(2);
}


but when i try to render it with this methods in render() function it doesn't work, how can i change this code to get it working? Can i make shaders and use it?

Edit// On the screen i see only white color? What to do?


OpenGL mesh with light

02 April 2013 - 09:46 AM

I wrote code for mesh and light but when i'm trying to see this things they don't show. On the screen i see only black color nothing else

My code is here https://github.com/DarkEngineer/Crion-World


OpenGL triangle example

22 January 2013 - 11:41 AM

I have problem with this code because it doesn't show anything in a window, vertex and fragment shaders are loading successfully but glGetError shows something wrong is here. Please someone help me.
#include "loadshader.h"
int main()
{
	glfwInit();
	glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
	glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
	glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
	glfwOpenWindow(800, 600, 0, 0, 0, 0, 0, 0, GLFW_WINDOW);
	glfwSetWindowTitle("Crion World Alpha");
	glewExperimental = GL_TRUE;
	glewInit();
	GLuint vertexBuffer;
	glGenBuffers( 1, &vertexBuffer );
	GLuint vao;
	glGenVertexArrays(1, & vao);
	glBindVertexArray( vao );
	GLuint vbo;
	glGenBuffers( 1, &vbo); //generate 1 buffer
	float vertices[] = {
		0.0f,	0.5f,
		0.5f,	-0.5f,
		-0.5f,	-0.5f
	};
	glBindBuffer( GL_ARRAY_BUFFER, vbo );
	glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_STATIC_DRAW );
	// vertex shader loading
	const string vShaderString = loadShaderFile("SimpleVertexShader.vertexshader").c_str();
	const GLchar * tempVShader = vShaderString.c_str();
	GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER );
	glShaderSource(vertexShader, 1, & tempVShader, NULL);
	glCompileShader( vertexShader ); 
	std::fstream logs;
	logs.open( "logs.txt", ios::in | ios::out | ios::trunc );
	char  vsbuffer[512];
	glGetShaderInfoLog( vertexShader, 512, NULL, vsbuffer );
	logs << vsbuffer;
	//fragment shader loading
	const string fShaderString = loadShaderFile("SimpleFragmentShader.fragmentshader").c_str();
	const GLchar * tempFShader = fShaderString.c_str();
	GLuint fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
	glShaderSource(fragmentShader, 1, & tempFShader, NULL );
	glCompileShader(fragmentShader);
	char  fsbuffer[512];
	glGetShaderInfoLog( fragmentShader, 512, NULL, fsbuffer );
	logs << fsbuffer;
	if(glGetError() != 0)
	{
		logs << "sth errors";
		cout << "sth wrong" << endl;
	}
	logs.close();
	GLuint shaderProgram = glCreateProgram();
	glAttachShader( shaderProgram, vertexShader );
	glAttachShader( shaderProgram, fragmentShader);
	glBindFragDataLocation( shaderProgram, 0, "outColor" );
	glLinkProgram( shaderProgram );
	glUseProgram( shaderProgram );
	GLint posAttrib = glGetAttribLocation( shaderProgram, "position" );
	glEnableVertexAttribArray( posAttrib );
	glVertexAttribPointer( posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0 );
	while( glfwGetWindowParam( GLFW_WINDOW ) )
	{
		glDrawArrays( GL_TRIANGLES, 0, 3);
		glfwSwapBuffers();
	}
	glDeleteProgram( shaderProgram );
	glDeleteShader( vertexShader );
	glDeleteShader( fragmentShader );
	glDeleteBuffers(1,  & vbo );
	glDeleteVertexArrays(1, &vao );
	glfwTerminate();
	return 0;
}

GLSL not working

21 January 2013 - 01:59 PM

I have problem with glsl on windows 8 in Microsoft Visual Studio Express 2012 with Radeon. Even #version 150 is not recognized in code.

 

#version 150
#include <glm\ext.hpp>
#include <glm\glm.hpp>
#include <GL\glew.h>
#include <GL\glut.h>
#include <GL\GL.h>
#include <GL\GLU.h>
#include <GL\glfw.h>

in vec2 position;
out vec4 outColor;

int main()
{
	 vec3 commeo;
	glfwInit();
	glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
	glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
	glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	
	glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
	glfwOpenWindow(800, 600, 0, 0, 0, 0, 0, 0, GLFW_WINDOW);
	glfwSetWindowTitle("Crion World Alpha");
	while( glfwGetWindowParam( GLFW_WINDOW ) )
	{
		glfwSwapBuffers();
	}

	if( glfwGetKey( GLFW_KEY_ESC ) == GLFW_PRESS )
		return 0;
	glewExperimental = GL_TRUE;
	glewInit();
	GLuint vertexBuffer;
	glGenBuffers( 1, &vertexBuffer );

	printf( "%u\n", vertexBuffer);
	 
	float vertices[] = {
		0.0f,	0.5f,
		0.5f,	-0.5f,
		-0.5f,	-0.5f
	};

	GLuint vbo;
	glGenBuffers( 1, &vbo); //generate 1 buffer
	glBindBuffer( GL_ARRAY_BUFFER, vbo );
	glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_STATIC_DRAW );

	GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER );
	glShaderSource( vertexShader, 1, &vertexSource, NULL );
	glCompileShader( vertexShader );
	glfwTerminate();
	return 0;
}

I have errors:

 

	1	IntelliSense: unrecognized preprocessing directive	 /// #version 150
	2	IntelliSense: identifier "in" is undefined	
	4	IntelliSense: identifier "out" is undefined	
	6	IntelliSense: identifier "vec3" is undefined	
	7	IntelliSense: identifier "vertexSource" is undefined	

I have linked:

opengl32.lib
glu32.lib
glut32.lib
glfwdll.lib
glew32d.lib


PARTNERS