Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 15

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

Hybrid View

  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,182
    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
    450
    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.

Posting Permissions

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