Jump to content
  • Advertisement
Sign in to follow this  
Radlabgaming

OpenGL C++ OpenGL loading Object Issue

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

Hello everyone!

 

I am having a small issue that has halted my progress on learning how to use opengl.  I am basically trying to load a .obj file using a .obj file loader I built and it seems to be working correctly (in terms of loading). With that being said, when I go to draw the 3d object to the screen it creates a large red square.

GLenum err = glewInit();
	if (err != GLEW_OK)
	{
		std::cout << "NOT WORKING" << std::endl;
	}
	const char* vertexShaderSource =
		"#version 330\n\
						                 in vec4 position;\
										 								 			          void main(void) {\
																							  																																										  																																				gl_Position = position;\
																																																																																																																																 }";
	// compile fragment shader source
	const GLchar* fragmentShaderSource =
		"#version 330\n\
												out vec4 fragColor;\
																		                void main(void) {\
																																						fragColor = vec4(1.0,0.3,0.3,0.15);\
																																																																 }";

	/* Creating Shader */
	this->programId = glCreateProgram();
	this->vId = glCreateShader(GL_VERTEX_SHADER);
	this->fId = glCreateShader(GL_FRAGMENT_SHADER);
	/* Get Shader Size */
	int vertexShaderLength = strlen(vertexShaderSource);
	int fragmentShaderLength = strlen(fragmentShaderSource);
	/* Loading and binding shader */
	glShaderSource(this->vId, 1, &vertexShaderSource, NULL);
	glShaderSource(this->fId, 1, &fragmentShaderSource, NULL);
	/* Compile Shaders */
	glCompileShader(vId);
	int iIsOk = 0;
	glGetShaderiv(this->vId, GL_COMPILE_STATUS, &iIsOk);
	if (!iIsOk)
	{
		return;
	}
	int iIsOk2;
	glCompileShader(fId);
	glGetShaderiv(this->fId, GL_COMPILE_STATUS, &iIsOk2);
	if (!iIsOk2)
	{
		std::cout << "U SUCK!" << std::endl;
		std::cout << glGetError() << std::endl;
	}
	/* Attach Shaders */
	glAttachShader(this->programId, this->vId);
	glAttachShader(this->programId, this->fId);
	/* Bind Attributes from Shader */
	glBindAttribLocation(this->programId, 0, "position");
	glBindFragDataLocation(this->programId, 0, "fragcolor");
	/* Linkg program */
	glLinkProgram(this->programId);
	/* Use and bind attribute */
	glUseProgram(this->programId);
	this->positionId = glGetAttribLocation(this->programId, "position");
	glUseProgram(0);
	/* VAO Time */
	glGenVertexArrays(1, &this->vaoId);
	glBindVertexArray(this->vaoId);
	/* VBO Time assigning to VAO */
	glGenBuffers(1, &this->vboId);
	glBindBuffer(GL_ARRAY_BUFFER, this->vboId);
	glBufferData(GL_ARRAY_BUFFER, vertcies.size() * sizeof(sf::Vector3f), &vertcies[0], GL_STATIC_DRAW);
	glEnableVertexAttribArray(this->positionId);
	glVertexAttribPointer(this->positionId, 2, GL_FLOAT, GL_FALSE, sizeof(sf::Vector3f), 0);
	/* Close out bindings */
	glBindVertexArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, 0);

DRAW:


	glUseProgram(this->programId);
	glBindVertexArray(this->vaoId);
	glDrawArrays(GL_TRIANGLES, 0, this->vertcies.size());
	glBindVertexArray(0);
	glUseProgram(0);

I uploaded an image of what the red box displayed as. I am curious if I am missing something in the openGL code or what I am doing wrong? I am able to display a triangle correctly, but when I go to load my .obj files this is the stuff I get displayed. sad.png

 

The above is all the openGL code I have in the project, is there a simple error in my code...or forgetting something?

 

 

Thanks so much, been stuck for about a week on this! sad.png

 

Share this post


Link to post
Share on other sites
Advertisement

In your shader you output that red color for all drawn geometry, so this would be expected if the mesh is so big it fills the entire screen.

 

Try scaling down the mesh so it wont take entire screen.

Share this post


Link to post
Share on other sites

In your shader you output that red color for all drawn geometry, so this would be expected if the mesh is so big it fills the entire screen.

 

Try scaling down the mesh so it wont take entire screen.

 

okay figured it out partial, will update post if I get stuck more!

Edited by Radlabgaming

Share this post


Link to post
Share on other sites

It would just be a quick hack to see if the mesh works at all - you could do something like gl_Position = position * 0.1; in the shader (or multiply with whatever constant is required to make the mesh a reasonable size on screen).

 

The coordinates go from -1 to 1 so if your mesh is like 100 wide, obviously its not going to fit without transforming the coordinates in one way or another.

 

 

Normally you would pass a matrix to the shader as a uniform, which will do the required transformations (rotation, scaling, translation, perspective projection if you want 3D...) called the "model view projection matrix" (as multiple linear transformations can nicely be combined into a single one like that). That also includes correcting for aspect ratio.

Share this post


Link to post
Share on other sites

Where are your transforms ? I would suggest you find some reference that goes over the fundamentals of 3D, especially the graphics pipeline and 3D transformation before evening wrangling with an API as the more you intend to do, the more confused you will be when stuff just 'doesn't work'. Learning the idiosyncrasies of OpenGL is headache enough for someone, and without any background its going to be more daunting.

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!