Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: can someone tell me why this code doesnt work?

  1. #1
    Intern Contributor
    Join Date
    Jun 2013
    Posts
    69

    Question can someone tell me why this code doesnt work?

    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):
    Code :
    #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.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,136
    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.

  3. #3
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    433
    Maybe it's the way vertices is defined:
    Code :
    //note the 4
    GLfloat vertices [6][4] = {
    And then used as a tightly packed 2 element attribute (stride = 0).

  4. #4
    Intern Contributor
    Join Date
    Jun 2013
    Posts
    69
    Quote Originally Posted by Dan Bartlett View Post
    Maybe it's the way vertices is defined:
    Code :
    //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?

  5. #5
    Intern Contributor
    Join Date
    Jun 2013
    Posts
    69
    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?

  6. #6
    Intern Contributor
    Join Date
    Jun 2013
    Posts
    69
    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.

  7. #7
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    645
    I think the GLEW section on this wiki page is relevant.

  8. #8
    Intern Contributor
    Join Date
    Jun 2013
    Posts
    69
    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

  9. #9
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948
    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.

  10. #10
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    474
    Quote Originally Posted by sandbucket View Post
    is all you need to program in 4.30 an up to date driver on a compatible graphics card?
    Yes.
    Quote Originally Posted by sandbucket View Post
    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. 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).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •