Jump to content
  • Advertisement
Sign in to follow this  
leagal4ever

OpenGL drawing 2D very basic shapes

This topic is 2160 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

Hello, im having problem with drawing 2D very basic shapes.

Questions:
1. How draw Quad just with lines, corner should be sharp.
2. Quad with border and filled color, each with different color.
3. Triangle is again not sharp also bottom line seems to be thiner.
4. Position in code is set to 100, 100 (quad) in window position does not match(y only?) its more like on half so on 300px.
Edit: just saw problem there are many width and height to set, forgot one


I have added code since its very small and have many mistakes inside.

Thanks for help.

qqqqqqqqqqqqqqqf.png

#include "sdl.h"
#include "sdl_opengl.h"
#include <iostream>
#include <SDL_image.h>
#include <string>
#include <vector>

using std::vector;
using std::cout;
using std::endl;

vector < vector <int> >  points_vec;

int WIDTH = 800;
int HEIGHT = 600;

int main(int argc, char* argv[])
{
    //initialize SDL
    SDL_Init(SDL_INIT_EVERYTHING);

    //Set OpenGL memory usage
    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);
    SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1);

    //Caption of the window
    SDL_WM_SetCaption("Our first game", NULL);

    //Size of the window
    SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL);

    //Specific the clear color
    glClearColor(1, 1, 1, 1);// RED, GREEN, BLUE, ALPHA

    //What portion of the screen we will display
    glViewport(0, 0, WIDTH, HEIGHT);

    //Shader model - use this
    glShadeModel(GL_SMOOTH);

    //2D rendering
    glMatrixMode(GL_PROJECTION | GL_MODELVIEW);

    //"Save it"
    glLoadIdentity();

    // Disable depth test
    glDisable(GL_DEPTH_TEST);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    //Handle the main loop
    bool isRunning = true;

    //For handling with event
    SDL_Event event;

    // Main game loop
    while(isRunning)
    {
        // events
        while( SDL_PollEvent(&event) )
        {
            if( event.type == SDL_QUIT )
            {
                isRunning = false;
            }
        }

        //RENDERING to the screen
        glClear(GL_COLOR_BUFFER_BIT);

        glPushMatrix();
            glOrtho(0, WIDTH, HEIGHT, 0, -1, 1);//Set the matrix
            glColor4ub(0, 0, 0, 255);     

            glLineWidth(10);

            // quad with lines
            glBegin(GL_LINES);
                glVertex2d(100, 100);
                glVertex2d(200, 100);

                glVertex2d(200, 100);
                glVertex2d(200, 200);

                glVertex2d(200, 200);
                glVertex2d(100, 200);

                glVertex2d(100, 200);
                glVertex2d(100, 100);           
            glEnd();   

            // triangle with lines
            glBegin(GL_LINES);
                glVertex2d(300, 300);
                glVertex2d(400, 400);

                glVertex2d(400, 400);
                glVertex2d(200, 400);

                glVertex2d(200, 400);
                glVertex2d(300, 300);         
            glEnd();

        glPopMatrix();

        SDL_GL_SwapBuffers();
    }

    SDL_Quit();

    return 0;
}.h>

Share this post


Link to post
Share on other sites
Advertisement

OpenGL is not meant to handle such high-level concepts as shapes by itself. Even a single basic line, except one pixel thick lines, are quite unspecified and unpredictable, and especially when you need control of the joints. You need a higher-level API to handle the geometry that can control the desired aspects of the geometry you want to draw, such as Cairo, Anti-Grain or FreeType.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!