PDA

View Full Version : can someone tell me why this code doesnt work?



sandbucket
06-16-2013, 06:07 PM
hi all.
ive been having trouble with this code. im 99% sure it has to do with my shaders, but i cant figure out the problem. its driving me nuts! i thought id ask here before setting up a mini program to check the shader compile logs and such, i will if you guys cant figure it out, or if you tell me to not be lazy and just check the logs.
im not sure if its a compiler problem either, so if you could try compiling this code in your compiler and letting me know if it works for you then thanks.

here is the code (written in c):

#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum VAO_IDs {Triangles, NumVAOs};
enum Buffer_IDs {ArrayBuffer, NumBuffers};
enum Attrib_IDs { vPosition = 0};

GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];

const GLuint NumVertices = 6;

void init();
void display();

int main(int argc, char **argv){

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(512,512);
glutInitContextVersion(4,3);
glutInitContextProfile(GLUT_CORE_PROFILE);

glutCreateWindow("first opengl program");

if (glewInit()) {
exit(1);
}

init();

glutDisplayFunc(display);

glutMainLoop();

return 0;

}

void init()
{

const char fshader[] =
"#version 430 core\n"
"out vec4 fColor;\n"
"void main()\n"
"{\n"
"fColor = vec4 (0.0, 0.0, 1.0, 1.0);\n"
"}\n";

const char vshader[] =
"#version 430 core\n"
"layout (location = 0) in vec4 vPosition;\n"
"void main()\n"
"{\n"
"gl_Position = vPosition;\n"
"}\n";

GLuint prog = glCreateProgram();

GLuint vert = glCreateShader(GL_VERTEX_SHADER);
GLuint frag = glCreateShader(GL_FRAGMENT_SHADER);

GLint vlen = (GLint)strlen(vshader);
GLint flen = (GLint)strlen(fshader);

glShaderSource(vert, 1, (const GLchar**)&vshader, &vlen);
glShaderSource(frag, 1, (const GLchar**)&fshader, &flen);
glCompileShader(vert);
glCompileShader(frag);


glAttachShader(prog, vert);
glAttachShader(prog, frag);
glLinkProgram(prog);
glUseProgram(prog);

glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);

GLfloat vertices[6][4] = {
{-0.90, -0.90},
{0.85, -0.90},
{-0.90, 0.85},
{0.90, -0.85},
{0.90, 0.90},
{-0.85, 0.90}
};
glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0,(void *)0);
glEnableVertexAttribArray(vPosition);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);

glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);

glFlush();
}

thanks.

mhagain
06-16-2013, 07:17 PM
Can you be more specific about what doesn't work? Right now you've just given a code dump and asked others to debug it for you; that generally isn't going to get a good response.

Dan Bartlett
06-17-2013, 02:03 AM
Maybe it's the way vertices is defined:

//note the 4
GLfloat vertices [6][4] = {
And then used as a tightly packed 2 element attribute (stride = 0).

sandbucket
06-17-2013, 06:05 AM
Maybe it's the way vertices is defined:

//note the 4
GLfloat vertices [6][4] = {
And then used as a tightly packed 2 element attribute (stride = 0).

ya its not that. i tried changing the array to 6x4 and then passing a size of 4 in glvertexattribpointer to vPosition because vposition is vec4 but it didnt change the result. i just tried it again having corrected that it doesn't work. can someone try compiling this for me and letting me know if it works on their compiler?

sandbucket
06-17-2013, 08:50 AM
hi, i figured out something weird. im using mingw with gcc and the eclipse ide. whenever i call an opengl function the program generates a runtime error can anyone help me with this?

sandbucket
06-17-2013, 11:08 AM
hi i just found out it is the newer functions of opengl that generate a runtime error. can you please let me know how to fix it.

carsten neumann
06-17-2013, 11:26 AM
I think the GLEW section on this wiki page (http://www.opengl.org/wiki/OpenGL_Loading_Library) is relevant.

sandbucket
06-17-2013, 01:54 PM
i tried with the glewExperimental variable set to true but it still didnt work. so i just have to wait for the fix? and 2 other quick questions, is all you need to program in 4.30 an up to date driver on a compatible graphics card? what is considered an extension in glew and gl3w... eyes? thanks so much

Alfonse Reinheart
06-17-2013, 02:02 PM
so i just have to wait for the fix?

Well, considering that GLEW hasn't fixed that since core OpenGL was introduced in OpenGL 3.1... I wouldn't hold my breath.

You know, there's a whole page that lists OpenGL loaders there that you could pick from.

GClements
06-17-2013, 03:14 PM
is all you need to program in 4.30 an up to date driver on a compatible graphics card?
Yes.

what is considered an extension in glew and gl3w?
OpenGL extensions are features which are added individually rather than being an integral part of a specific version of OpenGL. They are listed on this page (http://www.opengl.org/registry). Often, the same (or very similar) functionality is incorporated into a subsequent version of OpenGL. Functions and enumerants which are part of an extension have a suffix (ARB for officially-accepted extensions, or a vendor-specific suffix such as SGIX, NV, ATI, etc for an extension developed by a specific vendor).

GLEW doesn't particularly care whether a feature is an extension or part of some version of the core API. Because Windows's opengl32.dll only exports functions which are part of the OpenGL 1.1 API, anything else has to be accessed via a function pointer obtained via wglGetProcAddress(). Unix OpenGL libraries tend to export all "known" functions, but because a particular driver or X server can have newer functions, it's sometimes necessary to use glXGetProcAddress() to access newer functions.

GL3W makes a distinction between core API functions and extensions. It automatically provides function pointers for the former, but extensions still have to be queried manually (although it saves you the trouble of having to write separate Windows/Unix/Mac versions).

sandbucket
06-18-2013, 06:56 AM
well i tried the opengl loader generator.
i generated the files with this code from there website:

lua LoadGen.lua core_4_3 -style=pointer_c -spec=gl -version=4.3 -profile=core -stdext=gl_ubiquitous.txt -stdext=gl_plat_3_3.txt
my program compiles fine but still crashes. are there any compatibility issues for opengl loader generator? im gonna try gl3w. does opengl loader generator work for any of you?

here is my code. if you see a problem please let me know:


#include "gl_core_4_3.h"
#include <GL/gl.h>
#include <GL/freeglut.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>


GLuint vao, buffer;

void init();
void display();

int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(512,512);
glutInitContextVersion(4,3);
glutInitContextProfile(GLUT_CORE_PROFILE);

glutCreateWindow("first opengl program");

if(ogl_LoadFunctions() == ogl_LOAD_FAILED)
{
printf("error with GLEW");
exit(1);
}


init();


glutDisplayFunc(display);
glutMainLoop();

return 0;

}

void init()
{
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

GLfloat vertices[] = {
-0.90, -0.90,
0.85, -0.90,
-0.90, 0.85,
0.90, -0.85,
0.90, 0.90,
-0.85, 0.90
};
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

const char fshader[] =
"#version 430 core\n"
"out vec4 fColor;\n"
"void main()\n"
"{\n"
"fColor = vec4 (0.0, 0.0, 1.0, 1.0);\n"
"}\n";

const char vshader[] =
"#version 430 core\n"
"layout (location = 0) in vec4 vPosition;\n"
"void main()\n"
"{\n"
"gl_Position = vPosition;\n"
"}\n";

GLuint prog = glCreateProgram();

GLuint vert = glCreateShader(GL_VERTEX_SHADER);
GLuint frag = glCreateShader(GL_FRAGMENT_SHADER);

GLint vlen = (GLint)strlen(vshader);
GLint flen = (GLint)strlen(fshader);

glShaderSource(vert, 1, &vshader, &vlen);
glShaderSource(frag, 1, &fshader, &flen);
glCompileShader(vert);
glCompileShader(frag);


glAttachShader(prog, vert);
glAttachShader(prog, frag);
glLinkProgram(prog);
glUseProgram(prog);

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0,(void *)0);
glEnableVertexAttribArray(0);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);

glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 6);

glFlush();
}

thanks.

Alfonse Reinheart
06-18-2013, 07:21 AM
It'd be helpful to know where it crashed.

sandbucket
06-18-2013, 11:40 AM
it crashes in two different places. if i use the function init() after ogl_LoadFunctions() it crashes, but if i copy the code into the main function it crashes at the first glShaderSource(); do you have to call ogl_LoadFunctions() in every function that uses opengl code?

Jesse
06-18-2013, 12:00 PM
You're trying to get a core profile but you're manually including glu.h and gl.h. I always seem to have trouble if I include those headers. Try getting rid of those and also use glewExperimental if you're using Glew. Personally, I find gl3w to be much easier for core profile but that's all it will load.

sandbucket
06-18-2013, 12:02 PM
hey all.
i just want to thank you all so much for the help. i figured everything out and am super happy. thanks