Sign in to follow this  

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

This topic is 1792 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

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  

  • Similar Content

    • By _OskaR
      Hi,
      I have an OpenGL application but without possibility to wite own shaders.
      I need to perform small VS modification - is possible to do it in an alternative way? Do we have apps or driver modifictions which will catch the shader sent to GPU and override it?
    • By xhcao
      Does sync be needed to read texture content after access texture image in compute shader?
      My simple code is as below,
      glUseProgram(program.get());
      glBindImageTexture(0, texture[0], 0, GL_FALSE, 3, GL_READ_ONLY, GL_R32UI);
      glBindImageTexture(1, texture[1], 0, GL_FALSE, 4, GL_WRITE_ONLY, GL_R32UI);
      glDispatchCompute(1, 1, 1);
      // Does sync be needed here?
      glUseProgram(0);
      glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
      glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                     GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[1], 0);
      glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
       
      Compute shader is very simple, imageLoad content from texture[0], and imageStore content to texture[1]. Does need to sync after dispatchCompute?
    • By Jonathan2006
      My question: is it possible to transform multiple angular velocities so that they can be reinserted as one? My research is below:
      // This works quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); quat quaternion2 = GEMultiplyQuaternions(quaternion1, GEQuaternionFromAngleRadians(angleRadiansVector2)); quat quaternion3 = GEMultiplyQuaternions(quaternion2, GEQuaternionFromAngleRadians(angleRadiansVector3)); glMultMatrixf(GEMat4FromQuaternion(quaternion3).array); // The first two work fine but not the third. Why? quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); vec3 vector1 = GETransformQuaternionAndVector(quaternion1, angularVelocity1); quat quaternion2 = GEQuaternionFromAngleRadians(angleRadiansVector2); vec3 vector2 = GETransformQuaternionAndVector(quaternion2, angularVelocity2); // This doesn't work //quat quaternion3 = GEQuaternionFromAngleRadians(angleRadiansVector3); //vec3 vector3 = GETransformQuaternionAndVector(quaternion3, angularVelocity3); vec3 angleVelocity = GEAddVectors(vector1, vector2); // Does not work: vec3 angleVelocity = GEAddVectors(vector1, GEAddVectors(vector2, vector3)); static vec3 angleRadiansVector; vec3 angularAcceleration = GESetVector(0.0, 0.0, 0.0); // Sending it through one angular velocity later in my motion engine angleVelocity = GEAddVectors(angleVelocity, GEMultiplyVectorAndScalar(angularAcceleration, timeStep)); angleRadiansVector = GEAddVectors(angleRadiansVector, GEMultiplyVectorAndScalar(angleVelocity, timeStep)); glMultMatrixf(GEMat4FromEulerAngle(angleRadiansVector).array); Also how do I combine multiple angularAcceleration variables? Is there an easier way to transform the angular values?
    • By dpadam450
      I have this code below in both my vertex and fragment shader, however when I request glGetUniformLocation("Lights[0].diffuse") or "Lights[0].attenuation", it returns -1. It will only give me a valid uniform location if I actually use the diffuse/attenuation variables in the VERTEX shader. Because I use position in the vertex shader, it always returns a valid uniform location. I've read that I can share uniforms across both vertex and fragment, but I'm confused what this is even compiling to if this is the case.
       
      #define NUM_LIGHTS 2
      struct Light
      {
          vec3 position;
          vec3 diffuse;
          float attenuation;
      };
      uniform Light Lights[NUM_LIGHTS];
       
       
    • By pr033r
      Hello,
      I have a Bachelor project on topic "Implenet 3D Boid's algorithm in OpenGL". All OpenGL issues works fine for me, all rendering etc. But when I started implement the boid's algorithm it was getting worse and worse. I read article (http://natureofcode.com/book/chapter-6-autonomous-agents/) inspirate from another code (here: https://github.com/jyanar/Boids/tree/master/src) but it still doesn't work like in tutorials and videos. For example the main problem: when I apply Cohesion (one of three main laws of boids) it makes some "cycling knot". Second, when some flock touch to another it scary change the coordination or respawn in origin (x: 0, y:0. z:0). Just some streng things. 
      I followed many tutorials, change a try everything but it isn't so smooth, without lags like in another videos. I really need your help. 
      My code (optimalizing branch): https://github.com/pr033r/BachelorProject/tree/Optimalizing
      Exe file (if you want to look) and models folder (for those who will download the sources):
      http://leteckaposta.cz/367190436
      Thanks for any help...

  • Popular Now