Jump to content

  • Log In with Google      Sign In   
  • Create Account

OpenGL triangle example


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Dark Engineer   Members   -  Reputation: 132

Like
0Likes
Like

Posted 22 January 2013 - 11:41 AM

I have problem with this code because it doesn't show anything in a window, vertex and fragment shaders are loading successfully but glGetError shows something wrong is here. Please someone help me.
#include "loadshader.h"
int main()
{
	glfwInit();
	glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
	glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
	glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
	glfwOpenWindow(800, 600, 0, 0, 0, 0, 0, 0, GLFW_WINDOW);
	glfwSetWindowTitle("Crion World Alpha");
	glewExperimental = GL_TRUE;
	glewInit();
	GLuint vertexBuffer;
	glGenBuffers( 1, &vertexBuffer );
	GLuint vao;
	glGenVertexArrays(1, & vao);
	glBindVertexArray( vao );
	GLuint vbo;
	glGenBuffers( 1, &vbo); //generate 1 buffer
	float vertices[] = {
		0.0f,	0.5f,
		0.5f,	-0.5f,
		-0.5f,	-0.5f
	};
	glBindBuffer( GL_ARRAY_BUFFER, vbo );
	glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_STATIC_DRAW );
	// vertex shader loading
	const string vShaderString = loadShaderFile("SimpleVertexShader.vertexshader").c_str();
	const GLchar * tempVShader = vShaderString.c_str();
	GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER );
	glShaderSource(vertexShader, 1, & tempVShader, NULL);
	glCompileShader( vertexShader ); 
	std::fstream logs;
	logs.open( "logs.txt", ios::in | ios::out | ios::trunc );
	char  vsbuffer[512];
	glGetShaderInfoLog( vertexShader, 512, NULL, vsbuffer );
	logs << vsbuffer;
	//fragment shader loading
	const string fShaderString = loadShaderFile("SimpleFragmentShader.fragmentshader").c_str();
	const GLchar * tempFShader = fShaderString.c_str();
	GLuint fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
	glShaderSource(fragmentShader, 1, & tempFShader, NULL );
	glCompileShader(fragmentShader);
	char  fsbuffer[512];
	glGetShaderInfoLog( fragmentShader, 512, NULL, fsbuffer );
	logs << fsbuffer;
	if(glGetError() != 0)
	{
		logs << "sth errors";
		cout << "sth wrong" << endl;
	}
	logs.close();
	GLuint shaderProgram = glCreateProgram();
	glAttachShader( shaderProgram, vertexShader );
	glAttachShader( shaderProgram, fragmentShader);
	glBindFragDataLocation( shaderProgram, 0, "outColor" );
	glLinkProgram( shaderProgram );
	glUseProgram( shaderProgram );
	GLint posAttrib = glGetAttribLocation( shaderProgram, "position" );
	glEnableVertexAttribArray( posAttrib );
	glVertexAttribPointer( posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0 );
	while( glfwGetWindowParam( GLFW_WINDOW ) )
	{
		glDrawArrays( GL_TRIANGLES, 0, 3);
		glfwSwapBuffers();
	}
	glDeleteProgram( shaderProgram );
	glDeleteShader( vertexShader );
	glDeleteShader( fragmentShader );
	glDeleteBuffers(1,  & vbo );
	glDeleteVertexArrays(1, &vao );
	glfwTerminate();
	return 0;
}

Edited by Dark Engineer, 22 January 2013 - 05:29 PM.


Sponsor:

#2 BornToCode   Members   -  Reputation: 948

Like
1Likes
Like

Posted 22 January 2013 - 02:14 PM

Something is wrong exactly where. When you say something wrong is here, it does not help. Can you be a little bit more clear.



#3 beans222   Members   -  Reputation: 1170

Like
1Likes
Like

Posted 22 January 2013 - 02:20 PM

