PDA

View Full Version : Osx Lion Core Profile 3.2 + glfw



ShadeMii
02-15-2012, 06:42 PM
Hi I have been trying to render a simple diamond with GL3.2 as everyone knows it is quite a transition from 2.1 to 3.2.

I have done a ton of google searches and could not find what was wrong with my code so I have come here to ask if anyone can find anything wrong with my render loop and or my shader code, I have been trying for about 5 days this is pretty much my last resort. Here is the code.

The shader code compiles my issue is possibility the render loop.


//Shader code

#version 150 core

in vec3 ex_Color;

precision highp float;
out vec4 Frag;
void main(void){
Frag = vec4(ex_Color,1.0);
}

#version 150 core

in vec2 in_Position;
in vec3 in_Color;

out vec3 ex_Color;
void main(void)
{
gl_Position = vec4(in_Position.x,in_Position.y,0.0,1.0);
ex_Color = in_Color;
}


//



#include "glcontext.h"


GLContext::GLContext(): m_running(GL_TRUE)
{

}

void GLContext::GLInit()
{
if(!glfwInit())
{
std::cout << "Failed to create glcontext";
glfwTerminate();
exit(EXIT_FAILURE);
}


GLSettings();

if(!glfwOpenWindow(640,480,0,0,0,0,32,0,GLFW_WINDO W))
{
std::cout <<"Failed to create window";
glfwTerminate();
}

glfwSetWindowTitle("RenderMii Beta 0.1");




PrepareDiamond();

while(m_running)
{

for(iter = 2; iter <= 4; iter++)
{
glClearColor(1.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_LINE_LOOP,0,iter);
glfwSwapBuffers();
}


m_running = !glfwGetKey(GLFW_KEY_ESC) &amp;&amp; glfwGetWindowParam(GLFW_OPENED);
}
glfwTerminate();
exit(EXIT_SUCCESS);

}

void GLContext::GLSettings()
{
glfwOpenWindowHint(GLFW_FSAA_SAMPLES,4);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR,3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR,2 );
glfwOpenWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL _CORE_PROFILE);
}



char * GLContext::FileToBuffer(char * file)
{
FILE * fptr;
long length;
char * buffer;

fptr = fopen(file,"rb");
if(!fptr)
{
std::cout << "Failed to find file";
glfwTerminate();
exit(EXIT_FAILURE);
}


fseek(fptr,0,SEEK_END);
length = ftell(fptr);
buffer = (char*)malloc(length+1);
fseek(fptr,0,SEEK_SET);
fread(buffer,length,1,fptr);
fclose(fptr);
buffer[length] = 0; //NULL terminator

return buffer;
}


void GLContext::PrepareDiamond()
{

//Position coordinates
GLfloat diamond[4][2] = {
{0.0,1.0},
{1.0,0.0},
{0.0,-1.0},
{-1.0,0.0}
};
//Color values
GLfloat color[4][3] = {
{1.0,0.0,0.0},
{0.0,1.0,0.0},
{0.0,0.0,1.0},
{1.0,1.0,1.0}
};


//---VAO---//
//Create a vertex array object that will store to references to vertex buffer objects
(1,&amp;vertex_array_object);
//Select or bind the state of the current vertex array object as a current state
glBindVertexArray(vertex_array_object);
//Once it is binded we generate two vertex buffer objects

//---VBO---//
glGenBuffers(2,&amp;vertex_buffer_object[2]);

//We bind those to the current binded vertex array object
glBindBuffer(GL_ARRAY_BUFFER,vertex_buffer_object[0]);


//Copy data
//The first object to be binded is the diamond vertex buffer and since we arn't changing it much we are going to
//Static draw this
glBufferData(GL_ARRAY_BUFFER,8 * sizeof(GLfloat),diamond, GL_STATIC_DRAW);

//For the pointer first arg we are using attribute number 0
//Second arg we are using the number of verticies
//third arg we are using the size
glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,0);
//Enable pointing to this
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER,vertex_buffer_object[1]);

glBufferData(GL_ARRAY_BUFFER,12 * sizeof(GLfloat),color,GL_STATIC_DRAW);

glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,0);

glEnableVertexAttribArray(1);


//---ShaderSource---//
//Parse the shader source files
vertex_source = FileToBuffer("/Users/MichaelChung/RenderMiiGLFW/vertex_default.vert");
fragment_source = FileToBuffer("/Users/MichaelChung/RenderMiiGLFW/frag_default.frag");

//---VertexShader---//
//Create the shader handle
vertex_shader_handle = glCreateShader(GL_VERTEX_SHADER);

//Send shader source code to GL
//The shader code terminator is NULL char
//GL will detect the terminator NULL char, so the last param, length info can be 0

glShaderSource(vertex_shader_handle,1,(const GLchar**)&amp;vertex_source,0);

glCompileShader(vertex_shader_handle);

glGetShaderiv(vertex_shader_handle,GL_COMPILE_STAT US,&amp;Compiled_VS);

