Results 1 to 2 of 2

Thread: Triangle Black Screen

  1. #1
    Newbie Newbie
    Join Date
    Jun 2014
    Posts
    1

    Triangle Black Screen

    Hi,I try to draw a simple triangle without color attribute but nothing is rendered except
    for a black screen..

    #include"glew.h"
    #include<gl\GL.h>
    #include<gl\GLU.h>
    #include<freeglut.h>
    #include<iostream>
    using namespace std;

    GLuint positionID;
    GLuint vbo = 0;
    GLuint vao = 0;
    GLuint ProgramID = 0;

    void reshape(int x, int y)
    {
    glViewport(0, 0, x, y);
    }

    void render()
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glUseProgram(ProgramID);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glutSwapBuffers();
    }

    void TestCompile(GLuint ShaderID)
    {
    GLint status;
    glGetShaderiv(ShaderID, GL_COMPILE_STATUS, &status);
    if (status == GL_FALSE)
    {
    cout << "Compile Failed" << endl;
    GLint infoLogLength;
    glGetShaderiv(ShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
    cout << infoLogLength << endl;

    GLchar* strInfoLog = new GLchar[infoLogLength + 1];
    glGetShaderInfoLog(ShaderID, infoLogLength, NULL, strInfoLog);
    cout<<strInfoLog;

    }
    else if (status == GL_TRUE)
    {
    cout << "Compiled Successfully" << endl;
    }
    }

    int main(int argc, char** argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(600, 400);
    glutInitWindowPosition(300, 200);
    glutCreateWindow("My Window");
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glutReshapeFunc(reshape);

    glewInit();


    GLfloat vertex[] = { -0.5, -0.5, 0.0,
    0.5, -0.5, 0.0,
    0.0, 0.5, 0.0 };


    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);



    ProgramID = glCreateProgram();


    GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

    const GLchar* VertexSource = "#version 400\n in vec3 position; void main(){ gl_Position = vec4(position,1.0); }";
    const GLchar* FragmentSource = "#version 400\n void main() { gl_FragColor = vec4(1.0,1.0,1.0,1.0); }";
    glShaderSource(VertexShaderID, 1, (const GLchar**)&VertexSource, 0);
    glShaderSource(FragmentShaderID, 1, (const GLchar**)&FragmentSource, 0);
    glCompileShader(VertexShaderID);
    TestCompile(VertexShaderID);
    glCompileShader(FragmentShaderID);
    TestCompile(FragmentShaderID);


    glAttachShader(ProgramID, VertexShaderID);
    glAttachShader(ProgramID, FragmentShaderID);
    glLinkProgram(ProgramID);

    positionID = glGetAttribLocation(ProgramID, "position");
    glVertexAttribPointer(positionID, 3*sizeof(GLfloat), GL_FLOAT, GL_FALSE, sizeof(vertex), 0);

    glutDisplayFunc(render);
    glutMainLoop();
    return 0;
    }

  2. #2
    Member Newbie
    Join Date
    Oct 2009
    Posts
    34
    At first sight I can see one mitake: the stride parameter of glVertexAttribPointer.
    Try set it to 0 or 3 * sizeof(float) instead of sizeof(vertex)

Similar Threads

  1. black flicker at triangle edges
    By coffeeaddict in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 06-18-2011, 11:06 AM
  2. Black screen using Print Screen
    By specifikys in forum OpenGL: Windows
    Replies: 1
    Last Post: 11-01-2010, 01:32 PM
  3. Replies: 0
    Last Post: 07-29-2009, 12:54 PM
  4. colored black and white Sierpinski triangle
    By vulfh in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 11-10-2003, 11:02 AM
  5. Replies: 0
    Last Post: 09-30-2003, 08:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean