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 cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
    • By Picpenguin
      Hi
      I'm new to learning OpenGL and still learning C. I'm using SDL2, glew, OpenGL 3.3, linmath and stb_image.
      I started following through learnopengl.com and got through it until I had to load models. The problem is, it uses Assimp for loading models. Assimp is C++ and uses things I don't want in my program (boost for example) and C support doesn't seem that good.
      Things like glVertexAttribPointer and shaders are still confusing to me, but I have to start somewhere right?
      I can't seem to find any good loading/rendering tutorials or source code that is simple to use and easy to understand.
      I have tried this for over a week by myself, searching for solutions but so far no luck. With tinyobjloader-c and project that uses it, FantasyGolfSimulator, I was able to actually load the model with plain color (always the same color no matter what I do) on screen and move it around, but cannot figure out how to use textures or use its multiple textures with it.
      I don't ask much: I just want to load models with textures in them, maybe have lights affect them (directional spotlight etc). Also, some models have multiple parts and multiple textures in them, how can I handle those?
      Are there solutions anywhere?
      Thank you for your time. Sorry if this is a bit confusing, English isn't my native language
    • By dpadam450
      FINALLY, upgrading my engine to openGL 4. I was having some trouble so I started with a stripped down application and was wondering if VAO's are required, because I have a sample working, but if I remove the VAO then it doesn't seem to like drawing my triangle.
  • Popular Now