Jump to content
  • Advertisement
Sign in to follow this  
EddieV223

OpenGL Please help 2 days stuck, simple openGL app doesn't render anything!

This topic is 2063 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is a link to my vs11 express project.

http://www.filehosting.org/file/details/421749/OpenGLSpaceShip.7z

 

All it does is render the clear color it doesn't render my objects, I have tried loading from my obj loader and also just using vertex and index buffer from a working tutorial, neither work.

 

It is really just 1 Graphics class so it's not complicated code.  For 2 days I struggle with this, since I am new to openGL I don't know what is wrong and have been trying to fix it for days now.  

 

Here is the graphics class.

 

// header

#pragma once

#include "GL\glew.h"
#include "GL\freeglut.h"

#define GLM_FORCE_RADIANS
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>

class Graphics
{
public:
	Graphics();
	~Graphics();

	bool Init(int argc, char* argv[]);

	bool InitWindow(int argc, char* argv[]);

	void StartGlutLoop();

	bool LoadGraphics();
	void UpdateGraphics();
	void RenderGraphics();
	void CleanGraphics();

	int mWindowHandle;

	GLuint mVAO;
	GLuint mVBOCube;
	GLuint mVBOCubeIndices;
	GLuint mCubeIndexCount;

	GLuint mBasicShaderProgram;

	glm::mat4 mView;
	glm::mat4 mProj;
	glm::mat4 mProjView;
	glm::mat4 mCubeWorld;
};

// CPP


#include "Graphics.h"

#include "main.h"

Graphics::Graphics()
	{
	}


Graphics::~Graphics()
	{
	}

bool Graphics::Init(int argc, char* argv[])
	{
	GLenum glewInitResult;
	InitWindow(argc, argv);
	GLenum error = glGetError();
	
	glewExperimental = GL_TRUE;
	glewInitResult = glewInit();
	error = glGetError();
	if ( glewInitResult != GLEW_OK )	
		{
		cout << glewGetErrorString(error) << '\n';
		exit(EXIT_FAILURE);
		return false;
		}
	
	error = glGetError();
	// this triggers 1280 GL_INVALID_ENUM this is before I even set any gl enums manually
	if ( error != GL_NO_ERROR )	
		{
		cout << glewGetErrorString( error );
		exit(-1);
		return false;
		}
	glEnable(GL_DEPTH_TEST);
	glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
	if (LoadGraphics())
		return true;
	else
		{
		return false;
		}
	}

bool Graphics::InitWindow(int argc, char* argv[])
	{
	glutInit(&argc, argv);

	glutInitContextVersion(4, 0);
	glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
	glutInitContextProfile( GLUT_CORE_PROFILE );

	glutSetOption( GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS );

	glutInitWindowSize( 800, 600 );
	
	glutInitDisplayMode( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA );

	mWindowHandle = glutCreateWindow("Hello World!");

	if ( mWindowHandle < 1 )
		{
		exit(EXIT_FAILURE);
		}
	// TODO: add callbacks for freeGLUT
	glutIdleFunc( Update );
	glutDisplayFunc( Render );
	glutCloseFunc( Close );
	
	return true;
	}

bool Graphics::LoadGraphics()
	{
	GLenum error = glGetError();
	if ( error != GL_NO_ERROR )
		{
		cout << glewGetErrorString(error) << '\n';
		exit(-1);
		return false;
		}
	typedef struct
{
    float XYZW[4];
} Vertex2;

	Vertex2 vertices[] =
{
    { { 0.0f, 0.0f, 0.0f, 1.0f } },
    // Top
    { { -0.2f, 0.8f, 0.0f, 1.0f } },
    { { 0.2f, 0.8f, 0.0f, 1.0f } },
    { { 0.0f, 0.8f, 0.0f, 1.0f }},
    { { 0.0f, 1.0f, 0.0f, 1.0f }},
    // Bottom
    { { -0.2f, -0.8f, 0.0f, 1.0f } },
    { { 0.2f, -0.8f, 0.0f, 1.0f } },
    { { 0.0f, -0.8f, 0.0f, 1.0f } },
    { { 0.0f, -1.0f, 0.0f, 1.0f } },
    // Left
    { { -0.8f, -0.2f, 0.0f, 1.0f } },
    { { -0.8f, 0.2f, 0.0f, 1.0f } },
    { { -0.8f, 0.0f, 0.0f, 1.0f } },
    { { -1.0f, 0.0f, 0.0f, 1.0f } },
    // Right
    { { 0.8f, -0.2f, 0.0f, 1.0f } },
    { { 0.8f, 0.2f, 0.0f, 1.0f } },
    { { 0.8f, 0.0f, 0.0f, 1.0f }},
    { { 1.0f, 0.0f, 0.0f, 1.0f } }
};

GLubyte indices[] = {
    // Top
    0, 1, 3,
    0, 3, 2,
    3, 1, 4,
    3, 4, 2,
    // Bottom
    0, 5, 7,
    0, 7, 6,
    7, 5, 8,
    7, 8, 6,
    // Left
    0, 9, 11,
    0, 11, 10,
    11, 9, 12,
    11, 12, 10,
    // Right
    0, 13, 15,
    0, 15, 14,
    15, 13, 16,
    15, 16, 14
};


	// create VBO
	OBJLoader loader;
	
	UPMesh upMesh = loader.Load( "c:\\objBox.obj" );

	glGenBuffers(1, &mVBOCube);
	glBindBuffer(GL_ARRAY_BUFFER, mVBOCube);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW );
	//glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex2) * upMesh->mVertices.size(), upMesh->mVertices.data(), GL_STATIC_DRAW );

	glGenBuffers(1, &mVBOCubeIndices);
	glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mVBOCubeIndices);
	glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW );
	//glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(upMesh->mIndices[0]) * upMesh->mIndices.size(), upMesh->mIndices.data(), GL_STATIC_DRAW );

	
	// create VAO
	glGenVertexArrays(1, &mVAO);
	glBindVertexArray(mVAO);
	error = glGetError();

	if ( error != GL_NO_ERROR )
		{
		cout << glewGetErrorString( error ) << '\n';
		exit(-1);
		return false;
		}
	
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 );
	//glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)(3 * sizeof(GLfloat)) );
	//glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)(5 * sizeof(GLfloat)) );
	error = glGetError();

	if ( error != GL_NO_ERROR )
		{
		cout << glewGetErrorString( error ) << '\n';
		exit(-1);
		return false;
		}
	
	glEnableVertexAttribArray(0);
	//glEnableVertexAttribArray(1);
	//glEnableVertexAttribArray(2);
	

	mCubeIndexCount = upMesh->mIndices.size();

	mView = glm::lookAt( glm::vec3(0.f,0.f,10.f), glm::vec3(0.f,0.f,0.f), glm::vec3(0.f, 1.0f, 0.f));
	mProj = glm::perspective( 3.14f / 2.0f, 800.f / 600.f, 0.1f, 1000.f );
	mProjView = mProj * mView;

	mCubeWorld = glm::mat4(1.0f);

	error = glGetError();

	if ( error != GL_NO_ERROR )
		{
		cout << glewGetErrorString( error ) << '\n';
		exit(-1);
		return false;
		}

	ifstream ifs;
	ifs.open( "..\\Resources\\basicShader.vertex", ios::in );
	if ( !ifs.good() )
		{
		cout << "ifs open failed.\n";
		exit(-1);
		return false;
		}
	string buffer;
	string errorBuffer;
	errorBuffer.reserve(2048);
	string shaderCode;
	while ( getline(ifs, buffer ) )
		{
		shaderCode += buffer += '\n';
		}
	char const * pSource = shaderCode.c_str();	
	GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource( vertShader, 1, &pSource, NULL );
	glCompileShader(vertShader);
	glGetShaderInfoLog( vertShader, 2047, NULL, &errorBuffer[0] );
	ifs.close();
	
	error = glGetError();
	ifs.open( "..\\Resources\\basicShader.fragment", ios::in );
	if ( !ifs.good() )
		{
		exit(-1);
		return false;
		}

	shaderCode.clear();
	while ( getline( ifs, buffer ) )
		{
		shaderCode += buffer += '\n';
		}
	pSource = shaderCode.c_str();
	GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource( fragShader, 1, &pSource, NULL );
	glCompileShader( fragShader );
	glGetShaderInfoLog( fragShader, 2047, NULL, &errorBuffer[0] );
	mBasicShaderProgram = glCreateProgram();
	glAttachShader(mBasicShaderProgram, vertShader);
	glAttachShader(mBasicShaderProgram, fragShader);
	glLinkProgram( mBasicShaderProgram );
	glGetProgramInfoLog( mBasicShaderProgram, 2047, NULL, &errorBuffer[0] );
	glUseProgram( mBasicShaderProgram );

	error = glGetError();

	if ( error != GL_NO_ERROR )
		{
		cout << glewGetErrorString(error) << '\n';
		exit(-1);
		return false;
		}

	return true;
	}

void Graphics::UpdateGraphics()
	{
	glutPostRedisplay();
	}

void Graphics::RenderGraphics()
	{
	static float color = 0.0;
	static float dir = 1.0;
	color += (.0001f * dir);
	if ( 1.0f < color )
		{
		color = 0.999f;
		dir *= -1.0f;
		}
	if ( color < 0.0f )
		{
		color = 0.0001f;
		dir *= -1.0f;
		}
	glClearColor( color, color, color, 0.0);
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );

	glm::mat4 mvp = mProjView * mCubeWorld;

	GLint location = glGetUniformLocation(mBasicShaderProgram, "world");

	glProgramUniformMatrix4fv( mBasicShaderProgram, location, 1, GL_FALSE, &mvp[0][0] );

	glDrawElements( GL_TRIANGLES, mCubeIndexCount, GL_UNSIGNED_INT, (void*)0);

	glutSwapBuffers();
	glutPostRedisplay();

	GLenum error = glGetError();
	if ( error != GL_NO_ERROR )
		{
		cout << glewGetErrorString(error) << '\n';
		exit(-1);
		return;
		}
	}

void Graphics::CleanGraphics()
	{
	glDisableVertexAttribArray(mVAO);
	glDisableVertexAttribArray(0);

	glBindVertexArray(0);
	glDeleteVertexArrays(1, &mVAO);

	glBindBuffer( GL_ARRAY_BUFFER, 0 );

	glDeleteBuffers(1, &mVBOCube);

	GLenum error = glGetError();
	if ( error != GL_NO_ERROR )
		{
		exit(-1);
		}
	}

void Graphics::StartGlutLoop()
	{
	glutMainLoop();
	}
Edited by EddieV223

Share this post


Link to post
Share on other sites
Advertisement

It is really just 1 Graphics class so it's not complicated code.  For 2 days I struggle with this, since I am new to openGL I don't know what is wrong and have been trying to fix it for days now.

I don't know what's wrong either, because you haven't described any kind of problem. I see code, but I don't even know what kind of problem to look for.

 

You may not be able to know the cause of a problem, but we need to at least know what the problem is in order to help you find the cause of it.

Share this post


Link to post
Share on other sites

To find a problem, you should comment out most of the code and start from basics.

 

Get empty window to show up, draw something on it.

Enable perspective code.

Replace test drawing with your cube.

Add shaders.

And so on.

 

Not only you will find the error, but this way you will learn OpenGL easier. And you would ask much more specific questions :)

Edited by Nercury

Share this post


Link to post
Share on other sites

It is really just 1 Graphics class so it's not complicated code.  For 2 days I struggle with this, since I am new to openGL I don't know what is wrong and have been trying to fix it for days now.

I don't know what's wrong either, because you haven't described any kind of problem. I see code, but I don't even know what kind of problem to look for.

 

You may not be able to know the cause of a problem, but we need to at least know what the problem is in order to help you find the cause of it.

added some explaination.

Share this post


Link to post
Share on other sites
// this triggers 1280 GL_INVALID_ENUM this is before I even set any gl enums manually
	if ( error != GL_NO_ERROR )	
		{
		cout << glewGetErrorString( error );
		exit(-1);
		return false;
		}

 

This GL_INVALID_ENUM may be because you have a call to glGetError() before calling glewInit().

 

And, as was said, it's hard to know why it doesn't work.

Have you tried rendering a single triangle only, instead of more complicated objects?

Try disables back face culling, perhaps the vertices are being specified in the wrong winding.

 

Also, don't know if it's relevant but .obj files store vertices in 1 based indices (eg. indices start at 1 and not 0).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!