Advertisement Jump to content
test opty

OpenGL The simplest OpenGL example

This topic is 423 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

 

I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?

 

 

Share this post


Link to post
Share on other sites
Advertisement

Sorry but these are not what I need! :(

The window I'm talking about should presumably have only few lines of code.

We want a simple, say, 800x600, window with a title like "First Window". That's all. 

Share this post


Link to post
Share on other sites

Maybe this example will be usefull for you. I wrote a program that writes a point with glPointSize = 10.0 using VBO, VAO, FreeGLUT, OpengGL 3.3 Core, VS2015: DrawingPointUsingVBOAndVAO.zip

This is a program settings for VS2015:

Quote

Configuration: All Configurations; Platforms: All Platforms
C/C++ -> Genaral -> Additional Include Directories:
$(SolutionDir).\Libs\glm-0.9.8.4
$(SolutionDir).\Libs\glew-2.0.0-win32\include
$(SolutionDir).\Libs\freeglut-MSVC-3.0.0-2.mp\freeglut\include
Linker -> Input -> Additional Dependencies
freeglut.lib
glew32s.lib

Configuration: All Configurations; Platforms: Win32
Linker -> General -> Additional Library Directories:
$(SolutionDir).\Libs\glew-2.0.0-win32\lib\Platform_Win32
$(SolutionDir).\Libs\freeglut-MSVC-3.0.0-2.mp\freeglut\lib\Platform_Win32
Build Events -> Post-Build Event
xcopy /y /d $(SolutionDir)Libs\freeglut-MSVC-3.0.0-2.mp\freeglut\freeglut_bin\Platform_Win32\freeglut.dll $(OutDir)

Configuration: All Configurations; Platforms: x64
Linker -> General -> Additional Library Directories:
$(SolutionDir).\Libs\glew-2.0.0-win32\lib\Platform_x64
$(SolutionDir).\Libs\freeglut-MSVC-3.0.0-2.mp\freeglut\lib\Platform_x64
Build Events -> Post-Build Event
xcopy /y /d $(SolutionDir)Libs\freeglut-MSVC-3.0.0-2.mp\freeglut\freeglut_bin\Platform_x64\freeglut.dll $(OutDir)

#define GLEW_STATIC

#include <GL/glew.h>
#include <GL/freeglut.h>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include <string>
#include <iostream>
using namespace std;

extern "C" _declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;

const char *vertexShaderSource =
"#version 330\n \
 \
in vec2 a_Position; \
 \
void main()\
{ \
    gl_Position = vec4(a_Position, 0.0, 1.0); \
    gl_PointSize = 10.0; \
}";

const char *fragmentShaderSource =
"#version 330\n \
 \
precision mediump float; \
out vec4 fragColor; \
 \
void main()\
{ \
    fragColor = vec4(1.0, 0.0, 0.0, 1.0); \
}";

void CheckShaderCompileStatus(GLuint shader)
{
    GLint status;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
    if (status == GL_FALSE)
    {
        GLint infoLogLength;
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
        GLchar *infoLog = new GLchar[infoLogLength];
        glGetShaderInfoLog(shader, infoLogLength, NULL, infoLog);
        cerr << "Compile log: " << infoLog << endl;
        delete[] infoLog;
    }
}
 GLuint vao;
void Display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBindVertexArray(vao);
    glDrawArrays(GL_POINTS, 0, 1);
    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitContextVersion(3, 3);
    glutInitContextFlags(GLUT_CORE_PROFILE | GLUT_DEBUG);
    glutInitWindowSize(500, 500);

    string title = "Drawing Point Using VBO";
    glutCreateWindow(title.c_str());
    cout << title << endl;

    glewInit();

    GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vShader);
    CheckShaderCompileStatus(vShader);

    GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fShader);
    CheckShaderCompileStatus(fShader);

    GLuint program = glCreateProgram();
    glAttachShader(program, vShader);
    glAttachShader(program, fShader);
    glLinkProgram(program);
    glUseProgram(program);
    GLint status;
    glGetProgramiv(program, GL_LINK_STATUS, &status);
    if (status == GL_FALSE)
    {
        GLint infoLogLength;

        glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
        GLchar *infoLog = new GLchar[infoLogLength];
        glGetProgramInfoLog(program, infoLogLength, NULL, infoLog);
        cerr << "Link log: " << infoLog << endl;
        delete[] infoLog;
    }

    GLfloat vertices[] = { 0.0f, 0.0f };

    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, 2 * sizeof(GLfloat), vertices, GL_STATIC_DRAW);

    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    GLint a_Position = glGetAttribLocation(program, "a_Position");
    glVertexAttribPointer(a_Position, 2, GL_FLOAT, GL_FALSE, 0, NULL);
    glEnableVertexAttribArray(a_Position);

    glEnable(GL_PROGRAM_POINT_SIZE);

    glutDisplayFunc(Display);

    glutMainLoop();
    return 0;
}

 

Edited by 8Observer8

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!