Sign in to follow this  
EddieV223

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

Recommended Posts

EddieV223    1839

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
Cornstalks    7030

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
Nercury    812

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
EddieV223    1839

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
__SKYe    1784
// 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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By povilaslt2
      Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).
    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
       
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
  • Popular Now