if(Compiled_VS == true)
{
glGetShaderiv(vertex_shader_handle,GL_INFO_LOG_LEN GTH,&amp;max_length);
vertex_info_log = (char *)malloc(max_length);
glGetShaderInfoLog(vertex_shader_handle,max_length ,&amp;max_length,vertex_info_log);
std::string vertex_info_log_str = std::string(vertex_info_log);
free(vertex_info_log);
vertex_info_log = NULL;
std::cout << vertex_info_log_str << std::endl;
std::cout << "Pass VS " << std::endl;

}

if(Compiled_VS == false)
{
glGetShaderiv(vertex_shader_handle,GL_INFO_LOG_LEN GTH,&amp;max_length);
vertex_info_log = (char *)malloc(max_length);
glGetShaderInfoLog(vertex_shader_handle,max_length ,&amp;max_length,vertex_info_log);
std::string vertex_info_log_str = std::string(vertex_info_log);
free(vertex_info_log);
vertex_info_log = NULL;
std::cout << vertex_info_log_str << std::endl;
std::cout << "Fail VS" << std::endl;
glfwTerminate();
}

fragment_shader_handle = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource(fragment_shader_handle,1,(const GLchar**)&amp;fragment_source,0);

glCompileShader(fragment_shader_handle);

glGetShaderiv(fragment_shader_handle,GL_COMPILE_ST ATUS,&amp;Compiled_FS);


if(Compiled_FS == true)
{
glGetShaderiv(fragment_shader_handle,GL_INFO_LOG_L ENGTH,&amp;max_length);
fragment_info_log = (char *)malloc(max_length);
glGetShaderInfoLog(fragment_shader_handle,max_leng th,&amp;max_length,fragment_info_log);
std::string fragment_info_log_str = std::string(fragment_info_log);
std::cout << fragment_info_log_str << std::endl;
free(fragment_info_log);
fragment_info_log = NULL;
std::cout << "Pass FS" << std::endl;

}


if(Compiled_FS == false)
{
glGetShaderiv(fragment_shader_handle,GL_INFO_LOG_L ENGTH,&amp;max_length);
fragment_info_log = (char *)malloc(max_length);
glGetShaderInfoLog(fragment_shader_handle,max_leng th,&amp;max_length,fragment_info_log);
std::string fragment_info_log_str = std::string(fragment_info_log);
std::cout << fragment_info_log_str;
free(fragment_info_log);
fragment_info_log = NULL;
std::cout << "Fail FS" << std::endl;
glfwTerminate();
}

//if we have gotten this far we basically now create a gl shader program
//with these two shaders
//which is monolithic 1 single piece 1 vertex and 1 fragment

diamond_shader_program = glCreateProgram();
glAttachShader(diamond_shader_program,vertex_shade r_handle);
glAttachShader(diamond_shader_program,fragment_sha der_handle);


//glBindAttrib with each varaible
glBindAttribLocation(diamond_shader_program,0,"in_Position");
glBindAttribLocation(diamond_shader_program,1,"in_Color");

glLinkProgram(diamond_shader_program);

//check if it worked or not

glGetProgramiv(diamond_shader_program,GL_LINK_STAT US, (int *)&amp;Linked);
if(Linked == false)
{
glGetShaderiv(diamond_shader_program,GL_INFO_LOG_L ENGTH,&amp;max_length);

shader_program_info_log = (char *)malloc(max_length);
glGetProgramInfoLog(diamond_shader_program,max_len gth,&amp;max_length,shader_program_info_log);
std::string shader_info_log_str = std::string(shader_program_info_log);
std::cout << shader_info_log_str << std::endl;
free(shader_program_info_log);
shader_program_info_log = NULL;
std::cout << "Compilation has Failed";
glfwTerminate();
}

//Load the shader

glUseProgram(diamond_shader_program);



}

arekkusu
02-15-2012, 07:21 PM
That looks pretty correct. What does glGetError() say?

ShadeMii
02-15-2012, 08:02 PM
glGetError() says invalid operation right after the Prepare diamond I will investigate where in prepare diamond this is happening

ShadeMii
02-15-2012, 08:18 PM
glBufferData(GL_ARRAY_BUFFER,8 * sizeof(GLfloat),diamond, GL_STATIC_DRAW);


This is the line that contains the error not sure why any clue ?

ShadeMii
02-15-2012, 08:42 PM
/---VAO---//
//Create a vertex array object that will store to references to vertex buffer objects
(1,&amp;vertex_array_object);


HOW THE HELL DID THIS EVEN COMPILE?!?!!??!?!?!? anyways I believe that was the ISSUE! D:

ShadeMii
02-15-2012, 08:47 PM
Spoke to soon

ShadeMii
02-15-2012, 09:06 PM
Okay solved the problem I should have not passed in &amp;vertex_buffer_object[2] into glgenBindBuffer instead &amp;vertex_buffer_object resulting in the 0 vertex buffer reserve name 0 being used. Thanks for the glError() idea arekkusu =DD