There's missing () after glGetError. As is it's not getting called (what you have there is a comparison of the function's address).


New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

 


#4 Dark Engineer   Members   -  Reputation: 132

Like
0Likes
Like

Posted 22 January 2013 - 02:29 PM

It doesn't change anything. When i turn application on then debug error appears with Invalid allocation size: 4294967295 bytes

untitled.png


Edited by Dark Engineer, 22 January 2013 - 02:42 PM.


#5 SimonForsman   Crossbones+   -  Reputation: 6306

Like
1Likes
Like

Posted 22 January 2013 - 02:46 PM

Since you are using shaders ... post them.
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#6 Dark Engineer   Members   -  Reputation: 132

Like
0Likes
Like

Posted 22 January 2013 - 02:54 PM

SimpleFragmentShader

#version 150

out vec4 outColor;

void main()
{
    outColor = vec4( 1.0, 1.0, 1.0, 1.0 );
}

SimpleVertexShader

#version 150

in vec2 position;

void main()
{
    gl_Position = vec4( position, 0.0, 1.0 );
}

and function for loading them

string loadShaderFile(const string fileName)
{
    
    ifstream shaderFile( fileName.c_str());

    //find the file size
    shaderFile.seekg(0, ios::end);
    std::streampos length = shaderFile.tellg();
    shaderFile.seekg(0, ios::beg);

    //read whole file into a vector
    vector<char> buffer(length);
    shaderFile.read(&buffer[0], length);

    //return the shader string
    return string( buffer.begin(), buffer.end());
}

I think something is wrong under 53 line but i'm begginer in OpenGL


Edited by Dark Engineer, 22 January 2013 - 05:20 PM.


#7 Nyssa   Members   -  Reputation: 426

Like
1Likes
Like

Posted 23 January 2013 - 02:39 AM

Check that the functions you're using are returning valid data.

const GLchar * tempVShader = vShaderString.c_str();

 

Make sure "tempVShader" has a valid string.

char  vsbuffer[512];
glGetShaderInfoLog( vertexShader, 512, NULL, vsbuffer );
logs << vsbuffer;

 

Make sure "vsbuffer" has valid data and is NULL terminated.

const GLchar * tempFShader = fShaderString.c_str();

 

Check "tempFShader" is valid.


char  fsbuffer[512];
glGetShaderInfoLog( fragmentShader, 512, NULL, fsbuffer );
logs << fsbuffer;

 

 

Make sure "fsbuffer" has valid data and is NULL terminated.



#8 Nyssa   Members   -  Reputation: 426

Like
1Likes
Like

Posted 23 January 2013 - 02:50 AM

And to be totally safe, logging should be used as follows, and after the call to glCompileShader()

GLint retParam;
glGetShaderiv(m_openGLID, GL_COMPILE_STATUS, &retParam);
if(retParam != GL_TRUE) 
{
	GLint errorMsgLen = 0;
	glGetShaderiv(m_openGLID, GL_INFO_LOG_LENGTH, &errorMsgLen);
	if (errorMsgLen > 0)
	{
		GLsizei charsWritten = 0;
		GLchar *errorMsg = new GLchar[errorMsgLen];
		glGetShaderInfoLog(m_openGLID, errorMsgLen, &charsWritten, errorMsg);

                /*Do something with errorMsg*/
	}
}

Edited by Nyssa, 23 January 2013 - 02:56 AM.


#9 Dark Engineer   Members   -  Reputation: 132

Like
0Likes
Like

Posted 23 January 2013 - 05:45 AM

I have in console and logs that both shaders loaded successfully and program loaded successfully but still have this same error. Maybe i made something wrong with creating window.

Edited by Dark Engineer, 23 January 2013 - 09:12 AM.


#10 Dark Engineer   Members   -  Reputation: 132

Like
0Likes
Like

Posted 23 January 2013 - 09:48 AM

Solved it just needed to change in loop GLFW_WINDOW to GLFW_OPENED






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS